blob: ebf0a598de86b19117e44b1caf1e1ce8ee13f7d0 [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 Afanasyev903062f2012-07-04 18:25:26 -070033 typedef bi::member_hook< Container,
34 policy_hook_type,
35 &Container::policy_hook_ > type;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070036 };
37
38 template<class Base,
39 class Container,
40 class Hook>
41 struct policy
42 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -070043 static uint32_t& get_order (typename Container::iterator item)
44 {
45 return static_cast<typename policy_container::value_traits::hook_type*>
46 (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
47 }
48
49 static const uint32_t& get_order (typename Container::const_iterator item)
50 {
51 return static_cast<const typename policy_container::value_traits::hook_type*>
52 (policy_container::value_traits::to_node_ptr(*item))->randomOrder;
53 }
54
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070055 template<class Key>
56 struct MemberHookLess
Alexander Afanasyev9a989702012-06-29 17:44:00 -070057 {
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070058 bool operator () (const Key &a, const Key &b) const
59 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -070060 return get_order (&a) < get_order (&b);
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070061 }
62 };
63
64 typedef bi::set< Container,
65 bi::compare< MemberHookLess< Container > >,
Alexander Afanasyev903062f2012-07-04 18:25:26 -070066 Hook > policy_container;
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070067
68 // could be just typedef
69 class type : public policy_container
70 {
71 public:
72 typedef Container parent_trie;
73
74 type (Base &base)
75 : base_ (base)
76 , u_rand (0, std::numeric_limits<uint32_t>::max ())
77 , max_size_ (100)
78 {
79 }
80
81 inline void
82 update (typename parent_trie::iterator item)
83 {
84 // do nothing. it's random policy
85 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -070086
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070087 inline bool
88 insert (typename parent_trie::iterator item)
89 {
Alexander Afanasyev903062f2012-07-04 18:25:26 -070090 get_order (item) = u_rand.GetValue ();
Alexander Afanasyev9a989702012-06-29 17:44:00 -070091
Alexander Afanasyev9e96e362012-07-02 23:04:39 -070092 if (policy_container::size () >= max_size_)
93 {
94 if (MemberHookLess<Container>() (*item, *policy_container::begin ()))
95 {
96 std::cout << "Cannot add. Signaling fail\n";
97 // just return false. Indicating that insert "failed"
98 return false;
99 }
100 else
101 {
102 // removing some random element
103 base_.erase (&(*policy_container::begin ()));
104 }
105 }
106
107 policy_container::insert (*item);
108 return true;
109 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700110
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700111 inline void
112 lookup (typename parent_trie::iterator item)
113 {
114 // do nothing. it's random policy
115 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700116
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700117 inline void
118 erase (typename parent_trie::iterator item)
119 {
120 policy_container::erase (policy_container::s_iterator_to (*item));
121 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700122
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700123 inline void
124 set_max_size (size_t max_size)
125 {
126 max_size_ = max_size;
127 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700128
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700129 inline size_t
130 get_max_size () const
131 {
132 return max_size_;
133 }
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700134
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700135 private:
Alexander Afanasyev903062f2012-07-04 18:25:26 -0700136 type () : base_(*((Base*)0)) { };
137
138 private:
Alexander Afanasyev9e96e362012-07-02 23:04:39 -0700139 Base &base_;
140 ns3::UniformVariable u_rand;
141 size_t max_size_;
142 };
Alexander Afanasyev9a989702012-06-29 17:44:00 -0700143 };
144};
145
146#endif // RANDOM_POLICY_H