BinaryHeap as member
Era Scarecrow
rtcvb32 at yahoo.com
Tue Nov 14 04:13:16 UTC 2017
On Monday, 13 November 2017 at 16:26:20 UTC, balddenimhero wrote:
> In the course of writing a minimal example I removed more than
> necessary in the previous pastebin (the passed IntOrder has not
> even been used). Thus here is the corrected one:
> https://pastebin.com/SKae08GT. I'm trying to port this to D.
Throwing together a sample involves wrapping the value in a new
value. Still the idea is put across...
Not sure if this is the best way to do this, but only takes a
little dereferencing to access the value.
Compiled w/DMD v2.069.2
[code]
import std.container.binaryheap;
import std.range : iota;
import std.array;
import std.stdio;
void main()
{
int len = 10;
int[] a = iota(len).array;
auto foo = new WeightedHeap!int([0,2,4,6,8], a);
foreach(v; foo.h)
writeln(v.weight, "\t", *v.v);
}
struct WeightedHeap(T) {
this(int[] order, T[] arr) {
foreach(i, ref v; arr) {
a ~= E(order[i%$], &v);
}
h = BinaryHeap!(E[])(a);
}
E[] a;
BinaryHeap!(E[]) h;
// alias h this;
static struct E {
int weight;
T* v;
// alias v this;
int opCmp(E a) const {
return a.weight-weight;
}
}
}
[/code]
Output:
Weight Value
0 5
0 0
2 1
2 6
4 7
4 2
6 3
6 8
8 4
8 9
More information about the Digitalmars-d-learn
mailing list