20 #ifndef RANDOM_POLICY_H_
21 #define RANDOM_POLICY_H_
25 #include "ns3/random-variable.h"
27 #include <boost/intrusive/options.hpp>
28 #include <boost/intrusive/set.hpp>
37 struct random_policy_traits {
45 struct policy_hook_type :
public boost::intrusive::set_member_hook<> {
49 template<
class Container>
50 struct container_hook {
51 typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
55 template<
class Base,
class Container,
class Hook>
58 get_order(
typename Container::iterator item)
60 return static_cast<typename policy_container::value_traits::hook_type*
>(
61 policy_container::value_traits::to_node_ptr(*item))->randomOrder;
64 static const uint32_t&
65 get_order(
typename Container::const_iterator item)
67 return static_cast<const typename policy_container::value_traits::hook_type*
>(
68 policy_container::value_traits::to_node_ptr(*item))->randomOrder;
72 struct MemberHookLess {
74 operator()(
const Key& a,
const Key& b)
const
76 return get_order(&a) < get_order(&b);
80 typedef boost::intrusive::multiset<Container,
81 boost::intrusive::compare<MemberHookLess<Container>>,
82 Hook> policy_container;
85 class type :
public policy_container {
87 typedef policy policy_base;
88 typedef Container parent_trie;
92 , u_rand(0, std::numeric_limits<uint32_t>::max())
98 update(
typename parent_trie::iterator item)
104 insert(
typename parent_trie::iterator item)
106 get_order(item) = u_rand.GetValue();
108 if (max_size_ != 0 && policy_container::size() >= max_size_) {
109 if (MemberHookLess<Container>()(*item, *policy_container::begin())) {
116 base_.erase(&(*policy_container::begin()));
120 policy_container::insert(*item);
125 lookup(
typename parent_trie::iterator item)
131 erase(
typename parent_trie::iterator item)
133 policy_container::erase(policy_container::s_iterator_to(*item));
139 policy_container::clear();
143 set_max_size(
size_t max_size)
145 max_size_ = max_size;
156 : base_(*((Base*)0)){};
160 ns3::UniformVariable u_rand;
172 #endif // RANDOM_POLICY_H