3 // Copyright (C) 2005, 2006, 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_base.hpp
38 * Contains an implementation of trie_policy_base.
41 #ifndef PB_DS_TRIE_POLICY_BASE_HPP
42 #define PB_DS_TRIE_POLICY_BASE_HPP
44 #include <ext/pb_ds/detail/basic_tree_policy/basic_tree_policy_base.hpp>
51 #define PB_DS_CLASS_T_DEC \
53 class Const_Node_Iterator, \
54 class Node_Iterator, \
55 class E_Access_Traits, \
58 #define PB_DS_CLASS_C_DEC \
60 Const_Node_Iterator, \
65 #define PB_DS_BASE_C_DEC \
66 basic_tree_policy_base< \
67 Const_Node_Iterator, \
71 template<typename Const_Node_Iterator,
73 class E_Access_Traits,
75 class trie_policy_base : public PB_DS_BASE_C_DEC
80 typedef E_Access_Traits e_access_traits;
82 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
86 typedef null_node_metadata metadata_type;
88 typedef Const_Node_Iterator const_node_iterator;
90 typedef Node_Iterator node_iterator;
92 typedef typename const_node_iterator::value_type const_iterator;
94 typedef typename node_iterator::value_type iterator;
98 typedef typename PB_DS_BASE_C_DEC::key_type key_type;
101 typename PB_DS_BASE_C_DEC::const_key_reference
106 virtual const_iterator
112 virtual const_node_iterator
113 node_begin() const = 0;
115 virtual node_iterator
118 virtual const_node_iterator
119 node_end() const = 0;
121 virtual node_iterator
124 virtual const e_access_traits&
125 get_e_access_traits() const = 0;
130 typename e_access_traits::const_iterator,
131 typename e_access_traits::const_iterator>
134 typedef PB_DS_BASE_C_DEC base_type;
138 common_prefix_len(node_iterator nd_it, typename e_access_traits::const_iterator b_r, typename e_access_traits::const_iterator e_r, const e_access_traits& r_traits);
141 leftmost_it(node_iterator nd_it);
144 rightmost_it(node_iterator nd_it);
147 less(typename e_access_traits::const_iterator b_l, typename e_access_traits::const_iterator e_l, typename e_access_traits::const_iterator b_r, typename e_access_traits::const_iterator e_r, const e_access_traits& r_traits);
151 typename PB_DS_CLASS_C_DEC::size_type
153 common_prefix_len(node_iterator nd_it, typename e_access_traits::const_iterator b_r, typename e_access_traits::const_iterator e_r, const e_access_traits& r_traits)
155 prefix_range_t pref_range = nd_it.valid_prefix();
157 typename e_access_traits::const_iterator b_l = pref_range.first;
158 typename e_access_traits::const_iterator e_l = pref_range.second;
160 const size_type range_length_l =
161 std::distance(b_l, e_l);
163 const size_type range_length_r =
164 std::distance(b_r, e_r);
166 if (range_length_r < range_length_l)
177 if (r_traits.e_pos(*b_l) != r_traits.e_pos(*b_r))
191 typename PB_DS_CLASS_C_DEC::iterator
193 leftmost_it(node_iterator nd_it)
195 if (nd_it.num_children() == 0)
198 return (leftmost_it(nd_it.get_child(0)));
202 typename PB_DS_CLASS_C_DEC::iterator
204 rightmost_it(node_iterator nd_it)
206 const size_type num_children = nd_it.num_children();
208 if (num_children == 0)
211 return (rightmost_it(nd_it.get_child(num_children - 1)));
217 less(typename e_access_traits::const_iterator b_l, typename e_access_traits::const_iterator e_l, typename e_access_traits::const_iterator b_r, typename e_access_traits::const_iterator e_r, const e_access_traits& r_traits)
225 r_traits.e_pos(*b_l);
227 r_traits.e_pos(*b_r);
230 return (l_pos < r_pos);
239 #undef PB_DS_CLASS_T_DEC
241 #undef PB_DS_CLASS_C_DEC
243 #undef PB_DS_BASE_C_DEC
245 } // namespace detail
246 } // namespace __gnu_pbds
248 #endif // #ifndef PB_DS_TRIE_POLICY_BASE_HPP