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 lu_counter_metadata.hpp
39  * Contains implementation of a lu counter policy's metadata.
40  */
41 
42 namespace __gnu_pbds
43 {
44   namespace detail
45   {
46     template<typename Size_Type>
47       class lu_counter_policy_base;
48 
49     /// A list-update metadata type that moves elements to the front of
50     /// the list based on the counter algorithm.
51     template<typename Size_Type = std::size_t>
52       class lu_counter_metadata
53       {
54       public:
55 	typedef Size_Type 	size_type;
56 
57       private:
58 	lu_counter_metadata(size_type init_count) : m_count(init_count)
59 	{ }
60 
61 	friend class lu_counter_policy_base<size_type>;
62 
63 	mutable size_type 	m_count;
64     };
65 
66     /// Base class for list-update counter policy.
67     template<typename Size_Type>
68       class lu_counter_policy_base
69       {
70       protected:
71 	typedef Size_Type 	size_type;
72 
73 	lu_counter_metadata<size_type>
74 	operator()(size_type max_size) const
75 	{ return lu_counter_metadata<Size_Type>(std::rand() % max_size); }
76 
77 	template<typename Metadata_Reference>
78 	bool
79 	operator()(Metadata_Reference r_data, size_type m_max_count) const
80 	{
81 	  if (++r_data.m_count != m_max_count)
82 	    return false;
83 	  r_data.m_count = 0;
84 	  return true;
85 	}
86       };
87   } // namespace detail
88 } // namespace __gnu_pbds
89