Chris@16
|
1 //
|
Chris@16
|
2 //=======================================================================
|
Chris@16
|
3 // Copyright 2009 Trustees of Indiana University
|
Chris@16
|
4 // Authors: Jeremiah J. Willcock, Andrew Lumsdaine
|
Chris@16
|
5 //
|
Chris@16
|
6 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
7 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
8 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //=======================================================================
|
Chris@16
|
10 //
|
Chris@16
|
11 #ifndef BOOST_D_ARY_HEAP_HPP
|
Chris@16
|
12 #define BOOST_D_ARY_HEAP_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <vector>
|
Chris@16
|
15 #include <cstddef>
|
Chris@16
|
16 #include <algorithm>
|
Chris@16
|
17 #include <utility>
|
Chris@16
|
18 #include <boost/assert.hpp>
|
Chris@16
|
19 #include <boost/static_assert.hpp>
|
Chris@16
|
20 #include <boost/shared_array.hpp>
|
Chris@16
|
21 #include <boost/property_map/property_map.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 // WARNING: it is not safe to copy a d_ary_heap_indirect and then modify one of
|
Chris@16
|
24 // the copies. The class is required to be copyable so it can be passed around
|
Chris@16
|
25 // (without move support from C++11), but it deep-copies the heap contents yet
|
Chris@16
|
26 // shallow-copies the index_in_heap_map.
|
Chris@16
|
27
|
Chris@16
|
28 namespace boost {
|
Chris@16
|
29
|
Chris@16
|
30 // Swap two elements in a property map without assuming they model
|
Chris@16
|
31 // LvaluePropertyMap -- currently not used
|
Chris@16
|
32 template <typename PropMap>
|
Chris@16
|
33 inline void property_map_swap(
|
Chris@16
|
34 PropMap prop_map,
|
Chris@16
|
35 const typename boost::property_traits<PropMap>::key_type& ka,
|
Chris@16
|
36 const typename boost::property_traits<PropMap>::key_type& kb) {
|
Chris@16
|
37 typename boost::property_traits<PropMap>::value_type va = get(prop_map, ka);
|
Chris@16
|
38 put(prop_map, ka, get(prop_map, kb));
|
Chris@16
|
39 put(prop_map, kb, va);
|
Chris@16
|
40 }
|
Chris@16
|
41
|
Chris@16
|
42 namespace detail {
|
Chris@16
|
43 template <typename Value>
|
Chris@16
|
44 class fixed_max_size_vector {
|
Chris@16
|
45 boost::shared_array<Value> m_data;
|
Chris@16
|
46 std::size_t m_size;
|
Chris@16
|
47
|
Chris@16
|
48 public:
|
Chris@16
|
49 typedef std::size_t size_type;
|
Chris@16
|
50 fixed_max_size_vector(std::size_t max_size)
|
Chris@16
|
51 : m_data(new Value[max_size]), m_size(0) {}
|
Chris@16
|
52 std::size_t size() const {return m_size;}
|
Chris@16
|
53 bool empty() const {return m_size == 0;}
|
Chris@16
|
54 Value& operator[](std::size_t i) {return m_data[i];}
|
Chris@16
|
55 const Value& operator[](std::size_t i) const {return m_data[i];}
|
Chris@16
|
56 void push_back(Value v) {m_data[m_size++] = v;}
|
Chris@16
|
57 void pop_back() {--m_size;}
|
Chris@16
|
58 Value& back() {return m_data[m_size - 1];}
|
Chris@16
|
59 const Value& back() const {return m_data[m_size - 1];}
|
Chris@16
|
60 };
|
Chris@16
|
61 }
|
Chris@16
|
62
|
Chris@16
|
63 // D-ary heap using an indirect compare operator (use identity_property_map
|
Chris@16
|
64 // as DistanceMap to get a direct compare operator). This heap appears to be
|
Chris@16
|
65 // commonly used for Dijkstra's algorithm for its good practical performance
|
Chris@16
|
66 // on some platforms; asymptotically, it has an O(lg N) decrease-key
|
Chris@16
|
67 // operation while that can be done in constant time on a relaxed heap. The
|
Chris@16
|
68 // implementation is mostly based on the binary heap page on Wikipedia and
|
Chris@16
|
69 // online sources that state that the operations are the same for d-ary
|
Chris@16
|
70 // heaps. This code is not based on the old Boost d-ary heap code.
|
Chris@16
|
71 //
|
Chris@16
|
72 // - d_ary_heap_indirect is a model of UpdatableQueue as is needed for
|
Chris@16
|
73 // dijkstra_shortest_paths.
|
Chris@16
|
74 //
|
Chris@16
|
75 // - Value must model Assignable.
|
Chris@16
|
76 // - Arity must be at least 2 (optimal value appears to be 4, both in my and
|
Chris@16
|
77 // third-party experiments).
|
Chris@16
|
78 // - IndexInHeapMap must be a ReadWritePropertyMap from Value to
|
Chris@16
|
79 // Container::size_type (to store the index of each stored value within the
|
Chris@16
|
80 // heap for decrease-key aka update).
|
Chris@16
|
81 // - DistanceMap must be a ReadablePropertyMap from Value to something
|
Chris@16
|
82 // (typedef'ed as distance_type).
|
Chris@16
|
83 // - Compare must be a BinaryPredicate used as a less-than operator on
|
Chris@16
|
84 // distance_type.
|
Chris@16
|
85 // - Container must be a random-access, contiguous container (in practice,
|
Chris@16
|
86 // the operations used probably require that it is std::vector<Value>).
|
Chris@16
|
87 //
|
Chris@16
|
88 template <typename Value,
|
Chris@16
|
89 std::size_t Arity,
|
Chris@16
|
90 typename IndexInHeapPropertyMap,
|
Chris@16
|
91 typename DistanceMap,
|
Chris@16
|
92 typename Compare = std::less<Value>,
|
Chris@16
|
93 typename Container = std::vector<Value> >
|
Chris@16
|
94 class d_ary_heap_indirect {
|
Chris@16
|
95 BOOST_STATIC_ASSERT (Arity >= 2);
|
Chris@16
|
96
|
Chris@16
|
97 public:
|
Chris@16
|
98 typedef typename Container::size_type size_type;
|
Chris@16
|
99 typedef Value value_type;
|
Chris@16
|
100 typedef typename boost::property_traits<DistanceMap>::value_type key_type;
|
Chris@16
|
101 typedef DistanceMap key_map;
|
Chris@16
|
102
|
Chris@16
|
103 d_ary_heap_indirect(DistanceMap distance,
|
Chris@16
|
104 IndexInHeapPropertyMap index_in_heap,
|
Chris@16
|
105 const Compare& compare = Compare(),
|
Chris@16
|
106 const Container& data = Container())
|
Chris@16
|
107 : compare(compare), data(data), distance(distance),
|
Chris@16
|
108 index_in_heap(index_in_heap) {}
|
Chris@16
|
109 /* Implicit copy constructor */
|
Chris@16
|
110 /* Implicit assignment operator */
|
Chris@16
|
111
|
Chris@16
|
112 size_type size() const {
|
Chris@16
|
113 return data.size();
|
Chris@16
|
114 }
|
Chris@16
|
115
|
Chris@16
|
116 bool empty() const {
|
Chris@16
|
117 return data.empty();
|
Chris@16
|
118 }
|
Chris@16
|
119
|
Chris@16
|
120 void push(const Value& v) {
|
Chris@16
|
121 size_type index = data.size();
|
Chris@16
|
122 data.push_back(v);
|
Chris@16
|
123 put(index_in_heap, v, index);
|
Chris@16
|
124 preserve_heap_property_up(index);
|
Chris@16
|
125 verify_heap();
|
Chris@16
|
126 }
|
Chris@16
|
127
|
Chris@16
|
128 Value& top() {
|
Chris@16
|
129 BOOST_ASSERT (!this->empty());
|
Chris@16
|
130 return data[0];
|
Chris@16
|
131 }
|
Chris@16
|
132
|
Chris@16
|
133 const Value& top() const {
|
Chris@16
|
134 BOOST_ASSERT (!this->empty());
|
Chris@16
|
135 return data[0];
|
Chris@16
|
136 }
|
Chris@16
|
137
|
Chris@16
|
138 void pop() {
|
Chris@16
|
139 BOOST_ASSERT (!this->empty());
|
Chris@16
|
140 put(index_in_heap, data[0], (size_type)(-1));
|
Chris@16
|
141 if (data.size() != 1) {
|
Chris@16
|
142 data[0] = data.back();
|
Chris@16
|
143 put(index_in_heap, data[0], (size_type)(0));
|
Chris@16
|
144 data.pop_back();
|
Chris@16
|
145 preserve_heap_property_down();
|
Chris@16
|
146 verify_heap();
|
Chris@16
|
147 } else {
|
Chris@16
|
148 data.pop_back();
|
Chris@16
|
149 }
|
Chris@16
|
150 }
|
Chris@16
|
151
|
Chris@16
|
152 // This function assumes the key has been updated (using an external write
|
Chris@16
|
153 // to the distance map or such)
|
Chris@16
|
154 // See http://coding.derkeiler.com/Archive/General/comp.theory/2007-05/msg00043.html
|
Chris@16
|
155 void update(const Value& v) { /* decrease-key */
|
Chris@16
|
156 size_type index = get(index_in_heap, v);
|
Chris@16
|
157 preserve_heap_property_up(index);
|
Chris@16
|
158 verify_heap();
|
Chris@16
|
159 }
|
Chris@16
|
160
|
Chris@16
|
161 bool contains(const Value& v) const {
|
Chris@16
|
162 size_type index = get(index_in_heap, v);
|
Chris@16
|
163 return (index != (size_type)(-1));
|
Chris@16
|
164 }
|
Chris@16
|
165
|
Chris@16
|
166 void push_or_update(const Value& v) { /* insert if not present, else update */
|
Chris@16
|
167 size_type index = get(index_in_heap, v);
|
Chris@16
|
168 if (index == (size_type)(-1)) {
|
Chris@16
|
169 index = data.size();
|
Chris@16
|
170 data.push_back(v);
|
Chris@16
|
171 put(index_in_heap, v, index);
|
Chris@16
|
172 }
|
Chris@16
|
173 preserve_heap_property_up(index);
|
Chris@16
|
174 verify_heap();
|
Chris@16
|
175 }
|
Chris@16
|
176
|
Chris@16
|
177 DistanceMap keys() const {
|
Chris@16
|
178 return distance;
|
Chris@16
|
179 }
|
Chris@16
|
180
|
Chris@16
|
181 private:
|
Chris@16
|
182 Compare compare;
|
Chris@16
|
183 Container data;
|
Chris@16
|
184 DistanceMap distance;
|
Chris@16
|
185 IndexInHeapPropertyMap index_in_heap;
|
Chris@16
|
186
|
Chris@16
|
187 // The distances being compared using compare and that are stored in the
|
Chris@16
|
188 // distance map
|
Chris@16
|
189 typedef typename boost::property_traits<DistanceMap>::value_type distance_type;
|
Chris@16
|
190
|
Chris@16
|
191 // Get the parent of a given node in the heap
|
Chris@16
|
192 static size_type parent(size_type index) {
|
Chris@16
|
193 return (index - 1) / Arity;
|
Chris@16
|
194 }
|
Chris@16
|
195
|
Chris@16
|
196 // Get the child_idx'th child of a given node; 0 <= child_idx < Arity
|
Chris@16
|
197 static size_type child(size_type index, std::size_t child_idx) {
|
Chris@16
|
198 return index * Arity + child_idx + 1;
|
Chris@16
|
199 }
|
Chris@16
|
200
|
Chris@16
|
201 // Swap two elements in the heap by index, updating index_in_heap
|
Chris@16
|
202 void swap_heap_elements(size_type index_a, size_type index_b) {
|
Chris@16
|
203 using std::swap;
|
Chris@16
|
204 Value value_a = data[index_a];
|
Chris@16
|
205 Value value_b = data[index_b];
|
Chris@16
|
206 data[index_a] = value_b;
|
Chris@16
|
207 data[index_b] = value_a;
|
Chris@16
|
208 put(index_in_heap, value_a, index_b);
|
Chris@16
|
209 put(index_in_heap, value_b, index_a);
|
Chris@16
|
210 }
|
Chris@16
|
211
|
Chris@16
|
212 // Emulate the indirect_cmp that is now folded into this heap class
|
Chris@16
|
213 bool compare_indirect(const Value& a, const Value& b) const {
|
Chris@16
|
214 return compare(get(distance, a), get(distance, b));
|
Chris@16
|
215 }
|
Chris@16
|
216
|
Chris@16
|
217 // Verify that the array forms a heap; commented out by default
|
Chris@16
|
218 void verify_heap() const {
|
Chris@16
|
219 // This is a very expensive test so it should be disabled even when
|
Chris@16
|
220 // NDEBUG is not defined
|
Chris@16
|
221 #if 0
|
Chris@16
|
222 for (size_t i = 1; i < data.size(); ++i) {
|
Chris@16
|
223 if (compare_indirect(data[i], data[parent(i)])) {
|
Chris@16
|
224 BOOST_ASSERT (!"Element is smaller than its parent");
|
Chris@16
|
225 }
|
Chris@16
|
226 }
|
Chris@16
|
227 #endif
|
Chris@16
|
228 }
|
Chris@16
|
229
|
Chris@16
|
230 // Starting at a node, move up the tree swapping elements to preserve the
|
Chris@16
|
231 // heap property
|
Chris@16
|
232 void preserve_heap_property_up(size_type index) {
|
Chris@16
|
233 size_type orig_index = index;
|
Chris@16
|
234 size_type num_levels_moved = 0;
|
Chris@16
|
235 // The first loop just saves swaps that need to be done in order to avoid
|
Chris@16
|
236 // aliasing issues in its search; there is a second loop that does the
|
Chris@16
|
237 // necessary swap operations
|
Chris@16
|
238 if (index == 0) return; // Do nothing on root
|
Chris@16
|
239 Value currently_being_moved = data[index];
|
Chris@16
|
240 distance_type currently_being_moved_dist =
|
Chris@16
|
241 get(distance, currently_being_moved);
|
Chris@16
|
242 for (;;) {
|
Chris@16
|
243 if (index == 0) break; // Stop at root
|
Chris@16
|
244 size_type parent_index = parent(index);
|
Chris@16
|
245 Value parent_value = data[parent_index];
|
Chris@16
|
246 if (compare(currently_being_moved_dist, get(distance, parent_value))) {
|
Chris@16
|
247 ++num_levels_moved;
|
Chris@16
|
248 index = parent_index;
|
Chris@16
|
249 continue;
|
Chris@16
|
250 } else {
|
Chris@16
|
251 break; // Heap property satisfied
|
Chris@16
|
252 }
|
Chris@16
|
253 }
|
Chris@16
|
254 // Actually do the moves -- move num_levels_moved elements down in the
|
Chris@16
|
255 // tree, then put currently_being_moved at the top
|
Chris@16
|
256 index = orig_index;
|
Chris@16
|
257 for (size_type i = 0; i < num_levels_moved; ++i) {
|
Chris@16
|
258 size_type parent_index = parent(index);
|
Chris@16
|
259 Value parent_value = data[parent_index];
|
Chris@16
|
260 put(index_in_heap, parent_value, index);
|
Chris@16
|
261 data[index] = parent_value;
|
Chris@16
|
262 index = parent_index;
|
Chris@16
|
263 }
|
Chris@16
|
264 data[index] = currently_being_moved;
|
Chris@16
|
265 put(index_in_heap, currently_being_moved, index);
|
Chris@16
|
266 verify_heap();
|
Chris@16
|
267 }
|
Chris@16
|
268
|
Chris@16
|
269 // From the root, swap elements (each one with its smallest child) if there
|
Chris@16
|
270 // are any parent-child pairs that violate the heap property
|
Chris@16
|
271 void preserve_heap_property_down() {
|
Chris@16
|
272 if (data.empty()) return;
|
Chris@16
|
273 size_type index = 0;
|
Chris@16
|
274 Value currently_being_moved = data[0];
|
Chris@16
|
275 distance_type currently_being_moved_dist =
|
Chris@16
|
276 get(distance, currently_being_moved);
|
Chris@16
|
277 size_type heap_size = data.size();
|
Chris@16
|
278 Value* data_ptr = &data[0];
|
Chris@16
|
279 for (;;) {
|
Chris@16
|
280 size_type first_child_index = child(index, 0);
|
Chris@16
|
281 if (first_child_index >= heap_size) break; /* No children */
|
Chris@16
|
282 Value* child_base_ptr = data_ptr + first_child_index;
|
Chris@16
|
283 size_type smallest_child_index = 0;
|
Chris@16
|
284 distance_type smallest_child_dist = get(distance, child_base_ptr[smallest_child_index]);
|
Chris@16
|
285 if (first_child_index + Arity <= heap_size) {
|
Chris@16
|
286 // Special case for a statically known loop count (common case)
|
Chris@16
|
287 for (size_t i = 1; i < Arity; ++i) {
|
Chris@16
|
288 Value i_value = child_base_ptr[i];
|
Chris@16
|
289 distance_type i_dist = get(distance, i_value);
|
Chris@16
|
290 if (compare(i_dist, smallest_child_dist)) {
|
Chris@16
|
291 smallest_child_index = i;
|
Chris@16
|
292 smallest_child_dist = i_dist;
|
Chris@16
|
293 }
|
Chris@16
|
294 }
|
Chris@16
|
295 } else {
|
Chris@16
|
296 for (size_t i = 1; i < heap_size - first_child_index; ++i) {
|
Chris@16
|
297 distance_type i_dist = get(distance, child_base_ptr[i]);
|
Chris@16
|
298 if (compare(i_dist, smallest_child_dist)) {
|
Chris@16
|
299 smallest_child_index = i;
|
Chris@16
|
300 smallest_child_dist = i_dist;
|
Chris@16
|
301 }
|
Chris@16
|
302 }
|
Chris@16
|
303 }
|
Chris@16
|
304 if (compare(smallest_child_dist, currently_being_moved_dist)) {
|
Chris@16
|
305 swap_heap_elements(smallest_child_index + first_child_index, index);
|
Chris@16
|
306 index = smallest_child_index + first_child_index;
|
Chris@16
|
307 continue;
|
Chris@16
|
308 } else {
|
Chris@16
|
309 break; // Heap property satisfied
|
Chris@16
|
310 }
|
Chris@16
|
311 }
|
Chris@16
|
312 verify_heap();
|
Chris@16
|
313 }
|
Chris@16
|
314
|
Chris@16
|
315 };
|
Chris@16
|
316
|
Chris@16
|
317 } // namespace boost
|
Chris@16
|
318
|
Chris@16
|
319 #endif // BOOST_D_ARY_HEAP_HPP
|