diff --git a/std/container/binaryheap.d b/std/container/binaryheap.d index a2a5195a217..91dbf6ba752 100644 --- a/std/container/binaryheap.d +++ b/std/container/binaryheap.d @@ -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; + } } /** @@ -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, () + { + auto s = StructWithDup([1, 2]); + auto h = heapify(s); + h.dup(); + })); +}