1 // -*- C++ -*-
2 
3 // Copyright (C) 2005-2022 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 bin_search_tree_/insert_fn_imps.hpp
38  * Contains an implementation class for bin_search_tree_.
39  */
40 
41 #ifdef PB_DS_CLASS_C_DEC
42 
43 PB_DS_CLASS_T_DEC
44 inline std::pair<typename PB_DS_CLASS_C_DEC::point_iterator, bool>
45 PB_DS_CLASS_C_DEC::
insert_leaf(const_reference r_value)46 insert_leaf(const_reference r_value)
47 {
48   PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
49 
50   if (m_size == 0)
51     return std::make_pair(insert_imp_empty(r_value),
52                                 true);
53 
54   node_pointer p_nd = m_p_head->m_p_parent;
55   node_pointer p_pot = m_p_head;
56 
57   while (p_nd != 0)
58     if (!Cmp_Fn::operator()(PB_DS_V2F(p_nd->m_value),
59                                   PB_DS_V2F(r_value)))
60       {
61           p_pot = p_nd;
62 
63           p_nd = p_nd->m_p_left;
64       }
65     else
66       p_nd = p_nd->m_p_right;
67 
68   if (p_pot == m_p_head)
69     return std::make_pair(insert_leaf_new(r_value, m_p_head->m_p_right, false),
70                                 true);
71 
72   if (!Cmp_Fn::operator()(PB_DS_V2F(r_value),
73                                 PB_DS_V2F(p_pot->m_value)))
74     {
75       PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
76       PB_DS_CHECK_KEY_EXISTS(PB_DS_V2F(r_value))
77       return std::make_pair(p_pot, false);
78     }
79 
80   PB_DS_CHECK_KEY_DOES_NOT_EXIST(PB_DS_V2F(r_value))
81 
82   p_nd = p_pot->m_p_left;
83   if (p_nd == 0)
84     return std::make_pair(insert_leaf_new(r_value, p_pot, true),
85                                 true);
86 
87   while (p_nd->m_p_right != 0)
88     p_nd = p_nd->m_p_right;
89 
90   return std::make_pair(insert_leaf_new(r_value, p_nd, false),
91                               true);
92 }
93 
94 PB_DS_CLASS_T_DEC
95 inline typename PB_DS_CLASS_C_DEC::iterator
96 PB_DS_CLASS_C_DEC::
insert_leaf_new(const_reference r_value,node_pointer p_nd,bool left_nd)97 insert_leaf_new(const_reference r_value, node_pointer p_nd, bool left_nd)
98 {
99   node_pointer p_new_nd =
100     get_new_node_for_leaf_insert(r_value,
101                                          traits_base::m_no_throw_copies_indicator);
102 
103   if (left_nd)
104     {
105       _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_left == 0);
106       _GLIBCXX_DEBUG_ASSERT(Cmp_Fn::operator()(PB_DS_V2F(r_value),
107                                                          PB_DS_V2F(p_nd->m_value)));
108 
109       p_nd->m_p_left = p_new_nd;
110       if (m_p_head->m_p_left == p_nd)
111           m_p_head->m_p_left = p_new_nd;
112     }
113   else
114     {
115       _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_right == 0);
116       _GLIBCXX_DEBUG_ASSERT(Cmp_Fn::operator()(PB_DS_V2F(p_nd->m_value),
117                                                          PB_DS_V2F(r_value)));
118 
119       p_nd->m_p_right = p_new_nd;
120       if (m_p_head->m_p_right == p_nd)
121           m_p_head->m_p_right = p_new_nd;
122     }
123 
124   p_new_nd->m_p_parent = p_nd;
125   p_new_nd->m_p_left = p_new_nd->m_p_right = 0;
126   PB_DS_ASSERT_NODE_CONSISTENT(p_nd)
127 
128   update_to_top(p_new_nd, (node_update* )this);
129   _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(PB_DS_V2F(r_value));)
130   return iterator(p_new_nd);
131 }
132 
133 PB_DS_CLASS_T_DEC
134 inline typename PB_DS_CLASS_C_DEC::iterator
135 PB_DS_CLASS_C_DEC::
insert_imp_empty(const_reference r_value)136 insert_imp_empty(const_reference r_value)
137 {
138   node_pointer p_new_node =
139     get_new_node_for_leaf_insert(r_value, traits_base::m_no_throw_copies_indicator);
140 
141   m_p_head->m_p_left = m_p_head->m_p_right =
142     m_p_head->m_p_parent = p_new_node;
143 
144   p_new_node->m_p_parent = m_p_head;
145   p_new_node->m_p_left = p_new_node->m_p_right = 0;
146   _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(PB_DS_V2F(r_value));)
147 
148   update_to_top(m_p_head->m_p_parent, (node_update*)this);
149   return iterator(p_new_node);
150 }
151 
152 PB_DS_CLASS_T_DEC
153 inline typename PB_DS_CLASS_C_DEC::node_pointer
154 PB_DS_CLASS_C_DEC::
get_new_node_for_leaf_insert(const_reference r_val,false_type)155 get_new_node_for_leaf_insert(const_reference r_val, false_type)
156 {
157   node_pointer p_new_nd = s_node_allocator.allocate(1);
158   cond_dealtor_t cond(p_new_nd);
159 
160   new (const_cast<void* >(static_cast<const void* >(&p_new_nd->m_value)))
161     typename node::value_type(r_val);
162 
163   cond.set_no_action();
164   p_new_nd->m_p_left = p_new_nd->m_p_right = 0;
165   ++m_size;
166   return p_new_nd;
167 }
168 
169 PB_DS_CLASS_T_DEC
170 inline typename PB_DS_CLASS_C_DEC::node_pointer
171 PB_DS_CLASS_C_DEC::
get_new_node_for_leaf_insert(const_reference r_val,true_type)172 get_new_node_for_leaf_insert(const_reference r_val, true_type)
173 {
174   node_pointer p_new_nd = s_node_allocator.allocate(1);
175 
176   new (const_cast<void* >(static_cast<const void* >(&p_new_nd->m_value)))
177     typename node::value_type(r_val);
178 
179   p_new_nd->m_p_left = p_new_nd->m_p_right = 0;
180   ++m_size;
181   return p_new_nd;
182 }
183 #endif
184