Update concepts branch to revision 131834
[official-gcc.git] / libstdc++-v3 / include / ext / pb_ds / trie_policy.hpp
blob642c5300a660d8f593af2def89421808d06ac3b3
1 // -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2007 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 2, or (at your option) any later
9 // version.
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
29 // Public License.
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
40 // warranty.
42 /**
43 * @file trie_policy.hpp
44 * Contains trie-related policies.
47 #ifndef PB_DS_TRIE_POLICY_HPP
48 #define PB_DS_TRIE_POLICY_HPP
50 #include <string>
51 #include <ext/pb_ds/detail/type_utils.hpp>
52 #include <ext/pb_ds/detail/trie_policy/trie_policy_base.hpp>
54 namespace __gnu_pbds
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,
60 typename Allocator>
61 struct null_trie_node_update
62 { };
64 #define PB_DS_CLASS_T_DEC \
65 template<typename String, typename String::value_type Min_E_Val, typename String::value_type Max_E_Val, bool Reverse, typename Allocator>
67 #define PB_DS_CLASS_C_DEC \
68 string_trie_e_access_traits<String, Min_E_Val,Max_E_Val,Reverse,Allocator>
70 // Element access traits for string types.
71 template<typename String = std::string,
72 typename String::value_type Min_E_Val = detail::__numeric_traits<typename String::value_type>::__min,
73 typename String::value_type Max_E_Val = detail::__numeric_traits<typename String::value_type>::__max,
74 bool Reverse = false,
75 typename Allocator = std::allocator<char> >
76 struct string_trie_e_access_traits
78 public:
79 typedef typename Allocator::size_type size_type;
80 typedef String key_type;
81 typedef typename Allocator::template rebind<key_type>::other key_rebind;
82 typedef typename key_rebind::const_reference const_key_reference;
84 enum
86 reverse = Reverse
89 // Element const iterator type.
90 typedef typename detail::__conditional_type<Reverse, typename String::const_reverse_iterator, typename String::const_iterator>::__type const_iterator;
92 // Element type.
93 typedef typename std::iterator_traits<const_iterator>::value_type e_type;
95 enum
97 min_e_val = Min_E_Val,
98 max_e_val = Max_E_Val,
99 max_size = max_e_val - min_e_val + 1
101 PB_DS_STATIC_ASSERT(min_max_size, max_size >= 2);
103 // Returns a const_iterator to the first element of
104 // const_key_reference agumnet.
105 inline static const_iterator
106 begin(const_key_reference);
108 // Returns a const_iterator to the after-last element of
109 // const_key_reference argument.
110 inline static const_iterator
111 end(const_key_reference);
113 // Maps an element to a position.
114 inline static size_type
115 e_pos(e_type e);
117 private:
119 inline static const_iterator
120 begin_imp(const_key_reference, detail::false_type);
122 inline static const_iterator
123 begin_imp(const_key_reference, detail::true_type);
125 inline static const_iterator
126 end_imp(const_key_reference, detail::false_type);
128 inline static const_iterator
129 end_imp(const_key_reference, detail::true_type);
131 static detail::integral_constant<int, Reverse> s_rev_ind;
134 #include <ext/pb_ds/detail/trie_policy/string_trie_e_access_traits_imp.hpp>
136 #undef PB_DS_CLASS_T_DEC
137 #undef PB_DS_CLASS_C_DEC
139 #define PB_DS_CLASS_T_DEC \
140 template<typename Const_Node_Iterator,typename Node_Iterator,class E_Access_Traits, typename Allocator>
142 #define PB_DS_CLASS_C_DEC \
143 trie_prefix_search_node_update<Const_Node_Iterator, Node_Iterator, E_Access_Traits,Allocator>
145 #define PB_DS_BASE_C_DEC \
146 detail::trie_policy_base<Const_Node_Iterator,Node_Iterator,E_Access_Traits, Allocator>
148 // A node updator that allows tries to be searched for the range of
149 // values that match a certain prefix.
150 template<typename Const_Node_Iterator,
151 typename Node_Iterator,
152 typename E_Access_Traits,
153 typename Allocator>
154 class trie_prefix_search_node_update : private PB_DS_BASE_C_DEC
156 private:
157 typedef PB_DS_BASE_C_DEC base_type;
159 public:
160 typedef typename base_type::key_type key_type;
161 typedef typename base_type::const_key_reference const_key_reference;
163 // Element access traits.
164 typedef E_Access_Traits e_access_traits;
166 // Const element iterator.
167 typedef typename e_access_traits::const_iterator const_e_iterator;
169 // Allocator type.
170 typedef Allocator allocator_type;
172 // Size type.
173 typedef typename allocator_type::size_type size_type;
174 typedef detail::null_node_metadata metadata_type;
175 typedef Const_Node_Iterator const_node_iterator;
176 typedef Node_Iterator node_iterator;
177 typedef typename const_node_iterator::value_type const_iterator;
178 typedef typename node_iterator::value_type iterator;
180 // Finds the const iterator range corresponding to all values
181 // whose prefixes match r_key.
182 std::pair<const_iterator, const_iterator>
183 prefix_range(const_key_reference) const;
185 // Finds the iterator range corresponding to all values whose
186 // prefixes match r_key.
187 std::pair<iterator, iterator>
188 prefix_range(const_key_reference);
190 // Finds the const iterator range corresponding to all values
191 // whose prefixes match [b, e).
192 std::pair<const_iterator, const_iterator>
193 prefix_range(const_e_iterator, const_e_iterator) const;
195 // Finds the iterator range corresponding to all values whose
196 // prefixes match [b, e).
197 std::pair<iterator, iterator>
198 prefix_range(const_e_iterator, const_e_iterator);
200 protected:
201 // Called to update a node's metadata.
202 inline void
203 operator()(node_iterator node_it, const_node_iterator end_nd_it) const;
205 private:
206 // Returns the const iterator associated with the just-after last element.
207 virtual const_iterator
208 end() const = 0;
210 // Returns the iterator associated with the just-after last element.
211 virtual iterator
212 end() = 0;
214 // Returns the const_node_iterator associated with the trie's root node.
215 virtual const_node_iterator
216 node_begin() const = 0;
218 // Returns the node_iterator associated with the trie's root node.
219 virtual node_iterator
220 node_begin() = 0;
222 // Returns the const_node_iterator associated with a just-after leaf node.
223 virtual const_node_iterator
224 node_end() const = 0;
226 // Returns the node_iterator associated with a just-after leaf node.
227 virtual node_iterator
228 node_end() = 0;
230 // Access to the cmp_fn object.
231 virtual const e_access_traits&
232 get_e_access_traits() const = 0;
234 node_iterator
235 next_child(node_iterator, const_e_iterator, const_e_iterator,
236 node_iterator, const e_access_traits&);
239 #include <ext/pb_ds/detail/trie_policy/prefix_search_node_update_imp.hpp>
241 #undef PB_DS_CLASS_C_DEC
243 #define PB_DS_CLASS_C_DEC \
244 trie_order_statistics_node_update<Const_Node_Iterator, Node_Iterator,E_Access_Traits, Allocator>
246 // Functor updating ranks of entrees.
247 template<typename Const_Node_Iterator,
248 typename Node_Iterator,
249 typename E_Access_Traits,
250 typename Allocator>
251 class trie_order_statistics_node_update : private PB_DS_BASE_C_DEC
253 private:
254 typedef PB_DS_BASE_C_DEC base_type;
256 public:
257 typedef E_Access_Traits e_access_traits;
258 typedef typename e_access_traits::const_iterator const_e_iterator;
259 typedef Allocator allocator_type;
260 typedef typename allocator_type::size_type size_type;
261 typedef typename base_type::key_type key_type;
262 typedef typename base_type::const_key_reference const_key_reference;
264 typedef size_type metadata_type;
265 typedef Const_Node_Iterator const_node_iterator;
266 typedef Node_Iterator node_iterator;
267 typedef typename const_node_iterator::value_type const_iterator;
268 typedef typename node_iterator::value_type iterator;
270 // Finds an entry by __order. Returns a const_iterator to the
271 // entry with the __order order, or a const_iterator to the
272 // container object's end if order is at least the size of the
273 // container object.
274 inline const_iterator
275 find_by_order(size_type) const;
277 // Finds an entry by __order. Returns an iterator to the entry
278 // with the __order order, or an iterator to the container
279 // object's end if order is at least the size of the container
280 // object.
281 inline iterator
282 find_by_order(size_type);
284 // Returns the order of a key within a sequence. For exapmle, if
285 // r_key is the smallest key, this method will return 0; if r_key
286 // is a key between the smallest and next key, this method will
287 // return 1; if r_key is a key larger than the largest key, this
288 // method will return the size of r_c.
289 inline size_type
290 order_of_key(const_key_reference) const;
292 // Returns the order of a prefix within a sequence. For exapmle,
293 // if [b, e] is the smallest prefix, this method will return 0; if
294 // r_key is a key between the smallest and next key, this method
295 // will return 1; if r_key is a key larger than the largest key,
296 // this method will return the size of r_c.
297 inline size_type
298 order_of_prefix(const_e_iterator, const_e_iterator) const;
300 private:
301 typedef typename base_type::const_reference const_reference;
302 typedef typename base_type::const_pointer const_pointer;
304 typedef typename Allocator::template rebind<metadata_type>::other metadata_rebind;
305 typedef typename metadata_rebind::const_reference const_metadata_reference;
306 typedef typename metadata_rebind::reference metadata_reference;
308 // Returns true if the container is empty.
309 virtual bool
310 empty() const = 0;
312 // Returns the iterator associated with the trie's first element.
313 virtual iterator
314 begin() = 0;
316 // Returns the iterator associated with the trie's
317 // just-after-last element.
318 virtual iterator
319 end() = 0;
321 // Returns the const_node_iterator associated with the trie's root node.
322 virtual const_node_iterator
323 node_begin() const = 0;
325 // Returns the node_iterator associated with the trie's root node.
326 virtual node_iterator
327 node_begin() = 0;
329 // Returns the const_node_iterator associated with a just-after
330 // leaf node.
331 virtual const_node_iterator
332 node_end() const = 0;
334 // Returns the node_iterator associated with a just-after leaf node.
335 virtual node_iterator
336 node_end() = 0;
338 // Access to the cmp_fn object.
339 virtual e_access_traits&
340 get_e_access_traits() = 0;
342 protected:
343 // Updates the rank of a node through a node_iterator node_it;
344 // end_nd_it is the end node iterator.
345 inline void
346 operator()(node_iterator, const_node_iterator) const;
348 // Destructor.
349 virtual
350 ~trie_order_statistics_node_update();
353 #include <ext/pb_ds/detail/trie_policy/order_statistics_imp.hpp>
355 #undef PB_DS_CLASS_T_DEC
356 #undef PB_DS_CLASS_C_DEC
357 #undef PB_DS_BASE_C_DEC
359 } // namespace __gnu_pbds
361 #endif