Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #ifndef PAYLOAD_WITH_INDEX_H_ |
| 22 | #define PAYLOAD_WITH_INDEX_H_ |
| 23 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 24 | namespace ns3 { |
| 25 | namespace ndn { |
| 26 | namespace ndnSIM { |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 28 | template<typename PayloadTraits, typename IndexTraits> |
| 29 | class payload_with_index { |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 30 | public: |
| 31 | typedef PayloadTraits::pointer_type iterator; |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 33 | typedef typename IndexTraits:: |
| 34 | template index<PayloadTraits, |
| 35 | typename IndexTraits::template container_hook<parent_trie>::type>::type |
| 36 | index_container; |
| 37 | |
| 38 | inline payload_with_index() |
| 39 | : index_(*this) |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 40 | { |
| 41 | } |
| 42 | |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 43 | inline std::pair<iterator, bool> |
| 44 | insert(typename iterator payload) |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 45 | { |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 46 | bool ok = policy_.insert(s_iterator_to(item.first)); |
| 47 | if (!ok) { |
| 48 | item.first->erase(); // cannot insert |
| 49 | return std::make_pair(end(), false); |
| 50 | } |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 51 | |
| 52 | return item; |
| 53 | } |
| 54 | |
| 55 | // inline void |
| 56 | // erase (const FullKey &key) |
| 57 | // { |
| 58 | // iterator foundItem, lastItem; |
| 59 | // bool reachLast; |
Alexander Afanasyev | 0717901 | 2014-12-21 00:23:04 -0800 | [diff] [blame^] | 60 | // std::tie (foundItem, reachLast, lastItem) = trie_.find (key); |
Alexander Afanasyev | be55cf6 | 2014-12-20 17:51:09 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 62 | // if (!reachLast || lastItem->payload () == PayloadTraits::empty_payload) |
| 63 | // return; // nothing to invalidate |
| 64 | |
| 65 | // erase (lastItem); |
| 66 | // } |
| 67 | |
| 68 | // inline void |
| 69 | // erase (iterator node) |
| 70 | // { |
| 71 | // if (node == end ()) return; |
| 72 | |
| 73 | // policy_.erase (s_iterator_to (node)); |
| 74 | // node->erase (); // will do cleanup here |
| 75 | // } |
| 76 | |
| 77 | // inline void |
| 78 | // clear () |
| 79 | // { |
| 80 | // policy_.clear (); |
| 81 | // trie_.clear (); |
| 82 | // } |
| 83 | |
| 84 | // template<typename Modifier> |
| 85 | // bool |
| 86 | // modify (iterator position, Modifier mod) |
| 87 | // { |
| 88 | // if (position == end ()) return false; |
| 89 | // if (position->payload () == PayloadTraits::empty_payload) return false; |
| 90 | |
| 91 | // mod (*position->payload ()); |
| 92 | // policy_.update (position); |
| 93 | // return true; |
| 94 | // } |
| 95 | |
| 96 | private: |
| 97 | mutable index_container policy_; |
| 98 | }; |
| 99 | |
| 100 | } // ndnSIM |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 101 | } // ndn |
Alexander Afanasyev | e77db79 | 2012-08-09 11:10:58 -0700 | [diff] [blame] | 102 | } // ns3 |
Alexander Afanasyev | 36b4577 | 2012-07-10 16:57:42 -0700 | [diff] [blame] | 103 | |
| 104 | #endif // PAYLOAD_WITH_POLICY_H_ |