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