libstdc++
lu_counter_metadata.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the terms
00008 // of the GNU General Public License as published by the Free Software
00009 // Foundation; either version 3, or (at your option) any later
00010 // version.
00011 
00012 // This library is distributed in the hope that it will be useful, but
00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 // General Public License for more details.
00016 
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
00027 
00028 // Permission to use, copy, modify, sell, and distribute this software
00029 // is hereby granted without fee, provided that the above copyright
00030 // notice appears in all copies, and that both that copyright notice
00031 // and this permission notice appear in supporting documentation. None
00032 // of the above authors, nor IBM Haifa Research Laboratories, make any
00033 // representation about the suitability of this software for any
00034 // purpose. It is provided "as is" without express or implied
00035 // warranty.
00036 
00037 /**
00038  * @file lu_counter_metadata.hpp
00039  * Contains implementation of a lu counter policy's metadata.
00040  */
00041 
00042 namespace __gnu_pbds
00043 {
00044   namespace detail
00045   {
00046     template<typename Size_Type>
00047       class lu_counter_policy_base;
00048 
00049     /// A list-update metadata type that moves elements to the front of
00050     /// the list based on the counter algorithm.
00051     template<typename Size_Type = std::size_t>
00052       class lu_counter_metadata
00053       {
00054       public:
00055     typedef Size_Type   size_type;
00056 
00057       private:
00058     lu_counter_metadata(size_type init_count) : m_count(init_count)
00059     { }
00060 
00061     friend class lu_counter_policy_base<size_type>;
00062 
00063     mutable size_type   m_count;
00064     };
00065 
00066     /// Base class for list-update counter policy.
00067     template<typename Size_Type>
00068       class lu_counter_policy_base
00069       {
00070       protected:
00071     typedef Size_Type   size_type;
00072 
00073     lu_counter_metadata<size_type>
00074     operator()(size_type max_size) const
00075     { return lu_counter_metadata<Size_Type>(std::rand() % max_size); }
00076 
00077     template<typename Metadata_Reference>
00078     bool
00079     operator()(Metadata_Reference r_data, size_type m_max_count) const
00080     {
00081       if (++r_data.m_count != m_max_count)
00082         return false;
00083       r_data.m_count = 0;
00084       return true;
00085     }
00086       };
00087   } // namespace detail
00088 } // namespace __gnu_pbds