Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -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 TRIE_WITH_POLICY_H_ |
| 22 | #define TRIE_WITH_POLICY_H_ |
| 23 | |
| 24 | #include "trie.h" |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | e77db79 | 2012-08-09 11:10:58 -0700 | [diff] [blame^] | 26 | namespace ns3 |
| 27 | { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 28 | namespace ndnSIM |
| 29 | { |
| 30 | |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 31 | template<typename FullKey, |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 32 | typename PayloadTraits, |
| 33 | typename PolicyTraits |
| 34 | > |
| 35 | class trie_with_policy |
| 36 | { |
| 37 | public: |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 38 | typedef trie< FullKey, |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 39 | PayloadTraits, |
| 40 | typename PolicyTraits::policy_hook_type > parent_trie; |
| 41 | |
| 42 | typedef typename parent_trie::iterator iterator; |
Alexander Afanasyev | 1ba09b8 | 2012-07-09 09:16:14 -0700 | [diff] [blame] | 43 | typedef typename parent_trie::const_iterator const_iterator; |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 44 | |
| 45 | typedef typename PolicyTraits::template policy< |
| 46 | trie_with_policy<FullKey, PayloadTraits, PolicyTraits>, |
| 47 | parent_trie, |
| 48 | typename PolicyTraits::template container_hook<parent_trie>::type >::type policy_container; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 49 | |
| 50 | inline |
| 51 | trie_with_policy (size_t bucketSize = 10, size_t bucketIncrement = 10) |
| 52 | : trie_ ("", bucketSize, bucketIncrement) |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 53 | , policy_ (*this) |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 54 | { |
| 55 | } |
| 56 | |
| 57 | inline std::pair< iterator, bool > |
Alexander Afanasyev | 0845c09 | 2012-07-13 17:45:33 -0700 | [diff] [blame] | 58 | insert (const FullKey &key, typename PayloadTraits::insert_type payload) |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 59 | { |
| 60 | std::pair<iterator, bool> item = |
| 61 | trie_.insert (key, payload); |
| 62 | |
| 63 | if (item.second) // real insert |
| 64 | { |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 65 | bool ok = policy_.insert (s_iterator_to (item.first)); |
| 66 | if (!ok) |
| 67 | { |
| 68 | item.first->erase (); // cannot insert |
| 69 | return std::make_pair (end (), false); |
| 70 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 71 | } |
| 72 | else |
| 73 | { |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 74 | return std::make_pair (s_iterator_to (item.first), false); |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | return item; |
| 78 | } |
| 79 | |
| 80 | inline void |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 81 | erase (const FullKey &key) |
| 82 | { |
| 83 | iterator foundItem, lastItem; |
| 84 | bool reachLast; |
| 85 | boost::tie (foundItem, reachLast, lastItem) = trie_.find (key); |
| 86 | |
| 87 | if (!reachLast || lastItem->payload () == PayloadTraits::empty_payload) |
| 88 | return; // nothing to invalidate |
| 89 | |
| 90 | erase (lastItem); |
| 91 | } |
| 92 | |
| 93 | inline void |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 94 | erase (iterator node) |
| 95 | { |
| 96 | if (node == end ()) return; |
| 97 | |
| 98 | policy_.erase (s_iterator_to (node)); |
| 99 | node->erase (); // will do cleanup here |
| 100 | } |
| 101 | |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 102 | inline void |
| 103 | clear () |
| 104 | { |
| 105 | policy_.clear (); |
| 106 | trie_.clear (); |
| 107 | } |
| 108 | |
| 109 | template<typename Modifier> |
| 110 | bool |
| 111 | modify (iterator position, Modifier mod) |
| 112 | { |
| 113 | if (position == end ()) return false; |
| 114 | if (position->payload () == PayloadTraits::empty_payload) return false; |
| 115 | |
| 116 | mod (*position->payload ()); |
| 117 | policy_.update (position); |
| 118 | return true; |
| 119 | } |
| 120 | |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 121 | /** |
| 122 | * @brief Find a node that has the longest common prefix with key (FIB/PIT lookup) |
| 123 | */ |
| 124 | inline iterator |
| 125 | longest_prefix_match (const FullKey &key) |
| 126 | { |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 127 | iterator foundItem, lastItem; |
| 128 | bool reachLast; |
| 129 | boost::tie (foundItem, reachLast, lastItem) = trie_.find (key); |
| 130 | if (foundItem != trie_.end ()) |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 131 | { |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 132 | policy_.lookup (s_iterator_to (foundItem)); |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 133 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 134 | return foundItem; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 137 | // /** |
| 138 | // * @brief Const version of the longest common prefix match |
| 139 | // * (semi-const, because there could be update of the policy anyways) |
| 140 | // */ |
| 141 | // inline const_iterator |
| 142 | // longest_prefix_match (const FullKey &key) const |
| 143 | // { |
| 144 | // return static_cast<trie_with_policy*> (this)->longest_prefix_match (key); |
| 145 | // } |
| 146 | |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 147 | /** |
| 148 | * @brief Find a node that has prefix at least as the key (cache lookup) |
| 149 | */ |
| 150 | inline iterator |
| 151 | deepest_prefix_match (const FullKey &key) |
| 152 | { |
| 153 | iterator foundItem, lastItem; |
| 154 | bool reachLast; |
| 155 | boost::tie (foundItem, reachLast, lastItem) = trie_.find (key); |
| 156 | |
| 157 | // guard in case we don't have anything in the trie |
| 158 | if (lastItem == trie_.end ()) |
| 159 | return trie_.end (); |
| 160 | |
| 161 | if (reachLast) |
| 162 | { |
| 163 | if (foundItem == trie_.end ()) |
| 164 | { |
| 165 | foundItem = lastItem->find (); // should be something |
| 166 | } |
| 167 | policy_.lookup (s_iterator_to (foundItem)); |
| 168 | return foundItem; |
| 169 | } |
| 170 | else |
| 171 | { // couldn't find a node that has prefix at least as key |
| 172 | return trie_.end (); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @brief Find a node that has prefix at least as the key |
| 178 | */ |
| 179 | template<class Predicate> |
| 180 | inline iterator |
| 181 | deepest_prefix_match (const FullKey &key, Predicate pred) |
| 182 | { |
| 183 | iterator foundItem, lastItem; |
| 184 | bool reachLast; |
| 185 | boost::tie (foundItem, reachLast, lastItem) = trie_.find (key); |
| 186 | |
| 187 | // guard in case we don't have anything in the trie |
| 188 | if (lastItem == trie_.end ()) |
| 189 | return trie_.end (); |
| 190 | |
| 191 | if (reachLast) |
| 192 | { |
| 193 | foundItem = lastItem->find_if (pred); // may or may not find something |
| 194 | if (foundItem == trie_.end ()) |
| 195 | { |
| 196 | return trie_.end (); |
| 197 | } |
| 198 | policy_.lookup (s_iterator_to (foundItem)); |
| 199 | return foundItem; |
| 200 | } |
| 201 | else |
| 202 | { // couldn't find a node that has prefix at least as key |
| 203 | return trie_.end (); |
| 204 | } |
| 205 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 206 | |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 207 | iterator end () const |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 208 | { |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | const parent_trie & |
| 213 | getTrie () const { return trie_; } |
| 214 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 215 | parent_trie & |
| 216 | getTrie () { return trie_; } |
| 217 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 218 | const policy_container & |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 219 | getPolicy () const { return policy_; } |
| 220 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 221 | policy_container & |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 222 | getPolicy () { return policy_; } |
| 223 | |
| 224 | static inline iterator |
| 225 | s_iterator_to (typename parent_trie::iterator item) |
| 226 | { |
| 227 | if (item == 0) |
| 228 | return 0; |
| 229 | else |
| 230 | return &(*item); |
| 231 | } |
| 232 | |
| 233 | private: |
| 234 | parent_trie trie_; |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 235 | mutable policy_container policy_; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 236 | }; |
| 237 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 238 | } // ndnSIM |
Alexander Afanasyev | e77db79 | 2012-08-09 11:10:58 -0700 | [diff] [blame^] | 239 | } // ns3 |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 240 | |
| 241 | #endif // TRIE_WITH_POLICY_H_ |