blob: d47764399d982e7d3dfb5d25d7658c7804eaaa5a [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;
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 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 Afanasyev78057c32012-07-06 15:18:46 -070056 insert (const FullKey &key, typename PayloadTraits::pointer_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
135 /**
136 * @brief Find a node that has prefix at least as the key (cache lookup)
137 */
138 inline iterator
139 deepest_prefix_match (const FullKey &key)
140 {
141 iterator foundItem, lastItem;
142 bool reachLast;
143 boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
144
145 // guard in case we don't have anything in the trie
146 if (lastItem == trie_.end ())
147 return trie_.end ();
148
149 if (reachLast)
150 {
151 if (foundItem == trie_.end ())
152 {
153 foundItem = lastItem->find (); // should be something
154 }
155 policy_.lookup (s_iterator_to (foundItem));
156 return foundItem;
157 }
158 else
159 { // couldn't find a node that has prefix at least as key
160 return trie_.end ();
161 }
162 }
163
164 /**
165 * @brief Find a node that has prefix at least as the key
166 */
167 template<class Predicate>
168 inline iterator
169 deepest_prefix_match (const FullKey &key, Predicate pred)
170 {
171 iterator foundItem, lastItem;
172 bool reachLast;
173 boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
174
175 // guard in case we don't have anything in the trie
176 if (lastItem == trie_.end ())
177 return trie_.end ();
178
179 if (reachLast)
180 {
181 foundItem = lastItem->find_if (pred); // may or may not find something
182 if (foundItem == trie_.end ())
183 {
184 return trie_.end ();
185 }
186 policy_.lookup (s_iterator_to (foundItem));
187 return foundItem;
188 }
189 else
190 { // couldn't find a node that has prefix at least as key
191 return trie_.end ();
192 }
193 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700194
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700195 iterator end () const
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700196 {
197 return 0;
198 }
199
200 const parent_trie &
201 getTrie () const { return trie_; }
202
Alexander Afanasyev44bb6ea2012-07-09 08:44:41 -0700203 parent_trie &
204 getTrie () { return trie_; }
205
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700206 const policy_container &
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700207 getPolicy () const { return policy_; }
208
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700209 policy_container &
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700210 getPolicy () { return policy_; }
211
212 static inline iterator
213 s_iterator_to (typename parent_trie::iterator item)
214 {
215 if (item == 0)
216 return 0;
217 else
218 return &(*item);
219 }
220
221private:
222 parent_trie trie_;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700223 policy_container policy_;
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700224};
225
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700226} // ndnSIM
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700227
228#endif // TRIE_WITH_POLICY_H_