blob: 46fbe696e310e74c3880adafa14518bc728eb79c [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
26template<typename FullKey,
Alexander Afanasyev9a989702012-06-29 17:44:00 -070027 typename PayloadTraits,
28 typename PolicyTraits
29 >
30class trie_with_policy
31{
32public:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070033 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 Afanasyev9a989702012-06-29 17:44:00 -070044
45 inline
46 trie_with_policy (size_t bucketSize = 10, size_t bucketIncrement = 10)
47 : trie_ ("", bucketSize, bucketIncrement)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070048 , policy_ (*this)
Alexander Afanasyev9a989702012-06-29 17:44:00 -070049 {
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 Afanasyev9e96e362012-07-02 23:04:39 -070060 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 Afanasyev9a989702012-06-29 17:44:00 -070066 }
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 Afanasyev9a989702012-06-29 17:44:00 -0700158
159 iterator end ()
160 {
161 return 0;
162 }
163
164 const parent_trie &
165 getTrie () const { return trie_; }
166
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700167 const policy_container &
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700168 getPolicy () const { return policy_; }
169
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700170 policy_container &
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700171 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
182private:
183 parent_trie trie_;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700184 policy_container policy_;
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700185};
186
187
188#endif // TRIE_WITH_POLICY_H_
189