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