20913c47d590147c83553cc477c87a76e083c715
[gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / binary_heap_ / split_join_fn_imps.hpp
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 split_join_fn_imps.hpp
44 * Contains an implementation class for a binary_heap.
45 */
46
47 PB_DS_CLASS_T_DEC
48 template<typename Pred>
49 void
50 PB_DS_CLASS_C_DEC::
51 split(Pred pred, PB_DS_CLASS_C_DEC& other)
52 {
53 PB_DS_DBG_ONLY(assert_valid();)
54
55 typedef
56 typename entry_pred<
57 value_type,
58 Pred,
59 simple_value,
60 Allocator>::type
61 pred_t;
62
63 const size_type left = partition(pred_t(pred));
64
65 PB_DS_DBG_ASSERT(m_size >= left);
66
67 const size_type ersd = m_size - left;
68
69 PB_DS_DBG_ASSERT(m_size >= ersd);
70
71 const size_type actual_size =
72 resize_policy::get_new_size_for_arbitrary(left);
73
74 const size_type other_actual_size =
75 other.get_new_size_for_arbitrary(ersd);
76
77 entry_pointer a_entries = NULL;
78 entry_pointer a_other_entries = NULL;
79
80 try
81 {
82 a_entries = s_entry_allocator.allocate(actual_size);
83
84 a_other_entries = s_entry_allocator.allocate(other_actual_size);
85 }
86 catch(...)
87 {
88 if (a_entries != NULL)
89 s_entry_allocator.deallocate(a_entries, actual_size);
90
91 if (a_other_entries != NULL)
92 s_entry_allocator.deallocate(a_other_entries, other_actual_size);
93
94 throw;
95 };
96
97 for (size_type i = 0; i < other.m_size; ++i)
98 erase_at(other.m_a_entries, i, s_no_throw_copies_ind);
99
100 PB_DS_DBG_ASSERT(actual_size >= left);
101 std::copy(m_a_entries, m_a_entries + left, a_entries);
102 std::copy(m_a_entries + left, m_a_entries + m_size, a_other_entries);
103
104 s_entry_allocator.deallocate(m_a_entries, m_actual_size);
105 s_entry_allocator.deallocate(other.m_a_entries, other.m_actual_size);
106
107 m_actual_size = actual_size;
108 other.m_actual_size = other_actual_size;
109
110 m_size = left;
111 other.m_size = ersd;
112
113 m_a_entries = a_entries;
114 other.m_a_entries = a_other_entries;
115
116 std::make_heap(m_a_entries, m_a_entries + m_size, static_cast<entry_cmp& >(*this));
117 std::make_heap(other.m_a_entries, other.m_a_entries + other.m_size, static_cast<entry_cmp& >(other));
118
119 resize_policy::notify_arbitrary(m_actual_size);
120 other.notify_arbitrary(other.m_actual_size);
121
122 PB_DS_DBG_ONLY(assert_valid();)
123 PB_DS_DBG_ONLY(other.assert_valid();)
124 }
125
126 PB_DS_CLASS_T_DEC
127 inline void
128 PB_DS_CLASS_C_DEC::
129 join(PB_DS_CLASS_C_DEC& other)
130 {
131 PB_DS_DBG_ONLY(assert_valid();)
132 PB_DS_DBG_ONLY(other.assert_valid();)
133
134 const size_type size = m_size + other.m_size;
135 const size_type actual_size = resize_policy::get_new_size_for_arbitrary(size);
136
137 entry_pointer a_entries = NULL;
138 entry_pointer a_other_entries = NULL;
139
140 try
141 {
142 a_entries = s_entry_allocator.allocate(actual_size);
143
144 a_other_entries = s_entry_allocator.allocate(resize_policy::min_size);
145 }
146 catch(...)
147 {
148 if (a_entries != NULL)
149 s_entry_allocator.deallocate(a_entries, actual_size);
150
151 if (a_other_entries != NULL)
152 s_entry_allocator.deallocate(a_other_entries, resize_policy::min_size);
153
154 throw;
155 }
156
157 std::copy(m_a_entries, m_a_entries + m_size, a_entries);
158 std::copy(other.m_a_entries, other.m_a_entries + other.m_size, a_entries + m_size);
159
160 s_entry_allocator.deallocate(m_a_entries, m_actual_size);
161 m_a_entries = a_entries;
162 m_size = size;
163 m_actual_size = actual_size;
164
165 resize_policy::notify_arbitrary(actual_size);
166
167 std::make_heap(m_a_entries, m_a_entries + m_size, static_cast<entry_cmp& >(*this));
168
169 s_entry_allocator.deallocate(other.m_a_entries, other.m_actual_size);
170 other.m_a_entries = a_other_entries;
171 other.m_size = 0;
172 other.m_actual_size = resize_policy::min_size;
173
174 other.notify_arbitrary(resize_policy::min_size);
175
176 PB_DS_DBG_ONLY(assert_valid();)
177 PB_DS_DBG_ONLY(other.assert_valid();)
178 }
179