]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/pb_ds/detail/trie_policy/order_statistics_imp.hpp
Daily bump.
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / trie_policy / order_statistics_imp.hpp
CommitLineData
4569a895
AT
1// -*- C++ -*-
2
a945c346 3// Copyright (C) 2005-2024 Free Software Foundation, Inc.
4569a895
AT
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
748086b7 8// Foundation; either version 3, or (at your option) any later
4569a895
AT
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
748086b7
JJ
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/>.
4569a895
AT
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/**
a345e45d 37 * @file trie_policy/order_statistics_imp.hpp
4569a895
AT
38 * Contains forward declarations for order_statistics_key
39 */
40
574dfb67
JW
41#ifdef PB_DS_CLASS_C_DEC
42
4569a895
AT
43PB_DS_CLASS_T_DEC
44inline typename PB_DS_CLASS_C_DEC::iterator
45PB_DS_CLASS_C_DEC::
46find_by_order(size_type order)
47{
48 if (empty())
a345e45d 49 return end();
4569a895
AT
50
51 ++order;
4569a895
AT
52 node_iterator nd_it = node_begin();
53
4569a895
AT
54 while (true)
55 {
56 if (order > nd_it.get_metadata())
a345e45d 57 return ++base_type::rightmost_it(nd_it);
4569a895
AT
58
59 const size_type num_children = nd_it.num_children();
4569a895 60 if (num_children == 0)
a345e45d 61 return *nd_it;
4569a895
AT
62
63 for (size_type i = 0; i < num_children; ++i)
a345e45d 64 {
4569a895 65 node_iterator child_nd_it = nd_it.get_child(i);
4569a895 66 if (order <= child_nd_it.get_metadata())
a345e45d 67 {
4569a895 68 i = num_children;
4569a895 69 nd_it = child_nd_it;
a345e45d 70 }
4569a895
AT
71 else
72 order -= child_nd_it.get_metadata();
a345e45d 73 }
4569a895
AT
74 }
75}
76
77PB_DS_CLASS_T_DEC
78inline typename PB_DS_CLASS_C_DEC::const_iterator
79PB_DS_CLASS_C_DEC::
80find_by_order(size_type order) const
a345e45d 81{ return const_cast<PB_DS_CLASS_C_DEC*>(this)->find_by_order(order); }
4569a895
AT
82
83PB_DS_CLASS_T_DEC
84inline typename PB_DS_CLASS_C_DEC::size_type
85PB_DS_CLASS_C_DEC::
a345e45d 86order_of_key(key_const_reference r_key) const
4569a895 87{
a345e45d
BK
88 const _ATraits& r_traits =
89 const_cast<PB_DS_CLASS_C_DEC* >(this)->get_access_traits();
4569a895 90
a345e45d 91 return order_of_prefix(r_traits.begin(r_key), r_traits.end(r_key));
4569a895
AT
92}
93
94PB_DS_CLASS_T_DEC
95inline typename PB_DS_CLASS_C_DEC::size_type
96PB_DS_CLASS_C_DEC::
a345e45d
BK
97order_of_prefix(typename access_traits::const_iterator b,
98 typename access_traits::const_iterator e) const
4569a895
AT
99{
100 if (empty())
a345e45d 101 return 0;
4569a895 102
a345e45d
BK
103 const _ATraits& r_traits =
104 const_cast<PB_DS_CLASS_C_DEC*>(this)->get_access_traits();
4569a895 105
a345e45d
BK
106 node_const_iterator nd_it = node_begin();
107 node_const_iterator end_nd_it = node_end();
4569a895
AT
108 size_type ord = 0;
109
110 while (true)
111 {
112 const size_type num_children = nd_it.num_children();
4569a895 113 if (num_children == 0)
a345e45d
BK
114 {
115 key_const_reference r_key = base_type::extract_key(*(*nd_it));
116 typename access_traits::const_iterator key_b =
4569a895
AT
117 r_traits.begin(r_key);
118
a345e45d 119 typename access_traits::const_iterator key_e =
4569a895
AT
120 r_traits.end(r_key);
121
a345e45d
BK
122 return (base_type::less(key_b, key_e, b, e, r_traits)) ?
123 ord + 1 : ord;
124 }
4569a895 125
a345e45d 126 node_const_iterator next_nd_it = end_nd_it;
4569a895
AT
127 size_type i = num_children - 1;
128
129 do
a345e45d
BK
130 {
131 node_const_iterator child_nd_it = nd_it.get_child(i);
4569a895
AT
132
133 if (next_nd_it != end_nd_it)
134 ord += child_nd_it.get_metadata();
a345e45d 135 else if (!base_type::less(b, e,
4569a895
AT
136 child_nd_it.valid_prefix().first,
137 child_nd_it.valid_prefix().second,
138 r_traits))
139 next_nd_it = child_nd_it;
a345e45d 140 }
4569a895
AT
141 while (i-- > 0);
142
143 if (next_nd_it == end_nd_it)
a345e45d 144 return ord;
4569a895
AT
145
146 nd_it = next_nd_it;
147 }
148}
149
150PB_DS_CLASS_T_DEC
151inline void
152PB_DS_CLASS_C_DEC::
a345e45d 153operator()(node_iterator nd_it, node_const_iterator /*end_nd_it*/) const
4569a895
AT
154{
155 const size_type num_children = nd_it.num_children();
4569a895 156 size_type children_rank = 0;
4569a895
AT
157 for (size_type i = 0; i < num_children; ++i)
158 children_rank += nd_it.get_child(i).get_metadata();
159
a345e45d
BK
160 const size_type res = (num_children == 0) ? 1 : children_rank;
161 const_cast<size_type&>(nd_it.get_metadata()) = res;
4569a895 162}
574dfb67 163#endif