/usr/include/d/std/container/binaryheap.d is in libphobos2-ldc-dev 1:0.17.1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | /**
This module provides a $(D BinaryHeap) adaptor that makes a binary heap out of
any user-provided random-access range.
This module is a submodule of $(LINK2 std_container.html, std.container).
Source: $(PHOBOSSRC std/container/_binaryheap.d)
Macros:
WIKI = Phobos/StdContainer
TEXTWITHCOMMAS = $0
Copyright: Red-black tree code copyright (C) 2008- by Steven Schveighoffer. Other code
copyright 2010- Andrei Alexandrescu. All rights reserved by the respective holders.
License: Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at $(WEB
boost.org/LICENSE_1_0.txt)).
Authors: Steven Schveighoffer, $(WEB erdani.com, Andrei Alexandrescu)
*/
module std.container.binaryheap;
import std.range.primitives;
import std.traits;
public import std.container.util;
// BinaryHeap
/**
Implements a $(WEB en.wikipedia.org/wiki/Binary_heap, binary heap)
container on top of a given random-access range type (usually $(D
T[])) or a random-access container type (usually $(D Array!T)). The
documentation of $(D BinaryHeap) will refer to the underlying range or
container as the $(I store) of the heap.
The binary heap induces structure over the underlying store such that
accessing the largest element (by using the $(D front) property) is a
$(BIGOH 1) operation and extracting it (by using the $(D
removeFront()) method) is done fast in $(BIGOH log n) time.
If $(D less) is the less-than operator, which is the default option,
then $(D BinaryHeap) defines a so-called max-heap that optimizes
extraction of the $(I largest) elements. To define a min-heap,
instantiate BinaryHeap with $(D "a > b") as its predicate.
Simply extracting elements from a $(D BinaryHeap) container is
tantamount to lazily fetching elements of $(D Store) in descending
order. Extracting elements from the $(D BinaryHeap) to completion
leaves the underlying store sorted in ascending order but, again,
yields elements in descending order.
If $(D Store) is a range, the $(D BinaryHeap) cannot grow beyond the
size of that range. If $(D Store) is a container that supports $(D
insertBack), the $(D BinaryHeap) may grow by adding elements to the
container.
*/
struct BinaryHeap(Store, alias less = "a < b")
if (isRandomAccessRange!(Store) || isRandomAccessRange!(typeof(Store.init[])))
{
import std.functional : binaryFun;
import std.exception : enforce;
import std.algorithm : move, min;
import std.typecons : RefCounted, RefCountedAutoInitialize;
// Really weird @@BUG@@: if you comment out the "private:" label below,
// std.algorithm can't unittest anymore
//private:
// The payload includes the support store and the effective length
private static struct Data
{
Store _store;
size_t _length;
}
private RefCounted!(Data, RefCountedAutoInitialize.no) _payload;
// Comparison predicate
private alias comp = binaryFun!(less);
// Convenience accessors
private @property ref Store _store()
{
assert(_payload.refCountedStore.isInitialized);
return _payload._store;
}
private @property ref size_t _length()
{
assert(_payload.refCountedStore.isInitialized);
return _payload._length;
}
// Asserts that the heap property is respected.
private void assertValid()
{
debug
{
import std.conv : to;
if (!_payload.refCountedStore.isInitialized) return;
if (_length < 2) return;
for (size_t n = _length - 1; n >= 1; --n)
{
auto parentIdx = (n - 1) / 2;
assert(!comp(_store[parentIdx], _store[n]), to!string(n));
}
}
}
// Assuming the element at index i perturbs the heap property in
// store r, percolates it down the heap such that the heap
// property is restored.
private void percolateDown(Store r, size_t i, size_t length)
{
for (;;)
{
auto left = i * 2 + 1, right = left + 1;
if (right == length)
{
if (comp(r[i], r[left])) swap(r, i, left);
return;
}
if (right > length) return;
assert(left < length && right < length);
auto largest = comp(r[i], r[left])
? (comp(r[left], r[right]) ? right : left)
: (comp(r[i], r[right]) ? right : i);
if (largest == i) return;
swap(r, i, largest);
i = largest;
}
}
// @@@BUG@@@: add private here, std.algorithm doesn't unittest anymore
/*private*/ void pop(Store store)
{
assert(!store.empty, "Cannot pop an empty store.");
if (store.length == 1) return;
auto t1 = moveFront(store[]);
auto t2 = moveBack(store[]);
store.front = move(t2);
store.back = move(t1);
percolateDown(store, 0, store.length - 1);
}
/*private*/ static void swap(Store _store, size_t i, size_t j)
{
static if (is(typeof(swap(_store[i], _store[j]))))
{
swap(_store[i], _store[j]);
}
else static if (is(typeof(_store.moveAt(i))))
{
auto t1 = _store.moveAt(i);
auto t2 = _store.moveAt(j);
_store[i] = move(t2);
_store[j] = move(t1);
}
else // assume it's a container and access its range with []
{
auto t1 = _store[].moveAt(i);
auto t2 = _store[].moveAt(j);
_store[i] = move(t2);
_store[j] = move(t1);
}
}
public:
/**
Converts the store $(D s) into a heap. If $(D initialSize) is
specified, only the first $(D initialSize) elements in $(D s)
are transformed into a heap, after which the heap can grow up
to $(D r.length) (if $(D Store) is a range) or indefinitely (if
$(D Store) is a container with $(D insertBack)). Performs
$(BIGOH min(r.length, initialSize)) evaluations of $(D less).
*/
this(Store s, size_t initialSize = size_t.max)
{
acquire(s, initialSize);
}
/**
Takes ownership of a store. After this, manipulating $(D s) may make
the heap work incorrectly.
*/
void acquire(Store s, size_t initialSize = size_t.max)
{
_payload.refCountedStore.ensureInitialized();
_store = move(s);
_length = min(_store.length, initialSize);
if (_length < 2) return;
for (auto i = (_length - 2) / 2; ; )
{
this.percolateDown(_store, i, _length);
if (i-- == 0) break;
}
assertValid();
}
/**
Takes ownership of a store assuming it already was organized as a
heap.
*/
void assume(Store s, size_t initialSize = size_t.max)
{
_payload.refCountedStore.ensureInitialized();
_store = s;
_length = min(_store.length, initialSize);
assertValid();
}
/**
Clears the heap. Returns the portion of the store from $(D 0) up to
$(D length), which satisfies the $(LUCKY heap property).
*/
auto release()
{
if (!_payload.refCountedStore.isInitialized)
{
return typeof(_store[0 .. _length]).init;
}
assertValid();
auto result = _store[0 .. _length];
_payload = _payload.init;
return result;
}
/**
Returns $(D true) if the heap is _empty, $(D false) otherwise.
*/
@property bool empty()
{
return !length;
}
/**
Returns a duplicate of the heap. The underlying store must also
support a $(D dup) method.
*/
@property BinaryHeap dup()
{
BinaryHeap result;
if (!_payload.refCountedStore.isInitialized) return result;
result.assume(_store.dup, length);
return result;
}
/**
Returns the _length of the heap.
*/
@property size_t length()
{
return _payload.refCountedStore.isInitialized ? _length : 0;
}
/**
Returns the _capacity of the heap, which is the length of the
underlying store (if the store is a range) or the _capacity of the
underlying store (if the store is a container).
*/
@property size_t capacity()
{
if (!_payload.refCountedStore.isInitialized) return 0;
static if (is(typeof(_store.capacity) : size_t))
{
return _store.capacity;
}
else
{
return _store.length;
}
}
/**
Returns a copy of the _front of the heap, which is the largest element
according to $(D less).
*/
@property ElementType!Store front()
{
enforce(!empty, "Cannot call front on an empty heap.");
return _store.front;
}
/**
Clears the heap by detaching it from the underlying store.
*/
void clear()
{
_payload = _payload.init;
}
/**
Inserts $(D value) into the store. If the underlying store is a range
and $(D length == capacity), throws an exception.
*/
size_t insert(ElementType!Store value)
{
static if (is(typeof(_store.insertBack(value))))
{
_payload.refCountedStore.ensureInitialized();
if (length == _store.length)
{
// reallocate
_store.insertBack(value);
}
else
{
// no reallocation
_store[_length] = value;
}
}
else
{
// can't grow
enforce(length < _store.length,
"Cannot grow a heap created over a range");
_store[_length] = value;
}
// sink down the element
for (size_t n = _length; n; )
{
auto parentIdx = (n - 1) / 2;
if (!comp(_store[parentIdx], _store[n])) break; // done!
// must swap and continue
swap(_store, parentIdx, n);
n = parentIdx;
}
++_length;
debug(BinaryHeap) assertValid();
return 1;
}
/**
Removes the largest element from the heap.
*/
void removeFront()
{
enforce(!empty, "Cannot call removeFront on an empty heap.");
if (_length > 1)
{
auto t1 = moveFront(_store[]);
auto t2 = moveAt(_store[], _length - 1);
_store.front = move(t2);
_store[_length - 1] = move(t1);
}
--_length;
percolateDown(_store, 0, _length);
}
/// ditto
alias popFront = removeFront;
/**
Removes the largest element from the heap and returns a copy of
it. The element still resides in the heap's store. For performance
reasons you may want to use $(D removeFront) with heaps of objects
that are expensive to copy.
*/
ElementType!Store removeAny()
{
removeFront();
return _store[_length];
}
/**
Replaces the largest element in the store with $(D value).
*/
void replaceFront(ElementType!Store value)
{
// must replace the top
assert(!empty, "Cannot call replaceFront on an empty heap.");
_store.front = value;
percolateDown(_store, 0, _length);
debug(BinaryHeap) assertValid();
}
/**
If the heap has room to grow, inserts $(D value) into the store and
returns $(D true). Otherwise, if $(D less(value, front)), calls $(D
replaceFront(value)) and returns again $(D true). Otherwise, leaves
the heap unaffected and returns $(D false). This method is useful in
scenarios where the smallest $(D k) elements of a set of candidates
must be collected.
*/
bool conditionalInsert(ElementType!Store value)
{
_payload.refCountedStore.ensureInitialized();
if (_length < _store.length)
{
insert(value);
return true;
}
// must replace the top
assert(!_store.empty, "Cannot replace front of an empty heap.");
if (!comp(value, _store.front)) return false; // value >= largest
_store.front = value;
percolateDown(_store, 0, _length);
debug(BinaryHeap) assertValid();
return true;
}
}
/// Example from "Introduction to Algorithms" Cormen et al, p 146
unittest
{
import std.algorithm : equal;
int[] a = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ];
auto h = heapify(a);
// largest element
assert(h.front == 16);
// a has the heap property
assert(equal(a, [ 16, 14, 10, 8, 7, 9, 3, 2, 4, 1 ]));
}
/// $(D BinaryHeap) implements the standard input range interface, allowing
/// lazy iteration of the underlying range in descending order.
unittest
{
import std.algorithm : equal;
import std.range : take;
int[] a = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7];
auto top5 = heapify(a).take(5);
assert(top5.equal([16, 14, 10, 9, 8]));
}
/**
Convenience function that returns a $(D BinaryHeap!Store) object
initialized with $(D s) and $(D initialSize).
*/
BinaryHeap!(Store, less) heapify(alias less = "a < b", Store)(Store s,
size_t initialSize = size_t.max)
{
return BinaryHeap!(Store, less)(s, initialSize);
}
unittest
{
import std.conv : to;
{
// example from "Introduction to Algorithms" Cormen et al., p 146
int[] a = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ];
auto h = heapify(a);
h = heapify!"a < b"(a);
assert(h.front == 16);
assert(a == [ 16, 14, 10, 8, 7, 9, 3, 2, 4, 1 ]);
auto witness = [ 16, 14, 10, 9, 8, 7, 4, 3, 2, 1 ];
for (; !h.empty; h.removeFront(), witness.popFront())
{
assert(!witness.empty);
assert(witness.front == h.front);
}
assert(witness.empty);
}
{
int[] a = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ];
int[] b = new int[a.length];
BinaryHeap!(int[]) h = BinaryHeap!(int[])(b, 0);
foreach (e; a)
{
h.insert(e);
}
assert(b == [ 16, 14, 10, 8, 7, 3, 9, 1, 4, 2 ], to!string(b));
}
}
unittest
{
// Test range interface.
import std.algorithm : equal;
int[] a = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7];
auto h = heapify(a);
static assert(isInputRange!(typeof(h)));
assert(h.equal([16, 14, 10, 9, 8, 7, 4, 3, 2, 1]));
}
|