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 constructors_destructor_fn_imps.hpp
44 * Contains an implementation class for bin_search_tree_.
45 */
46
47PB_DS_CLASS_T_DEC
48typename PB_DS_CLASS_C_DEC::node_allocator
49PB_DS_CLASS_C_DEC::s_node_allocator;
50
51PB_DS_CLASS_T_DEC
52PB_DS_CLASS_C_DEC::
53PB_DS_CLASS_NAME() : m_p_head(s_node_allocator.allocate(1)), m_size(0)
54{
55  initialize();
56  _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
57}
58
59PB_DS_CLASS_T_DEC
60PB_DS_CLASS_C_DEC::
61PB_DS_CLASS_NAME(const Cmp_Fn& r_cmp_fn) :
62  Cmp_Fn(r_cmp_fn), m_p_head(s_node_allocator.allocate(1)), m_size(0)
63{
64  initialize();
65  _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
66}
67
68PB_DS_CLASS_T_DEC
69PB_DS_CLASS_C_DEC::
70PB_DS_CLASS_NAME(const Cmp_Fn& r_cmp_fn, const node_update& r_node_update) :
71  Cmp_Fn(r_cmp_fn),
72  node_update(r_node_update),
73  m_p_head(s_node_allocator.allocate(1)),
74  m_size(0)
75{
76  initialize();
77  _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
78}
79
80PB_DS_CLASS_T_DEC
81PB_DS_CLASS_C_DEC::
82PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other) :
83#ifdef _GLIBCXX_DEBUG
84  map_debug_base(other),
85#endif
86#ifdef PB_DS_TREE_TRACE
87  PB_DS_TREE_TRACE_BASE_C_DEC(other),
88#endif
89  Cmp_Fn(other),
90  node_update(other),
91  m_p_head(s_node_allocator.allocate(1)),
92  m_size(0)
93{
94  initialize();
95  m_size = other.m_size;
96  _GLIBCXX_DEBUG_ONLY(other.structure_only_assert_valid();)
97
98    try
99      {
100        m_p_head->m_p_parent = recursive_copy_node(other.m_p_head->m_p_parent);
101        if (m_p_head->m_p_parent != NULL)
102	  m_p_head->m_p_parent->m_p_parent = m_p_head;
103        m_size = other.m_size;
104        initialize_min_max();
105      }
106    catch(...)
107      {
108        _GLIBCXX_DEBUG_ONLY(map_debug_base::clear();)
109	s_node_allocator.deallocate(m_p_head, 1);
110        __throw_exception_again;
111      }
112  _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
113}
114
115PB_DS_CLASS_T_DEC
116void
117PB_DS_CLASS_C_DEC::
118swap(PB_DS_CLASS_C_DEC& other)
119{
120  _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
121  _GLIBCXX_DEBUG_ONLY(other.structure_only_assert_valid();)
122  value_swap(other);
123  std::swap((Cmp_Fn& )(*this), (Cmp_Fn& )other);
124  _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
125  _GLIBCXX_DEBUG_ONLY(other.structure_only_assert_valid();)
126}
127
128PB_DS_CLASS_T_DEC
129void
130PB_DS_CLASS_C_DEC::
131value_swap(PB_DS_CLASS_C_DEC& other)
132{
133  _GLIBCXX_DEBUG_ONLY(map_debug_base::swap(other);)
134  std::swap(m_p_head, other.m_p_head);
135  std::swap(m_size, other.m_size);
136}
137
138PB_DS_CLASS_T_DEC
139PB_DS_CLASS_C_DEC::
140~PB_DS_CLASS_NAME()
141{
142  clear();
143  s_node_allocator.deallocate(m_p_head, 1);
144}
145
146PB_DS_CLASS_T_DEC
147void
148PB_DS_CLASS_C_DEC::
149initialize()
150{
151  m_p_head->m_p_parent = NULL;
152  m_p_head->m_p_left = m_p_head;
153  m_p_head->m_p_right = m_p_head;
154  m_size = 0;
155}
156
157PB_DS_CLASS_T_DEC
158typename PB_DS_CLASS_C_DEC::node_pointer
159PB_DS_CLASS_C_DEC::
160recursive_copy_node(const node_pointer p_nd)
161{
162  if (p_nd == NULL)
163    return (NULL);
164
165  node_pointer p_ret = s_node_allocator.allocate(1);
166  try
167    {
168      new (p_ret) node(*p_nd);
169    }
170  catch(...)
171    {
172      s_node_allocator.deallocate(p_ret, 1);
173      __throw_exception_again;
174    }
175
176  p_ret->m_p_left = p_ret->m_p_right = NULL;
177
178  try
179    {
180      p_ret->m_p_left = recursive_copy_node(p_nd->m_p_left);
181      p_ret->m_p_right = recursive_copy_node(p_nd->m_p_right);
182    }
183  catch(...)
184    {
185      clear_imp(p_ret);
186      __throw_exception_again;
187    }
188
189  if (p_ret->m_p_left != NULL)
190    p_ret->m_p_left->m_p_parent = p_ret;
191
192  if (p_ret->m_p_right != NULL)
193    p_ret->m_p_right->m_p_parent = p_ret;
194
195  _GLIBCXX_DEBUG_ONLY(assert_node_consistent(p_ret);)
196  return p_ret;
197}
198
199PB_DS_CLASS_T_DEC
200void
201PB_DS_CLASS_C_DEC::
202initialize_min_max()
203{
204  if (m_p_head->m_p_parent == NULL)
205    {
206      m_p_head->m_p_left = m_p_head->m_p_right = m_p_head;
207      return;
208    }
209
210  {
211    node_pointer p_min = m_p_head->m_p_parent;
212    while (p_min->m_p_left != NULL)
213      p_min = p_min->m_p_left;
214    m_p_head->m_p_left = p_min;
215  }
216
217  {
218    node_pointer p_max = m_p_head->m_p_parent;
219    while (p_max->m_p_right != NULL)
220      p_max = p_max->m_p_right;
221    m_p_head->m_p_right = p_max;
222  }
223}
224
225