1169691Skan// -*- C++ -*-
2169691Skan
3169691Skan// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4169691Skan//
5169691Skan// This file is part of the GNU ISO C++ Library.  This library is free
6169691Skan// software; you can redistribute it and/or modify it under the terms
7169691Skan// of the GNU General Public License as published by the Free Software
8169691Skan// Foundation; either version 2, or (at your option) any later
9169691Skan// version.
10169691Skan
11169691Skan// This library is distributed in the hope that it will be useful, but
12169691Skan// WITHOUT ANY WARRANTY; without even the implied warranty of
13169691Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14169691Skan// General Public License for more details.
15169691Skan
16169691Skan// You should have received a copy of the GNU General Public License
17169691Skan// along with this library; see the file COPYING.  If not, write to
18169691Skan// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19169691Skan// MA 02111-1307, USA.
20169691Skan
21169691Skan// As a special exception, you may use this file as part of a free
22169691Skan// software library without restriction.  Specifically, if other files
23169691Skan// instantiate templates or use macros or inline functions from this
24169691Skan// file, or you compile this file and link it with other files to
25169691Skan// produce an executable, this file does not by itself cause the
26169691Skan// resulting executable to be covered by the GNU General Public
27169691Skan// License.  This exception does not however invalidate any other
28169691Skan// reasons why the executable file might be covered by the GNU General
29169691Skan// Public License.
30169691Skan
31169691Skan// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32169691Skan
33169691Skan// Permission to use, copy, modify, sell, and distribute this software
34169691Skan// is hereby granted without fee, provided that the above copyright
35169691Skan// notice appears in all copies, and that both that copyright notice
36169691Skan// and this permission notice appear in supporting documentation. None
37169691Skan// of the above authors, nor IBM Haifa Research Laboratories, make any
38169691Skan// representation about the suitability of this software for any
39169691Skan// purpose. It is provided "as is" without express or implied
40169691Skan// warranty.
41169691Skan
42169691Skan/**
43169691Skan * @file constructors_destructor_fn_imps.hpp
44169691Skan * Contains an implementation class for bin_search_tree_.
45169691Skan */
46169691Skan
47169691SkanPB_DS_CLASS_T_DEC
48169691Skantypename PB_DS_CLASS_C_DEC::head_allocator
49169691SkanPB_DS_CLASS_C_DEC::s_head_allocator;
50169691Skan
51169691SkanPB_DS_CLASS_T_DEC
52169691Skantypename PB_DS_CLASS_C_DEC::internal_node_allocator
53169691SkanPB_DS_CLASS_C_DEC::s_internal_node_allocator;
54169691Skan
55169691SkanPB_DS_CLASS_T_DEC
56169691Skantypename PB_DS_CLASS_C_DEC::leaf_allocator
57169691SkanPB_DS_CLASS_C_DEC::s_leaf_allocator;
58169691Skan
59169691SkanPB_DS_CLASS_T_DEC
60169691SkanPB_DS_CLASS_C_DEC::
61169691SkanPB_DS_CLASS_NAME() :
62169691Skan  m_p_head(s_head_allocator.allocate(1)),
63169691Skan  m_size(0)
64169691Skan{
65169691Skan  initialize();
66169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
67169691Skan}
68169691Skan
69169691SkanPB_DS_CLASS_T_DEC
70169691SkanPB_DS_CLASS_C_DEC::
71169691SkanPB_DS_CLASS_NAME(const e_access_traits& r_e_access_traits) :
72169691Skan  synth_e_access_traits(r_e_access_traits),
73169691Skan  m_p_head(s_head_allocator.allocate(1)),
74169691Skan  m_size(0)
75169691Skan{
76169691Skan  initialize();
77169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
78169691Skan}
79169691Skan
80169691SkanPB_DS_CLASS_T_DEC
81169691SkanPB_DS_CLASS_C_DEC::
82169691SkanPB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other) :
83169691Skan#ifdef _GLIBCXX_DEBUG
84169691Skan  map_debug_base(other),
85169691Skan#endif
86169691Skan  synth_e_access_traits(other),
87169691Skan  node_update(other),
88169691Skan  m_p_head(s_head_allocator.allocate(1)),
89169691Skan  m_size(0)
90169691Skan{
91169691Skan  initialize();
92169691Skan  m_size = other.m_size;
93169691Skan  _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
94169691Skan    if (other.m_p_head->m_p_parent == NULL)
95169691Skan      {
96169691Skan        _GLIBCXX_DEBUG_ONLY(assert_valid();)
97169691Skan        return;
98169691Skan      }
99169691Skan  try
100169691Skan    {
101169691Skan      m_p_head->m_p_parent = recursive_copy_node(other.m_p_head->m_p_parent);
102169691Skan    }
103169691Skan  catch(...)
104169691Skan    {
105169691Skan      s_head_allocator.deallocate(m_p_head, 1);
106169691Skan      __throw_exception_again;
107169691Skan    }
108169691Skan
109169691Skan  m_p_head->m_p_min = leftmost_descendant(m_p_head->m_p_parent);
110169691Skan  m_p_head->m_p_max = rightmost_descendant(m_p_head->m_p_parent);
111169691Skan  m_p_head->m_p_parent->m_p_parent = m_p_head;
112169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
113169691Skan}
114169691Skan
115169691SkanPB_DS_CLASS_T_DEC
116169691Skanvoid
117169691SkanPB_DS_CLASS_C_DEC::
118169691Skanswap(PB_DS_CLASS_C_DEC& other)
119169691Skan{
120169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
121169691Skan  _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
122169691Skan  value_swap(other);
123169691Skan  std::swap((e_access_traits& )(*this), (e_access_traits& )other);
124169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
125169691Skan  _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
126169691Skan}
127169691Skan
128169691SkanPB_DS_CLASS_T_DEC
129169691Skanvoid
130169691SkanPB_DS_CLASS_C_DEC::
131169691Skanvalue_swap(PB_DS_CLASS_C_DEC& other)
132169691Skan{
133169691Skan  _GLIBCXX_DEBUG_ONLY(map_debug_base::swap(other);)
134169691Skan  std::swap(m_p_head, other.m_p_head);
135169691Skan  std::swap(m_size, other.m_size);
136169691Skan}
137169691Skan
138169691SkanPB_DS_CLASS_T_DEC
139169691SkanPB_DS_CLASS_C_DEC::
140169691Skan~PB_DS_CLASS_NAME()
141169691Skan{
142169691Skan  clear();
143169691Skan  s_head_allocator.deallocate(m_p_head, 1);
144169691Skan}
145169691Skan
146169691SkanPB_DS_CLASS_T_DEC
147169691Skanvoid
148169691SkanPB_DS_CLASS_C_DEC::
149169691Skaninitialize()
150169691Skan{
151169691Skan  new (m_p_head) head();
152169691Skan  m_p_head->m_p_parent = NULL;
153169691Skan  m_p_head->m_p_min = m_p_head;
154169691Skan  m_p_head->m_p_max = m_p_head;
155169691Skan  m_size = 0;
156169691Skan}
157169691Skan
158169691SkanPB_DS_CLASS_T_DEC
159169691Skantemplate<typename It>
160169691Skanvoid
161169691SkanPB_DS_CLASS_C_DEC::
162169691Skancopy_from_range(It first_it, It last_it)
163169691Skan{
164169691Skan  while (first_it != last_it)
165169691Skan    insert(*(first_it++));
166169691Skan}
167169691Skan
168169691SkanPB_DS_CLASS_T_DEC
169169691Skantypename PB_DS_CLASS_C_DEC::node_pointer
170169691SkanPB_DS_CLASS_C_DEC::
171169691Skanrecursive_copy_node(const_node_pointer p_other_nd)
172169691Skan{
173169691Skan  _GLIBCXX_DEBUG_ASSERT(p_other_nd != NULL);
174169691Skan  if (p_other_nd->m_type == pat_trie_leaf_node_type)
175169691Skan    {
176169691Skan      const_leaf_pointer p_other_leaf = static_cast<const_leaf_pointer>(p_other_nd);
177169691Skan
178169691Skan      leaf_pointer p_new_lf = s_leaf_allocator.allocate(1);
179169691Skan      cond_dealtor cond(p_new_lf);
180169691Skan      new (p_new_lf) leaf(p_other_leaf->value());
181169691Skan      apply_update(p_new_lf, (node_update* )this);
182169691Skan      cond.set_no_action_dtor();
183169691Skan      return (p_new_lf);
184169691Skan    }
185169691Skan
186169691Skan  _GLIBCXX_DEBUG_ASSERT(p_other_nd->m_type == pat_trie_internal_node_type);
187169691Skan  node_pointer a_p_children[internal_node::arr_size];
188169691Skan  size_type child_i = 0;
189169691Skan  const_internal_node_pointer p_other_internal_nd =
190169691Skan    static_cast<const_internal_node_pointer>(p_other_nd);
191169691Skan
192169691Skan  typename internal_node::const_iterator child_it =
193169691Skan    p_other_internal_nd->begin();
194169691Skan
195169691Skan  internal_node_pointer p_ret;
196169691Skan  try
197169691Skan    {
198169691Skan      while (child_it != p_other_internal_nd->end())
199169691Skan	a_p_children[child_i++] = recursive_copy_node(*(child_it++));
200169691Skan      p_ret = s_internal_node_allocator.allocate(1);
201169691Skan    }
202169691Skan  catch(...)
203169691Skan    {
204169691Skan      while (child_i-- > 0)
205169691Skan	clear_imp(a_p_children[child_i]);
206169691Skan      __throw_exception_again;
207169691Skan    }
208169691Skan
209169691Skan  new (p_ret) internal_node(p_other_internal_nd->get_e_ind(),
210169691Skan			    pref_begin(a_p_children[0]));
211169691Skan
212169691Skan  --child_i;
213169691Skan  _GLIBCXX_DEBUG_ASSERT(child_i > 1);
214169691Skan  do
215169691Skan    p_ret->add_child(a_p_children[child_i], pref_begin(a_p_children[child_i]),
216169691Skan		     pref_end(a_p_children[child_i]), this);
217169691Skan  while (child_i-- > 0);
218169691Skan  apply_update(p_ret, (node_update* )this);
219169691Skan  return p_ret;
220169691Skan}
221