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 | |
| 26 | template<typename FullKey, |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 27 | typename PayloadTraits, |
| 28 | typename PolicyTraits |
| 29 | > |
| 30 | class trie_with_policy |
| 31 | { |
| 32 | public: |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 33 | typedef trie< FullKey, |
| 34 | typename PayloadTraits::payload_type, |
| 35 | PayloadTraits, |
| 36 | typename PolicyTraits::policy_hook_type > parent_trie; |
| 37 | |
| 38 | typedef typename parent_trie::iterator iterator; |
| 39 | |
| 40 | typedef typename PolicyTraits::template policy< |
| 41 | trie_with_policy<FullKey, PayloadTraits, PolicyTraits>, |
| 42 | parent_trie, |
| 43 | typename PolicyTraits::template container_hook<parent_trie>::type >::type policy_container; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 44 | |
| 45 | inline |
| 46 | trie_with_policy (size_t bucketSize = 10, size_t bucketIncrement = 10) |
| 47 | : trie_ ("", bucketSize, bucketIncrement) |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 48 | , policy_ (*this) |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | |
| 52 | inline std::pair< iterator, bool > |
| 53 | insert (const FullKey &key, typename PayloadTraits::const_pointer_type payload) |
| 54 | { |
| 55 | std::pair<iterator, bool> item = |
| 56 | trie_.insert (key, payload); |
| 57 | |
| 58 | if (item.second) // real insert |
| 59 | { |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 60 | bool ok = policy_.insert (s_iterator_to (item.first)); |
| 61 | if (!ok) |
| 62 | { |
| 63 | item.first->erase (); // cannot insert |
| 64 | return std::make_pair (end (), false); |
| 65 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 66 | } |
| 67 | else |
| 68 | { |
| 69 | item.first->set_payload (payload); |
| 70 | policy_.update (s_iterator_to (item.first)); |
| 71 | } |
| 72 | |
| 73 | return item; |
| 74 | } |
| 75 | |
| 76 | inline void |
| 77 | erase (iterator node) |
| 78 | { |
| 79 | if (node == end ()) return; |
| 80 | |
| 81 | policy_.erase (s_iterator_to (node)); |
| 82 | node->erase (); // will do cleanup here |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @brief Find a node that has the longest common prefix with key (FIB/PIT lookup) |
| 87 | */ |
| 88 | inline iterator |
| 89 | longest_prefix_match (const FullKey &key) |
| 90 | { |
| 91 | boost::tuple< iterator, bool, iterator > item = trie_.find (key); |
| 92 | if (item.template get<0> () != trie_.end ()) |
| 93 | { |
| 94 | policy_.lookup (s_iterator_to (item.template get<0> ())); |
| 95 | } |
| 96 | return item.template get<0> (); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @brief Find a node that has prefix at least as the key (cache lookup) |
| 101 | */ |
| 102 | inline iterator |
| 103 | deepest_prefix_match (const FullKey &key) |
| 104 | { |
| 105 | iterator foundItem, lastItem; |
| 106 | bool reachLast; |
| 107 | boost::tie (foundItem, reachLast, lastItem) = trie_.find (key); |
| 108 | |
| 109 | // guard in case we don't have anything in the trie |
| 110 | if (lastItem == trie_.end ()) |
| 111 | return trie_.end (); |
| 112 | |
| 113 | if (reachLast) |
| 114 | { |
| 115 | if (foundItem == trie_.end ()) |
| 116 | { |
| 117 | foundItem = lastItem->find (); // should be something |
| 118 | } |
| 119 | policy_.lookup (s_iterator_to (foundItem)); |
| 120 | return foundItem; |
| 121 | } |
| 122 | else |
| 123 | { // couldn't find a node that has prefix at least as key |
| 124 | return trie_.end (); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @brief Find a node that has prefix at least as the key |
| 130 | */ |
| 131 | template<class Predicate> |
| 132 | inline iterator |
| 133 | deepest_prefix_match (const FullKey &key, Predicate pred) |
| 134 | { |
| 135 | iterator foundItem, lastItem; |
| 136 | bool reachLast; |
| 137 | boost::tie (foundItem, reachLast, lastItem) = trie_.find (key); |
| 138 | |
| 139 | // guard in case we don't have anything in the trie |
| 140 | if (lastItem == trie_.end ()) |
| 141 | return trie_.end (); |
| 142 | |
| 143 | if (reachLast) |
| 144 | { |
| 145 | foundItem = lastItem->find_if (pred); // may or may not find something |
| 146 | if (foundItem == trie_.end ()) |
| 147 | { |
| 148 | return trie_.end (); |
| 149 | } |
| 150 | policy_.lookup (s_iterator_to (foundItem)); |
| 151 | return foundItem; |
| 152 | } |
| 153 | else |
| 154 | { // couldn't find a node that has prefix at least as key |
| 155 | return trie_.end (); |
| 156 | } |
| 157 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 158 | |
| 159 | iterator end () |
| 160 | { |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | const parent_trie & |
| 165 | getTrie () const { return trie_; } |
| 166 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 167 | const policy_container & |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 168 | getPolicy () const { return policy_; } |
| 169 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 170 | policy_container & |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 171 | getPolicy () { return policy_; } |
| 172 | |
| 173 | static inline iterator |
| 174 | s_iterator_to (typename parent_trie::iterator item) |
| 175 | { |
| 176 | if (item == 0) |
| 177 | return 0; |
| 178 | else |
| 179 | return &(*item); |
| 180 | } |
| 181 | |
| 182 | private: |
| 183 | parent_trie trie_; |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 184 | policy_container policy_; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | |
| 188 | #endif // TRIE_WITH_POLICY_H_ |
| 189 | |