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