blob: ad3a3823ddaace9e478e11ee3864f7ba87d57850 [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Alexander Afanasyev
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
9 */
10
11#ifndef LRU_POLICY_H_
12#define LRU_POLICY_H_
13
14#include <boost/intrusive/options.hpp>
15#include <boost/intrusive/list.hpp>
16
17namespace ndn {
18namespace trie {
19
20/**
21 * @brief Traits for Least Recently Used replacement policy
22 */
23struct lru_policy_traits
24{
25 /// @brief Name that can be used to identify the policy (e.g., for logging)
26 static std::string GetName () { return "Lru"; }
27
28 struct policy_hook_type : public boost::intrusive::list_member_hook<> {};
29
30 template<class Container>
31 struct container_hook
32 {
33 typedef boost::intrusive::member_hook< Container,
34 policy_hook_type,
35 &Container::policy_hook_ > type;
36 };
37
38 template<class Base,
39 class Container,
40 class Hook>
41 struct policy
42 {
43 typedef typename boost::intrusive::list< Container, Hook > policy_container;
44
45 // could be just typedef
46 class type : public policy_container
47 {
48 public:
49 typedef Container parent_trie;
50
51 type (Base &base)
52 : base_ (base)
53 , max_size_ (100)
54 {
55 }
56
57 inline void
58 update (typename parent_trie::iterator item)
59 {
60 // do relocation
61 policy_container::splice (policy_container::end (),
62 *this,
63 policy_container::s_iterator_to (*item));
64 }
65
66 inline bool
67 insert (typename parent_trie::iterator item)
68 {
69 if (max_size_ != 0 && policy_container::size () >= max_size_)
70 {
71 base_.erase (&(*policy_container::begin ()));
72 }
73
74 policy_container::push_back (*item);
75 return true;
76 }
77
78 inline void
79 lookup (typename parent_trie::iterator item)
80 {
81 // do relocation
82 policy_container::splice (policy_container::end (),
83 *this,
84 policy_container::s_iterator_to (*item));
85 }
86
87 inline void
88 erase (typename parent_trie::iterator item)
89 {
90 policy_container::erase (policy_container::s_iterator_to (*item));
91 }
92
93 inline void
94 clear ()
95 {
96 policy_container::clear ();
97 }
98
99 inline void
100 set_max_size (size_t max_size)
101 {
102 max_size_ = max_size;
103 }
104
105 inline size_t
106 get_max_size () const
107 {
108 return max_size_;
109 }
110
111 private:
112 type () : base_(*((Base*)0)) { };
113
114 private:
115 Base &base_;
116 size_t max_size_;
117 };
118 };
119};
120
121} // trie
122} // ndn
123
124#endif