1// -*- C++ -*-
2
3// Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011
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/lu_counter_metadata.hpp>
48#include <ext/pb_ds/tag_and_trait.hpp>
49
50namespace __gnu_pbds
51{
52  /**
53   *  A list-update policy that unconditionally moves elements to the
54   *  front of the list. A null type means that each link in a
55   *  list-based container does not actually need metadata.
56   */
57 template<typename _Alloc = std::allocator<char> >
58   class lu_move_to_front_policy
59   {
60   public:
61     typedef _Alloc 					allocator_type;
62
63     /// Metadata on which this functor operates.
64     typedef null_type 					metadata_type;
65
66   private:
67     typedef typename _Alloc::template rebind<metadata_type> __rebind_m;
68
69   public:
70     /// Reference to metadata on which this functor operates.
71     typedef typename __rebind_m::other::reference 	metadata_reference;
72
73     /// Creates a metadata object.
74     metadata_type
75     operator()() const
76     { return s_metadata; }
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     { return true; }
83
84   private:
85     static null_type 					s_metadata;
86   };
87
88  /**
89   *  A list-update policy that moves elements to the front of the
90   *  list based on the counter algorithm.
91   */
92  template<std::size_t Max_Count = 5, typename _Alloc = std::allocator<char> >
93    class lu_counter_policy
94    : private detail::lu_counter_policy_base<typename _Alloc::size_type>
95    {
96    public:
97      typedef _Alloc 					allocator_type;
98      typedef typename allocator_type::size_type       	size_type;
99
100      enum
101	{
102	  /// When some element is accessed this number of times, it
103	  /// will be moved to the front of the list.
104	  max_count = Max_Count
105	};
106
107      /// Metadata on which this functor operates.
108      typedef detail::lu_counter_metadata<size_type> 	metadata_type;
109
110    private:
111      typedef detail::lu_counter_policy_base<size_type> 	base_type;
112      typedef typename _Alloc::template rebind<metadata_type> __rebind_m;
113
114    public:
115      /// Reference to metadata on which this functor operates.
116      typedef typename __rebind_m::other::reference 	metadata_reference;
117
118      /// Creates a metadata object.
119      metadata_type
120      operator()() const
121      { return base_type::operator()(max_count); }
122
123      /// Decides whether a metadata object should be moved to the front
124      /// of the list.
125      bool
126      operator()(metadata_reference r_data) const
127      { return base_type::operator()(r_data, max_count); }
128    };
129} // namespace __gnu_pbds
130
131#endif
132