xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/ext/pb_ds/trie_policy.hpp (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj // -*- C++ -*-
2*38fd1498Szrj 
3*38fd1498Szrj // Copyright (C) 2005-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library.  This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the terms
7*38fd1498Szrj // of the GNU General Public License as published by the Free Software
8*38fd1498Szrj // Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj // version.
10*38fd1498Szrj 
11*38fd1498Szrj // This library is distributed in the hope that it will be useful, but
12*38fd1498Szrj // WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*38fd1498Szrj // General Public License for more details.
15*38fd1498Szrj 
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj 
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj 
25*38fd1498Szrj // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
26*38fd1498Szrj 
27*38fd1498Szrj // Permission to use, copy, modify, sell, and distribute this software
28*38fd1498Szrj // is hereby granted without fee, provided that the above copyright
29*38fd1498Szrj // notice appears in all copies, and that both that copyright notice
30*38fd1498Szrj // and this permission notice appear in supporting documentation. None
31*38fd1498Szrj // of the above authors, nor IBM Haifa Research Laboratories, make any
32*38fd1498Szrj // representation about the suitability of this software for any
33*38fd1498Szrj // purpose. It is provided "as is" without express or implied
34*38fd1498Szrj // warranty.
35*38fd1498Szrj 
36*38fd1498Szrj /**
37*38fd1498Szrj  * @file trie_policy.hpp
38*38fd1498Szrj  * Contains trie-related policies.
39*38fd1498Szrj  */
40*38fd1498Szrj 
41*38fd1498Szrj #ifndef PB_DS_TRIE_POLICY_HPP
42*38fd1498Szrj #define PB_DS_TRIE_POLICY_HPP
43*38fd1498Szrj 
44*38fd1498Szrj #include <bits/c++config.h>
45*38fd1498Szrj #include <string>
46*38fd1498Szrj #include <ext/pb_ds/detail/type_utils.hpp>
47*38fd1498Szrj #include <ext/pb_ds/detail/trie_policy/trie_policy_base.hpp>
48*38fd1498Szrj 
49*38fd1498Szrj namespace __gnu_pbds
50*38fd1498Szrj {
51*38fd1498Szrj #define PB_DS_CLASS_T_DEC \
52*38fd1498Szrj   template<typename String, typename String::value_type Min_E_Val, \
53*38fd1498Szrj 	   typename String::value_type Max_E_Val, bool Reverse, \
54*38fd1498Szrj 	   typename _Alloc>
55*38fd1498Szrj 
56*38fd1498Szrj #define PB_DS_CLASS_C_DEC \
57*38fd1498Szrj   trie_string_access_traits<String, Min_E_Val,Max_E_Val,Reverse,_Alloc>
58*38fd1498Szrj 
59*38fd1498Szrj   /**
60*38fd1498Szrj    *  Element access traits for string types.
61*38fd1498Szrj    *
62*38fd1498Szrj    *  @tparam String 	    	String type.
63*38fd1498Szrj    *  @tparam Min_E_Val        	Minimal element value.
64*38fd1498Szrj    *  @tparam Max_E_Val	    	Maximum element value.
65*38fd1498Szrj    *  @tparam Reverse	        Reverse iteration should be used.
66*38fd1498Szrj    *                            Default: false.
67*38fd1498Szrj    *  @tparam _Alloc 	    	Allocator type.
68*38fd1498Szrj    */
69*38fd1498Szrj   template<typename String = std::string,
70*38fd1498Szrj 	   typename String::value_type Min_E_Val = detail::__numeric_traits<typename String::value_type>::__min,
71*38fd1498Szrj 	   typename String::value_type Max_E_Val = detail::__numeric_traits<typename String::value_type>::__max,
72*38fd1498Szrj 	   bool Reverse = false,
73*38fd1498Szrj 	   typename _Alloc = std::allocator<char> >
74*38fd1498Szrj   struct trie_string_access_traits
75*38fd1498Szrj   {
76*38fd1498Szrj   public:
77*38fd1498Szrj     typedef typename _Alloc::size_type			  size_type;
78*38fd1498Szrj     typedef String 					  key_type;
79*38fd1498Szrj     typedef typename _Alloc::template rebind<key_type>	  __rebind_k;
80*38fd1498Szrj     typedef typename __rebind_k::other::const_reference   key_const_reference;
81*38fd1498Szrj 
82*38fd1498Szrj     enum
83*38fd1498Szrj       {
84*38fd1498Szrj 	reverse = Reverse
85*38fd1498Szrj       };
86*38fd1498Szrj 
87*38fd1498Szrj     /// Element const iterator type.
88*38fd1498Szrj     typedef typename detail::__conditional_type<Reverse, \
89*38fd1498Szrj 		       typename String::const_reverse_iterator, \
90*38fd1498Szrj 		       typename String::const_iterator>::__type const_iterator;
91*38fd1498Szrj 
92*38fd1498Szrj     /// Element type.
93*38fd1498Szrj     typedef typename std::iterator_traits<const_iterator>::value_type e_type;
94*38fd1498Szrj 
95*38fd1498Szrj     enum
96*38fd1498Szrj       {
97*38fd1498Szrj 	min_e_val = Min_E_Val,
98*38fd1498Szrj 	max_e_val = Max_E_Val,
99*38fd1498Szrj 	max_size = max_e_val - min_e_val + 1
100*38fd1498Szrj       };
101*38fd1498Szrj     PB_DS_STATIC_ASSERT(min_max_size, max_size >= 2);
102*38fd1498Szrj 
103*38fd1498Szrj     /// Returns a const_iterator to the first element of
104*38fd1498Szrj     /// key_const_reference agumnet.
105*38fd1498Szrj     inline static const_iterator
106*38fd1498Szrj     begin(key_const_reference);
107*38fd1498Szrj 
108*38fd1498Szrj     /// Returns a const_iterator to the after-last element of
109*38fd1498Szrj     /// key_const_reference argument.
110*38fd1498Szrj     inline static const_iterator
111*38fd1498Szrj     end(key_const_reference);
112*38fd1498Szrj 
113*38fd1498Szrj     /// Maps an element to a position.
114*38fd1498Szrj     inline static size_type
115*38fd1498Szrj     e_pos(e_type e);
116*38fd1498Szrj 
117*38fd1498Szrj   private:
118*38fd1498Szrj     inline static const_iterator
119*38fd1498Szrj     begin_imp(key_const_reference, detail::false_type);
120*38fd1498Szrj 
121*38fd1498Szrj     inline static const_iterator
122*38fd1498Szrj     begin_imp(key_const_reference, detail::true_type);
123*38fd1498Szrj 
124*38fd1498Szrj     inline static const_iterator
125*38fd1498Szrj     end_imp(key_const_reference, detail::false_type);
126*38fd1498Szrj 
127*38fd1498Szrj     inline static const_iterator
128*38fd1498Szrj     end_imp(key_const_reference, detail::true_type);
129*38fd1498Szrj 
130*38fd1498Szrj     static detail::integral_constant<int, Reverse> s_rev_ind;
131*38fd1498Szrj   };
132*38fd1498Szrj 
133*38fd1498Szrj #include <ext/pb_ds/detail/trie_policy/trie_string_access_traits_imp.hpp>
134*38fd1498Szrj 
135*38fd1498Szrj #undef PB_DS_CLASS_T_DEC
136*38fd1498Szrj #undef PB_DS_CLASS_C_DEC
137*38fd1498Szrj 
138*38fd1498Szrj #define PB_DS_CLASS_T_DEC \
139*38fd1498Szrj   template<typename Node_CItr,typename Node_Itr, \
140*38fd1498Szrj 	   typename _ATraits, typename _Alloc>
141*38fd1498Szrj 
142*38fd1498Szrj #define PB_DS_CLASS_C_DEC \
143*38fd1498Szrj   trie_prefix_search_node_update<Node_CItr, Node_Itr, \
144*38fd1498Szrj 				 _ATraits,_Alloc>
145*38fd1498Szrj 
146*38fd1498Szrj #define PB_DS_TRIE_POLICY_BASE \
147*38fd1498Szrj   detail::trie_policy_base<Node_CItr,Node_Itr,_ATraits, _Alloc>
148*38fd1498Szrj 
149*38fd1498Szrj   /// A node updator that allows tries to be searched for the range of
150*38fd1498Szrj   /// values that match a certain prefix.
151*38fd1498Szrj   template<typename Node_CItr,
152*38fd1498Szrj 	   typename Node_Itr,
153*38fd1498Szrj 	   typename _ATraits,
154*38fd1498Szrj 	   typename _Alloc>
155*38fd1498Szrj   class trie_prefix_search_node_update : private PB_DS_TRIE_POLICY_BASE
156*38fd1498Szrj   {
157*38fd1498Szrj   private:
158*38fd1498Szrj     typedef PB_DS_TRIE_POLICY_BASE 		       	base_type;
159*38fd1498Szrj 
160*38fd1498Szrj   public:
161*38fd1498Szrj     typedef typename base_type::key_type 		key_type;
162*38fd1498Szrj     typedef typename base_type::key_const_reference 	key_const_reference;
163*38fd1498Szrj 
164*38fd1498Szrj     /// Element access traits.
165*38fd1498Szrj     typedef _ATraits 				access_traits;
166*38fd1498Szrj 
167*38fd1498Szrj     /// Const element iterator.
168*38fd1498Szrj     typedef typename access_traits::const_iterator 	a_const_iterator;
169*38fd1498Szrj 
170*38fd1498Szrj     /// _Alloc type.
171*38fd1498Szrj     typedef _Alloc 	       				allocator_type;
172*38fd1498Szrj 
173*38fd1498Szrj     /// Size type.
174*38fd1498Szrj     typedef typename allocator_type::size_type 		size_type;
175*38fd1498Szrj     typedef null_type 					metadata_type;
176*38fd1498Szrj     typedef Node_Itr 					node_iterator;
177*38fd1498Szrj     typedef Node_CItr 					node_const_iterator;
178*38fd1498Szrj     typedef typename node_iterator::value_type 		iterator;
179*38fd1498Szrj     typedef typename node_const_iterator::value_type 	const_iterator;
180*38fd1498Szrj 
181*38fd1498Szrj     /// Finds the const iterator range corresponding to all values
182*38fd1498Szrj     /// whose prefixes match r_key.
183*38fd1498Szrj     std::pair<const_iterator, const_iterator>
184*38fd1498Szrj     prefix_range(key_const_reference) const;
185*38fd1498Szrj 
186*38fd1498Szrj     /// Finds the iterator range corresponding to all values whose
187*38fd1498Szrj     /// prefixes match r_key.
188*38fd1498Szrj     std::pair<iterator, iterator>
189*38fd1498Szrj     prefix_range(key_const_reference);
190*38fd1498Szrj 
191*38fd1498Szrj     /// Finds the const iterator range corresponding to all values
192*38fd1498Szrj     /// whose prefixes match [b, e).
193*38fd1498Szrj     std::pair<const_iterator, const_iterator>
194*38fd1498Szrj     prefix_range(a_const_iterator, a_const_iterator) const;
195*38fd1498Szrj 
196*38fd1498Szrj     /// Finds the iterator range corresponding to all values whose
197*38fd1498Szrj     /// prefixes match [b, e).
198*38fd1498Szrj     std::pair<iterator, iterator>
199*38fd1498Szrj     prefix_range(a_const_iterator, a_const_iterator);
200*38fd1498Szrj 
201*38fd1498Szrj   protected:
202*38fd1498Szrj     /// Called to update a node's metadata.
203*38fd1498Szrj     inline void
204*38fd1498Szrj     operator()(node_iterator node_it, node_const_iterator end_nd_it) const;
205*38fd1498Szrj 
206*38fd1498Szrj   private:
207*38fd1498Szrj     node_iterator
208*38fd1498Szrj     next_child(node_iterator, a_const_iterator, a_const_iterator,
209*38fd1498Szrj 	       node_iterator, const access_traits&);
210*38fd1498Szrj 
211*38fd1498Szrj     /// Returns the const iterator associated with the just-after last element.
212*38fd1498Szrj     virtual const_iterator
213*38fd1498Szrj     end() const = 0;
214*38fd1498Szrj 
215*38fd1498Szrj     /// Returns the iterator associated with the just-after last element.
216*38fd1498Szrj     virtual iterator
217*38fd1498Szrj     end() = 0;
218*38fd1498Szrj 
219*38fd1498Szrj     /// Returns the node_const_iterator associated with the trie's root node.
220*38fd1498Szrj     virtual node_const_iterator
221*38fd1498Szrj     node_begin() const = 0;
222*38fd1498Szrj 
223*38fd1498Szrj     /// Returns the node_iterator associated with the trie's root node.
224*38fd1498Szrj     virtual node_iterator
225*38fd1498Szrj     node_begin() = 0;
226*38fd1498Szrj 
227*38fd1498Szrj     /// Returns the node_const_iterator associated with a just-after leaf node.
228*38fd1498Szrj     virtual node_const_iterator
229*38fd1498Szrj     node_end() const = 0;
230*38fd1498Szrj 
231*38fd1498Szrj     /// Returns the node_iterator associated with a just-after leaf node.
232*38fd1498Szrj     virtual node_iterator
233*38fd1498Szrj     node_end() = 0;
234*38fd1498Szrj 
235*38fd1498Szrj     /// Access to the cmp_fn object.
236*38fd1498Szrj     virtual const access_traits&
237*38fd1498Szrj     get_access_traits() const = 0;
238*38fd1498Szrj   };
239*38fd1498Szrj 
240*38fd1498Szrj #include <ext/pb_ds/detail/trie_policy/prefix_search_node_update_imp.hpp>
241*38fd1498Szrj 
242*38fd1498Szrj #undef PB_DS_CLASS_C_DEC
243*38fd1498Szrj 
244*38fd1498Szrj #define PB_DS_CLASS_C_DEC \
245*38fd1498Szrj   trie_order_statistics_node_update<Node_CItr, Node_Itr, \
246*38fd1498Szrj 				    _ATraits, _Alloc>
247*38fd1498Szrj 
248*38fd1498Szrj   /// Functor updating ranks of entrees.
249*38fd1498Szrj   template<typename Node_CItr,
250*38fd1498Szrj 	   typename Node_Itr,
251*38fd1498Szrj 	   typename _ATraits,
252*38fd1498Szrj 	   typename _Alloc>
253*38fd1498Szrj   class trie_order_statistics_node_update : private PB_DS_TRIE_POLICY_BASE
254*38fd1498Szrj   {
255*38fd1498Szrj   private:
256*38fd1498Szrj     typedef PB_DS_TRIE_POLICY_BASE 		       	base_type;
257*38fd1498Szrj 
258*38fd1498Szrj   public:
259*38fd1498Szrj     typedef _ATraits 				access_traits;
260*38fd1498Szrj     typedef typename access_traits::const_iterator 	a_const_iterator;
261*38fd1498Szrj     typedef _Alloc 					allocator_type;
262*38fd1498Szrj     typedef typename allocator_type::size_type 		size_type;
263*38fd1498Szrj     typedef typename base_type::key_type 		key_type;
264*38fd1498Szrj     typedef typename base_type::key_const_reference 	key_const_reference;
265*38fd1498Szrj 
266*38fd1498Szrj     typedef size_type 					metadata_type;
267*38fd1498Szrj     typedef Node_CItr 					node_const_iterator;
268*38fd1498Szrj     typedef Node_Itr 					node_iterator;
269*38fd1498Szrj     typedef typename node_const_iterator::value_type 	const_iterator;
270*38fd1498Szrj     typedef typename node_iterator::value_type 		iterator;
271*38fd1498Szrj 
272*38fd1498Szrj     /// Finds an entry by __order. Returns a const_iterator to the
273*38fd1498Szrj     /// entry with the __order order, or a const_iterator to the
274*38fd1498Szrj     /// container object's end if order is at least the size of the
275*38fd1498Szrj     /// container object.
276*38fd1498Szrj     inline const_iterator
277*38fd1498Szrj     find_by_order(size_type) const;
278*38fd1498Szrj 
279*38fd1498Szrj     /// Finds an entry by __order. Returns an iterator to the entry
280*38fd1498Szrj     /// with the __order order, or an iterator to the container
281*38fd1498Szrj     /// object's end if order is at least the size of the container
282*38fd1498Szrj     /// object.
283*38fd1498Szrj     inline iterator
284*38fd1498Szrj     find_by_order(size_type);
285*38fd1498Szrj 
286*38fd1498Szrj     /// Returns the order of a key within a sequence. For exapmle, if
287*38fd1498Szrj     /// r_key is the smallest key, this method will return 0; if r_key
288*38fd1498Szrj     /// is a key between the smallest and next key, this method will
289*38fd1498Szrj     /// return 1; if r_key is a key larger than the largest key, this
290*38fd1498Szrj     /// method will return the size of r_c.
291*38fd1498Szrj     inline size_type
292*38fd1498Szrj     order_of_key(key_const_reference) const;
293*38fd1498Szrj 
294*38fd1498Szrj     /// Returns the order of a prefix within a sequence. For exapmle,
295*38fd1498Szrj     /// if [b, e] is the smallest prefix, this method will return 0; if
296*38fd1498Szrj     /// r_key is a key between the smallest and next key, this method
297*38fd1498Szrj     /// will return 1; if r_key is a key larger than the largest key,
298*38fd1498Szrj     /// this method will return the size of r_c.
299*38fd1498Szrj     inline size_type
300*38fd1498Szrj     order_of_prefix(a_const_iterator, a_const_iterator) const;
301*38fd1498Szrj 
302*38fd1498Szrj   protected:
303*38fd1498Szrj     /// Updates the rank of a node through a node_iterator node_it;
304*38fd1498Szrj     /// end_nd_it is the end node iterator.
305*38fd1498Szrj     inline void
306*38fd1498Szrj     operator()(node_iterator, node_const_iterator) const;
307*38fd1498Szrj 
308*38fd1498Szrj   private:
309*38fd1498Szrj     typedef typename base_type::const_reference 	const_reference;
310*38fd1498Szrj     typedef typename base_type::const_pointer 		const_pointer;
311*38fd1498Szrj 
312*38fd1498Szrj     typedef typename _Alloc::template rebind<metadata_type> __rebind_m;
313*38fd1498Szrj     typedef typename __rebind_m::other 			__rebind_ma;
314*38fd1498Szrj     typedef typename __rebind_ma::const_reference      metadata_const_reference;
315*38fd1498Szrj     typedef typename __rebind_ma::reference 		metadata_reference;
316*38fd1498Szrj 
317*38fd1498Szrj     /// Returns true if the container is empty.
318*38fd1498Szrj     virtual bool
319*38fd1498Szrj     empty() const = 0;
320*38fd1498Szrj 
321*38fd1498Szrj     /// Returns the iterator associated with the trie's first element.
322*38fd1498Szrj     virtual iterator
323*38fd1498Szrj     begin() = 0;
324*38fd1498Szrj 
325*38fd1498Szrj     /// Returns the iterator associated with the trie's
326*38fd1498Szrj     /// just-after-last element.
327*38fd1498Szrj     virtual iterator
328*38fd1498Szrj     end() = 0;
329*38fd1498Szrj 
330*38fd1498Szrj     /// Returns the node_const_iterator associated with the trie's root node.
331*38fd1498Szrj     virtual node_const_iterator
332*38fd1498Szrj     node_begin() const = 0;
333*38fd1498Szrj 
334*38fd1498Szrj     /// Returns the node_iterator associated with the trie's root node.
335*38fd1498Szrj     virtual node_iterator
336*38fd1498Szrj     node_begin() = 0;
337*38fd1498Szrj 
338*38fd1498Szrj     /// Returns the node_const_iterator associated with a just-after
339*38fd1498Szrj     /// leaf node.
340*38fd1498Szrj     virtual node_const_iterator
341*38fd1498Szrj     node_end() const = 0;
342*38fd1498Szrj 
343*38fd1498Szrj     /// Returns the node_iterator associated with a just-after leaf node.
344*38fd1498Szrj     virtual node_iterator
345*38fd1498Szrj     node_end() = 0;
346*38fd1498Szrj 
347*38fd1498Szrj     /// Access to the cmp_fn object.
348*38fd1498Szrj     virtual access_traits&
349*38fd1498Szrj     get_access_traits() = 0;
350*38fd1498Szrj   };
351*38fd1498Szrj 
352*38fd1498Szrj #include <ext/pb_ds/detail/trie_policy/order_statistics_imp.hpp>
353*38fd1498Szrj 
354*38fd1498Szrj #undef PB_DS_CLASS_T_DEC
355*38fd1498Szrj #undef PB_DS_CLASS_C_DEC
356*38fd1498Szrj #undef PB_DS_TRIE_POLICY_BASE
357*38fd1498Szrj 
358*38fd1498Szrj } // namespace __gnu_pbds
359*38fd1498Szrj 
360*38fd1498Szrj #endif
361