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 | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 26 | #include <boost/intrusive/options.hpp> |
| 27 | #include <boost/intrusive/set.hpp> |
| 28 | |
| 29 | namespace ndnSIM |
| 30 | { |
| 31 | |
Alexander Afanasyev | 30f60e3 | 2012-07-10 14:21:16 -0700 | [diff] [blame] | 32 | /** |
| 33 | * @brief Traits for random replacement policy |
| 34 | */ |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 35 | struct random_policy_traits |
| 36 | { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 37 | struct policy_hook_type : public boost::intrusive::set_member_hook<> { uint32_t randomOrder; }; |
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 | template<class Container> |
| 40 | struct container_hook |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 41 | { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 42 | typedef boost::intrusive::member_hook< Container, |
| 43 | policy_hook_type, |
| 44 | &Container::policy_hook_ > type; |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | template<class Base, |
| 48 | class Container, |
| 49 | class Hook> |
| 50 | struct policy |
| 51 | { |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame] | 52 | static uint32_t& get_order (typename Container::iterator item) |
| 53 | { |
| 54 | return static_cast<typename policy_container::value_traits::hook_type*> |
| 55 | (policy_container::value_traits::to_node_ptr(*item))->randomOrder; |
| 56 | } |
| 57 | |
| 58 | static const uint32_t& get_order (typename Container::const_iterator item) |
| 59 | { |
| 60 | return static_cast<const typename policy_container::value_traits::hook_type*> |
| 61 | (policy_container::value_traits::to_node_ptr(*item))->randomOrder; |
| 62 | } |
| 63 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 64 | template<class Key> |
| 65 | struct MemberHookLess |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 66 | { |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 67 | bool operator () (const Key &a, const Key &b) const |
| 68 | { |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame] | 69 | return get_order (&a) < get_order (&b); |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 70 | } |
| 71 | }; |
| 72 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 73 | typedef boost::intrusive::set< Container, |
| 74 | boost::intrusive::compare< MemberHookLess< Container > >, |
| 75 | Hook > policy_container; |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 76 | |
| 77 | // could be just typedef |
| 78 | class type : public policy_container |
| 79 | { |
| 80 | public: |
| 81 | typedef Container parent_trie; |
| 82 | |
| 83 | type (Base &base) |
| 84 | : base_ (base) |
| 85 | , u_rand (0, std::numeric_limits<uint32_t>::max ()) |
| 86 | , max_size_ (100) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | inline void |
| 91 | update (typename parent_trie::iterator item) |
| 92 | { |
| 93 | // do nothing. it's random policy |
| 94 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 95 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 96 | inline bool |
| 97 | insert (typename parent_trie::iterator item) |
| 98 | { |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame] | 99 | get_order (item) = u_rand.GetValue (); |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 100 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 101 | if (policy_container::size () >= max_size_) |
| 102 | { |
| 103 | if (MemberHookLess<Container>() (*item, *policy_container::begin ())) |
| 104 | { |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 105 | // std::cout << "Cannot add. Signaling fail\n"; |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 106 | // just return false. Indicating that insert "failed" |
| 107 | return false; |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | // removing some random element |
| 112 | base_.erase (&(*policy_container::begin ())); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | policy_container::insert (*item); |
| 117 | return true; |
| 118 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 120 | inline void |
| 121 | lookup (typename parent_trie::iterator item) |
| 122 | { |
| 123 | // do nothing. it's random policy |
| 124 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 125 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 126 | inline void |
| 127 | erase (typename parent_trie::iterator item) |
| 128 | { |
| 129 | policy_container::erase (policy_container::s_iterator_to (*item)); |
| 130 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 131 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 132 | inline void |
Alexander Afanasyev | 78057c3 | 2012-07-06 15:18:46 -0700 | [diff] [blame] | 133 | clear () |
| 134 | { |
| 135 | policy_container::clear (); |
| 136 | } |
| 137 | |
| 138 | inline void |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 139 | set_max_size (size_t max_size) |
| 140 | { |
| 141 | max_size_ = max_size; |
| 142 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 143 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 144 | inline size_t |
| 145 | get_max_size () const |
| 146 | { |
| 147 | return max_size_; |
| 148 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 149 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 150 | private: |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame] | 151 | type () : base_(*((Base*)0)) { }; |
| 152 | |
| 153 | private: |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 154 | Base &base_; |
| 155 | ns3::UniformVariable u_rand; |
| 156 | size_t max_size_; |
| 157 | }; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 158 | }; |
| 159 | }; |
| 160 | |
Alexander Afanasyev | 30cb117 | 2012-07-06 10:47:39 -0700 | [diff] [blame] | 161 | } // ndnSIM |
| 162 | |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 163 | #endif // RANDOM_POLICY_H |