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