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 split_join_fn_imps.hpp
44169691Skan * Contains an implementation class for a binary_heap.
45169691Skan */
46169691Skan
47169691SkanPB_DS_CLASS_T_DEC
48169691Skantemplate<typename Pred>
49169691Skanvoid
50169691SkanPB_DS_CLASS_C_DEC::
51169691Skansplit(Pred pred, PB_DS_CLASS_C_DEC& other)
52169691Skan{
53169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
54169691Skan
55169691Skan    typedef
56169691Skan    typename entry_pred<
57169691Skan    value_type,
58169691Skan    Pred,
59169691Skan    simple_value,
60169691Skan    Allocator>::type
61169691Skan    pred_t;
62169691Skan
63169691Skan  const size_type left = partition(pred_t(pred));
64169691Skan
65169691Skan  _GLIBCXX_DEBUG_ASSERT(m_size >= left);
66169691Skan
67169691Skan  const size_type ersd = m_size - left;
68169691Skan
69169691Skan  _GLIBCXX_DEBUG_ASSERT(m_size >= ersd);
70169691Skan
71169691Skan  const size_type actual_size =
72169691Skan    resize_policy::get_new_size_for_arbitrary(left);
73169691Skan
74169691Skan  const size_type other_actual_size =
75169691Skan    other.get_new_size_for_arbitrary(ersd);
76169691Skan
77169691Skan  entry_pointer a_entries = NULL;
78169691Skan  entry_pointer a_other_entries = NULL;
79169691Skan
80169691Skan  try
81169691Skan    {
82169691Skan      a_entries = s_entry_allocator.allocate(actual_size);
83169691Skan
84169691Skan      a_other_entries = s_entry_allocator.allocate(other_actual_size);
85169691Skan    }
86169691Skan  catch(...)
87169691Skan    {
88169691Skan      if (a_entries != NULL)
89169691Skan	s_entry_allocator.deallocate(a_entries, actual_size);
90169691Skan
91169691Skan      if (a_other_entries != NULL)
92169691Skan	s_entry_allocator.deallocate(a_other_entries, other_actual_size);
93169691Skan
94169691Skan      __throw_exception_again;
95169691Skan    };
96169691Skan
97169691Skan  for (size_type i = 0; i < other.m_size; ++i)
98169691Skan    erase_at(other.m_a_entries, i, s_no_throw_copies_ind);
99169691Skan
100169691Skan  _GLIBCXX_DEBUG_ASSERT(actual_size >= left);
101169691Skan  std::copy(m_a_entries, m_a_entries + left, a_entries);
102169691Skan  std::copy(m_a_entries + left, m_a_entries + m_size, a_other_entries);
103169691Skan
104169691Skan  s_entry_allocator.deallocate(m_a_entries, m_actual_size);
105169691Skan  s_entry_allocator.deallocate(other.m_a_entries, other.m_actual_size);
106169691Skan
107169691Skan  m_actual_size = actual_size;
108169691Skan  other.m_actual_size = other_actual_size;
109169691Skan
110169691Skan  m_size = left;
111169691Skan  other.m_size = ersd;
112169691Skan
113169691Skan  m_a_entries = a_entries;
114169691Skan  other.m_a_entries = a_other_entries;
115169691Skan
116169691Skan  std::make_heap(m_a_entries, m_a_entries + m_size, static_cast<entry_cmp& >(*this));
117169691Skan  std::make_heap(other.m_a_entries, other.m_a_entries + other.m_size, static_cast<entry_cmp& >(other));
118169691Skan
119169691Skan  resize_policy::notify_arbitrary(m_actual_size);
120169691Skan  other.notify_arbitrary(other.m_actual_size);
121169691Skan
122169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
123169691Skan    _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
124169691Skan    }
125169691Skan
126169691SkanPB_DS_CLASS_T_DEC
127169691Skaninline void
128169691SkanPB_DS_CLASS_C_DEC::
129169691Skanjoin(PB_DS_CLASS_C_DEC& other)
130169691Skan{
131169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
132169691Skan  _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
133169691Skan
134169691Skan  const size_type len = m_size + other.m_size;
135169691Skan  const size_type actual_size = resize_policy::get_new_size_for_arbitrary(len);
136169691Skan
137169691Skan  entry_pointer a_entries = NULL;
138169691Skan  entry_pointer a_other_entries = NULL;
139169691Skan
140169691Skan  try
141169691Skan    {
142169691Skan      a_entries = s_entry_allocator.allocate(actual_size);
143169691Skan      a_other_entries = s_entry_allocator.allocate(resize_policy::min_size);
144169691Skan    }
145169691Skan  catch(...)
146169691Skan    {
147169691Skan      if (a_entries != NULL)
148169691Skan	s_entry_allocator.deallocate(a_entries, actual_size);
149169691Skan
150169691Skan      if (a_other_entries != NULL)
151169691Skan	s_entry_allocator.deallocate(a_other_entries, resize_policy::min_size);
152169691Skan
153169691Skan      __throw_exception_again;
154169691Skan    }
155169691Skan
156169691Skan  std::copy(m_a_entries, m_a_entries + m_size, a_entries);
157169691Skan  std::copy(other.m_a_entries, other.m_a_entries + other.m_size, a_entries + m_size);
158169691Skan
159169691Skan  s_entry_allocator.deallocate(m_a_entries, m_actual_size);
160169691Skan  m_a_entries = a_entries;
161169691Skan  m_size = len;
162169691Skan  m_actual_size = actual_size;
163169691Skan
164169691Skan  resize_policy::notify_arbitrary(actual_size);
165169691Skan
166169691Skan  std::make_heap(m_a_entries, m_a_entries + m_size, static_cast<entry_cmp& >(*this));
167169691Skan
168169691Skan  s_entry_allocator.deallocate(other.m_a_entries, other.m_actual_size);
169169691Skan  other.m_a_entries = a_other_entries;
170169691Skan  other.m_size = 0;
171169691Skan  other.m_actual_size = resize_policy::min_size;
172169691Skan
173169691Skan  other.notify_arbitrary(resize_policy::min_size);
174169691Skan
175169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
176169691Skan  _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
177169691Skan}
178169691Skan
179