1 // -*- C++ -*- 2 3 // Copyright (C) 2005, 2006, 2009 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the terms 7 // of the GNU General Public License as published by the Free Software 8 // Foundation; either version 3, or (at your option) any later 9 // version. 10 11 // This library is distributed in the hope that it will be useful, but 12 // WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 // General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. 26 27 // Permission to use, copy, modify, sell, and distribute this software 28 // is hereby granted without fee, provided that the above copyright 29 // notice appears in all copies, and that both that copyright notice 30 // and this permission notice appear in supporting documentation. None 31 // of the above authors, nor IBM Haifa Research Laboratories, make any 32 // representation about the suitability of this software for any 33 // purpose. It is provided "as is" without express or implied 34 // warranty. 35 36 /** 37 * @file synth_e_access_traits.hpp 38 * Contains an implementation class for a patricia tree. 39 */ 40 41 #ifndef PB_DS_SYNTH_E_ACCESS_TRAITS_HPP 42 #define PB_DS_SYNTH_E_ACCESS_TRAITS_HPP 43 44 #include <ext/pb_ds/detail/type_utils.hpp> 45 46 namespace __gnu_pbds 47 { 48 namespace detail 49 { 50 51 #define PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC \ 52 template<typename Type_Traits, bool Set, class E_Access_Traits> 53 54 #define PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC \ 55 synth_e_access_traits< \ 56 Type_Traits, \ 57 Set, \ 58 E_Access_Traits> 59 60 template<typename Type_Traits, bool Set, class E_Access_Traits> 61 struct synth_e_access_traits : public E_Access_Traits 62 { 63 64 private: 65 typedef E_Access_Traits base_type; 66 67 typedef Type_Traits type_traits; 68 69 typedef typename type_traits::const_key_reference const_key_reference; 70 71 typedef typename type_traits::const_reference const_reference; 72 73 public: 74 synth_e_access_traits(); 75 76 synth_e_access_traits(const E_Access_Traits& r_traits); 77 78 inline bool 79 equal_prefixes(typename base_type::const_iterator b_l, typename base_type::const_iterator e_l, typename base_type::const_iterator b_r, typename base_type::const_iterator e_r, bool compare_after = true) const; 80 81 bool 82 equal_keys(const_key_reference r_lhs_key, const_key_reference r_rhs_key) const; 83 84 bool 85 cmp_prefixes(typename base_type::const_iterator b_l, typename base_type::const_iterator e_l, typename base_type::const_iterator b_r, typename base_type::const_iterator e_r, bool compare_after = false) const; 86 87 bool 88 cmp_keys(const_key_reference r_lhs_key, const_key_reference r_rhs_key) const; 89 90 inline static const_key_reference 91 extract_key(const_reference r_val); 92 93 #ifdef _GLIBCXX_DEBUG 94 bool 95 operator()(const_key_reference r_lhs, const_key_reference r_rhs); 96 #endif 97 98 private: 99 inline static const_key_reference 100 extract_key(const_reference r_val, true_type); 101 102 inline static const_key_reference 103 extract_key(const_reference r_val, false_type); 104 105 private: 106 static integral_constant<int,Set> s_set_ind; 107 }; 108 109 PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC 110 integral_constant<int,Set> 111 PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::s_set_ind; 112 113 PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC 114 PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC:: 115 synth_e_access_traits() 116 { } 117 118 PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC 119 PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC:: 120 synth_e_access_traits(const E_Access_Traits& r_traits) : 121 E_Access_Traits(r_traits) 122 { } 123 124 PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC 125 inline bool 126 PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC:: 127 equal_prefixes(typename base_type::const_iterator b_l, typename base_type::const_iterator e_l, typename base_type::const_iterator b_r, typename base_type::const_iterator e_r, bool compare_after /*= false */) const 128 { 129 while (b_l != e_l) 130 { 131 if (b_r == e_r) 132 return (false); 133 if (base_type::e_pos(*b_l) != base_type::e_pos(*b_r)) 134 return (false); 135 ++b_l; 136 ++b_r; 137 } 138 return (!compare_after || b_r == e_r); 139 } 140 141 PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC 142 bool 143 PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC:: 144 equal_keys(const_key_reference r_lhs_key, const_key_reference r_rhs_key) const 145 { 146 return (equal_prefixes(base_type::begin(r_lhs_key), 147 base_type::end(r_lhs_key), 148 base_type::begin(r_rhs_key), 149 base_type::end(r_rhs_key), 150 true)); 151 } 152 153 PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC 154 bool 155 PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC:: 156 cmp_prefixes(typename base_type::const_iterator b_l, typename base_type::const_iterator e_l, typename base_type::const_iterator b_r, typename base_type::const_iterator e_r, bool compare_after /* = false*/) const 157 { 158 while (b_l != e_l) 159 { 160 if (b_r == e_r) 161 return (false); 162 const typename base_type::size_type l_pos = 163 base_type::e_pos(*b_l); 164 const typename base_type::size_type r_pos = 165 base_type::e_pos(*b_r); 166 if (l_pos != r_pos) 167 return (l_pos < r_pos); 168 ++b_l; 169 ++b_r; 170 } 171 172 if (!compare_after) 173 return (false); 174 return (b_r != e_r); 175 } 176 177 PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC 178 bool 179 PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC:: 180 cmp_keys(const_key_reference r_lhs_key, const_key_reference r_rhs_key) const 181 { 182 return (cmp_prefixes(base_type::begin(r_lhs_key), 183 base_type::end(r_lhs_key), 184 base_type::begin(r_rhs_key), 185 base_type::end(r_rhs_key), 186 true)); 187 } 188 189 PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC 190 inline typename PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::const_key_reference 191 PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC:: 192 extract_key(const_reference r_val) 193 { 194 return (extract_key(r_val, s_set_ind)); 195 } 196 197 PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC 198 inline typename PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::const_key_reference 199 PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC:: 200 extract_key(const_reference r_val, true_type) 201 { 202 return (r_val); 203 } 204 205 PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC 206 inline typename PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC::const_key_reference 207 PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC:: 208 extract_key(const_reference r_val, false_type) 209 { 210 return (r_val.first); 211 } 212 213 #ifdef _GLIBCXX_DEBUG 214 PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC 215 bool 216 PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC:: 217 operator()(const_key_reference r_lhs, const_key_reference r_rhs) 218 { 219 return (cmp_keys(r_lhs, r_rhs)); 220 } 221 #endif 222 223 #undef PB_DS_SYNTH_E_ACCESS_TRAITS_T_DEC 224 #undef PB_DS_SYNTH_E_ACCESS_TRAITS_C_DEC 225 226 } // namespace detail 227 } // namespace __gnu_pbds 228 229 #endif 230