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