1// -*- C++ -*-
2
3// Copyright (C) 2005, 2006, 2007, 2008, 2009 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 3, 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// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23// <http://www.gnu.org/licenses/>.
24
25// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
26
27// Permission to use, copy, modify, sell, and distribute this software
28// is hereby granted without fee, provided that the above copyright
29// notice appears in all copies, and that both that copyright notice
30// and this permission notice appear in supporting documentation. None
31// of the above authors, nor IBM Haifa Research Laboratories, make any
32// representation about the suitability of this software for any
33// purpose. It is provided "as is" without express or implied
34// warranty.
35
36/**
37 * @file split_join_fn_imps.hpp
38 * Contains an implementation class for a binary_heap.
39 */
40
41PB_DS_CLASS_T_DEC
42template<typename Pred>
43void
44PB_DS_CLASS_C_DEC::
45split(Pred pred, PB_DS_CLASS_C_DEC& other)
46{
47  _GLIBCXX_DEBUG_ONLY(assert_valid();)
48
49    typedef
50    typename entry_pred<
51    value_type,
52    Pred,
53    simple_value,
54    Allocator>::type
55    pred_t;
56
57  const size_type left = partition(pred_t(pred));
58
59  _GLIBCXX_DEBUG_ASSERT(m_size >= left);
60
61  const size_type ersd = m_size - left;
62
63  _GLIBCXX_DEBUG_ASSERT(m_size >= ersd);
64
65  const size_type actual_size =
66    resize_policy::get_new_size_for_arbitrary(left);
67
68  const size_type other_actual_size =
69    other.get_new_size_for_arbitrary(ersd);
70
71  entry_pointer a_entries = NULL;
72  entry_pointer a_other_entries = NULL;
73
74  __try
75    {
76      a_entries = s_entry_allocator.allocate(actual_size);
77
78      a_other_entries = s_entry_allocator.allocate(other_actual_size);
79    }
80  __catch(...)
81    {
82      if (a_entries != NULL)
83	s_entry_allocator.deallocate(a_entries, actual_size);
84
85      if (a_other_entries != NULL)
86	s_entry_allocator.deallocate(a_other_entries, other_actual_size);
87
88      __throw_exception_again;
89    };
90
91  for (size_type i = 0; i < other.m_size; ++i)
92    erase_at(other.m_a_entries, i, s_no_throw_copies_ind);
93
94  _GLIBCXX_DEBUG_ASSERT(actual_size >= left);
95  std::copy(m_a_entries, m_a_entries + left, a_entries);
96  std::copy(m_a_entries + left, m_a_entries + m_size, a_other_entries);
97
98  s_entry_allocator.deallocate(m_a_entries, m_actual_size);
99  s_entry_allocator.deallocate(other.m_a_entries, other.m_actual_size);
100
101  m_actual_size = actual_size;
102  other.m_actual_size = other_actual_size;
103
104  m_size = left;
105  other.m_size = ersd;
106
107  m_a_entries = a_entries;
108  other.m_a_entries = a_other_entries;
109
110  std::make_heap(m_a_entries, m_a_entries + m_size, static_cast<entry_cmp& >(*this));
111  std::make_heap(other.m_a_entries, other.m_a_entries + other.m_size, static_cast<entry_cmp& >(other));
112
113  resize_policy::notify_arbitrary(m_actual_size);
114  other.notify_arbitrary(other.m_actual_size);
115
116  _GLIBCXX_DEBUG_ONLY(assert_valid();)
117    _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
118    }
119
120PB_DS_CLASS_T_DEC
121inline void
122PB_DS_CLASS_C_DEC::
123join(PB_DS_CLASS_C_DEC& other)
124{
125  _GLIBCXX_DEBUG_ONLY(assert_valid();)
126  _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
127
128  const size_type len = m_size + other.m_size;
129  const size_type actual_size = resize_policy::get_new_size_for_arbitrary(len);
130
131  entry_pointer a_entries = NULL;
132  entry_pointer a_other_entries = NULL;
133
134  __try
135    {
136      a_entries = s_entry_allocator.allocate(actual_size);
137      a_other_entries = s_entry_allocator.allocate(resize_policy::min_size);
138    }
139  __catch(...)
140    {
141      if (a_entries != NULL)
142	s_entry_allocator.deallocate(a_entries, actual_size);
143
144      if (a_other_entries != NULL)
145	s_entry_allocator.deallocate(a_other_entries, resize_policy::min_size);
146
147      __throw_exception_again;
148    }
149
150  std::copy(m_a_entries, m_a_entries + m_size, a_entries);
151  std::copy(other.m_a_entries, other.m_a_entries + other.m_size, a_entries + m_size);
152
153  s_entry_allocator.deallocate(m_a_entries, m_actual_size);
154  m_a_entries = a_entries;
155  m_size = len;
156  m_actual_size = actual_size;
157
158  resize_policy::notify_arbitrary(actual_size);
159
160  std::make_heap(m_a_entries, m_a_entries + m_size, static_cast<entry_cmp& >(*this));
161
162  s_entry_allocator.deallocate(other.m_a_entries, other.m_actual_size);
163  other.m_a_entries = a_other_entries;
164  other.m_size = 0;
165  other.m_actual_size = resize_policy::min_size;
166
167  other.notify_arbitrary(resize_policy::min_size);
168
169  _GLIBCXX_DEBUG_ONLY(assert_valid();)
170  _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
171}
172
173