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