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 FIFO_POLICY_H_ |
| 22 | #define FIFO_POLICY_H_ |
| 23 | |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 24 | struct fifo_policy_traits |
| 25 | { |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame^] | 26 | struct policy_hook_type : public bi::list_member_hook<> {}; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 28 | template<class Container> |
| 29 | struct container_hook |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 30 | { |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 31 | // could be class/struct implementation |
| 32 | typedef bi::member_hook< Container, |
| 33 | policy_hook_type, |
| 34 | &Container::policy_hook_ > type; |
| 35 | }; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 36 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 37 | template<class Base, |
| 38 | class Container, |
| 39 | class Hook> |
| 40 | struct policy |
| 41 | { |
| 42 | typedef typename bi::list< Container, Hook > policy_container; |
| 43 | |
| 44 | // could be just typedef |
| 45 | class type : public policy_container |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 46 | { |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 47 | public: |
| 48 | typedef Container parent_trie; |
| 49 | |
| 50 | type (Base &base) |
| 51 | : base_ (base) |
| 52 | , max_size_ (100) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | inline void |
| 57 | update (typename parent_trie::iterator item) |
| 58 | { |
| 59 | // do nothing |
| 60 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 61 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 62 | inline bool |
| 63 | insert (typename parent_trie::iterator item) |
| 64 | { |
| 65 | if (policy_container::size () >= max_size_) |
| 66 | { |
| 67 | base_.erase (&(*policy_container::begin ())); |
| 68 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 69 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 70 | policy_container::push_back (*item); |
| 71 | return true; |
| 72 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 73 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 74 | inline void |
| 75 | lookup (typename parent_trie::iterator item) |
| 76 | { |
| 77 | // do nothing |
| 78 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 79 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 80 | inline void |
| 81 | erase (typename parent_trie::iterator item) |
| 82 | { |
| 83 | policy_container::erase (policy_container::s_iterator_to (*item)); |
| 84 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 85 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 86 | inline void |
| 87 | set_max_size (size_t max_size) |
| 88 | { |
| 89 | max_size_ = max_size; |
| 90 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 92 | inline size_t |
| 93 | get_max_size () const |
| 94 | { |
| 95 | return max_size_; |
| 96 | } |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 97 | |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 98 | private: |
Alexander Afanasyev | 903062f | 2012-07-04 18:25:26 -0700 | [diff] [blame^] | 99 | type () : base_(*((Base*)0)) { }; |
| 100 | |
| 101 | private: |
Alexander Afanasyev | 9e96e36 | 2012-07-02 23:04:39 -0700 | [diff] [blame] | 102 | Base &base_; |
| 103 | size_t max_size_; |
| 104 | }; |
Alexander Afanasyev | 9a98970 | 2012-06-29 17:44:00 -0700 | [diff] [blame] | 105 | }; |
| 106 | }; |
| 107 | |
| 108 | #endif |