blob: db6bd9f1ccdde594bbebda6ef4d22bac148a7c8f [file] [log] [blame]
Alexander Afanasyev9a989702012-06-29 17:44:00 -07001/* -*- 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 FIFO_POLICY_H_
22#define FIFO_POLICY_H_
23
24template<typename FullKey,
25 typename Payload,
26 typename PayloadTraits
27 >
28struct fifo_policy_traits
29{
30 typedef bi::list_member_hook<> policy_hook_type;
31 typedef trie< FullKey, Payload, PayloadTraits, policy_hook_type> parent_trie;
32 typedef typename bi::list< parent_trie,
33 bi::member_hook< parent_trie,
34 policy_hook_type,
35 &parent_trie::policy_hook_ > > policy_container;
36
37
38 class policy : public policy_container
39 {
40 public:
41 policy ()
42 : max_size_ (100)
43 {
44 }
45
46 inline void
47 update (typename parent_trie::iterator item)
48 {
49 // do nothing
50 }
51
52 inline void
53 insert (typename parent_trie::iterator item)
54 {
55 if (policy_container::size () >= max_size_)
56 {
57 typename parent_trie::iterator oldItem = &(*policy_container::begin ());
58 policy_container::pop_front ();
59 oldItem->erase ();
60 }
61
62 policy_container::push_back (*item);
63 }
64
65 inline void
66 lookup (typename parent_trie::iterator item)
67 {
68 // do nothing
69 }
70
71 inline void
72 erase (typename parent_trie::iterator item)
73 {
74 policy_container::erase (policy_container::s_iterator_to (*item));
75 }
76
77 inline void
78 set_max_size (size_t max_size)
79 {
80 max_size_ = max_size;
81 }
82
83 inline size_t
84 get_max_size () const
85 {
86 return max_size_;
87 }
88
89 private:
90 size_t max_size_;
91 };
92};
93
94#endif