blob: 3bfb6e253b30e98ea5d6ac94c8e05de6b5897cc9 [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
Alexander Afanasyeve77db792012-08-09 11:10:58 -070029namespace ns3
30{
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070031namespace ndnSIM
32{
33
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070034/**
35 * @brief Traits for random replacement policy
36 */
Alexander Afanasyev9a989702012-06-29 17:44:00 -070037struct random_policy_traits
38{
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070039 struct policy_hook_type : public boost::intrusive::set_member_hook<> { uint32_t randomOrder; };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070040
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070041 template<class Container>
42 struct container_hook
Alexander Afanasyev9a989702012-06-29 17:44:00 -070043 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070044 typedef boost::intrusive::member_hook< Container,
45 policy_hook_type,
46 &Container::policy_hook_ > type;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070047 };
48
49 template<class Base,
50 class Container,
51 class Hook>
52 struct policy
53 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -070054 static uint32_t& get_order (typename Container::iterator item)
55 {
56 return static_cast<typename policy_container::value_traits::hook_type*>
57 (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
58 }
59
60 static const uint32_t& get_order (typename Container::const_iterator item)
61 {
62 return static_cast<const typename policy_container::value_traits::hook_type*>
63 (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
64 }
65
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070066 template<class Key>
67 struct MemberHookLess
Alexander Afanasyev9a989702012-06-29 17:44:00 -070068 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070069 bool operator () (const Key &a, const Key &b) const
70 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -070071 return get_order (&a) < get_order (&b);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070072 }
73 };
74
Alexander Afanasyevad82e252012-07-29 23:51:21 -070075 typedef boost::intrusive::multiset< Container,
Alexander Afanasyev30cb1172012-07-06 10:47:39 -070076 boost::intrusive::compare< MemberHookLess< Container > >,
77 Hook > policy_container;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070078
79 // could be just typedef
80 class type : public policy_container
81 {
82 public:
83 typedef Container parent_trie;
84
85 type (Base &base)
86 : base_ (base)
87 , u_rand (0, std::numeric_limits<uint32_t>::max ())
88 , max_size_ (100)
89 {
90 }
91
92 inline void
93 update (typename parent_trie::iterator item)
94 {
95 // do nothing. it's random policy
96 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070097
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070098 inline bool
99 insert (typename parent_trie::iterator item)
100 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700101 get_order (item) = u_rand.GetValue ();
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700102
Alexander Afanasyevbd6f3f42012-07-26 17:50:17 -0700103 if (max_size_ != 0 && policy_container::size () >= max_size_)
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700104 {
105 if (MemberHookLess<Container>() (*item, *policy_container::begin ()))
106 {
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700107 // std::cout << "Cannot add. Signaling fail\n";
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700108 // just return false. Indicating that insert "failed"
109 return false;
110 }
111 else
112 {
113 // removing some random element
114 base_.erase (&(*policy_container::begin ()));
115 }
116 }
117
118 policy_container::insert (*item);
119 return true;
120 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700121
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700122 inline void
123 lookup (typename parent_trie::iterator item)
124 {
125 // do nothing. it's random policy
126 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700127
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700128 inline void
129 erase (typename parent_trie::iterator item)
130 {
131 policy_container::erase (policy_container::s_iterator_to (*item));
132 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700133
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700134 inline void
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700135 clear ()
136 {
137 policy_container::clear ();
138 }
139
140 inline void
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700141 set_max_size (size_t max_size)
142 {
143 max_size_ = max_size;
144 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700145
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700146 inline size_t
147 get_max_size () const
148 {
149 return max_size_;
150 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700151
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700152 private:
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700153 type () : base_(*((Base*)0)) { };
154
155 private:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700156 Base &base_;
157 ns3::UniformVariable u_rand;
158 size_t max_size_;
159 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700160 };
161};
162
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700163} // ndnSIM
Alexander Afanasyeve77db792012-08-09 11:10:58 -0700164} // ns3
Alexander Afanasyev30cb1172012-07-06 10:47:39 -0700165
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700166#endif // RANDOM_POLICY_H