Skip to content

Commit

Permalink
fix(heaps): add DHeap ICopy/IEmpty impls, fix return types
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 22, 2018
1 parent c4bbee0 commit 5894572
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/heaps/src/dheap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { compare } from "@thi.ng/api/compare";

import { DHeapOpts } from "./api";
import { Heap } from "./heap";
import { ICopy, IEmpty } from "@thi.ng/api/api";

/**
* Generic d-ary heap / priority queue with configurable arity (default
Expand All @@ -12,7 +13,9 @@ import { Heap } from "./heap";
*
* https://en.wikipedia.org/wiki/D-ary_heap
*/
export class DHeap<T> extends Heap<T> {
export class DHeap<T> extends Heap<T> implements
ICopy<DHeap<T>>,
IEmpty<DHeap<T>> {

/**
* Returns index of parent node or -1 if `idx < 1`.
Expand Down Expand Up @@ -45,6 +48,14 @@ export class DHeap<T> extends Heap<T> {
}
}

copy() {
return <DHeap<T>>super.copy();
}

empty() {
return new DHeap<T>(null, { compare: this.compare, d: this.d });
}

parent(n: number) {
n = DHeap.parentIndex(n, this.d);
return n >= 0 ? this.values[n] : undefined;
Expand Down

0 comments on commit 5894572

Please sign in to comment.