libstdc++
synth_access_traits.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2009 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
00026 
00027 // Permission to use, copy, modify, sell, and distribute this software
00028 // is hereby granted without fee, provided that the above copyright
00029 // notice appears in all copies, and that both that copyright notice
00030 // and this permission notice appear in supporting documentation. None
00031 // of the above authors, nor IBM Haifa Research Laboratories, make any
00032 // representation about the suitability of this software for any
00033 // purpose. It is provided "as is" without express or implied
00034 // warranty.
00035 
00036 /**
00037  * @file pat_trie_/synth_access_traits.hpp
00038  * Contains an implementation class for a patricia tree.
00039  */
00040 
00041 #ifndef PB_DS_SYNTH_E_ACCESS_TRAITS_HPP
00042 #define PB_DS_SYNTH_E_ACCESS_TRAITS_HPP
00043 
00044 #include <ext/pb_ds/detail/type_utils.hpp>
00045 
00046 namespace __gnu_pbds
00047 {
00048   namespace detail
00049   {
00050 
00051 #define PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC \
00052     template<typename Type_Traits, bool Set, typename _ATraits>
00053 
00054 #define PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC \
00055     synth_access_traits<Type_Traits, Set, _ATraits>
00056 
00057     /// Synthetic element access traits.
00058     template<typename Type_Traits, bool Set, typename _ATraits>
00059     struct synth_access_traits : public _ATraits
00060     {
00061       typedef _ATraits                  base_type;
00062       typedef typename base_type::const_iterator    const_iterator;
00063       typedef Type_Traits               type_traits;
00064       typedef typename type_traits::const_reference     const_reference;
00065       typedef typename type_traits::key_const_reference key_const_reference;
00066 
00067       synth_access_traits();
00068 
00069       synth_access_traits(const base_type&);
00070 
00071       inline bool
00072       equal_prefixes(const_iterator, const_iterator, const_iterator,
00073              const_iterator, bool compare_after = true) const;
00074 
00075       bool
00076       equal_keys(key_const_reference, key_const_reference) const;
00077 
00078       bool
00079       cmp_prefixes(const_iterator, const_iterator, const_iterator,
00080            const_iterator, bool compare_after = false) const;
00081 
00082       bool
00083       cmp_keys(key_const_reference, key_const_reference) const;
00084 
00085       inline static key_const_reference
00086       extract_key(const_reference);
00087 
00088 #ifdef _GLIBCXX_DEBUG
00089       bool
00090       operator()(key_const_reference, key_const_reference);
00091 #endif
00092 
00093     private:
00094       inline static key_const_reference
00095       extract_key(const_reference, true_type);
00096 
00097       inline static key_const_reference
00098       extract_key(const_reference, false_type);
00099 
00100       static integral_constant<int, Set> s_set_ind;
00101     };
00102 
00103     PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC
00104     integral_constant<int,Set>
00105     PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::s_set_ind;
00106 
00107     PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC
00108     PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::
00109     synth_access_traits()
00110     { }
00111 
00112     PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC
00113     PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::
00114     synth_access_traits(const _ATraits& r_traits) 
00115     : _ATraits(r_traits)
00116     { }
00117 
00118     PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC
00119     inline bool
00120     PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::
00121     equal_prefixes(const_iterator b_l, const_iterator e_l, const_iterator b_r,
00122            const_iterator e_r, bool compare_after /*= false */) const
00123     {
00124       while (b_l != e_l)
00125     {
00126       if (b_r == e_r)
00127         return false;
00128       if (base_type::e_pos(*b_l) != base_type::e_pos(*b_r))
00129         return false;
00130       ++b_l;
00131       ++b_r;
00132     }
00133       return (!compare_after || b_r == e_r);
00134     }
00135 
00136     PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC
00137     bool
00138     PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::
00139     equal_keys(key_const_reference r_lhs_key,
00140            key_const_reference r_rhs_key) const
00141     {
00142       return equal_prefixes(base_type::begin(r_lhs_key),
00143                 base_type::end(r_lhs_key),
00144                 base_type::begin(r_rhs_key),
00145                 base_type::end(r_rhs_key),
00146                 true);
00147     }
00148 
00149     PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC
00150     bool
00151     PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::
00152     cmp_prefixes(const_iterator b_l, const_iterator e_l, const_iterator b_r,
00153          const_iterator e_r, bool compare_after /* = false*/) const
00154     {
00155       while (b_l != e_l)
00156     {
00157       if (b_r == e_r)
00158         return false;
00159 
00160       const typename base_type::size_type l_pos = base_type::e_pos(*b_l);
00161       const typename base_type::size_type r_pos = base_type::e_pos(*b_r);
00162       if (l_pos != r_pos)
00163         return l_pos < r_pos;
00164       ++b_l;
00165       ++b_r;
00166     }
00167 
00168       if (!compare_after)
00169     return false;
00170       return b_r != e_r;
00171     }
00172 
00173     PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC
00174     bool
00175     PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::
00176     cmp_keys(key_const_reference r_lhs_key,
00177          key_const_reference r_rhs_key) const
00178     {
00179       return cmp_prefixes(base_type::begin(r_lhs_key),
00180               base_type::end(r_lhs_key),
00181               base_type::begin(r_rhs_key),
00182               base_type::end(r_rhs_key),
00183               true);
00184     }
00185 
00186     PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC
00187     inline typename PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::key_const_reference
00188     PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::
00189     extract_key(const_reference r_val)
00190     { return extract_key(r_val, s_set_ind); }
00191 
00192     PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC
00193     inline typename PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::key_const_reference
00194     PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::
00195     extract_key(const_reference r_val, true_type)
00196     { return r_val; }
00197 
00198     PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC
00199     inline typename PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::key_const_reference
00200     PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::
00201     extract_key(const_reference r_val, false_type)
00202     { return r_val.first; }
00203 
00204 #ifdef _GLIBCXX_DEBUG
00205     PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC
00206     bool
00207     PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::
00208     operator()(key_const_reference r_lhs, key_const_reference r_rhs)
00209     { return cmp_keys(r_lhs, r_rhs); }
00210 #endif
00211 
00212 #undef PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC
00213 #undef PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC
00214 
00215   } // namespace detail
00216 } // namespace __gnu_pbds
00217 
00218 #endif