NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
lfu-policy.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #ifndef LFU_POLICY_H_
21 #define LFU_POLICY_H_
22 
24 
25 #include <boost/intrusive/options.hpp>
26 #include <boost/intrusive/set.hpp>
27 
28 namespace ns3 {
29 namespace ndn {
30 namespace ndnSIM {
31 
35 struct lfu_policy_traits {
37  static std::string
38  GetName()
39  {
40  return "Lfu";
41  }
42 
43  struct policy_hook_type : public boost::intrusive::set_member_hook<> {
44  double frequency;
45  };
46 
47  template<class Container>
48  struct container_hook {
49  typedef boost::intrusive::member_hook<Container, policy_hook_type, &Container::policy_hook_>
50  type;
51  };
52 
53  template<class Base, class Container, class Hook>
54  struct policy {
55  static double&
56  get_order(typename Container::iterator item)
57  {
58  return static_cast<policy_hook_type*>(policy_container::value_traits::to_node_ptr(*item))
59  ->frequency;
60  }
61 
62  static const double&
63  get_order(typename Container::const_iterator item)
64  {
65  return static_cast<const policy_hook_type*>(
66  policy_container::value_traits::to_node_ptr(*item))->frequency;
67  }
68 
69  template<class Key>
70  struct MemberHookLess {
71  bool
72  operator()(const Key& a, const Key& b) const
73  {
74  return get_order(&a) < get_order(&b);
75  }
76  };
77 
78  typedef boost::intrusive::multiset<Container,
79  boost::intrusive::compare<MemberHookLess<Container>>,
80  Hook> policy_container;
81 
82  // could be just typedef
83  class type : public policy_container {
84  public:
85  typedef policy policy_base; // to get access to get_order methods from outside
86  typedef Container parent_trie;
87 
88  type(Base& base)
89  : base_(base)
90  , max_size_(100)
91  {
92  }
93 
94  inline void
95  update(typename parent_trie::iterator item)
96  {
97  policy_container::erase(policy_container::s_iterator_to(*item));
98  get_order(item) += 1;
99  policy_container::insert(*item);
100  }
101 
102  inline bool
103  insert(typename parent_trie::iterator item)
104  {
105  get_order(item) = 0;
106 
107  if (max_size_ != 0 && policy_container::size() >= max_size_) {
108  // this erases the "least frequently used item" from cache
109  base_.erase(&(*policy_container::begin()));
110  }
111 
112  policy_container::insert(*item);
113  return true;
114  }
115 
116  inline void
117  lookup(typename parent_trie::iterator item)
118  {
119  policy_container::erase(policy_container::s_iterator_to(*item));
120  get_order(item) += 1;
121  policy_container::insert(*item);
122  }
123 
124  inline void
125  erase(typename parent_trie::iterator item)
126  {
127  policy_container::erase(policy_container::s_iterator_to(*item));
128  }
129 
130  inline void
131  clear()
132  {
133  policy_container::clear();
134  }
135 
136  inline void
137  set_max_size(size_t max_size)
138  {
139  max_size_ = max_size;
140  }
141 
142  inline size_t
143  get_max_size() const
144  {
145  return max_size_;
146  }
147 
148  private:
149  type()
150  : base_(*((Base*)0)){};
151 
152  private:
153  Base& base_;
154  size_t max_size_;
155  };
156  };
157 };
158 
159 } // ndnSIM
160 } // ndn
161 } // ns3
162 
164 
165 #endif // LFU_POLICY_H
Copyright (c) 2011-2015 Regents of the University of California.
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Copyright (c) 2011-2015 Regents of the University of California.