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::head_allocator
49PB_DS_CLASS_C_DEC::s_head_allocator;
50
51PB_DS_CLASS_T_DEC
52typename PB_DS_CLASS_C_DEC::internal_node_allocator
53PB_DS_CLASS_C_DEC::s_internal_node_allocator;
54
55PB_DS_CLASS_T_DEC
56typename PB_DS_CLASS_C_DEC::leaf_allocator
57PB_DS_CLASS_C_DEC::s_leaf_allocator;
58
59PB_DS_CLASS_T_DEC
60PB_DS_CLASS_C_DEC::
61PB_DS_CLASS_NAME() :
62  m_p_head(s_head_allocator.allocate(1)),
63  m_size(0)
64{
65  initialize();
66  _GLIBCXX_DEBUG_ONLY(assert_valid();)
67}
68
69PB_DS_CLASS_T_DEC
70PB_DS_CLASS_C_DEC::
71PB_DS_CLASS_NAME(const e_access_traits& r_e_access_traits) :
72  synth_e_access_traits(r_e_access_traits),
73  m_p_head(s_head_allocator.allocate(1)),
74  m_size(0)
75{
76  initialize();
77  _GLIBCXX_DEBUG_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  synth_e_access_traits(other),
87  node_update(other),
88  m_p_head(s_head_allocator.allocate(1)),
89  m_size(0)
90{
91  initialize();
92  m_size = other.m_size;
93  _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
94    if (other.m_p_head->m_p_parent == NULL)
95      {
96        _GLIBCXX_DEBUG_ONLY(assert_valid();)
97        return;
98      }
99  try
100    {
101      m_p_head->m_p_parent = recursive_copy_node(other.m_p_head->m_p_parent);
102    }
103  catch(...)
104    {
105      s_head_allocator.deallocate(m_p_head, 1);
106      __throw_exception_again;
107    }
108
109  m_p_head->m_p_min = leftmost_descendant(m_p_head->m_p_parent);
110  m_p_head->m_p_max = rightmost_descendant(m_p_head->m_p_parent);
111  m_p_head->m_p_parent->m_p_parent = m_p_head;
112  _GLIBCXX_DEBUG_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(assert_valid();)
121  _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
122  value_swap(other);
123  std::swap((e_access_traits& )(*this), (e_access_traits& )other);
124  _GLIBCXX_DEBUG_ONLY(assert_valid();)
125  _GLIBCXX_DEBUG_ONLY(other.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_head_allocator.deallocate(m_p_head, 1);
144}
145
146PB_DS_CLASS_T_DEC
147void
148PB_DS_CLASS_C_DEC::
149initialize()
150{
151  new (m_p_head) head();
152  m_p_head->m_p_parent = NULL;
153  m_p_head->m_p_min = m_p_head;
154  m_p_head->m_p_max = m_p_head;
155  m_size = 0;
156}
157
158PB_DS_CLASS_T_DEC
159template<typename It>
160void
161PB_DS_CLASS_C_DEC::
162copy_from_range(It first_it, It last_it)
163{
164  while (first_it != last_it)
165    insert(*(first_it++));
166}
167
168PB_DS_CLASS_T_DEC
169typename PB_DS_CLASS_C_DEC::node_pointer
170PB_DS_CLASS_C_DEC::
171recursive_copy_node(const_node_pointer p_other_nd)
172{
173  _GLIBCXX_DEBUG_ASSERT(p_other_nd != NULL);
174  if (p_other_nd->m_type == pat_trie_leaf_node_type)
175    {
176      const_leaf_pointer p_other_leaf = static_cast<const_leaf_pointer>(p_other_nd);
177
178      leaf_pointer p_new_lf = s_leaf_allocator.allocate(1);
179      cond_dealtor cond(p_new_lf);
180      new (p_new_lf) leaf(p_other_leaf->value());
181      apply_update(p_new_lf, (node_update* )this);
182      cond.set_no_action_dtor();
183      return (p_new_lf);
184    }
185
186  _GLIBCXX_DEBUG_ASSERT(p_other_nd->m_type == pat_trie_internal_node_type);
187  node_pointer a_p_children[internal_node::arr_size];
188  size_type child_i = 0;
189  const_internal_node_pointer p_other_internal_nd =
190    static_cast<const_internal_node_pointer>(p_other_nd);
191
192  typename internal_node::const_iterator child_it =
193    p_other_internal_nd->begin();
194
195  internal_node_pointer p_ret;
196  try
197    {
198      while (child_it != p_other_internal_nd->end())
199	a_p_children[child_i++] = recursive_copy_node(*(child_it++));
200      p_ret = s_internal_node_allocator.allocate(1);
201    }
202  catch(...)
203    {
204      while (child_i-- > 0)
205	clear_imp(a_p_children[child_i]);
206      __throw_exception_again;
207    }
208
209  new (p_ret) internal_node(p_other_internal_nd->get_e_ind(),
210			    pref_begin(a_p_children[0]));
211
212  --child_i;
213  _GLIBCXX_DEBUG_ASSERT(child_i > 1);
214  do
215    p_ret->add_child(a_p_children[child_i], pref_begin(a_p_children[child_i]),
216		     pref_end(a_p_children[child_i]), this);
217  while (child_i-- > 0);
218  apply_update(p_ret, (node_update* )this);
219  return p_ret;
220}
221