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 ov_tree_.
45 */
46
47PB_DS_CLASS_T_DEC
48typename PB_DS_CLASS_C_DEC::value_allocator
49PB_DS_CLASS_C_DEC::s_value_alloc;
50
51PB_DS_CLASS_T_DEC
52typename PB_DS_CLASS_C_DEC::metadata_allocator
53PB_DS_CLASS_C_DEC::s_metadata_alloc;
54
55PB_DS_CLASS_T_DEC
56PB_DS_CLASS_C_DEC::
57PB_DS_OV_TREE_CLASS_NAME() :
58  m_a_values(NULL),
59  m_a_metadata(NULL),
60  m_end_it(NULL),
61  m_size(0)
62{ _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();) }
63
64PB_DS_CLASS_T_DEC
65PB_DS_CLASS_C_DEC::
66PB_DS_OV_TREE_CLASS_NAME(const Cmp_Fn& r_cmp_fn) :
67  cmp_fn_base(r_cmp_fn),
68  m_a_values(NULL),
69  m_a_metadata(NULL),
70  m_end_it(NULL),
71  m_size(0)
72{ _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();) }
73
74PB_DS_CLASS_T_DEC
75PB_DS_CLASS_C_DEC::
76PB_DS_OV_TREE_CLASS_NAME(const Cmp_Fn& r_cmp_fn, const node_update& r_node_update) :
77  cmp_fn_base(r_cmp_fn),
78  node_update(r_node_update),
79  m_a_values(NULL),
80  m_a_metadata(NULL),
81  m_end_it(NULL),
82  m_size(0)
83{ _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();) }
84
85PB_DS_CLASS_T_DEC
86PB_DS_CLASS_C_DEC::
87PB_DS_OV_TREE_CLASS_NAME(const PB_DS_CLASS_C_DEC& other) :
88#ifdef _GLIBCXX_DEBUG
89  map_debug_base(other),
90#endif
91#ifdef PB_DS_TREE_TRACE
92  PB_DS_TREE_TRACE_BASE_C_DEC(other),
93#endif
94  cmp_fn_base(other),
95  node_update(other),
96  m_a_values(NULL),
97  m_a_metadata(NULL),
98  m_end_it(NULL),
99  m_size(0)
100{
101  copy_from_ordered_range(other.begin(), other.end());
102  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
103}
104
105PB_DS_CLASS_T_DEC
106template<typename It>
107inline void
108PB_DS_CLASS_C_DEC::
109copy_from_range(It first_it, It last_it)
110{
111#ifdef PB_DS_DATA_TRUE_INDICATOR
112  typedef
113    std::map<
114    key_type,
115    mapped_type,
116    Cmp_Fn,
117    typename Allocator::template rebind<
118    value_type>::other>
119    map_type;
120#else
121  typedef
122    std::set<
123    key_type,
124    Cmp_Fn,
125    typename Allocator::template rebind<
126    Key>::other>
127    map_type;
128#endif
129
130  map_type m(first_it, last_it);
131  copy_from_ordered_range(m.begin(), m.end());
132}
133
134PB_DS_CLASS_T_DEC
135template<typename It>
136void
137PB_DS_CLASS_C_DEC::
138copy_from_ordered_range(It first_it, It last_it)
139{
140  const size_type len = std::distance(first_it, last_it);
141  if (len == 0)
142    return;
143
144  value_vector a_values = s_value_alloc.allocate(len);
145  iterator target_it = a_values;
146  It source_it = first_it;
147  It source_end_it = last_it;
148
149  cond_dtor<size_type> cd(a_values, target_it, len);
150  while (source_it != source_end_it)
151    {
152      new (const_cast<void* >(static_cast<const void* >(target_it)))
153	value_type(*source_it++);
154
155      ++target_it;
156    }
157
158  reallocate_metadata((node_update* )this, len);
159  cd.set_no_action();
160  m_a_values = a_values;
161  m_size = len;
162  m_end_it = m_a_values + m_size;
163  update(PB_DS_node_begin_imp(), (node_update* )this);
164
165#ifdef _GLIBCXX_DEBUG
166  const_iterator dbg_it = m_a_values;
167  while (dbg_it != m_end_it)
168    {
169      map_debug_base::insert_new(PB_DS_V2F(*dbg_it));
170      dbg_it++;
171    }
172  PB_DS_CLASS_C_DEC::assert_valid();
173#endif
174}
175
176PB_DS_CLASS_T_DEC
177template<typename It>
178void
179PB_DS_CLASS_C_DEC::
180copy_from_ordered_range(It first_it, It last_it, It other_first_it,
181			It other_last_it)
182{
183  clear();
184  const size_type len = std::distance(first_it, last_it)
185    		         + std::distance(other_first_it, other_last_it);
186
187  value_vector a_values = s_value_alloc.allocate(len);
188
189  iterator target_it = a_values;
190  It source_it = first_it;
191  It source_end_it = last_it;
192
193  cond_dtor<size_type> cd(a_values, target_it, len);
194  while (source_it != source_end_it)
195    {
196      new (const_cast<void* >(static_cast<const void* >(target_it)))
197	value_type(*source_it++);
198      ++target_it;
199    }
200
201  source_it = other_first_it;
202  source_end_it = other_last_it;
203
204  while (source_it != source_end_it)
205    {
206      new (const_cast<void* >(static_cast<const void* >(target_it)))
207	value_type(*source_it++);
208      ++target_it;
209    }
210
211  reallocate_metadata((node_update* )this, len);
212  cd.set_no_action();
213  m_a_values = a_values;
214  m_size = len;
215  m_end_it = m_a_values + m_size;
216  update(PB_DS_node_begin_imp(), (node_update* )this);
217
218#ifdef _GLIBCXX_DEBUG
219  const_iterator dbg_it = m_a_values;
220  while (dbg_it != m_end_it)
221    {
222      map_debug_base::insert_new(PB_DS_V2F(*dbg_it));
223      dbg_it++;
224    }
225  PB_DS_CLASS_C_DEC::assert_valid();
226#endif
227}
228
229PB_DS_CLASS_T_DEC
230void
231PB_DS_CLASS_C_DEC::
232swap(PB_DS_CLASS_C_DEC& other)
233{
234  _GLIBCXX_DEBUG_ONLY(assert_valid();)
235  value_swap(other);
236  std::swap((Cmp_Fn& )(*this), (Cmp_Fn& )other);
237  _GLIBCXX_DEBUG_ONLY(assert_valid();)
238}
239
240PB_DS_CLASS_T_DEC
241void
242PB_DS_CLASS_C_DEC::
243value_swap(PB_DS_CLASS_C_DEC& other)
244{
245  std::swap(m_a_values, other.m_a_values);
246  std::swap(m_a_metadata, other.m_a_metadata);
247  std::swap(m_size, other.m_size);
248  std::swap(m_end_it, other.m_end_it);
249  _GLIBCXX_DEBUG_ONLY(map_debug_base::swap(other);)
250}
251
252PB_DS_CLASS_T_DEC
253PB_DS_CLASS_C_DEC::
254~PB_DS_OV_TREE_CLASS_NAME()
255{
256  _GLIBCXX_DEBUG_ONLY(assert_valid();)
257  cond_dtor<size_type> cd(m_a_values, m_end_it, m_size);
258  reallocate_metadata((node_update* )this, 0);
259}
260
261PB_DS_CLASS_T_DEC
262inline void
263PB_DS_CLASS_C_DEC::
264update(node_iterator /*it*/, null_node_update_pointer)
265{ }
266
267PB_DS_CLASS_T_DEC
268template<typename Node_Update>
269void
270PB_DS_CLASS_C_DEC::
271update(node_iterator nd_it, Node_Update* p_update)
272{
273  const_node_iterator end_it = PB_DS_node_end_imp();
274  if (nd_it == end_it)
275    return;
276  update(nd_it.get_l_child(), p_update);
277  update(nd_it.get_r_child(), p_update);
278  node_update::operator()(nd_it, end_it);
279}
280