blob: 2c35ac49236e987576d291d9bd4b018a38a955e0 [file] [log] [blame]
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2012 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#include "ns3/core-module.h"
22#include "ns3/ndnSIM-module.h"
Alexander Afanasyev9a989702012-06-29 17:44:00 -070023#include "../utils/trie-with-policy.h"
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070024#include "../utils/lru-policy.h"
25#include "../utils/random-policy.h"
26#include "../utils/fifo-policy.h"
27#include <boost/intrusive/parent_from_member.hpp>
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -070028
29using namespace ns3;
30
31NS_LOG_COMPONENT_DEFINE ("Trie");
32
33class Integer : public ns3::SimpleRefCount<Integer>
34{
35public:
36 Integer (int value) : value_ (value) {}
Alexander Afanasyev89fb5352012-06-12 22:43:16 -070037
38 operator int () const { return value_; }
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -070039private:
40 int value_;
41};
42
Alexander Afanasyev89fb5352012-06-12 22:43:16 -070043std::ostream &
44operator << (std::ostream &os, const Integer &i)
45{
46 os << (int)i;
47 return os;
48}
49
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070050template<class Policy1, class Policy2>
51struct multi_policy_hook
52{
53 Policy1 hook1_;
54 Policy2 hook2_;
55};
56
57template<class Base, class Container1, class Container2>
58struct multi_policy_container
59{
60 typedef Container1 nth_container1;
61 typedef Container2 nth_container2;
62
63 multi_policy_container (Base &base)
64 : container1_ (base)
65 , container2_ (base)
66 {
67 }
68
69 nth_container1 container1_;
70 nth_container2 container2_;
71};
72
73template<class BaseHook, class ValueType, class HookType>
74struct Functor1
75{
76 typedef HookType hook_type;
77 typedef hook_type* hook_ptr;
78 typedef const hook_type* const_hook_ptr;
79
80 typedef ValueType value_type;
81 typedef value_type* pointer;
82 typedef const value_type* const_pointer;
83
84 //Required static functions
85 static hook_ptr to_hook_ptr (value_type &value)
86 { return &value.policy_hook_.hook1_; }
87
88 static const_hook_ptr to_hook_ptr(const value_type &value)
89 { return &value.policy_hook_.hook1_; }
90
91 static pointer to_value_ptr(hook_ptr n)
92 {
93 return bi::get_parent_from_member<value_type>
94 (bi::get_parent_from_member<BaseHook >
95 (n, &BaseHook::hook1_)
96 , &value_type::policy_hook_
97 );
98 }
99 static const_pointer to_value_ptr(const_hook_ptr n)
100 {
101 return bi::get_parent_from_member<value_type>
102 (bi::get_parent_from_member<BaseHook>
103 (n, &BaseHook::hook1_)
104 , &value_type::policy_hook_
105 );
106 }
107};
108
109template<class BaseHook, class ValueType, class HookType>
110struct Functor2
111{
112 typedef HookType hook_type;
113 typedef hook_type* hook_ptr;
114 typedef const hook_type* const_hook_ptr;
115
116 typedef ValueType value_type;
117 typedef value_type* pointer;
118 typedef const value_type* const_pointer;
119
120 //Required static functions
121 static hook_ptr to_hook_ptr (value_type &value)
122 { return &value.policy_hook_.hook2_; }
123
124 static const_hook_ptr to_hook_ptr(const value_type &value)
125 { return &value.policy_hook_.hook2_; }
126
127 static pointer to_value_ptr(hook_ptr n)
128 {
129 return bi::get_parent_from_member<value_type>
130 (bi::get_parent_from_member<BaseHook >
131 (n, &BaseHook::hook2_)
132 , &value_type::policy_hook_
133 );
134 }
135 static const_pointer to_value_ptr(const_hook_ptr n)
136 {
137 return bi::get_parent_from_member<value_type>
138 (bi::get_parent_from_member<BaseHook>
139 (n, &BaseHook::hook2_)
140 , &value_type::policy_hook_
141 );
142 }
143};
144
145
146struct multi_policy_traits
147{
148 typedef multi_policy_hook< lru_policy_traits::policy_hook_type
149 ,
150 random_policy_traits::policy_hook_type
151 > policy_hook_type;
152
153 template<class Container>
154 struct container_hook
155 {
156 struct type
157 {
158 typedef bi::function_hook< Functor1 <policy_hook_type,
159 Container,
160 typename lru_policy_traits::policy_hook_type> > hook1;
161
162 struct hook2
163 {
164 typedef bi::function_hook< Functor2 <policy_hook_type,
165 Container,
166 typename random_policy_traits::policy_hook_type> > hook_type;
167
168 static uint32_t& get_order (typename Container::iterator item)
169 {
170 return item->policy_hook_.hook2_.randomOrder;
171 }
172
173 static const uint32_t& get_order (typename Container::const_iterator item)
174 {
175 return item->policy_hook_.hook2_.randomOrder;
176 }
177 };
178
179 };
180 };
181
182 template<class Base,
183 class Container,
184 class Hook>
185 struct policy
186 {
187 typedef multi_policy_container <
188 Base,
189 typename lru_policy_traits::policy< Base, Container, typename Hook::hook1 >::type
190 ,
191 typename random_policy_traits::policy< Base, Container, typename Hook::hook2 >::type
192 > policy_container;
193
194 class type : public policy_container
195 {
196 public:
197 typedef Container parent_trie;
198
199 type (Base &base)
200 : policy_container (base)
201 {
202 }
203
204 inline void
205 update (typename parent_trie::iterator item)
206 {
207 this->container1_.update (item);
208 this->container2_.update (item);
209 }
210
211 inline bool
212 insert (typename parent_trie::iterator item)
213 {
214 bool ok;
215 ok = this->container1_.insert (item);
216 if (!ok)
217 {
218 // nothing to undo. just return false
219 return false;
220 }
221
222 ok = this->container2_.insert (item);
223 if (!ok)
224 {
225 // undo and return false
226 this->container1_.erase (item);
227 return false;
228 }
229 return true;
230 }
231
232 inline void
233 lookup (typename parent_trie::iterator item)
234 {
235 this->container1_.lookup (item);
236 this->container2_.lookup (item);
237 }
238
239 inline void
240 erase (typename parent_trie::iterator item)
241 {
242 this->container1_.erase (item);
243 this->container2_.erase (item);
244 }
245
246 inline typename policy_container::nth_container1 &
247 get1 ()
248 {
249 return this->container1_;
250 }
251
252 inline typename policy_container::nth_container2 &
253 get2 ()
254 {
255 return this->container2_;
256 }
257 // template<>
258 // inline typename policy_container::container1_type &
259 // get<1> ()
260 // {
261 // return this->container1_;
262 // }
263 // inline void
264 // set_max_size (size_t max_size)
265 // {
266 // this->container1_.set_max_size (max_size);
267 // this->container2_.set_max_size (max_size);
268 // }
269
270 // inline size_t
271 // get_max_size () const
272 // {
273 // return this->container1_.get_max_size ();
274 // }
275 };
276 };
277};
278
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700279int
280main (int argc, char *argv[])
281{
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700282 CommandLine args;
283 args.Parse (argc, argv);
284
285 typedef trie_with_policy<
286 ns3::CcnxNameComponents,
287 smart_pointer_payload_traits<Integer>,
288 multi_policy_traits > trie;
289
Alexander Afanasyev89fb5352012-06-12 22:43:16 -0700290 trie x;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700291 x.getPolicy ().get1 ().set_max_size (10);
292 x.getPolicy ().get2 ().set_max_size (3);
Alexander Afanasyev89fb5352012-06-12 22:43:16 -0700293
294 // x.getTrie ().PrintStat (std::cout);
295
296 ns3::CcnxNameComponents n1,n2,n3,n4;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700297 // n1("a")("b")("c");
298 // n2("a")("b")("d");
299 // n3("a")("b")("f");
300 // n4("a")("b");
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700301
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700302 n1("a");
303 n2("b");
304 n3("c");
305 n4("d");
Alexander Afanasyev89fb5352012-06-12 22:43:16 -0700306
307 x.insert (n1, ns3::Create<Integer> (1));
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700308 x.insert (n2, ns3::Create<Integer> (2));
309 x.longest_prefix_match (n1);
310 x.insert (n3, ns3::Create<Integer> (3));
Alexander Afanasyev89fb5352012-06-12 22:43:16 -0700311 x.insert (n4, ns3::Create<Integer> (4));
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700312 // x.insert (n4, ns3::Create<Integer> (4));
Alexander Afanasyev89fb5352012-06-12 22:43:16 -0700313
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700314 std::cout << "digraph trie {\n";
315 std::cout << x.getTrie ();
316 std::cout << "}\n";
317
318 // BOOST_FOREACH (const trie::parent_trie &item, x.getPolicy ())
319 // {
320 // std::cout << *item.payload () << " " << std::endl;
321 // }
Alexander Afanasyev89fb5352012-06-12 22:43:16 -0700322
323 // ns3::CcnxNameComponents n4;
324 // n4("a")("c");
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700325
Alexander Afanasyev89fb5352012-06-12 22:43:16 -0700326 // // std::cout << *x->find (n4).get<0> ();
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700327
Alexander Afanasyev89fb5352012-06-12 22:43:16 -0700328 // x->prune ();
329 // // x->find (n5).get<0> ()->erase ();
330 // x->find (n1).get<0> ()->erase ();
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700331
Alexander Afanasyev89fb5352012-06-12 22:43:16 -0700332 // std::cout << "digraph trie {\n";
333 // std::cout << *x;
334 // std::cout << "}\n";
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700335
Alexander Afanasyev89fb5352012-06-12 22:43:16 -0700336 // x->PrintStat (std::cout);
337
338 // delete x;
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700339
340 return 0;
341}
342