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