libstdc++
list_update_policy.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 list_update_policy.hpp
00039  * Contains policies for list update containers.
00040  */
00041 
00042 #ifndef PB_DS_LU_POLICY_HPP
00043 #define PB_DS_LU_POLICY_HPP
00044 
00045 #include <bits/c++config.h>
00046 #include <cstdlib>
00047 #include <ext/pb_ds/detail/list_update_policy/lu_counter_metadata.hpp>
00048 #include <ext/pb_ds/tag_and_trait.hpp>
00049 
00050 namespace __gnu_pbds
00051 {
00052   /**
00053    *  A list-update policy that unconditionally moves elements to the
00054    *  front of the list. A null type means that each link in a
00055    *  list-based container does not actually need metadata.
00056    */
00057  template<typename _Alloc = std::allocator<char> >
00058    class lu_move_to_front_policy
00059    {
00060    public:
00061      typedef _Alloc                     allocator_type;
00062 
00063      /// Metadata on which this functor operates.
00064      typedef null_type                  metadata_type;
00065 
00066    private:
00067      typedef typename _Alloc::template rebind<metadata_type> __rebind_m;
00068 
00069    public:
00070      /// Reference to metadata on which this functor operates.
00071      typedef typename __rebind_m::other::reference  metadata_reference;
00072 
00073      /// Creates a metadata object.
00074      metadata_type
00075      operator()() const
00076      { return s_metadata; }
00077 
00078      /// Decides whether a metadata object should be moved to the front
00079      /// of the list.
00080      inline bool
00081      operator()(metadata_reference r_metadata) const
00082      { return true; }
00083 
00084    private:
00085      static null_type                   s_metadata;
00086    };
00087 
00088   /**
00089    *  A list-update policy that moves elements to the front of the
00090    *  list based on the counter algorithm.
00091    */
00092   template<std::size_t Max_Count = 5, typename _Alloc = std::allocator<char> >
00093     class lu_counter_policy
00094     : private detail::lu_counter_policy_base<typename _Alloc::size_type>
00095     {
00096     public:
00097       typedef _Alloc                    allocator_type;
00098       typedef typename allocator_type::size_type        size_type;
00099 
00100       enum
00101     {
00102       /// When some element is accessed this number of times, it
00103       /// will be moved to the front of the list.
00104       max_count = Max_Count
00105     };
00106 
00107       /// Metadata on which this functor operates.
00108       typedef detail::lu_counter_metadata<size_type>    metadata_type;
00109 
00110     private:
00111       typedef detail::lu_counter_policy_base<size_type>     base_type;
00112       typedef typename _Alloc::template rebind<metadata_type> __rebind_m;
00113 
00114     public:
00115       /// Reference to metadata on which this functor operates.
00116       typedef typename __rebind_m::other::reference     metadata_reference;
00117 
00118       /// Creates a metadata object.
00119       metadata_type
00120       operator()() const
00121       { return base_type::operator()(max_count); }
00122 
00123       /// Decides whether a metadata object should be moved to the front
00124       /// of the list.
00125       bool
00126       operator()(metadata_reference r_data) const
00127       { return base_type::operator()(r_data, max_count); }
00128     };
00129 } // namespace __gnu_pbds
00130 
00131 #endif