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 | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame^] | 33 | typedef bi::member_hook< Container, |
| 34 | policy_hook_type, |
| 35 | &Container::policy_hook_ > type; |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | template<class Base, |
| 39 | class Container, |
| 40 | class Hook> |
| 41 | struct policy |
| 42 | { |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame^] | 43 | 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 Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 55 | template<class Key> |
| 56 | struct MemberHookLess |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 57 | { |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 58 | bool operator () (const Key &a, const Key &b) const |
| 59 | { |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame^] | 60 | return get_order (&a) < get_order (&b); |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 61 | } |
| 62 | }; |
| 63 | |
| 64 | typedef bi::set< Container, |
| 65 | bi::compare< MemberHookLess< Container > >, |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame^] | 66 | Hook > policy_container; |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 67 | |
| 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 Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 86 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 87 | inline bool |
| 88 | insert (typename parent_trie::iterator item) |
| 89 | { |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame^] | 90 | get_order (item) = u_rand.GetValue (); |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 92 | 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 Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 110 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 111 | inline void |
| 112 | lookup (typename parent_trie::iterator item) |
| 113 | { |
| 114 | // do nothing. it's random policy |
| 115 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 116 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 117 | inline void |
| 118 | erase (typename parent_trie::iterator item) |
| 119 | { |
| 120 | policy_container::erase (policy_container::s_iterator_to (*item)); |
| 121 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 122 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 123 | inline void |
| 124 | set_max_size (size_t max_size) |
| 125 | { |
| 126 | max_size_ = max_size; |
| 127 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 128 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 129 | inline size_t |
| 130 | get_max_size () const |
| 131 | { |
| 132 | return max_size_; |
| 133 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 134 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 135 | private: |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame^] | 136 | type () : base_(*((Base*)0)) { }; |
| 137 | |
| 138 | private: |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 139 | Base &base_; |
| 140 | ns3::UniformVariable u_rand; |
| 141 | size_t max_size_; |
| 142 | }; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 143 | }; |
| 144 | }; |
| 145 | |
| 146 | #endif // RANDOM_POLICY_H |