Making policy container more flexible
diff --git a/utils/random-policy.h b/utils/random-policy.h
index 1f47f00..28a9800 100644
--- a/utils/random-policy.h
+++ b/utils/random-policy.h
@@ -23,89 +23,121 @@
#include "ns3/random-variable.h"
-struct bla : public bi::set_member_hook<>
-{
- uint32_t randomOrder;
-};
-
-template<class Key>
-struct MemberHookLess
-{
- bool operator () (const Key &a, const Key &b) const
- {
- return a.policy_hook_.randomOrder < b.policy_hook_.randomOrder;
- }
-};
-
-template<typename FullKey,
- typename Payload, typename PayloadTraits
- >
struct random_policy_traits
{
- typedef bla policy_hook_type;
- typedef trie< FullKey, Payload, PayloadTraits, policy_hook_type > parent_trie;
- typedef typename bi::set< parent_trie,
- bi::compare< MemberHookLess< parent_trie > >,
- bi::member_hook< parent_trie,
- policy_hook_type,
- &parent_trie::policy_hook_ > > policy_container;
+ struct policy_hook_type : public bi::set_member_hook<> { uint32_t randomOrder; };
- class policy : public policy_container
+ template<class Container>
+ struct container_hook
{
- public:
- policy ()
- : u_rand (0, std::numeric_limits<uint32_t>::max ())
- , max_size_ (100)
+ struct type
{
- }
+ typedef bi::member_hook< Container,
+ policy_hook_type,
+ &Container::policy_hook_ > hook_type;
- inline void
- update (typename parent_trie::iterator item)
+ static uint32_t& get_order (typename Container::iterator item)
+ {
+ return item->policy_hook_.randomOrder;
+ }
+
+ static const uint32_t& get_order (typename Container::const_iterator item)
+ {
+ return item->policy_hook_.randomOrder;
+ }
+ };
+ };
+
+ template<class Base,
+ class Container,
+ class Hook>
+ struct policy
+ {
+ template<class Key>
+ struct MemberHookLess
{
- // do nothing. it's random policy
- }
+ bool operator () (const Key &a, const Key &b) const
+ {
+ return Hook::get_order (&a) < Hook::get_order (&b);
+ }
+ };
+
+ typedef bi::set< Container,
+ bi::compare< MemberHookLess< Container > >,
+ typename Hook::hook_type > policy_container;
+
+ // could be just typedef
+ class type : public policy_container
+ {
+ public:
+ typedef Container parent_trie;
+
+ type (Base &base)
+ : base_ (base)
+ , u_rand (0, std::numeric_limits<uint32_t>::max ())
+ , max_size_ (100)
+ {
+ }
+
+ inline void
+ update (typename parent_trie::iterator item)
+ {
+ // do nothing. it's random policy
+ }
- inline void
- insert (typename parent_trie::iterator item)
- {
- item->policy_hook_.randomOrder = u_rand.GetValue ();
- if (policy_container::size () >= max_size_)
- {
- typename parent_trie::iterator oldItem = &(*policy_container::begin ());
- policy_container::erase (policy_container::begin ());
- oldItem->erase ();
- }
+ inline bool
+ insert (typename parent_trie::iterator item)
+ {
+ Hook::get_order (item) = u_rand.GetValue ();
- policy_container::insert (*item);
- }
+ if (policy_container::size () >= max_size_)
+ {
+ if (MemberHookLess<Container>() (*item, *policy_container::begin ()))
+ {
+ std::cout << "Cannot add. Signaling fail\n";
+ // just return false. Indicating that insert "failed"
+ return false;
+ }
+ else
+ {
+ // removing some random element
+ base_.erase (&(*policy_container::begin ()));
+ }
+ }
+
+ policy_container::insert (*item);
+ return true;
+ }
- inline void
- lookup (typename parent_trie::iterator item)
- {
- // do nothing. it's random policy
- }
+ inline void
+ lookup (typename parent_trie::iterator item)
+ {
+ // do nothing. it's random policy
+ }
- inline void
- erase (typename parent_trie::iterator item)
- {
- policy_container::erase (policy_container::s_iterator_to (*item));
- }
+ inline void
+ erase (typename parent_trie::iterator item)
+ {
+ policy_container::erase (policy_container::s_iterator_to (*item));
+ }
- inline void
- set_max_size (size_t max_size)
- {
- max_size_ = max_size;
- }
+ inline void
+ set_max_size (size_t max_size)
+ {
+ max_size_ = max_size;
+ }
- inline size_t
- get_max_size () const
- {
- return max_size_;
- }
+ inline size_t
+ get_max_size () const
+ {
+ return max_size_;
+ }
- private:
- ns3::UniformVariable u_rand;
- size_t max_size_;
+ private:
+ Base &base_;
+ ns3::UniformVariable u_rand;
+ size_t max_size_;
+ };
};
};