blob: 28a980080917026e3372b4e8fde627a250046174 [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 Afanasyev9a989702012-06-29 17:44:00 -070026struct random_policy_traits
27{
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070028 struct policy_hook_type : public bi::set_member_hook<> { uint32_t randomOrder; };
Alexander Afanasyev9a989702012-06-29 17:44:00 -070029
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070030 template<class Container>
31 struct container_hook
Alexander Afanasyev9a989702012-06-29 17:44:00 -070032 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070033 struct type
Alexander Afanasyev9a989702012-06-29 17:44:00 -070034 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070035 typedef bi::member_hook< Container,
36 policy_hook_type,
37 &Container::policy_hook_ > hook_type;
Alexander Afanasyev9a989702012-06-29 17:44:00 -070038
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070039 static uint32_t& get_order (typename Container::iterator item)
40 {
41 return item->policy_hook_.randomOrder;
42 }
43
44 static const uint32_t& get_order (typename Container::const_iterator item)
45 {
46 return item->policy_hook_.randomOrder;
47 }
48 };
49 };
50
51 template<class Base,
52 class Container,
53 class Hook>
54 struct policy
55 {
56 template<class Key>
57 struct MemberHookLess
Alexander Afanasyev9a989702012-06-29 17:44:00 -070058 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070059 bool operator () (const Key &a, const Key &b) const
60 {
61 return Hook::get_order (&a) < Hook::get_order (&b);
62 }
63 };
64
65 typedef bi::set< Container,
66 bi::compare< MemberHookLess< Container > >,
67 typename Hook::hook_type > policy_container;
68
69 // could be just typedef
70 class type : public policy_container
71 {
72 public:
73 typedef Container parent_trie;
74
75 type (Base &base)
76 : base_ (base)
77 , u_rand (0, std::numeric_limits<uint32_t>::max ())
78 , max_size_ (100)
79 {
80 }
81
82 inline void
83 update (typename parent_trie::iterator item)
84 {
85 // do nothing. it's random policy
86 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070087
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070088 inline bool
89 insert (typename parent_trie::iterator item)
90 {
91 Hook::get_order (item) = u_rand.GetValue ();
Alexander Afanasyev9a989702012-06-29 17:44:00 -070092
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070093 if (policy_container::size () >= max_size_)
94 {
95 if (MemberHookLess<Container>() (*item, *policy_container::begin ()))
96 {
97 std::cout << "Cannot add. Signaling fail\n";
98 // just return false. Indicating that insert "failed"
99 return false;
100 }
101 else
102 {
103 // removing some random element
104 base_.erase (&(*policy_container::begin ()));
105 }
106 }
107
108 policy_container::insert (*item);
109 return true;
110 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700111
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700112 inline void
113 lookup (typename parent_trie::iterator item)
114 {
115 // do nothing. it's random policy
116 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700117
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700118 inline void
119 erase (typename parent_trie::iterator item)
120 {
121 policy_container::erase (policy_container::s_iterator_to (*item));
122 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700123
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700124 inline void
125 set_max_size (size_t max_size)
126 {
127 max_size_ = max_size;
128 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700129
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700130 inline size_t
131 get_max_size () const
132 {
133 return max_size_;
134 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700135
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700136 private:
137 Base &base_;
138 ns3::UniformVariable u_rand;
139 size_t max_size_;
140 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700141 };
142};
143
144#endif // RANDOM_POLICY_H