ff2799e3e95402f6c2f8395cc6745fd052fd76fc
[gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / trie_policy / prefix_search_node_update_imp.hpp
1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006, 2009 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 3, or (at your option) any later
9 // version.
10
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.
15
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.
19
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/>.
24
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
26
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
34 // warranty.
35
36 /**
37 * @file prefix_search_node_update_imp.hpp
38 * Contains an implementation of prefix_search_node_update.
39 */
40
41 PB_DS_CLASS_T_DEC
42 std::pair<
43 typename PB_DS_CLASS_C_DEC::const_iterator,
44 typename PB_DS_CLASS_C_DEC::const_iterator>
45 PB_DS_CLASS_C_DEC::
46 prefix_range(const_key_reference r_key) const
47 {
48 const e_access_traits& r_traits = get_e_access_traits();
49
50 return (prefix_range(
51 r_traits.begin(r_key),
52 r_traits.end(r_key)));
53 }
54
55 PB_DS_CLASS_T_DEC
56 std::pair<
57 typename PB_DS_CLASS_C_DEC::iterator,
58 typename PB_DS_CLASS_C_DEC::iterator>
59 PB_DS_CLASS_C_DEC::
60 prefix_range(const_key_reference r_key)
61 {
62 return (prefix_range(
63 get_e_access_traits().begin(r_key),
64 get_e_access_traits().end(r_key)));
65 }
66
67 PB_DS_CLASS_T_DEC
68 std::pair<
69 typename PB_DS_CLASS_C_DEC::const_iterator,
70 typename PB_DS_CLASS_C_DEC::const_iterator>
71 PB_DS_CLASS_C_DEC::
72 prefix_range(typename e_access_traits::const_iterator b, typename e_access_traits::const_iterator e) const
73 {
74 const std::pair<iterator, iterator> non_const_ret =
75 const_cast<PB_DS_CLASS_C_DEC* >(this)->prefix_range(b, e);
76
77 return (std::make_pair(
78 const_iterator(non_const_ret.first),
79 const_iterator(non_const_ret.second)));
80 }
81
82 PB_DS_CLASS_T_DEC
83 std::pair<
84 typename PB_DS_CLASS_C_DEC::iterator,
85 typename PB_DS_CLASS_C_DEC::iterator>
86 PB_DS_CLASS_C_DEC::
87 prefix_range(typename e_access_traits::const_iterator b, typename e_access_traits::const_iterator e)
88 {
89 Node_Iterator nd_it = node_begin();
90 Node_Iterator end_nd_it = node_end();
91
92 const e_access_traits& r_traits =
93 get_e_access_traits();
94
95 const size_type given_range_length = std::distance(b, e);
96
97 while (true)
98 {
99 if (nd_it == end_nd_it)
100 return (std::make_pair(end(), end()));
101
102 const size_type common_range_length =
103 PB_DS_BASE_C_DEC::common_prefix_len(nd_it, b, e, r_traits);
104
105 if (common_range_length >= given_range_length)
106 {
107 iterator ret_b = this->leftmost_it(nd_it);
108
109 iterator ret_e = this->rightmost_it(nd_it);
110
111 return (std::make_pair(ret_b, ++ret_e));
112 }
113
114 nd_it = next_child(nd_it, b, e, end_nd_it, r_traits);
115 }
116 }
117
118 PB_DS_CLASS_T_DEC
119 typename PB_DS_CLASS_C_DEC::node_iterator
120 PB_DS_CLASS_C_DEC::
121 next_child(node_iterator nd_it, typename e_access_traits::const_iterator b, typename e_access_traits::const_iterator e, node_iterator end_nd_it, const e_access_traits& r_traits)
122 {
123 const size_type num_children = nd_it.num_children();
124
125 node_iterator ret = end_nd_it;
126
127 size_type max_length = 0;
128
129 for (size_type i = 0; i < num_children; ++i)
130 {
131 node_iterator pot = nd_it.get_child(i);
132
133 const size_type common_range_length =
134 PB_DS_BASE_C_DEC::common_prefix_len( pot, b, e, r_traits);
135
136 if (common_range_length > max_length)
137 {
138 ret = pot;
139
140 max_length = common_range_length;
141 }
142 }
143
144 return (ret);
145 }
146
147 PB_DS_CLASS_T_DEC
148 inline void
149 PB_DS_CLASS_C_DEC::
150 operator()(node_iterator /*nd_it*/, const_node_iterator /*end_nd_it*/) const
151 { }