blob: a2be29ae7910a7451f8a21fc507f781c4e0f939d [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,
37 typename PayloadTraits::payload_type,
38 PayloadTraits,
39 typename PolicyTraits::policy_hook_type > parent_trie;
40
41 typedef typename parent_trie::iterator iterator;
Alexander Afanasyev1ba09b82012-07-09 09:16:14 -070042 typedef typename parent_trie::const_iterator const_iterator;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070043
44 typedef typename PolicyTraits::template policy<
45 trie_with_policy<FullKey, PayloadTraits, PolicyTraits>,
46 parent_trie,
47 typename PolicyTraits::template container_hook<parent_trie>::type >::type policy_container;
Alexander Afanasyev9a989702012-06-29 17:44:00 -070048
49 inline
50 trie_with_policy (size_t bucketSize = 10, size_t bucketIncrement = 10)
51 : trie_ ("", bucketSize, bucketIncrement)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070052 , policy_ (*this)
Alexander Afanasyev9a989702012-06-29 17:44:00 -070053 {
54 }
55
56 inline std::pair< iterator, bool >
Alexander Afanasyev78057c32012-07-06 15:18:46 -070057 insert (const FullKey &key, typename PayloadTraits::pointer_type payload)
Alexander Afanasyev9a989702012-06-29 17:44:00 -070058 {
59 std::pair<iterator, bool> item =
60 trie_.insert (key, payload);
61
62 if (item.second) // real insert
63 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070064 bool ok = policy_.insert (s_iterator_to (item.first));
65 if (!ok)
66 {
67 item.first->erase (); // cannot insert
68 return std::make_pair (end (), false);
69 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070070 }
71 else
72 {
Alexander Afanasyev78057c32012-07-06 15:18:46 -070073 return std::make_pair (s_iterator_to (item.first), false);
Alexander Afanasyev9a989702012-06-29 17:44:00 -070074 }
75
76 return item;
77 }
78
79 inline void
Alexander Afanasyev78057c32012-07-06 15:18:46 -070080 erase (const FullKey &key)
81 {
82 iterator foundItem, lastItem;
83 bool reachLast;
84 boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
85
86 if (!reachLast || lastItem->payload () == PayloadTraits::empty_payload)
87 return; // nothing to invalidate
88
89 erase (lastItem);
90 }
91
92 inline void
Alexander Afanasyev9a989702012-06-29 17:44:00 -070093 erase (iterator node)
94 {
95 if (node == end ()) return;
96
97 policy_.erase (s_iterator_to (node));
98 node->erase (); // will do cleanup here
99 }
100
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700101 inline void
102 clear ()
103 {
104 policy_.clear ();
105 trie_.clear ();
106 }
107
108 template<typename Modifier>
109 bool
110 modify (iterator position, Modifier mod)
111 {
112 if (position == end ()) return false;
113 if (position->payload () == PayloadTraits::empty_payload) return false;
114
115 mod (*position->payload ());
116 policy_.update (position);
117 return true;
118 }
119
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700120 /**
121 * @brief Find a node that has the longest common prefix with key (FIB/PIT lookup)
122 */
123 inline iterator
124 longest_prefix_match (const FullKey &key)
125 {
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700126 iterator foundItem, lastItem;
127 bool reachLast;
128 boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
129 if (foundItem != trie_.end ())
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700130 {
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700131 policy_.lookup (s_iterator_to (foundItem));
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700132 }
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700133 return foundItem;
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700134 }
135
136 /**
137 * @brief Find a node that has prefix at least as the key (cache lookup)
138 */
139 inline iterator
140 deepest_prefix_match (const FullKey &key)
141 {
142 iterator foundItem, lastItem;
143 bool reachLast;
144 boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
145
146 // guard in case we don't have anything in the trie
147 if (lastItem == trie_.end ())
148 return trie_.end ();
149
150 if (reachLast)
151 {
152 if (foundItem == trie_.end ())
153 {
154 foundItem = lastItem->find (); // should be something
155 }
156 policy_.lookup (s_iterator_to (foundItem));
157 return foundItem;
158 }
159 else
160 { // couldn't find a node that has prefix at least as key
161 return trie_.end ();
162 }
163 }
164
165 /**
166 * @brief Find a node that has prefix at least as the key
167 */
168 template<class Predicate>
169 inline iterator
170 deepest_prefix_match (const FullKey &key, Predicate pred)
171 {
172 iterator foundItem, lastItem;
173 bool reachLast;
174 boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
175
176 // guard in case we don't have anything in the trie
177 if (lastItem == trie_.end ())
178 return trie_.end ();
179
180 if (reachLast)
181 {
182 foundItem = lastItem->find_if (pred); // may or may not find something
183 if (foundItem == trie_.end ())
184 {
185 return trie_.end ();
186 }
187 policy_.lookup (s_iterator_to (foundItem));
188 return foundItem;
189 }
190 else
191 { // couldn't find a node that has prefix at least as key
192 return trie_.end ();
193 }
194 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700195
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700196 iterator end () const
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700197 {
198 return 0;
199 }
200
201 const parent_trie &
202 getTrie () const { return trie_; }
203
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700204 parent_trie &
205 getTrie () { return trie_; }
206
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700207 const policy_container &
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700208 getPolicy () const { return policy_; }
209
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700210 policy_container &
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700211 getPolicy () { return policy_; }
212
213 static inline iterator
214 s_iterator_to (typename parent_trie::iterator item)
215 {
216 if (item == 0)
217 return 0;
218 else
219 return &(*item);
220 }
221
222private:
223 parent_trie trie_;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700224 policy_container policy_;
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700225};
226
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700227} // ndnSIM
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700228
229#endif // TRIE_WITH_POLICY_H_