Fix for instantiating BinaryHeap!(Array!int))

This commit is contained in:
Xinok 2015-07-08 15:10:11 -05:00
parent d52fb87ce7
commit 08f0b242d5
2 changed files with 13 additions and 9 deletions

View file

@ -1185,7 +1185,7 @@ package(std) template HeapOps(alias less, Range)
if(r.length < 2) return; if(r.length < 2) return;
// Build Heap // Build Heap
heapify(r); buildHeap(r);
// Sort // Sort
size_t i = r.length - 1; size_t i = r.length - 1;
@ -1198,7 +1198,7 @@ package(std) template HeapOps(alias less, Range)
} }
//template because of @@@12410@@@ //template because of @@@12410@@@
void heapify()(Range r) void buildHeap()(Range r)
{ {
size_t i = r.length / 2; size_t i = r.length / 2;
while(i > 0) percolate(r, --i, r.length); while(i > 0) percolate(r, --i, r.length);

View file

@ -62,8 +62,12 @@ if (isRandomAccessRange!(Store) || isRandomAccessRange!(typeof(Store.init[])))
import std.algorithm : move, min, HeapOps, swapAt; import std.algorithm : move, min, HeapOps, swapAt;
import std.typecons : RefCounted, RefCountedAutoInitialize; import std.typecons : RefCounted, RefCountedAutoInitialize;
alias percolate = HeapOps!(less, Store).percolate; static if(isRandomAccessRange!Store)
alias heapify = HeapOps!(less, Store).heapify; alias Range = Store;
else
alias Range = typeof(Store.init[]);
alias percolate = HeapOps!(less, Range).percolate;
alias buildHeap = HeapOps!(less, Range).buildHeap;
// Really weird @@BUG@@: if you comment out the "private:" label below, // Really weird @@BUG@@: if you comment out the "private:" label below,
// std.algorithm can't unittest anymore // std.algorithm can't unittest anymore
@ -115,7 +119,7 @@ if (isRandomAccessRange!(Store) || isRandomAccessRange!(typeof(Store.init[])))
auto t2 = moveBack(store[]); auto t2 = moveBack(store[]);
store.front = move(t2); store.front = move(t2);
store.back = move(t1); store.back = move(t1);
percolate(store, 0, store.length - 1); percolate(store[], 0, store.length - 1);
} }
public: public:
@ -143,7 +147,7 @@ the heap work incorrectly.
_store = move(s); _store = move(s);
_length = min(_store.length, initialSize); _length = min(_store.length, initialSize);
if (_length < 2) return; if (_length < 2) return;
heapify(s); buildHeap(s[]);
assertValid(); assertValid();
} }
@ -295,7 +299,7 @@ Removes the largest element from the heap.
_store[_length - 1] = move(t1); _store[_length - 1] = move(t1);
} }
--_length; --_length;
percolate(_store, 0, _length); percolate(_store[], 0, _length);
} }
/// ditto /// ditto
@ -321,7 +325,7 @@ Replaces the largest element in the store with $(D value).
// must replace the top // must replace the top
assert(!empty, "Cannot call replaceFront on an empty heap."); assert(!empty, "Cannot call replaceFront on an empty heap.");
_store.front = value; _store.front = value;
percolate(_store, 0, _length); percolate(_store[], 0, _length);
debug(BinaryHeap) assertValid(); debug(BinaryHeap) assertValid();
} }
@ -345,7 +349,7 @@ must be collected.
assert(!_store.empty, "Cannot replace front of an empty heap."); assert(!_store.empty, "Cannot replace front of an empty heap.");
if (!comp(value, _store.front)) return false; // value >= largest if (!comp(value, _store.front)) return false; // value >= largest
_store.front = value; _store.front = value;
percolate(_store, 0, _length); percolate(_store[], 0, _length);
debug(BinaryHeap) assertValid(); debug(BinaryHeap) assertValid();
return true; return true;
} }