Import gcc-4.4.1
[dragonfly.git] / contrib / gcc-4.4 / libstdc++-v3 / include / ext / pb_ds / detail / bin_search_tree_ / insert_fn_imps.hpp
1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006, 2009 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 insert_fn_imps.hpp
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 {
46   _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
47
48     if (m_size == 0)
49       return (std::make_pair(
50                              insert_imp_empty(r_value),
51                              true));
52
53   node_pointer p_nd = m_p_head->m_p_parent;
54   node_pointer p_pot = m_p_head;
55
56   while (p_nd != NULL)
57     if (!Cmp_Fn::operator()(
58                             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(
70                            insert_leaf_new(r_value,  m_p_head->m_p_right, false),
71                            true));
72
73   if (!Cmp_Fn::operator()(
74                           PB_DS_V2F(r_value),
75                           PB_DS_V2F(p_pot->m_value)))
76     {
77       _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
78
79         _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(
80                                                         PB_DS_V2F(r_value)));
81
82       return (std::make_pair(p_pot, false));
83     }
84
85   _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(
86                                                           PB_DS_V2F(r_value)));
87
88   p_nd = p_pot->m_p_left;
89   if (p_nd == NULL)
90     return (std::make_pair(
91                            insert_leaf_new(r_value, p_pot, true),
92                            true));
93
94   while (p_nd->m_p_right != NULL)
95     p_nd = p_nd->m_p_right;
96
97   return (std::make_pair(
98                          insert_leaf_new(r_value, p_nd, false),
99                          true));
100 }
101
102 PB_DS_CLASS_T_DEC
103 inline typename PB_DS_CLASS_C_DEC::iterator
104 PB_DS_CLASS_C_DEC::
105 insert_leaf_new(const_reference r_value, node_pointer p_nd, bool left_nd)
106 {
107   node_pointer p_new_nd =
108     get_new_node_for_leaf_insert(            r_value, traits_base::m_no_throw_copies_indicator);
109
110   if (left_nd)
111     {
112       _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_left == NULL);
113       _GLIBCXX_DEBUG_ASSERT(Cmp_Fn::operator()(
114                                           PB_DS_V2F(r_value),
115                                           PB_DS_V2F(p_nd->m_value)));
116
117       p_nd->m_p_left = p_new_nd;
118
119       if (m_p_head->m_p_left == p_nd)
120         m_p_head->m_p_left = p_new_nd;
121     }
122   else
123     {
124       _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_right == NULL);
125       _GLIBCXX_DEBUG_ASSERT(Cmp_Fn::operator()(
126                                           PB_DS_V2F(p_nd->m_value),
127                                           PB_DS_V2F(r_value)));
128
129       p_nd->m_p_right = p_new_nd;
130
131       if (m_p_head->m_p_right == p_nd)
132         m_p_head->m_p_right = p_new_nd;
133     }
134
135   p_new_nd->m_p_parent = p_nd;
136
137   p_new_nd->m_p_left = p_new_nd->m_p_right = NULL;
138
139   _GLIBCXX_DEBUG_ONLY(assert_node_consistent(p_nd));
140
141   update_to_top(p_new_nd, (node_update* )this);
142
143   _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(
144                                             PB_DS_V2F(r_value)));
145
146   return (iterator(p_new_nd));
147 }
148
149 PB_DS_CLASS_T_DEC
150 inline typename PB_DS_CLASS_C_DEC::iterator
151 PB_DS_CLASS_C_DEC::
152 insert_imp_empty(const_reference r_value)
153 {
154   node_pointer p_new_node =
155     get_new_node_for_leaf_insert(        r_value, traits_base::m_no_throw_copies_indicator);
156
157   m_p_head->m_p_left = m_p_head->m_p_right =
158     m_p_head->m_p_parent = p_new_node;
159
160   p_new_node->m_p_parent = m_p_head;
161
162   p_new_node->m_p_left = p_new_node->m_p_right = NULL;
163
164   _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(
165                                             PB_DS_V2F(r_value)));
166
167   update_to_top(m_p_head->m_p_parent, (node_update* )this);
168
169   return (iterator(p_new_node));
170 }
171
172 PB_DS_CLASS_T_DEC
173 inline typename PB_DS_CLASS_C_DEC::node_pointer
174 PB_DS_CLASS_C_DEC::
175 get_new_node_for_leaf_insert(const_reference r_val, false_type)
176 {
177   node_pointer p_new_nd = s_node_allocator.allocate(1);
178
179   cond_dealtor_t cond(p_new_nd);
180
181   new (const_cast<void* >(
182                           static_cast<const void* >(&p_new_nd->m_value)))
183     typename node::value_type(r_val);
184
185   cond.set_no_action();
186
187   p_new_nd->m_p_left = p_new_nd->m_p_right = NULL;
188
189   ++m_size;
190
191   return (p_new_nd);
192 }
193
194 PB_DS_CLASS_T_DEC
195 inline typename PB_DS_CLASS_C_DEC::node_pointer
196 PB_DS_CLASS_C_DEC::
197 get_new_node_for_leaf_insert(const_reference r_val, true_type)
198 {
199   node_pointer p_new_nd = s_node_allocator.allocate(1);
200
201   new (const_cast<void* >(
202                           static_cast<const void* >(&p_new_nd->m_value)))
203     typename node::value_type(r_val);
204
205   p_new_nd->m_p_left = p_new_nd->m_p_right = NULL;
206
207   ++m_size;
208
209   return (p_new_nd);
210 }
211