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