1 // -*- C++ -*-
2 
3 // Copyright (C) 2005-2020 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 tree_policy/order_statistics_imp.hpp
38  * Contains forward declarations for order_statistics_key
39  */
40 
41 #ifdef PB_DS_CLASS_C_DEC
42 
43 PB_DS_CLASS_T_DEC
44 inline typename PB_DS_CLASS_C_DEC::iterator
45 PB_DS_CLASS_C_DEC::
find_by_order(size_type order)46 find_by_order(size_type order)
47 {
48   node_iterator it = node_begin();
49   node_iterator end_it = node_end();
50 
51   while (it != end_it)
52     {
53       node_iterator l_it = it.get_l_child();
54       const size_type o = (l_it == end_it)? 0 : l_it.get_metadata();
55 
56       if (order == o)
57 	return *it;
58       else if (order < o)
59 	it = l_it;
60       else
61         {
62 	  order -= o + 1;
63 	  it = it.get_r_child();
64         }
65     }
66 
67   return base_type::end_iterator();
68 }
69 
70 PB_DS_CLASS_T_DEC
71 inline typename PB_DS_CLASS_C_DEC::const_iterator
72 PB_DS_CLASS_C_DEC::
find_by_order(size_type order) const73 find_by_order(size_type order) const
74 { return const_cast<PB_DS_CLASS_C_DEC*>(this)->find_by_order(order); }
75 
76 PB_DS_CLASS_T_DEC
77 inline typename PB_DS_CLASS_C_DEC::size_type
78 PB_DS_CLASS_C_DEC::
order_of_key(key_const_reference r_key) const79 order_of_key(key_const_reference r_key) const
80 {
81   node_const_iterator it = node_begin();
82   node_const_iterator end_it = node_end();
83 
84   const cmp_fn& r_cmp_fn = const_cast<PB_DS_CLASS_C_DEC*>(this)->get_cmp_fn();
85   size_type ord = 0;
86   while (it != end_it)
87     {
88       node_const_iterator l_it = it.get_l_child();
89 
90       if (r_cmp_fn(r_key, this->extract_key(*(*it))))
91 	it = l_it;
92       else if (r_cmp_fn(this->extract_key(*(*it)), r_key))
93         {
94 	  ord += (l_it == end_it)? 1 : 1 + l_it.get_metadata();
95 	  it = it.get_r_child();
96         }
97       else
98         {
99 	  ord += (l_it == end_it)? 0 : l_it.get_metadata();
100 	  it = end_it;
101         }
102     }
103   return ord;
104 }
105 
106 PB_DS_CLASS_T_DEC
107 inline void
108 PB_DS_CLASS_C_DEC::
operator ()(node_iterator node_it,node_const_iterator end_nd_it) const109 operator()(node_iterator node_it, node_const_iterator end_nd_it) const
110 {
111   node_iterator l_it = node_it.get_l_child();
112   const size_type l_rank = (l_it == end_nd_it) ? 0 : l_it.get_metadata();
113 
114   node_iterator r_it = node_it.get_r_child();
115   const size_type r_rank = (r_it == end_nd_it) ? 0 : r_it.get_metadata();
116 
117   const_cast<metadata_reference>(node_it.get_metadata())= 1 + l_rank + r_rank;
118 }
119 
120 PB_DS_CLASS_T_DEC
121 PB_DS_CLASS_C_DEC::
~tree_order_statistics_node_update()122 ~tree_order_statistics_node_update()
123 { }
124 #endif
125