xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/ext/pb_ds/detail/bin_search_tree_/insert_fn_imps.hpp (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1 // -*- C++ -*-
2 
3 // Copyright (C) 2005, 2006 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING.  If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
20 
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction.  Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License.  This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
29 // Public License.
30 
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32 
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
40 // warranty.
41 
42 /**
43  * @file insert_fn_imps.hpp
44  * Contains an implementation class for bin_search_tree_.
45  */
46 
47 PB_DS_CLASS_T_DEC
48 inline std::pair<typename PB_DS_CLASS_C_DEC::point_iterator, bool>
49 PB_DS_CLASS_C_DEC::
insert_leaf(const_reference r_value)50 insert_leaf(const_reference r_value)
51 {
52   _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
53 
54     if (m_size == 0)
55       return (std::make_pair(
56 			     insert_imp_empty(r_value),
57 			     true));
58 
59   node_pointer p_nd = m_p_head->m_p_parent;
60   node_pointer p_pot = m_p_head;
61 
62   while (p_nd != NULL)
63     if (!Cmp_Fn::operator()(
64 			    PB_DS_V2F(p_nd->m_value),
65 			    PB_DS_V2F(r_value)))
66       {
67 	p_pot = p_nd;
68 
69 	p_nd = p_nd->m_p_left;
70       }
71     else
72       p_nd = p_nd->m_p_right;
73 
74   if (p_pot == m_p_head)
75     return (std::make_pair(
76 			   insert_leaf_new(r_value,  m_p_head->m_p_right, false),
77 			   true));
78 
79   if (!Cmp_Fn::operator()(
80 			  PB_DS_V2F(r_value),
81 			  PB_DS_V2F(p_pot->m_value)))
82     {
83       _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
84 
85         _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(
86 							PB_DS_V2F(r_value)));
87 
88       return (std::make_pair(p_pot, false));
89     }
90 
91   _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_does_not_exist(
92 							  PB_DS_V2F(r_value)));
93 
94   p_nd = p_pot->m_p_left;
95   if (p_nd == NULL)
96     return (std::make_pair(
97 			   insert_leaf_new(r_value, p_pot, true),
98 			   true));
99 
100   while (p_nd->m_p_right != NULL)
101     p_nd = p_nd->m_p_right;
102 
103   return (std::make_pair(
104 			 insert_leaf_new(r_value, p_nd, false),
105 			 true));
106 }
107 
108 PB_DS_CLASS_T_DEC
109 inline typename PB_DS_CLASS_C_DEC::iterator
110 PB_DS_CLASS_C_DEC::
insert_leaf_new(const_reference r_value,node_pointer p_nd,bool left_nd)111 insert_leaf_new(const_reference r_value, node_pointer p_nd, bool left_nd)
112 {
113   node_pointer p_new_nd =
114     get_new_node_for_leaf_insert(            r_value, traits_base::m_no_throw_copies_indicator);
115 
116   if (left_nd)
117     {
118       _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_left == NULL);
119       _GLIBCXX_DEBUG_ASSERT(Cmp_Fn::operator()(
120 					  PB_DS_V2F(r_value),
121 					  PB_DS_V2F(p_nd->m_value)));
122 
123       p_nd->m_p_left = p_new_nd;
124 
125       if (m_p_head->m_p_left == p_nd)
126 	m_p_head->m_p_left = p_new_nd;
127     }
128   else
129     {
130       _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_right == NULL);
131       _GLIBCXX_DEBUG_ASSERT(Cmp_Fn::operator()(
132 					  PB_DS_V2F(p_nd->m_value),
133 					  PB_DS_V2F(r_value)));
134 
135       p_nd->m_p_right = p_new_nd;
136 
137       if (m_p_head->m_p_right == p_nd)
138 	m_p_head->m_p_right = p_new_nd;
139     }
140 
141   p_new_nd->m_p_parent = p_nd;
142 
143   p_new_nd->m_p_left = p_new_nd->m_p_right = NULL;
144 
145   _GLIBCXX_DEBUG_ONLY(assert_node_consistent(p_nd));
146 
147   update_to_top(p_new_nd, (node_update* )this);
148 
149   _GLIBCXX_DEBUG_ONLY(map_debug_base::insert_new(
150 					    PB_DS_V2F(r_value)));
151 
152   return (iterator(p_new_nd));
153 }
154 
155 PB_DS_CLASS_T_DEC
156 inline typename PB_DS_CLASS_C_DEC::iterator
157 PB_DS_CLASS_C_DEC::
insert_imp_empty(const_reference r_value)158 insert_imp_empty(const_reference r_value)
159 {
160   node_pointer p_new_node =
161     get_new_node_for_leaf_insert(        r_value, traits_base::m_no_throw_copies_indicator);
162 
163   m_p_head->m_p_left = m_p_head->m_p_right =
164     m_p_head->m_p_parent = p_new_node;
165 
166   p_new_node->m_p_parent = m_p_head;
167 
168   p_new_node->m_p_left = p_new_node->m_p_right = NULL;
169 
170   _GLIBCXX_DEBUG_ONLY(map_debug_base::insert_new(
171 					    PB_DS_V2F(r_value)));
172 
173   update_to_top(m_p_head->m_p_parent, (node_update* )this);
174 
175   return (iterator(p_new_node));
176 }
177 
178 PB_DS_CLASS_T_DEC
179 inline typename PB_DS_CLASS_C_DEC::node_pointer
180 PB_DS_CLASS_C_DEC::
get_new_node_for_leaf_insert(const_reference r_val,false_type)181 get_new_node_for_leaf_insert(const_reference r_val, false_type)
182 {
183   node_pointer p_new_nd = s_node_allocator.allocate(1);
184 
185   cond_dealtor_t cond(p_new_nd);
186 
187   new (const_cast<void* >(
188 			  static_cast<const void* >(&p_new_nd->m_value)))
189     typename node::value_type(r_val);
190 
191   cond.set_no_action();
192 
193   p_new_nd->m_p_left = p_new_nd->m_p_right = NULL;
194 
195   ++m_size;
196 
197   return (p_new_nd);
198 }
199 
200 PB_DS_CLASS_T_DEC
201 inline typename PB_DS_CLASS_C_DEC::node_pointer
202 PB_DS_CLASS_C_DEC::
get_new_node_for_leaf_insert(const_reference r_val,true_type)203 get_new_node_for_leaf_insert(const_reference r_val, true_type)
204 {
205   node_pointer p_new_nd = s_node_allocator.allocate(1);
206 
207   new (const_cast<void* >(
208 			  static_cast<const void* >(&p_new_nd->m_value)))
209     typename node::value_type(r_val);
210 
211   p_new_nd->m_p_left = p_new_nd->m_p_right = NULL;
212 
213   ++m_size;
214 
215   return (p_new_nd);
216 }
217 
218