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 | |
| 26 | struct bla : public bi::set_member_hook<> |
| 27 | { |
| 28 | uint32_t randomOrder; |
| 29 | }; |
| 30 | |
| 31 | template<class Key> |
| 32 | struct MemberHookLess |
| 33 | { |
| 34 | bool operator () (const Key &a, const Key &b) const |
| 35 | { |
| 36 | return a.policy_hook_.randomOrder < b.policy_hook_.randomOrder; |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | template<typename FullKey, |
| 41 | typename Payload, typename PayloadTraits |
| 42 | > |
| 43 | struct random_policy_traits |
| 44 | { |
| 45 | typedef bla policy_hook_type; |
| 46 | typedef trie< FullKey, Payload, PayloadTraits, policy_hook_type > parent_trie; |
| 47 | typedef typename bi::set< parent_trie, |
| 48 | bi::compare< MemberHookLess< parent_trie > >, |
| 49 | bi::member_hook< parent_trie, |
| 50 | policy_hook_type, |
| 51 | &parent_trie::policy_hook_ > > policy_container; |
| 52 | |
| 53 | class policy : public policy_container |
| 54 | { |
| 55 | public: |
| 56 | policy () |
| 57 | : u_rand (0, std::numeric_limits<uint32_t>::max ()) |
| 58 | , max_size_ (100) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | inline void |
| 63 | update (typename parent_trie::iterator item) |
| 64 | { |
| 65 | // do nothing. it's random policy |
| 66 | } |
| 67 | |
| 68 | inline void |
| 69 | insert (typename parent_trie::iterator item) |
| 70 | { |
| 71 | item->policy_hook_.randomOrder = u_rand.GetValue (); |
| 72 | if (policy_container::size () >= max_size_) |
| 73 | { |
| 74 | typename parent_trie::iterator oldItem = &(*policy_container::begin ()); |
| 75 | policy_container::erase (policy_container::begin ()); |
| 76 | oldItem->erase (); |
| 77 | } |
| 78 | |
| 79 | policy_container::insert (*item); |
| 80 | } |
| 81 | |
| 82 | inline void |
| 83 | lookup (typename parent_trie::iterator item) |
| 84 | { |
| 85 | // do nothing. it's random policy |
| 86 | } |
| 87 | |
| 88 | inline void |
| 89 | erase (typename parent_trie::iterator item) |
| 90 | { |
| 91 | policy_container::erase (policy_container::s_iterator_to (*item)); |
| 92 | } |
| 93 | |
| 94 | inline void |
| 95 | set_max_size (size_t max_size) |
| 96 | { |
| 97 | max_size_ = max_size; |
| 98 | } |
| 99 | |
| 100 | inline size_t |
| 101 | get_max_size () const |
| 102 | { |
| 103 | return max_size_; |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | ns3::UniformVariable u_rand; |
| 108 | size_t max_size_; |
| 109 | }; |
| 110 | }; |
| 111 | |
| 112 | #endif // RANDOM_POLICY_H |