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