Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 1 | /* -*- 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 LRU_POLICY_H_ |
| 22 | #define LRU_POLICY_H_ |
| 23 | |
| 24 | template<typename FullKey, |
| 25 | typename Payload, |
| 26 | typename PayloadTraits |
| 27 | > |
| 28 | struct lru_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 relocation |
| 50 | policy_container::splice (policy_container::end (), |
| 51 | *this, |
| 52 | policy_container::s_iterator_to (*item)); |
| 53 | } |
| 54 | |
| 55 | inline void |
| 56 | insert (typename parent_trie::iterator item) |
| 57 | { |
| 58 | if (policy_container::size () >= max_size_) |
| 59 | { |
| 60 | typename parent_trie::iterator oldItem = &(*policy_container::begin ()); |
| 61 | policy_container::pop_front (); |
| 62 | oldItem->erase (); |
| 63 | } |
| 64 | |
| 65 | policy_container::push_back (*item); |
| 66 | } |
| 67 | |
| 68 | inline void |
| 69 | lookup (typename parent_trie::iterator item) |
| 70 | { |
| 71 | // do relocation |
| 72 | policy_container::splice (policy_container::end (), |
| 73 | *this, |
| 74 | policy_container::s_iterator_to (*item)); |
| 75 | } |
| 76 | |
| 77 | inline void |
| 78 | erase (typename parent_trie::iterator item) |
| 79 | { |
| 80 | policy_container::erase (policy_container::s_iterator_to (*item)); |
| 81 | } |
| 82 | |
| 83 | inline void |
| 84 | set_max_size (size_t max_size) |
| 85 | { |
| 86 | max_size_ = max_size; |
| 87 | } |
| 88 | |
| 89 | inline size_t |
| 90 | get_max_size () const |
| 91 | { |
| 92 | return max_size_; |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | size_t max_size_; |
| 97 | }; |
| 98 | }; |
| 99 | |
| 100 | #endif |