3 // Copyright (C) 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
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
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.
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.
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/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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
37 * @file trie_policy.hpp
38 * Contains trie-related policies.
41 #ifndef PB_DS_TRIE_POLICY_HPP
42 #define PB_DS_TRIE_POLICY_HPP
45 #include <ext/pb_ds/detail/type_utils.hpp>
46 #include <ext/pb_ds/detail/trie_policy/trie_policy_base.hpp>
50 // A null node updator, indicating that no node updates are required.
51 template<typename Const_Node_Iterator
,
52 typename Node_Iterator
,
53 typename E_Access_Traits
,
55 struct null_trie_node_update
58 #define PB_DS_CLASS_T_DEC \
59 template<typename String, typename String::value_type Min_E_Val, typename String::value_type Max_E_Val, bool Reverse, typename Allocator>
61 #define PB_DS_CLASS_C_DEC \
62 string_trie_e_access_traits<String, Min_E_Val,Max_E_Val,Reverse,Allocator>
64 // Element access traits for string types.
65 template<typename String
= std::string
,
66 typename
String::value_type Min_E_Val
= detail::__numeric_traits
<typename
String::value_type
>::__min
,
67 typename
String::value_type Max_E_Val
= detail::__numeric_traits
<typename
String::value_type
>::__max
,
69 typename Allocator
= std::allocator
<char> >
70 struct string_trie_e_access_traits
73 typedef typename
Allocator::size_type size_type
;
74 typedef String key_type
;
75 typedef typename
Allocator::template rebind
<key_type
>::other key_rebind
;
76 typedef typename
key_rebind::const_reference const_key_reference
;
83 // Element const iterator type.
84 typedef typename
detail::__conditional_type
<Reverse
, typename
String::const_reverse_iterator
, typename
String::const_iterator
>::__type const_iterator
;
87 typedef typename
std::iterator_traits
<const_iterator
>::value_type e_type
;
91 min_e_val
= Min_E_Val
,
92 max_e_val
= Max_E_Val
,
93 max_size
= max_e_val
- min_e_val
+ 1
95 PB_DS_STATIC_ASSERT(min_max_size
, max_size
>= 2);
97 // Returns a const_iterator to the first element of
98 // const_key_reference agumnet.
99 inline static const_iterator
100 begin(const_key_reference
);
102 // Returns a const_iterator to the after-last element of
103 // const_key_reference argument.
104 inline static const_iterator
105 end(const_key_reference
);
107 // Maps an element to a position.
108 inline static size_type
113 inline static const_iterator
114 begin_imp(const_key_reference
, detail::false_type
);
116 inline static const_iterator
117 begin_imp(const_key_reference
, detail::true_type
);
119 inline static const_iterator
120 end_imp(const_key_reference
, detail::false_type
);
122 inline static const_iterator
123 end_imp(const_key_reference
, detail::true_type
);
125 static detail::integral_constant
<int, Reverse
> s_rev_ind
;
128 #include <ext/pb_ds/detail/trie_policy/string_trie_e_access_traits_imp.hpp>
130 #undef PB_DS_CLASS_T_DEC
131 #undef PB_DS_CLASS_C_DEC
133 #define PB_DS_CLASS_T_DEC \
134 template<typename Const_Node_Iterator,typename Node_Iterator,class E_Access_Traits, typename Allocator>
136 #define PB_DS_CLASS_C_DEC \
137 trie_prefix_search_node_update<Const_Node_Iterator, Node_Iterator, E_Access_Traits,Allocator>
139 #define PB_DS_BASE_C_DEC \
140 detail::trie_policy_base<Const_Node_Iterator,Node_Iterator,E_Access_Traits, Allocator>
142 // A node updator that allows tries to be searched for the range of
143 // values that match a certain prefix.
144 template<typename Const_Node_Iterator
,
145 typename Node_Iterator
,
146 typename E_Access_Traits
,
148 class trie_prefix_search_node_update
: private PB_DS_BASE_C_DEC
151 typedef PB_DS_BASE_C_DEC base_type
;
154 typedef typename
base_type::key_type key_type
;
155 typedef typename
base_type::const_key_reference const_key_reference
;
157 // Element access traits.
158 typedef E_Access_Traits e_access_traits
;
160 // Const element iterator.
161 typedef typename
e_access_traits::const_iterator const_e_iterator
;
164 typedef Allocator allocator_type
;
167 typedef typename
allocator_type::size_type size_type
;
168 typedef detail::null_node_metadata metadata_type
;
169 typedef Const_Node_Iterator const_node_iterator
;
170 typedef Node_Iterator node_iterator
;
171 typedef typename
const_node_iterator::value_type const_iterator
;
172 typedef typename
node_iterator::value_type iterator
;
174 // Finds the const iterator range corresponding to all values
175 // whose prefixes match r_key.
176 std::pair
<const_iterator
, const_iterator
>
177 prefix_range(const_key_reference
) const;
179 // Finds the iterator range corresponding to all values whose
180 // prefixes match r_key.
181 std::pair
<iterator
, iterator
>
182 prefix_range(const_key_reference
);
184 // Finds the const iterator range corresponding to all values
185 // whose prefixes match [b, e).
186 std::pair
<const_iterator
, const_iterator
>
187 prefix_range(const_e_iterator
, const_e_iterator
) const;
189 // Finds the iterator range corresponding to all values whose
190 // prefixes match [b, e).
191 std::pair
<iterator
, iterator
>
192 prefix_range(const_e_iterator
, const_e_iterator
);
195 // Called to update a node's metadata.
197 operator()(node_iterator node_it
, const_node_iterator end_nd_it
) const;
200 // Returns the const iterator associated with the just-after last element.
201 virtual const_iterator
204 // Returns the iterator associated with the just-after last element.
208 // Returns the const_node_iterator associated with the trie's root node.
209 virtual const_node_iterator
210 node_begin() const = 0;
212 // Returns the node_iterator associated with the trie's root node.
213 virtual node_iterator
216 // Returns the const_node_iterator associated with a just-after leaf node.
217 virtual const_node_iterator
218 node_end() const = 0;
220 // Returns the node_iterator associated with a just-after leaf node.
221 virtual node_iterator
224 // Access to the cmp_fn object.
225 virtual const e_access_traits
&
226 get_e_access_traits() const = 0;
229 next_child(node_iterator
, const_e_iterator
, const_e_iterator
,
230 node_iterator
, const e_access_traits
&);
233 #include <ext/pb_ds/detail/trie_policy/prefix_search_node_update_imp.hpp>
235 #undef PB_DS_CLASS_C_DEC
237 #define PB_DS_CLASS_C_DEC \
238 trie_order_statistics_node_update<Const_Node_Iterator, Node_Iterator,E_Access_Traits, Allocator>
240 // Functor updating ranks of entrees.
241 template<typename Const_Node_Iterator
,
242 typename Node_Iterator
,
243 typename E_Access_Traits
,
245 class trie_order_statistics_node_update
: private PB_DS_BASE_C_DEC
248 typedef PB_DS_BASE_C_DEC base_type
;
251 typedef E_Access_Traits e_access_traits
;
252 typedef typename
e_access_traits::const_iterator const_e_iterator
;
253 typedef Allocator allocator_type
;
254 typedef typename
allocator_type::size_type size_type
;
255 typedef typename
base_type::key_type key_type
;
256 typedef typename
base_type::const_key_reference const_key_reference
;
258 typedef size_type metadata_type
;
259 typedef Const_Node_Iterator const_node_iterator
;
260 typedef Node_Iterator node_iterator
;
261 typedef typename
const_node_iterator::value_type const_iterator
;
262 typedef typename
node_iterator::value_type iterator
;
264 // Finds an entry by __order. Returns a const_iterator to the
265 // entry with the __order order, or a const_iterator to the
266 // container object's end if order is at least the size of the
268 inline const_iterator
269 find_by_order(size_type
) const;
271 // Finds an entry by __order. Returns an iterator to the entry
272 // with the __order order, or an iterator to the container
273 // object's end if order is at least the size of the container
276 find_by_order(size_type
);
278 // Returns the order of a key within a sequence. For exapmle, if
279 // r_key is the smallest key, this method will return 0; if r_key
280 // is a key between the smallest and next key, this method will
281 // return 1; if r_key is a key larger than the largest key, this
282 // method will return the size of r_c.
284 order_of_key(const_key_reference
) const;
286 // Returns the order of a prefix within a sequence. For exapmle,
287 // if [b, e] is the smallest prefix, this method will return 0; if
288 // r_key is a key between the smallest and next key, this method
289 // will return 1; if r_key is a key larger than the largest key,
290 // this method will return the size of r_c.
292 order_of_prefix(const_e_iterator
, const_e_iterator
) const;
295 typedef typename
base_type::const_reference const_reference
;
296 typedef typename
base_type::const_pointer const_pointer
;
298 typedef typename
Allocator::template rebind
<metadata_type
>::other metadata_rebind
;
299 typedef typename
metadata_rebind::const_reference const_metadata_reference
;
300 typedef typename
metadata_rebind::reference metadata_reference
;
302 // Returns true if the container is empty.
306 // Returns the iterator associated with the trie's first element.
310 // Returns the iterator associated with the trie's
311 // just-after-last element.
315 // Returns the const_node_iterator associated with the trie's root node.
316 virtual const_node_iterator
317 node_begin() const = 0;
319 // Returns the node_iterator associated with the trie's root node.
320 virtual node_iterator
323 // Returns the const_node_iterator associated with a just-after
325 virtual const_node_iterator
326 node_end() const = 0;
328 // Returns the node_iterator associated with a just-after leaf node.
329 virtual node_iterator
332 // Access to the cmp_fn object.
333 virtual e_access_traits
&
334 get_e_access_traits() = 0;
337 // Updates the rank of a node through a node_iterator node_it;
338 // end_nd_it is the end node iterator.
340 operator()(node_iterator
, const_node_iterator
) const;
344 ~trie_order_statistics_node_update();
347 #include <ext/pb_ds/detail/trie_policy/order_statistics_imp.hpp>
349 #undef PB_DS_CLASS_T_DEC
350 #undef PB_DS_CLASS_C_DEC
351 #undef PB_DS_BASE_C_DEC
353 } // namespace __gnu_pbds