libstdc++
list_update_policy.hpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 // Copyright (C) 2005, 2006, 2007, 2009, 2010
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the terms
8 // of the GNU General Public License as published by the Free Software
9 // Foundation; either version 3, or (at your option) any later
10 // version.
11 
12 // This library is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
16 
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20 
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25 
26 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
27 
28 // Permission to use, copy, modify, sell, and distribute this software
29 // is hereby granted without fee, provided that the above copyright
30 // notice appears in all copies, and that both that copyright notice
31 // and this permission notice appear in supporting documentation. None
32 // of the above authors, nor IBM Haifa Research Laboratories, make any
33 // representation about the suitability of this software for any
34 // purpose. It is provided "as is" without express or implied
35 // warranty.
36 
37 /**
38  * @file list_update_policy.hpp
39  * Contains policies for list update containers.
40  */
41 
42 #ifndef PB_DS_LU_POLICY_HPP
43 #define PB_DS_LU_POLICY_HPP
44 
45 #include <bits/c++config.h>
46 #include <cstdlib>
47 #include <ext/pb_ds/detail/list_update_policy/counter_lu_metadata.hpp>
48 
49 namespace __gnu_pbds
50 {
51  // A null type that means that each link in a list-based container
52  // does not actually need metadata.
53  struct null_lu_metadata
54  { };
55 
56 #define PB_DS_CLASS_T_DEC template<typename Allocator>
57 #define PB_DS_CLASS_C_DEC move_to_front_lu_policy<Allocator>
58 
59  // A list-update policy that unconditionally moves elements to the
60  // front of the list.
61  template<typename Allocator = std::allocator<char> >
62  class move_to_front_lu_policy
63  {
64  public:
65  typedef Allocator allocator_type;
66 
67  // Metadata on which this functor operates.
68  typedef null_lu_metadata metadata_type;
69 
70  // Reference to metadata on which this functor operates.
71  typedef typename allocator_type::template rebind<metadata_type>::other metadata_rebind;
72  typedef typename metadata_rebind::reference metadata_reference;
73 
74  // Creates a metadata object.
75  metadata_type
76  operator()() const;
77 
78  // Decides whether a metadata object should be moved to the front
79  // of the list.
80  inline bool
81  operator()(metadata_reference r_metadata) const;
82 
83  private:
84  static null_lu_metadata s_metadata;
85  };
86 
87 #include <ext/pb_ds/detail/list_update_policy/mtf_lu_policy_imp.hpp>
88 
89 #undef PB_DS_CLASS_T_DEC
90 #undef PB_DS_CLASS_C_DEC
91 
92 #define PB_DS_CLASS_T_DEC template<std::size_t Max_Count, class Allocator>
93 #define PB_DS_CLASS_C_DEC counter_lu_policy<Max_Count, Allocator>
94 
95  // A list-update policy that moves elements to the front of the list
96  // based on the counter algorithm.
97  template<std::size_t Max_Count = 5,
98  typename Allocator = std::allocator<char> >
99  class counter_lu_policy
100  : private detail::counter_lu_policy_base<typename Allocator::size_type>
101  {
102  public:
103  typedef Allocator allocator_type;
104 
105  enum
106  {
107  max_count = Max_Count
108  };
109 
110  typedef typename allocator_type::size_type size_type;
111 
112  // Metadata on which this functor operates.
113  typedef detail::counter_lu_metadata<size_type> metadata_type;
114 
115  // Reference to metadata on which this functor operates.
116  typedef typename Allocator::template rebind<metadata_type>::other metadata_rebind;
117  typedef typename metadata_rebind::reference metadata_reference;
118 
119  // Creates a metadata object.
120  metadata_type
121  operator()() const;
122 
123  // Decides whether a metadata object should be moved to the front
124  // of the list.
125  bool
126  operator()(metadata_reference r_metadata) const;
127 
128  private:
129  typedef detail::counter_lu_policy_base<typename Allocator::size_type> base_type;
130  };
131 
132 #include <ext/pb_ds/detail/list_update_policy/counter_lu_policy_imp.hpp>
133 
134 #undef PB_DS_CLASS_T_DEC
135 #undef PB_DS_CLASS_C_DEC
136 
137 } // namespace __gnu_pbds
138 
139 #endif