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