Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 26 | struct random_policy_traits |
| 27 | { |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 28 | struct policy_hook_type : public bi::set_member_hook<> { uint32_t randomOrder; }; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 30 | template<class Container> |
| 31 | struct container_hook |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 32 | { |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 33 | struct type |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 34 | { |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 35 | typedef bi::member_hook< Container, |
| 36 | policy_hook_type, |
| 37 | &Container::policy_hook_ > hook_type; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 38 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 39 | 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 Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 58 | { |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 59 | 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 Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 87 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 88 | inline bool |
| 89 | insert (typename parent_trie::iterator item) |
| 90 | { |
| 91 | Hook::get_order (item) = u_rand.GetValue (); |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 92 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 93 | 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 Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 111 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 112 | inline void |
| 113 | lookup (typename parent_trie::iterator item) |
| 114 | { |
| 115 | // do nothing. it's random policy |
| 116 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 117 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 118 | inline void |
| 119 | erase (typename parent_trie::iterator item) |
| 120 | { |
| 121 | policy_container::erase (policy_container::s_iterator_to (*item)); |
| 122 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 123 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 124 | inline void |
| 125 | set_max_size (size_t max_size) |
| 126 | { |
| 127 | max_size_ = max_size; |
| 128 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 129 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 130 | inline size_t |
| 131 | get_max_size () const |
| 132 | { |
| 133 | return max_size_; |
| 134 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 135 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 136 | private: |
| 137 | Base &base_; |
| 138 | ns3::UniformVariable u_rand; |
| 139 | size_t max_size_; |
| 140 | }; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 141 | }; |
| 142 | }; |
| 143 | |
| 144 | #endif // RANDOM_POLICY_H |