Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 1 | /* -*- 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 FIFO_POLICY_H_ |
| 12 | #define FIFO_POLICY_H_ |
| 13 | |
| 14 | #include <boost/intrusive/options.hpp> |
| 15 | #include <boost/intrusive/list.hpp> |
| 16 | |
| 17 | namespace ndn { |
| 18 | namespace trie { |
| 19 | |
| 20 | /** |
| 21 | * @brief Traits for First In First Out replacement policy |
| 22 | */ |
| 23 | struct fifo_policy_traits |
| 24 | { |
| 25 | /// @brief Name that can be used to identify the policy (e.g., for logging) |
| 26 | static std::string GetName () { return "Fifo"; } |
| 27 | |
| 28 | struct policy_hook_type : public boost::intrusive::list_member_hook<> {}; |
| 29 | |
| 30 | template<class Container> |
| 31 | struct container_hook |
| 32 | { |
| 33 | // could be class/struct implementation |
| 34 | typedef boost::intrusive::member_hook< Container, |
| 35 | policy_hook_type, |
| 36 | &Container::policy_hook_ > type; |
| 37 | }; |
| 38 | |
| 39 | template<class Base, |
| 40 | class Container, |
| 41 | class Hook> |
| 42 | struct policy |
| 43 | { |
| 44 | typedef typename boost::intrusive::list< Container, Hook > policy_container; |
| 45 | |
| 46 | // could be just typedef |
| 47 | class type : public policy_container |
| 48 | { |
| 49 | public: |
| 50 | typedef Container parent_trie; |
| 51 | |
| 52 | type (Base &base) |
| 53 | : base_ (base) |
| 54 | , max_size_ (100) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | inline void |
| 59 | update (typename parent_trie::iterator item) |
| 60 | { |
| 61 | // do nothing |
| 62 | } |
| 63 | |
| 64 | inline bool |
| 65 | insert (typename parent_trie::iterator item) |
| 66 | { |
| 67 | if (max_size_ != 0 && policy_container::size () >= max_size_) |
| 68 | { |
| 69 | base_.erase (&(*policy_container::begin ())); |
| 70 | } |
| 71 | |
| 72 | policy_container::push_back (*item); |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | inline void |
| 77 | lookup (typename parent_trie::iterator item) |
| 78 | { |
| 79 | // do nothing |
| 80 | } |
| 81 | |
| 82 | inline void |
| 83 | erase (typename parent_trie::iterator item) |
| 84 | { |
| 85 | policy_container::erase (policy_container::s_iterator_to (*item)); |
| 86 | } |
| 87 | |
| 88 | inline void |
| 89 | clear () |
| 90 | { |
| 91 | policy_container::clear (); |
| 92 | } |
| 93 | |
| 94 | inline void |
| 95 | set_max_size (size_t max_size) |
| 96 | { |
| 97 | max_size_ = max_size; |
| 98 | } |
| 99 | |
| 100 | inline size_t |
| 101 | get_max_size () const |
| 102 | { |
| 103 | return max_size_; |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | type () : base_(*((Base*)0)) { }; |
| 108 | |
| 109 | private: |
| 110 | Base &base_; |
| 111 | size_t max_size_; |
| 112 | }; |
| 113 | }; |
| 114 | }; |
| 115 | |
| 116 | } // trie |
| 117 | } // ndn |
| 118 | |
| 119 | #endif |