3 // Copyright (C) 2005, 2006 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
43 * @file trie_policy.hpp
44 * Contains trie-related policies.
47 #ifndef PB_DS_TRIE_POLICY_HPP
48 #define PB_DS_TRIE_POLICY_HPP
51 #include <ext/pb_ds/detail/type_utils.hpp>
52 #include <ext/pb_ds/detail/trie_policy/trie_policy_base.hpp>
56 // A null node updator, indicating that no node updates are required.
57 template<typename Const_Node_Iterator,
58 typename Node_Iterator,
59 typename E_Access_Traits,
61 struct null_trie_node_update
64 #define PB_DS_STATIC_ASSERT(UNIQUE, E) \
65 typedef detail::static_assert_dumclass<sizeof(detail::static_assert<bool(E)>)> UNIQUE##_static_assert_type
67 #define PB_DS_CLASS_T_DEC \
68 template<typename String, typename String::value_type Min_E_Val, typename String::value_type Max_E_Val, bool Reverse, typename Allocator>
70 #define PB_DS_CLASS_C_DEC \
71 string_trie_e_access_traits<String, Min_E_Val,Max_E_Val,Reverse,Allocator>
73 // Element access traits for string types.
74 template<typename String = std::string,
75 typename String::value_type Min_E_Val = detail::__numeric_traits<typename String::value_type>::__min,
76 typename String::value_type Max_E_Val = detail::__numeric_traits<typename String::value_type>::__max,
78 typename Allocator = std::allocator<char> >
79 struct string_trie_e_access_traits
82 typedef typename Allocator::size_type size_type;
83 typedef String key_type;
84 typedef typename Allocator::template rebind<key_type>::other key_rebind;
85 typedef typename key_rebind::const_reference const_key_reference;
92 // Element const iterator type.
93 typedef typename detail::__conditional_type<Reverse, typename String::const_reverse_iterator, typename String::const_iterator>::__type const_iterator;
96 typedef typename std::iterator_traits<const_iterator>::value_type e_type;
100 min_e_val = Min_E_Val,
101 max_e_val = Max_E_Val,
102 max_size = max_e_val - min_e_val + 1
104 PB_DS_STATIC_ASSERT(min_max_size, max_size >= 2);
106 // Returns a const_iterator to the first element of
107 // const_key_reference agumnet.
108 inline static const_iterator
109 begin(const_key_reference);
111 // Returns a const_iterator to the after-last element of
112 // const_key_reference argument.
113 inline static const_iterator
114 end(const_key_reference);
116 // Maps an element to a position.
117 inline static size_type
122 inline static const_iterator
123 begin_imp(const_key_reference, detail::false_type);
125 inline static const_iterator
126 begin_imp(const_key_reference, detail::true_type);
128 inline static const_iterator
129 end_imp(const_key_reference, detail::false_type);
131 inline static const_iterator
132 end_imp(const_key_reference, detail::true_type);
134 static detail::integral_constant<int, Reverse> s_rev_ind;
137 #include <ext/pb_ds/detail/trie_policy/string_trie_e_access_traits_imp.hpp>
139 #undef PB_DS_CLASS_T_DEC
140 #undef PB_DS_CLASS_C_DEC
142 #define PB_DS_CLASS_T_DEC \
143 template<typename Const_Node_Iterator,typename Node_Iterator,class E_Access_Traits, typename Allocator>
145 #define PB_DS_CLASS_C_DEC \
146 trie_prefix_search_node_update<Const_Node_Iterator, Node_Iterator, E_Access_Traits,Allocator>
148 #define PB_DS_BASE_C_DEC \
149 detail::trie_policy_base<Const_Node_Iterator,Node_Iterator,E_Access_Traits, Allocator>
151 // A node updator that allows tries to be searched for the range of
152 // values that match a certain prefix.
153 template<typename Const_Node_Iterator,
154 typename Node_Iterator,
155 typename E_Access_Traits,
157 class trie_prefix_search_node_update : private PB_DS_BASE_C_DEC
160 typedef PB_DS_BASE_C_DEC base_type;
163 typedef typename base_type::key_type key_type;
164 typedef typename base_type::const_key_reference const_key_reference;
166 // Element access traits.
167 typedef E_Access_Traits e_access_traits;
169 // Const element iterator.
170 typedef typename e_access_traits::const_iterator const_e_iterator;
173 typedef Allocator allocator;
176 typedef typename allocator::size_type size_type;
177 typedef detail::null_node_metadata metadata_type;
178 typedef Const_Node_Iterator const_node_iterator;
179 typedef Node_Iterator node_iterator;
180 typedef typename const_node_iterator::value_type const_iterator;
181 typedef typename node_iterator::value_type iterator;
183 // Finds the const iterator range corresponding to all values
184 // whose prefixes match r_key.
185 std::pair<const_iterator, const_iterator>
186 prefix_range(const_key_reference) const;
188 // Finds the iterator range corresponding to all values whose
189 // prefixes match r_key.
190 std::pair<iterator, iterator>
191 prefix_range(const_key_reference);
193 // Finds the const iterator range corresponding to all values
194 // whose prefixes match [b, e).
195 std::pair<const_iterator, const_iterator>
196 prefix_range(const_e_iterator, const_e_iterator) const;
198 // Finds the iterator range corresponding to all values whose
199 // prefixes match [b, e).
200 std::pair<iterator, iterator>
201 prefix_range(const_e_iterator, const_e_iterator);
204 // Called to update a node's metadata.
206 operator()(node_iterator node_it, const_node_iterator end_nd_it) const;
209 // Returns the const iterator associated with the just-after last element.
210 virtual const_iterator
213 // Returns the iterator associated with the just-after last element.
217 // Returns the const_node_iterator associated with the trie's root node.
218 virtual const_node_iterator
219 node_begin() const = 0;
221 // Returns the node_iterator associated with the trie's root node.
222 virtual node_iterator
225 // Returns the const_node_iterator associated with a just-after leaf node.
226 virtual const_node_iterator
227 node_end() const = 0;
229 // Returns the node_iterator associated with a just-after leaf node.
230 virtual node_iterator
233 // Access to the cmp_fn object.
234 virtual const e_access_traits&
235 get_e_access_traits() const = 0;
238 next_child(node_iterator, const_e_iterator, const_e_iterator,
239 node_iterator, const e_access_traits&);
242 #include <ext/pb_ds/detail/trie_policy/prefix_search_node_update_imp.hpp>
244 #undef PB_DS_CLASS_C_DEC
246 #define PB_DS_CLASS_C_DEC \
247 trie_order_statistics_node_update<Const_Node_Iterator, Node_Iterator,E_Access_Traits, Allocator>
249 // Functor updating ranks of entrees.
250 template<typename Const_Node_Iterator,
251 typename Node_Iterator,
252 typename E_Access_Traits,
254 class trie_order_statistics_node_update : private PB_DS_BASE_C_DEC
257 typedef PB_DS_BASE_C_DEC base_type;
260 typedef E_Access_Traits e_access_traits;
261 typedef typename e_access_traits::const_iterator const_e_iterator;
262 typedef Allocator allocator;
263 typedef typename allocator::size_type size_type;
264 typedef typename base_type::key_type key_type;
265 typedef typename base_type::const_key_reference const_key_reference;
267 typedef size_type metadata_type;
268 typedef Const_Node_Iterator const_node_iterator;
269 typedef Node_Iterator node_iterator;
270 typedef typename const_node_iterator::value_type const_iterator;
271 typedef typename node_iterator::value_type iterator;
273 // Finds an entry by __order. Returns a const_iterator to the
274 // entry with the __order order, or a const_iterator to the
275 // container object's end if order is at least the size of the
277 inline const_iterator
278 find_by_order(size_type) const;
280 // Finds an entry by __order. Returns an iterator to the entry
281 // with the __order order, or an iterator to the container
282 // object's end if order is at least the size of the container
285 find_by_order(size_type);
287 // Returns the order of a key within a sequence. For exapmle, if
288 // r_key is the smallest key, this method will return 0; if r_key
289 // is a key between the smallest and next key, this method will
290 // return 1; if r_key is a key larger than the largest key, this
291 // method will return the size of r_c.
293 order_of_key(const_key_reference) const;
295 // Returns the order of a prefix within a sequence. For exapmle,
296 // if [b, e] is the smallest prefix, this method will return 0; if
297 // r_key is a key between the smallest and next key, this method
298 // will return 1; if r_key is a key larger than the largest key,
299 // this method will return the size of r_c.
301 order_of_prefix(const_e_iterator, const_e_iterator) const;
304 typedef typename base_type::const_reference const_reference;
305 typedef typename base_type::const_pointer const_pointer;
307 typedef typename Allocator::template rebind<metadata_type>::other metadata_rebind;
308 typedef typename metadata_rebind::const_reference const_metadata_reference;
309 typedef typename metadata_rebind::reference metadata_reference;
311 // Returns true if the container is empty.
315 // Returns the iterator associated with the trie's first element.
319 // Returns the iterator associated with the trie's
320 // just-after-last element.
324 // Returns the const_node_iterator associated with the trie's root node.
325 virtual const_node_iterator
326 node_begin() const = 0;
328 // Returns the node_iterator associated with the trie's root node.
329 virtual node_iterator
332 // Returns the const_node_iterator associated with a just-after
334 virtual const_node_iterator
335 node_end() const = 0;
337 // Returns the node_iterator associated with a just-after leaf node.
338 virtual node_iterator
341 // Access to the cmp_fn object.
342 virtual e_access_traits&
343 get_e_access_traits() = 0;
346 // Updates the rank of a node through a node_iterator node_it;
347 // end_nd_it is the end node iterator.
349 operator()(node_iterator, const_node_iterator) const;
353 ~trie_order_statistics_node_update();
356 #include <ext/pb_ds/detail/trie_policy/order_statistics_imp.hpp>
358 #undef PB_DS_CLASS_T_DEC
359 #undef PB_DS_CLASS_C_DEC
360 #undef PB_DS_BASE_C_DEC
361 #undef PB_DS_STATIC_ASSERT