Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #ifndef TRIE_WITH_POLICY_H_ |
| 22 | #define TRIE_WITH_POLICY_H_ |
| 23 | |
| 24 | #include "trie.h" |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame^] | 26 | namespace ndnSIM |
| 27 | { |
| 28 | |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 29 | template<typename FullKey, |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 30 | typename PayloadTraits, |
| 31 | typename PolicyTraits |
| 32 | > |
| 33 | class trie_with_policy |
| 34 | { |
| 35 | public: |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 36 | typedef trie< FullKey, |
| 37 | typename PayloadTraits::payload_type, |
| 38 | PayloadTraits, |
| 39 | typename PolicyTraits::policy_hook_type > parent_trie; |
| 40 | |
| 41 | typedef typename parent_trie::iterator iterator; |
| 42 | |
| 43 | typedef typename PolicyTraits::template policy< |
| 44 | trie_with_policy<FullKey, PayloadTraits, PolicyTraits>, |
| 45 | parent_trie, |
| 46 | typename PolicyTraits::template container_hook<parent_trie>::type >::type policy_container; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 47 | |
| 48 | inline |
| 49 | trie_with_policy (size_t bucketSize = 10, size_t bucketIncrement = 10) |
| 50 | : trie_ ("", bucketSize, bucketIncrement) |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 51 | , policy_ (*this) |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | |
| 55 | inline std::pair< iterator, bool > |
| 56 | insert (const FullKey &key, typename PayloadTraits::const_pointer_type payload) |
| 57 | { |
| 58 | std::pair<iterator, bool> item = |
| 59 | trie_.insert (key, payload); |
| 60 | |
| 61 | if (item.second) // real insert |
| 62 | { |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 63 | bool ok = policy_.insert (s_iterator_to (item.first)); |
| 64 | if (!ok) |
| 65 | { |
| 66 | item.first->erase (); // cannot insert |
| 67 | return std::make_pair (end (), false); |
| 68 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 69 | } |
| 70 | else |
| 71 | { |
| 72 | item.first->set_payload (payload); |
| 73 | policy_.update (s_iterator_to (item.first)); |
| 74 | } |
| 75 | |
| 76 | return item; |
| 77 | } |
| 78 | |
| 79 | inline void |
| 80 | erase (iterator node) |
| 81 | { |
| 82 | if (node == end ()) return; |
| 83 | |
| 84 | policy_.erase (s_iterator_to (node)); |
| 85 | node->erase (); // will do cleanup here |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @brief Find a node that has the longest common prefix with key (FIB/PIT lookup) |
| 90 | */ |
| 91 | inline iterator |
| 92 | longest_prefix_match (const FullKey &key) |
| 93 | { |
| 94 | boost::tuple< iterator, bool, iterator > item = trie_.find (key); |
| 95 | if (item.template get<0> () != trie_.end ()) |
| 96 | { |
| 97 | policy_.lookup (s_iterator_to (item.template get<0> ())); |
| 98 | } |
| 99 | return item.template get<0> (); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @brief Find a node that has prefix at least as the key (cache lookup) |
| 104 | */ |
| 105 | inline iterator |
| 106 | deepest_prefix_match (const FullKey &key) |
| 107 | { |
| 108 | iterator foundItem, lastItem; |
| 109 | bool reachLast; |
| 110 | boost::tie (foundItem, reachLast, lastItem) = trie_.find (key); |
| 111 | |
| 112 | // guard in case we don't have anything in the trie |
| 113 | if (lastItem == trie_.end ()) |
| 114 | return trie_.end (); |
| 115 | |
| 116 | if (reachLast) |
| 117 | { |
| 118 | if (foundItem == trie_.end ()) |
| 119 | { |
| 120 | foundItem = lastItem->find (); // should be something |
| 121 | } |
| 122 | policy_.lookup (s_iterator_to (foundItem)); |
| 123 | return foundItem; |
| 124 | } |
| 125 | else |
| 126 | { // couldn't find a node that has prefix at least as key |
| 127 | return trie_.end (); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @brief Find a node that has prefix at least as the key |
| 133 | */ |
| 134 | template<class Predicate> |
| 135 | inline iterator |
| 136 | deepest_prefix_match (const FullKey &key, Predicate pred) |
| 137 | { |
| 138 | iterator foundItem, lastItem; |
| 139 | bool reachLast; |
| 140 | boost::tie (foundItem, reachLast, lastItem) = trie_.find (key); |
| 141 | |
| 142 | // guard in case we don't have anything in the trie |
| 143 | if (lastItem == trie_.end ()) |
| 144 | return trie_.end (); |
| 145 | |
| 146 | if (reachLast) |
| 147 | { |
| 148 | foundItem = lastItem->find_if (pred); // may or may not find something |
| 149 | if (foundItem == trie_.end ()) |
| 150 | { |
| 151 | return trie_.end (); |
| 152 | } |
| 153 | policy_.lookup (s_iterator_to (foundItem)); |
| 154 | return foundItem; |
| 155 | } |
| 156 | else |
| 157 | { // couldn't find a node that has prefix at least as key |
| 158 | return trie_.end (); |
| 159 | } |
| 160 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 161 | |
| 162 | iterator end () |
| 163 | { |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | const parent_trie & |
| 168 | getTrie () const { return trie_; } |
| 169 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 170 | const policy_container & |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 171 | getPolicy () const { return policy_; } |
| 172 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 173 | policy_container & |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 174 | getPolicy () { return policy_; } |
| 175 | |
| 176 | static inline iterator |
| 177 | s_iterator_to (typename parent_trie::iterator item) |
| 178 | { |
| 179 | if (item == 0) |
| 180 | return 0; |
| 181 | else |
| 182 | return &(*item); |
| 183 | } |
| 184 | |
| 185 | private: |
| 186 | parent_trie trie_; |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 187 | policy_container policy_; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame^] | 190 | } // ndnSIM |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 191 | |
| 192 | #endif // TRIE_WITH_POLICY_H_ |
| 193 | |