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