blob: bb5a668bc0d476cf7c1408374a5217259ef94d58 [file] [log] [blame]
Alexander Afanasyev9a989702012-06-29 17:44:00 -07001/* -*- 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 Afanasyev9a989702012-06-29 17:44:00 -070025
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070026namespace ndnSIM
27{
28
Alexander Afanasyev9a989702012-06-29 17:44:00 -070029template<typename FullKey,
Alexander Afanasyev9a989702012-06-29 17:44:00 -070030 typename PayloadTraits,
31 typename PolicyTraits
32 >
33class trie_with_policy
34{
35public:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070036 typedef trie< FullKey,
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070037 PayloadTraits,
38 typename PolicyTraits::policy_hook_type > parent_trie;
39
40 typedef typename parent_trie::iterator iterator;
Alexander Afanasyev1ba09b82012-07-09 09:16:14 -070041 typedef typename parent_trie::const_iterator const_iterator;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070042
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 Afanasyev9a989702012-06-29 17:44:00 -070047
48 inline
49 trie_with_policy (size_t bucketSize = 10, size_t bucketIncrement = 10)
50 : trie_ ("", bucketSize, bucketIncrement)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070051 , policy_ (*this)
Alexander Afanasyev9a989702012-06-29 17:44:00 -070052 {
53 }
54
55 inline std::pair< iterator, bool >
Alexander Afanasyev0845c092012-07-13 17:45:33 -070056 insert (const FullKey &key, typename PayloadTraits::insert_type payload)
Alexander Afanasyev9a989702012-06-29 17:44:00 -070057 {
58 std::pair<iterator, bool> item =
59 trie_.insert (key, payload);
60
61 if (item.second) // real insert
62 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070063 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 Afanasyev9a989702012-06-29 17:44:00 -070069 }
70 else
71 {
Alexander Afanasyev78057c32012-07-06 15:18:46 -070072 return std::make_pair (s_iterator_to (item.first), false);
Alexander Afanasyev9a989702012-06-29 17:44:00 -070073 }
74
75 return item;
76 }
77
78 inline void
Alexander Afanasyev78057c32012-07-06 15:18:46 -070079 erase (const FullKey &key)
80 {
81 iterator foundItem, lastItem;
82 bool reachLast;
83 boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
84
85 if (!reachLast || lastItem->payload () == PayloadTraits::empty_payload)
86 return; // nothing to invalidate
87
88 erase (lastItem);
89 }
90
91 inline void
Alexander Afanasyev9a989702012-06-29 17:44:00 -070092 erase (iterator node)
93 {
94 if (node == end ()) return;
95
96 policy_.erase (s_iterator_to (node));
97 node->erase (); // will do cleanup here
98 }
99
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700100 inline void
101 clear ()
102 {
103 policy_.clear ();
104 trie_.clear ();
105 }
106
107 template<typename Modifier>
108 bool
109 modify (iterator position, Modifier mod)
110 {
111 if (position == end ()) return false;
112 if (position->payload () == PayloadTraits::empty_payload) return false;
113
114 mod (*position->payload ());
115 policy_.update (position);
116 return true;
117 }
118
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700119 /**
120 * @brief Find a node that has the longest common prefix with key (FIB/PIT lookup)
121 */
122 inline iterator
123 longest_prefix_match (const FullKey &key)
124 {
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700125 iterator foundItem, lastItem;
126 bool reachLast;
127 boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
128 if (foundItem != trie_.end ())
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700129 {
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700130 policy_.lookup (s_iterator_to (foundItem));
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700131 }
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700132 return foundItem;
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700133 }
134
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700135 // /**
136 // * @brief Const version of the longest common prefix match
137 // * (semi-const, because there could be update of the policy anyways)
138 // */
139 // inline const_iterator
140 // longest_prefix_match (const FullKey &key) const
141 // {
142 // return static_cast<trie_with_policy*> (this)->longest_prefix_match (key);
143 // }
144
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700145 /**
146 * @brief Find a node that has prefix at least as the key (cache lookup)
147 */
148 inline iterator
149 deepest_prefix_match (const FullKey &key)
150 {
151 iterator foundItem, lastItem;
152 bool reachLast;
153 boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
154
155 // guard in case we don't have anything in the trie
156 if (lastItem == trie_.end ())
157 return trie_.end ();
158
159 if (reachLast)
160 {
161 if (foundItem == trie_.end ())
162 {
163 foundItem = lastItem->find (); // should be something
164 }
165 policy_.lookup (s_iterator_to (foundItem));
166 return foundItem;
167 }
168 else
169 { // couldn't find a node that has prefix at least as key
170 return trie_.end ();
171 }
172 }
173
174 /**
175 * @brief Find a node that has prefix at least as the key
176 */
177 template<class Predicate>
178 inline iterator
179 deepest_prefix_match (const FullKey &key, Predicate pred)
180 {
181 iterator foundItem, lastItem;
182 bool reachLast;
183 boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
184
185 // guard in case we don't have anything in the trie
186 if (lastItem == trie_.end ())
187 return trie_.end ();
188
189 if (reachLast)
190 {
191 foundItem = lastItem->find_if (pred); // may or may not find something
192 if (foundItem == trie_.end ())
193 {
194 return trie_.end ();
195 }
196 policy_.lookup (s_iterator_to (foundItem));
197 return foundItem;
198 }
199 else
200 { // couldn't find a node that has prefix at least as key
201 return trie_.end ();
202 }
203 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700204
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700205 iterator end () const
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700206 {
207 return 0;
208 }
209
210 const parent_trie &
211 getTrie () const { return trie_; }
212
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700213 parent_trie &
214 getTrie () { return trie_; }
215
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700216 const policy_container &
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700217 getPolicy () const { return policy_; }
218
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700219 policy_container &
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700220 getPolicy () { return policy_; }
221
222 static inline iterator
223 s_iterator_to (typename parent_trie::iterator item)
224 {
225 if (item == 0)
226 return 0;
227 else
228 return &(*item);
229 }
230
231private:
232 parent_trie trie_;
Alexander Afanasyev30f60e32012-07-10 14:21:16 -0700233 mutable policy_container policy_;
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700234};
235
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700236} // ndnSIM
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700237
238#endif // TRIE_WITH_POLICY_H_