00001 // -*- C++ -*- 00002 00003 // Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the terms 00007 // of the GNU General Public License as published by the Free Software 00008 // Foundation; either version 2, or (at your option) any later 00009 // version. 00010 00011 // This library is distributed in the hope that it will be useful, but 00012 // WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 // General Public License for more details. 00015 00016 // You should have received a copy of the GNU General Public License 00017 // along with this library; see the file COPYING. If not, write to 00018 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston, 00019 // MA 02111-1307, USA. 00020 00021 // As a special exception, you may use this file as part of a free 00022 // software library without restriction. Specifically, if other files 00023 // instantiate templates or use macros or inline functions from this 00024 // file, or you compile this file and link it with other files to 00025 // produce an executable, this file does not by itself cause the 00026 // resulting executable to be covered by the GNU General Public 00027 // License. This exception does not however invalidate any other 00028 // reasons why the executable file might be covered by the GNU General 00029 // Public License. 00030 00031 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. 00032 00033 // Permission to use, copy, modify, sell, and distribute this software 00034 // is hereby granted without fee, provided that the above copyright 00035 // notice appears in all copies, and that both that copyright notice 00036 // and this permission notice appear in supporting documentation. None 00037 // of the above authors, nor IBM Haifa Research Laboratories, make any 00038 // representation about the suitability of this software for any 00039 // purpose. It is provided "as is" without express or implied 00040 // warranty. 00041 00042 /** 00043 * @file list_update_policy.hpp 00044 * Contains policies for list update containers. 00045 */ 00046 00047 #ifndef PB_DS_LU_POLICY_HPP 00048 #define PB_DS_LU_POLICY_HPP 00049 00050 #include <cstdlib> 00051 #include <ext/pb_ds/detail/list_update_policy/counter_lu_metadata.hpp> 00052 00053 namespace __gnu_pbds 00054 { 00055 // A null type that means that each link in a list-based container 00056 // does not actually need metadata. 00057 struct null_lu_metadata 00058 { }; 00059 00060 #define PB_DS_CLASS_T_DEC template<typename Allocator> 00061 #define PB_DS_CLASS_C_DEC move_to_front_lu_policy<Allocator> 00062 00063 // A list-update policy that unconditionally moves elements to the 00064 // front of the list. 00065 template<typename Allocator = std::allocator<char> > 00066 class move_to_front_lu_policy 00067 { 00068 public: 00069 typedef Allocator allocator; 00070 00071 // Metadata on which this functor operates. 00072 typedef null_lu_metadata metadata_type; 00073 00074 // Reference to metadata on which this functor operates. 00075 typedef typename allocator::template rebind<metadata_type>::other metadata_rebind; 00076 typedef typename metadata_rebind::reference metadata_reference; 00077 00078 // Creates a metadata object. 00079 metadata_type 00080 operator()() const; 00081 00082 // Decides whether a metadata object should be moved to the front 00083 // of the list. 00084 inline bool 00085 operator()(metadata_reference r_metadata) const; 00086 00087 private: 00088 static null_lu_metadata s_metadata; 00089 }; 00090 00091 #include <ext/pb_ds/detail/list_update_policy/mtf_lu_policy_imp.hpp> 00092 00093 #undef PB_DS_CLASS_T_DEC 00094 #undef PB_DS_CLASS_C_DEC 00095 00096 #define PB_DS_CLASS_T_DEC template<size_t Max_Count, class Allocator> 00097 #define PB_DS_CLASS_C_DEC counter_lu_policy<Max_Count, Allocator> 00098 00099 // A list-update policy that moves elements to the front of the list 00100 // based on the counter algorithm. 00101 template<size_t Max_Count = 5, typename Allocator = std::allocator<char> > 00102 class counter_lu_policy 00103 : private detail::counter_lu_policy_base<typename Allocator::size_type> 00104 { 00105 public: 00106 typedef Allocator allocator; 00107 00108 enum 00109 { 00110 max_count = Max_Count 00111 }; 00112 00113 typedef typename allocator::size_type size_type; 00114 00115 // Metadata on which this functor operates. 00116 typedef detail::counter_lu_metadata<size_type> metadata_type; 00117 00118 // Reference to metadata on which this functor operates. 00119 typedef typename Allocator::template rebind<metadata_type>::other metadata_rebind; 00120 typedef typename metadata_rebind::reference metadata_reference; 00121 00122 // Creates a metadata object. 00123 metadata_type 00124 operator()() const; 00125 00126 // Decides whether a metadata object should be moved to the front 00127 // of the list. 00128 bool 00129 operator()(metadata_reference r_metadata) const; 00130 00131 private: 00132 typedef detail::counter_lu_policy_base<typename Allocator::size_type> base_type; 00133 }; 00134 00135 #include <ext/pb_ds/detail/list_update_policy/counter_lu_policy_imp.hpp> 00136 00137 #undef PB_DS_CLASS_T_DEC 00138 #undef PB_DS_CLASS_C_DEC 00139 00140 } // namespace __gnu_pbds 00141 00142 #endif