Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 71 additions & 7 deletions std/container/binaryheap.d
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,18 @@ Returns $(D true) if the heap is _empty, $(D false) otherwise.
}

/**
Returns a duplicate of the heap. The underlying store must also
support a $(D dup) method.
Returns a duplicate of the heap. The $(D dup) method is available only if the
underlying store supports it.
*/
@property BinaryHeap dup()
static if (is(typeof((Store s) { return s.dup; }(Store.init)) == Store))
{
BinaryHeap result;
if (!_payload.refCountedStore.isInitialized) return result;
result.assume(_store.dup, length);
return result;
@property BinaryHeap dup()
{
BinaryHeap result;
if (!_payload.refCountedStore.isInitialized) return result;
result.assume(_store.dup, length);
return result;
}
}

/**
Expand Down Expand Up @@ -473,3 +476,64 @@ unittest // 16072

assert(r.front == 99);
}

unittest
{
import std.algorithm.comparison : equal;
int[] a = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7];
auto heap = heapify(a);
auto dup = heap.dup();
assert(dup.equal([16, 14, 10, 9, 8, 7, 4, 3, 2, 1]));
}

unittest
{
static struct StructWithoutDup
{
int[] a;
@disable StructWithoutDup dup()
{
StructWithoutDup d;
return d;
}
alias a this;
}

// Assert Binary heap can be created when Store doesn't have dup
// if dup is not used.
assert(__traits(compiles, ()
{
auto s = StructWithoutDup([1,2]);
auto h = heapify(s);
}));

// Assert dup can't be used on BinaryHeaps when Store doesn't have dup
assert(!__traits(compiles, ()
{
auto s = StructWithoutDup([1,2]);
auto h = heapify(s);
h.dup();
}));
}

unittest
{
static struct StructWithDup
{
int[] a;
StructWithDup dup()
{
StructWithDup d;
return d;
}
alias a this;
}

// Assert dup can be used on BinaryHeaps when Store has dup
assert(__traits(compiles, ()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess here you could simply paste the code in the unittest instead of asserting it compiles.

{
auto s = StructWithDup([1, 2]);
auto h = heapify(s);
h.dup();
}));
}