blob: c745b3be3175eec1672f832341c56f479e2fe1cc [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 RANDOM_POLICY_H_
22#define RANDOM_POLICY_H_
23
24#include "ns3/random-variable.h"
25
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070026#include <boost/intrusive/options.hpp>
27#include <boost/intrusive/set.hpp>
28
29namespace ndnSIM
30{
31
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070032/**
33 * @brief Traits for random replacement policy
34 */
Alexander Afanasyev9a989702012-06-29 17:44:00 -070035struct random_policy_traits
36{
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070037 struct policy_hook_type : public boost::intrusive::set_member_hook<> { uint32_t randomOrder; };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070038
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070039 template<class Container>
40 struct container_hook
Alexander Afanasyev9a989702012-06-29 17:44:00 -070041 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070042 typedef boost::intrusive::member_hook< Container,
43 policy_hook_type,
44 &Container::policy_hook_ > type;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070045 };
46
47 template<class Base,
48 class Container,
49 class Hook>
50 struct policy
51 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -070052 static uint32_t& get_order (typename Container::iterator item)
53 {
54 return static_cast<typename policy_container::value_traits::hook_type*>
55 (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
56 }
57
58 static const uint32_t& get_order (typename Container::const_iterator item)
59 {
60 return static_cast<const typename policy_container::value_traits::hook_type*>
61 (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
62 }
63
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070064 template<class Key>
65 struct MemberHookLess
Alexander Afanasyev9a989702012-06-29 17:44:00 -070066 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070067 bool operator () (const Key &a, const Key &b) const
68 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -070069 return get_order (&a) < get_order (&b);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070070 }
71 };
72
Alexander Afanasyevad82e252012-07-29 23:51:21 -070073 typedef boost::intrusive::multiset< Container,
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070074 boost::intrusive::compare< MemberHookLess< Container > >,
75 Hook > policy_container;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070076
77 // could be just typedef
78 class type : public policy_container
79 {
80 public:
81 typedef Container parent_trie;
82
83 type (Base &base)
84 : base_ (base)
85 , u_rand (0, std::numeric_limits<uint32_t>::max ())
86 , max_size_ (100)
87 {
88 }
89
90 inline void
91 update (typename parent_trie::iterator item)
92 {
93 // do nothing. it's random policy
94 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070095
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070096 inline bool
97 insert (typename parent_trie::iterator item)
98 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -070099 get_order (item) = u_rand.GetValue ();
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700100
Alexander Afanasyevbd6f3f42012-07-26 17:50:17 -0700101 if (max_size_ != 0 && policy_container::size () >= max_size_)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700102 {
103 if (MemberHookLess<Container>() (*item, *policy_container::begin ()))
104 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700105 // std::cout << "Cannot add. Signaling fail\n";
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700106 // just return false. Indicating that insert "failed"
107 return false;
108 }
109 else
110 {
111 // removing some random element
112 base_.erase (&(*policy_container::begin ()));
113 }
114 }
115
116 policy_container::insert (*item);
117 return true;
118 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700119
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700120 inline void
121 lookup (typename parent_trie::iterator item)
122 {
123 // do nothing. it's random policy
124 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700125
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700126 inline void
127 erase (typename parent_trie::iterator item)
128 {
129 policy_container::erase (policy_container::s_iterator_to (*item));
130 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700131
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700132 inline void
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700133 clear ()
134 {
135 policy_container::clear ();
136 }
137
138 inline void
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700139 set_max_size (size_t max_size)
140 {
141 max_size_ = max_size;
142 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700143
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700144 inline size_t
145 get_max_size () const
146 {
147 return max_size_;
148 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700149
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700150 private:
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700151 type () : base_(*((Base*)0)) { };
152
153 private:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700154 Base &base_;
155 ns3::UniformVariable u_rand;
156 size_t max_size_;
157 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700158 };
159};
160
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700161} // ndnSIM
162
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700163#endif // RANDOM_POLICY_H