Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -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 | |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 21 | #ifndef TRIE_H_ |
| 22 | #define TRIE_H_ |
| 23 | |
| 24 | #include "ns3/ptr.h" |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 25 | |
| 26 | #include <boost/intrusive/unordered_set.hpp> |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 27 | #include <boost/intrusive/list.hpp> |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 28 | #include <boost/intrusive/set.hpp> |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 29 | #include <boost/functional/hash.hpp> |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 30 | #include <boost/interprocess/smart_ptr/unique_ptr.hpp> |
| 31 | #include <boost/tuple/tuple.hpp> |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 32 | #include <boost/foreach.hpp> |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 33 | #include <boost/mpl/if.hpp> |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 35 | namespace ndnSIM |
| 36 | { |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 37 | |
| 38 | ///////////////////////////////////////////////////// |
| 39 | // Allow customization for payload |
| 40 | // |
| 41 | template<typename Payload> |
| 42 | struct pointer_payload_traits |
| 43 | { |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 44 | typedef Payload payload_type; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 45 | typedef Payload* pointer_type; |
| 46 | typedef const Payload* const_pointer_type; |
| 47 | |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 48 | static Payload* empty_payload; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | template<typename Payload> |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 52 | Payload* |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 53 | pointer_payload_traits<Payload>::empty_payload = 0; |
| 54 | |
| 55 | template<typename Payload> |
| 56 | struct smart_pointer_payload_traits |
| 57 | { |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 58 | typedef Payload payload_type; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 59 | typedef ns3::Ptr<Payload> pointer_type; |
| 60 | typedef ns3::Ptr<const Payload> const_pointer_type; |
| 61 | |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 62 | static ns3::Ptr<Payload> empty_payload; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | template<typename Payload> |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 66 | ns3::Ptr<Payload> |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 67 | smart_pointer_payload_traits<Payload>::empty_payload = 0; |
| 68 | |
| 69 | |
| 70 | //////////////////////////////////////////////////// |
| 71 | // forward declarations |
| 72 | // |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 73 | template<typename FullKey, |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 74 | typename PayloadTraits, |
| 75 | typename PolicyHook > |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 76 | class trie; |
| 77 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 78 | template<typename FullKey, typename PayloadTraits, typename PolicyHook> |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 79 | inline std::ostream& |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 80 | operator << (std::ostream &os, |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 81 | const trie<FullKey, PayloadTraits, PolicyHook> &trie_node); |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 82 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 83 | template<typename FullKey, typename PayloadTraits, typename PolicyHook> |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 84 | bool |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 85 | operator== (const trie<FullKey, PayloadTraits, PolicyHook> &a, |
| 86 | const trie<FullKey, PayloadTraits, PolicyHook> &b); |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 87 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 88 | template<typename FullKey, typename PayloadTraits, typename PolicyHook > |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 89 | std::size_t |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 90 | hash_value (const trie<FullKey, PayloadTraits, PolicyHook> &trie_node); |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 91 | |
| 92 | /////////////////////////////////////////////////// |
| 93 | // actual definition |
| 94 | // |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 95 | template<class T> |
| 96 | class trie_iterator; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 97 | |
| 98 | template<typename FullKey, |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 99 | typename PayloadTraits, |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 100 | typename PolicyHook > |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 101 | class trie |
| 102 | { |
| 103 | public: |
| 104 | typedef typename FullKey::partial_type Key; |
| 105 | |
| 106 | typedef trie* iterator; |
| 107 | typedef const trie* const_iterator; |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 108 | |
| 109 | typedef trie_iterator<trie> recursive_iterator; |
| 110 | typedef trie_iterator<const trie> const_recursive_iterator; |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 111 | |
| 112 | typedef PayloadTraits payload_traits; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 113 | |
| 114 | inline |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 115 | trie (const Key &key, size_t bucketSize = 10, size_t bucketIncrement = 10) |
| 116 | : key_ (key) |
| 117 | , initialBucketSize_ (bucketSize) |
| 118 | , bucketIncrement_ (bucketIncrement) |
| 119 | , bucketSize_ (initialBucketSize_) |
| 120 | , buckets_ (new bucket_type [bucketSize_]) //cannot use normal pointer, because lifetime of buckets should be larger than lifetime of the container |
| 121 | , children_ (bucket_traits (buckets_.get (), bucketSize_)) |
| 122 | , payload_ (PayloadTraits::empty_payload) |
| 123 | , parent_ (0) |
| 124 | { |
| 125 | } |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 126 | |
| 127 | inline |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 128 | ~trie () |
| 129 | { |
| 130 | payload_ = PayloadTraits::empty_payload; // necessary for smart pointers... |
| 131 | children_.clear_and_dispose (trie_delete_disposer ()); |
| 132 | } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 133 | |
| 134 | void |
| 135 | clear () |
| 136 | { |
| 137 | children_.clear_and_dispose (trie_delete_disposer ()); |
| 138 | } |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 139 | |
| 140 | template<class Predicate> |
| 141 | void |
| 142 | clear_if (Predicate cond) |
| 143 | { |
| 144 | recursive_iterator trieNode (this); |
| 145 | recursive_iterator end (0); |
| 146 | |
| 147 | while (trieNode != end) |
| 148 | { |
| 149 | if (cond (*trieNode)) |
| 150 | { |
| 151 | trieNode = recursive_iterator (trieNode->erase ()); |
| 152 | } |
| 153 | trieNode ++; |
| 154 | } |
| 155 | } |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 156 | |
| 157 | // actual entry |
| 158 | friend bool |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 159 | operator== <> (const trie<FullKey, PayloadTraits, PolicyHook> &a, |
| 160 | const trie<FullKey, PayloadTraits, PolicyHook> &b); |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 161 | |
| 162 | friend std::size_t |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 163 | hash_value <> (const trie<FullKey, PayloadTraits, PolicyHook> &trie_node); |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 164 | |
| 165 | inline std::pair<iterator, bool> |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 166 | insert (const FullKey &key, |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 167 | typename PayloadTraits::pointer_type payload) |
| 168 | { |
| 169 | trie *trieNode = this; |
| 170 | |
| 171 | BOOST_FOREACH (const Key &subkey, key) |
| 172 | { |
| 173 | typename unordered_set::iterator item = trieNode->children_.find (subkey); |
| 174 | if (item == trieNode->children_.end ()) |
| 175 | { |
| 176 | trie *newNode = new trie (subkey, initialBucketSize_, bucketIncrement_); |
| 177 | // std::cout << "new " << newNode << "\n"; |
| 178 | newNode->parent_ = trieNode; |
| 179 | |
| 180 | if (trieNode->children_.size () >= trieNode->bucketSize_) |
| 181 | { |
| 182 | trieNode->bucketSize_ += trieNode->bucketIncrement_; |
| 183 | buckets_array newBuckets (new bucket_type [trieNode->bucketSize_]); |
| 184 | trieNode->children_.rehash (bucket_traits (newBuckets.get (), trieNode->bucketSize_)); |
| 185 | trieNode->buckets_.swap (newBuckets); |
| 186 | } |
| 187 | |
| 188 | std::pair< typename unordered_set::iterator, bool > ret = |
| 189 | trieNode->children_.insert (*newNode); |
| 190 | |
| 191 | trieNode = &(*ret.first); |
| 192 | } |
| 193 | else |
| 194 | trieNode = &(*item); |
| 195 | } |
| 196 | |
| 197 | if (trieNode->payload_ == PayloadTraits::empty_payload) |
| 198 | { |
| 199 | trieNode->payload_ = payload; |
| 200 | return std::make_pair (trieNode, true); |
| 201 | } |
| 202 | else |
| 203 | return std::make_pair (trieNode, false); |
| 204 | } |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * @brief Removes payload (if it exists) and if there are no children, prunes parents trie |
| 208 | */ |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 209 | inline iterator |
| 210 | erase () |
| 211 | { |
| 212 | payload_ = PayloadTraits::empty_payload; |
| 213 | return prune (); |
| 214 | } |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 215 | |
| 216 | /** |
| 217 | * @brief Do exactly as erase, but without erasing the payload |
| 218 | */ |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 219 | inline iterator |
| 220 | prune () |
| 221 | { |
| 222 | if (payload_ == PayloadTraits::empty_payload && |
| 223 | children_.size () == 0) |
| 224 | { |
| 225 | if (parent_ == 0) return this; |
| 226 | |
| 227 | trie *parent = parent_; |
| 228 | parent->children_.erase_and_dispose (*this, trie_delete_disposer ()); // delete this; basically, committing a suicide |
| 229 | |
| 230 | return parent->prune (); |
| 231 | } |
| 232 | return this; |
| 233 | } |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 234 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 235 | // inline boost::tuple<const iterator, bool, const iterator> |
| 236 | // find (const FullKey &key) const |
| 237 | // { |
| 238 | // return const_cast<trie*> (this)->find (key); |
| 239 | // } |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 240 | |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 241 | /** |
| 242 | * @brief Perform the longest prefix match |
| 243 | * @param key the key for which to perform the longest prefix match |
| 244 | * |
| 245 | * @return ->second is true if prefix in ->first is longer than key |
| 246 | */ |
| 247 | inline boost::tuple<iterator, bool, iterator> |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 248 | find (const FullKey &key) |
| 249 | { |
| 250 | trie *trieNode = this; |
| 251 | iterator foundNode = (payload_ != PayloadTraits::empty_payload) ? this : 0; |
| 252 | bool reachLast = true; |
| 253 | |
| 254 | BOOST_FOREACH (const Key &subkey, key) |
| 255 | { |
| 256 | typename unordered_set::iterator item = trieNode->children_.find (subkey); |
| 257 | if (item == trieNode->children_.end ()) |
| 258 | { |
| 259 | reachLast = false; |
| 260 | break; |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | trieNode = &(*item); |
| 265 | |
| 266 | if (trieNode->payload_ != PayloadTraits::empty_payload) |
| 267 | foundNode = trieNode; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | return boost::make_tuple (foundNode, reachLast, trieNode); |
| 272 | } |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 273 | |
| 274 | /** |
| 275 | * @brief Find next payload of the sub-trie |
| 276 | * @returns end() or a valid iterator pointing to the trie leaf (order is not defined, enumeration ) |
| 277 | */ |
| 278 | inline iterator |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 279 | find () |
| 280 | { |
| 281 | if (payload_ != PayloadTraits::empty_payload) |
| 282 | return this; |
| 283 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 284 | typedef trie<FullKey, PayloadTraits, PolicyHook> trie; |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 285 | for (typename trie::unordered_set::iterator subnode = children_.begin (); |
| 286 | subnode != children_.end (); |
| 287 | subnode++ ) |
| 288 | // BOOST_FOREACH (trie &subnode, children_) |
| 289 | { |
| 290 | iterator value = subnode->find (); |
| 291 | if (value != 0) |
| 292 | return value; |
| 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 297 | |
| 298 | /** |
| 299 | * @brief Find next payload of the sub-trie satisfying the predicate |
| 300 | * @param pred predicate |
| 301 | * @returns end() or a valid iterator pointing to the trie leaf (order is not defined, enumeration ) |
| 302 | */ |
| 303 | template<class Predicate> |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 304 | inline const iterator |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 305 | find_if (Predicate pred) |
| 306 | { |
| 307 | if (payload_ != PayloadTraits::empty_payload && pred (payload_)) |
| 308 | return this; |
| 309 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 310 | typedef trie<FullKey, PayloadTraits, PolicyHook> trie; |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 311 | for (typename trie::unordered_set::iterator subnode = children_.begin (); |
| 312 | subnode != children_.end (); |
| 313 | subnode++ ) |
| 314 | // BOOST_FOREACH (const trie &subnode, children_) |
| 315 | { |
| 316 | iterator value = subnode->find (); |
| 317 | if (value != 0) |
| 318 | return value; |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 323 | |
| 324 | iterator end () |
| 325 | { |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | const_iterator end () const |
| 330 | { |
| 331 | return 0; |
| 332 | } |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 333 | |
| 334 | typename PayloadTraits::const_pointer_type |
| 335 | payload () const |
| 336 | { |
| 337 | return payload_; |
| 338 | } |
| 339 | |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 340 | typename PayloadTraits::pointer_type |
| 341 | payload () |
| 342 | { |
| 343 | return payload_; |
| 344 | } |
| 345 | |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 346 | void |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 347 | set_payload (typename PayloadTraits::pointer_type payload) |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 348 | { |
| 349 | payload_ = payload; |
| 350 | } |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 351 | |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 352 | inline void |
| 353 | PrintStat (std::ostream &os) const; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 354 | |
| 355 | private: |
| 356 | //The disposer object function |
| 357 | struct trie_delete_disposer |
| 358 | { |
| 359 | void operator() (trie *delete_this) |
| 360 | { |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 361 | delete delete_this; |
| 362 | } |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 363 | }; |
| 364 | |
| 365 | template<class D> |
| 366 | struct array_disposer |
| 367 | { |
| 368 | void operator() (D *array) |
| 369 | { |
| 370 | delete [] array; |
| 371 | } |
| 372 | }; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 373 | |
| 374 | friend |
| 375 | std::ostream& |
| 376 | operator<< < > (std::ostream &os, const trie &trie_node); |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 377 | |
| 378 | public: |
| 379 | PolicyHook policy_hook_; |
| 380 | |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 381 | private: |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 382 | boost::intrusive::unordered_set_member_hook<> unordered_set_member_hook_; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 383 | |
| 384 | // necessary typedefs |
| 385 | typedef trie self_type; |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 386 | typedef boost::intrusive::member_hook< trie, |
| 387 | boost::intrusive::unordered_set_member_hook< >, |
| 388 | &trie::unordered_set_member_hook_ > member_hook; |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 389 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 390 | typedef boost::intrusive::unordered_set< trie, member_hook > unordered_set; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 391 | typedef typename unordered_set::bucket_type bucket_type; |
| 392 | typedef typename unordered_set::bucket_traits bucket_traits; |
| 393 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 394 | template<class T> |
| 395 | friend class trie_iterator; |
| 396 | |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 397 | //////////////////////////////////////////////// |
| 398 | // Actual data |
| 399 | //////////////////////////////////////////////// |
| 400 | |
| 401 | Key key_; ///< name component |
| 402 | |
| 403 | size_t initialBucketSize_; |
| 404 | size_t bucketIncrement_; |
| 405 | |
| 406 | size_t bucketSize_; |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 407 | typedef boost::interprocess::unique_ptr< bucket_type, array_disposer<bucket_type> > buckets_array; |
| 408 | buckets_array buckets_; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 409 | unordered_set children_; |
| 410 | |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 411 | typename PayloadTraits::pointer_type payload_; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 412 | trie *parent_; // to make cleaning effective |
| 413 | }; |
| 414 | |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 415 | |
| 416 | |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 417 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 418 | template<typename FullKey, typename PayloadTraits, typename PolicyHook> |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 419 | inline std::ostream& |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 420 | operator << (std::ostream &os, const trie<FullKey, PayloadTraits, PolicyHook> &trie_node) |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 421 | { |
| 422 | os << "# " << trie_node.key_ << ((trie_node.payload_ != 0)?"*":"") << std::endl; |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 423 | typedef trie<FullKey, PayloadTraits, PolicyHook> trie; |
Alexander Afanasyev | 051d378 | 2012-07-03 10:10:15 -0700 | [diff] [blame] | 424 | |
| 425 | for (typename trie::unordered_set::const_iterator subnode = trie_node.children_.begin (); |
| 426 | subnode != trie_node.children_.end (); |
| 427 | subnode++ ) |
| 428 | // BOOST_FOREACH (const trie &subnode, trie_node.children_) |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 429 | { |
| 430 | os << "\"" << &trie_node << "\"" << " [label=\"" << trie_node.key_ << ((trie_node.payload_ != 0)?"*":"") << "\"]\n"; |
Alexander Afanasyev | 051d378 | 2012-07-03 10:10:15 -0700 | [diff] [blame] | 431 | os << "\"" << &(*subnode) << "\"" << " [label=\"" << subnode->key_ << ((subnode->payload_ != 0)?"*":"") << "\"]""\n"; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 432 | |
Alexander Afanasyev | 051d378 | 2012-07-03 10:10:15 -0700 | [diff] [blame] | 433 | os << "\"" << &trie_node << "\"" << " -> " << "\"" << &(*subnode) << "\"" << "\n"; |
| 434 | os << *subnode; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | return os; |
| 438 | } |
| 439 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 440 | template<typename FullKey, typename PayloadTraits, typename PolicyHook> |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 441 | inline void |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 442 | trie<FullKey, PayloadTraits, PolicyHook> |
Alexander Afanasyev | b0c4389 | 2012-06-13 00:52:58 -0700 | [diff] [blame] | 443 | ::PrintStat (std::ostream &os) const |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 444 | { |
| 445 | os << "# " << key_ << ((payload_ != 0)?"*":"") << ": " << children_.size() << " children" << std::endl; |
| 446 | for (size_t bucket = 0, maxbucket = children_.bucket_count (); |
| 447 | bucket < maxbucket; |
| 448 | bucket++) |
| 449 | { |
| 450 | os << " " << children_.bucket_size (bucket); |
| 451 | } |
| 452 | os << "\n"; |
| 453 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 454 | typedef trie<FullKey, PayloadTraits, PolicyHook> trie; |
Alexander Afanasyev | 051d378 | 2012-07-03 10:10:15 -0700 | [diff] [blame] | 455 | for (typename trie::unordered_set::const_iterator subnode = children_.begin (); |
| 456 | subnode != children_.end (); |
| 457 | subnode++ ) |
| 458 | // BOOST_FOREACH (const trie &subnode, children_) |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 459 | { |
Alexander Afanasyev | 051d378 | 2012-07-03 10:10:15 -0700 | [diff] [blame] | 460 | subnode->PrintStat (os); |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 465 | template<typename FullKey, typename PayloadTraits, typename PolicyHook> |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 466 | inline bool |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 467 | operator == (const trie<FullKey, PayloadTraits, PolicyHook> &a, |
| 468 | const trie<FullKey, PayloadTraits, PolicyHook> &b) |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 469 | { |
| 470 | return a.key_ == b.key_; |
| 471 | } |
| 472 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 473 | template<typename FullKey, typename PayloadTraits, typename PolicyHook> |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 474 | inline std::size_t |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 475 | hash_value (const trie<FullKey, PayloadTraits, PolicyHook> &trie_node) |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 476 | { |
| 477 | return boost::hash_value (trie_node.key_); |
| 478 | } |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 479 | |
Alexander Afanasyev | 44bb6ea | 2012-07-09 08:44:41 -0700 | [diff] [blame] | 480 | |
| 481 | |
| 482 | template<class Trie> |
| 483 | class trie_iterator |
| 484 | { |
| 485 | public: |
| 486 | trie_iterator () : trie_ (0) {} |
| 487 | trie_iterator (typename Trie::iterator item) : trie_ (item) {} |
| 488 | trie_iterator (Trie &item) : trie_ (&item) {} |
| 489 | |
| 490 | Trie & operator* () { return *trie_; } |
| 491 | const Trie & operator* () const { return *trie_; } |
| 492 | Trie * operator-> () { return trie_; } |
| 493 | const Trie * operator-> () const { return trie_; } |
| 494 | bool operator== (trie_iterator<const Trie> &other) const { return (trie_ == other.trie_); } |
| 495 | bool operator== (trie_iterator<Trie> &other) { return (trie_ == other.trie_); } |
| 496 | bool operator!= (trie_iterator<const Trie> &other) const { return !(*this == other); } |
| 497 | bool operator!= (trie_iterator<Trie> &other) { return !(*this == other); } |
| 498 | |
| 499 | trie_iterator<Trie> & |
| 500 | operator++ (int) |
| 501 | { |
| 502 | if (trie_->children_.size () > 0) |
| 503 | trie_ = &(*trie_->children_.begin ()); |
| 504 | else |
| 505 | trie_ = goUp (); |
| 506 | return *this; |
| 507 | } |
| 508 | |
| 509 | trie_iterator<Trie> & |
| 510 | operator++ () |
| 511 | { |
| 512 | (*this)++; |
| 513 | return *this; |
| 514 | } |
| 515 | |
| 516 | private: |
| 517 | typedef typename boost::mpl::if_< boost::is_same<Trie, const Trie>, |
| 518 | typename Trie::unordered_set::const_iterator, |
| 519 | typename Trie::unordered_set::iterator>::type set_iterator; |
| 520 | |
| 521 | Trie* goUp () |
| 522 | { |
| 523 | if (trie_->parent_ != 0) |
| 524 | { |
| 525 | // typename Trie::unordered_set::iterator item = |
| 526 | set_iterator item = trie_->parent_->children_.iterator_to (*trie_); |
| 527 | item++; |
| 528 | if (item != trie_->parent_->children_.end ()) |
| 529 | { |
| 530 | return &(*item); |
| 531 | } |
| 532 | else |
| 533 | { |
| 534 | trie_ = trie_->parent_; |
| 535 | return goUp (); |
| 536 | } |
| 537 | } |
| 538 | else |
| 539 | return 0; |
| 540 | } |
| 541 | private: |
| 542 | Trie *trie_; |
| 543 | }; |
| 544 | |
| 545 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 546 | } // ndnSIM |
| 547 | |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 548 | #endif // TRIE_H_ |