New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@thi.ng/heaps

Package Overview
Dependencies
Maintainers
1
Versions
191
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thi.ng/heaps - npm Package Compare versions

Comparing version 1.1.6 to 1.2.0

pairing.d.ts

1

api.d.ts

@@ -8,1 +8,2 @@ import { Comparator } from "@thi.ng/api";

}
//# sourceMappingURL=api.d.ts.map

@@ -6,2 +6,18 @@ # Change Log

# [1.2.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/heaps@1.1.6...@thi.ng/heaps@1.2.0) (2020-01-24)
### Features
* **heaps:** add PairingHeap ([748da44](https://github.com/thi-ng/umbrella/commit/748da4405f9b4ab49bbdb3d4b49131df1f0cae88))
### Performance Improvements
* **heap:** add benchmarks ([2208353](https://github.com/thi-ng/umbrella/commit/220835345b1e842950a7288a8cc618585fda593f))
## [1.1.6](https://github.com/thi-ng/umbrella/compare/@thi.ng/heaps@1.1.5...@thi.ng/heaps@1.1.6) (2019-11-30)

@@ -8,0 +24,0 @@

22

dheap.d.ts

@@ -6,8 +6,11 @@ import { ICopy, IEmpty, IStack } from "@thi.ng/api";

* Generic d-ary heap / priority queue with configurable arity (default
* = 4) and ordering via user-supplied comparator. By default,
* implements min-heap ordering and uses @thi.ng/compare. The arity
* `d` must be >= 2 (default: 4). If `d=2`, the default binary `Heap` implementation
* will be faster.
* = 4) and ordering via user-supplied comparator.
*
* https://en.wikipedia.org/wiki/D-ary_heap
* @remarks
* By default, implements min-heap ordering and uses
* {@link @thi.ng/compare#compare}. The arity `d` must be >= 2 (default:
* 4). If `d=2`, the default binary {@link Heap} implementation will be
* faster.
*
* {@link https://en.wikipedia.org/wiki/D-ary_heap }
*/

@@ -18,4 +21,4 @@ export declare class DHeap<T> extends Heap<T> implements ICopy<DHeap<T>>, IEmpty<DHeap<T>>, IStack<T, T, DHeap<T>> {

*
* @param idx
* @param d
* @param idx - index
* @param d - branch factor
*/

@@ -26,4 +29,4 @@ static parentIndex(idx: number, d?: number): number;

*
* @param idx
* @param d
* @param idx - index
* @param d - branch factor
*/

@@ -42,1 +45,2 @@ static childIndex(idx: number, d?: number): number;

}
//# sourceMappingURL=dheap.d.ts.map

@@ -5,8 +5,11 @@ import { compare } from "@thi.ng/compare";

* Generic d-ary heap / priority queue with configurable arity (default
* = 4) and ordering via user-supplied comparator. By default,
* implements min-heap ordering and uses @thi.ng/compare. The arity
* `d` must be >= 2 (default: 4). If `d=2`, the default binary `Heap` implementation
* will be faster.
* = 4) and ordering via user-supplied comparator.
*
* https://en.wikipedia.org/wiki/D-ary_heap
* @remarks
* By default, implements min-heap ordering and uses
* {@link @thi.ng/compare#compare}. The arity `d` must be >= 2 (default:
* 4). If `d=2`, the default binary {@link Heap} implementation will be
* faster.
*
* {@link https://en.wikipedia.org/wiki/D-ary_heap }
*/

@@ -25,4 +28,4 @@ export class DHeap extends Heap {

*
* @param idx
* @param d
* @param idx - index
* @param d - branch factor
*/

@@ -35,4 +38,4 @@ static parentIndex(idx, d = 4) {

*
* @param idx
* @param d
* @param idx - index
* @param d - branch factor
*/

@@ -39,0 +42,0 @@ static childIndex(idx, d = 4) {

@@ -1,2 +0,2 @@

import { Comparator, ICopy, IEmpty, ILength, IStack } from "@thi.ng/api";
import { Comparator, IClear, ICopy, IEmpty, ILength, IStack } from "@thi.ng/api";
import { HeapOpts } from "./api";

@@ -8,3 +8,4 @@ /**

*
* ```
* @example
* ```ts
* h = new Heap([20, 5, 10]);

@@ -20,3 +21,3 @@ * h.push(15);

*/
export declare class Heap<T> implements ICopy<Heap<T>>, IEmpty<Heap<T>>, ILength, IStack<T, T, Heap<T>> {
export declare class Heap<T> implements Iterable<T>, IClear, ICopy<Heap<T>>, IEmpty<Heap<T>>, ILength, IStack<T, T, Heap<T>> {
static parentIndex(idx: number): number;

@@ -38,7 +39,7 @@ static childIndex(idx: number): number;

/**
* Calls `pushPop()` for each given value in `vals` and returns last
* result (i.e. the smallest value in heap after processing all
* `vals`).
* Calls {@link Heap.pushPop} for each given value in `vals` and
* returns last result (i.e. the smallest value in heap after
* processing all `vals`).
*
* @param vals
* @param vals - values to insert
*/

@@ -52,3 +53,3 @@ pushPopAll(vals: Iterable<T>): T;

*
* @param n
* @param n - number of values
*/

@@ -60,3 +61,3 @@ max(n?: number): T[];

*
* @param n
* @param n - number of values
*/

@@ -70,1 +71,2 @@ min(n?: number): T[];

}
//# sourceMappingURL=heap.d.ts.map

@@ -7,3 +7,4 @@ import { compare } from "@thi.ng/compare";

*
* ```
* @example
* ```ts
* h = new Heap([20, 5, 10]);

@@ -89,7 +90,7 @@ * h.push(15);

/**
* Calls `pushPop()` for each given value in `vals` and returns last
* result (i.e. the smallest value in heap after processing all
* `vals`).
* Calls {@link Heap.pushPop} for each given value in `vals` and
* returns last result (i.e. the smallest value in heap after
* processing all `vals`).
*
* @param vals
* @param vals - values to insert
*/

@@ -118,3 +119,3 @@ pushPopAll(vals) {

*
* @param n
* @param n - number of values
*/

@@ -138,3 +139,3 @@ max(n = this.values.length) {

*
* @param n
* @param n - number of values
*/

@@ -141,0 +142,0 @@ min(n = this.values.length) {

export * from "./api";
export * from "./heap";
export * from "./dheap";
export * from "./heap";
export * from "./pairing";
//# sourceMappingURL=index.d.ts.map

@@ -0,2 +1,3 @@

export * from "./heap";
export * from "./dheap";
export * from "./heap";
export * from "./pairing";

@@ -281,3 +281,127 @@ 'use strict';

class PairingHeap {
constructor(vals, opts) {
opts = Object.assign({ compare: compare.compare }, opts);
this.compare = opts.compare;
this.clear();
vals && this.into(vals);
}
get length() {
return this._size;
}
*[Symbol.iterator]() {
const acc = [];
this.visit((x) => (acc.push(x), true));
yield* acc;
}
clear() {
this.root = { v: undefined, p: undefined, c: [] };
this._size = 0;
}
empty() {
return new PairingHeap(undefined, { compare: compare.compare });
}
copy() {
const heap = this.empty();
return heap;
}
push(v) {
this.root = this.merge(this.root, {
v,
p: undefined,
c: []
});
this._size++;
return this;
}
pop() {
const res = this.root.v;
if (res !== undefined) {
const children = this.root.c;
if (children.length) {
this.root = this.mergePairs(children);
}
else {
this.root.v = undefined;
}
this._size--;
}
return res;
}
pushPop(x) {
if (!this._size || this.compare(x, this.root.v) < 0) {
return x;
}
const res = this.pop();
this.push(x);
return res;
}
peek() {
return this._size ? this.root.v : undefined;
}
into(vals) {
for (let i of vals) {
this.push(i);
}
return this;
}
meld(heap) {
if (!heap._size)
return this;
if (!this._size) {
this.root = heap.root;
this._size = heap._size;
}
else {
this._size += heap._size;
this.root =
this.compare(this.peek(), heap.peek()) <= 0
? this.merge(this.root, heap.root)
: this.merge(heap.root, this.root);
}
heap.clear();
return this;
}
visit(fn) {
this._size && this.doVisit(fn, this.root);
}
doVisit(fn, root) {
if (fn(root.v)) {
const children = root.c;
let ok = true;
for (let i = children.length; ok && --i >= 0;) {
ok = this.doVisit(fn, children[i]);
}
return ok;
}
return false;
}
merge(a, b) {
if (a.v === undefined) {
return b;
}
if (this.compare(a.v, b.v) < 0) {
a.c.push(b);
b.p = a;
return a;
}
b.c.push(a);
a.p = b;
return b;
}
mergePairs(heaps) {
const n = heaps.length - 1;
let root = heaps[n];
if (n > 0) {
for (let i = n; --i >= 0;) {
root = this.merge(root, heaps[i]);
}
}
root.p = undefined;
return root;
}
}
exports.DHeap = DHeap;
exports.Heap = Heap;
exports.PairingHeap = PairingHeap;

@@ -1,1 +0,1 @@

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@thi.ng/compare")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/compare"],t):t(((e=e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.heaps={}),e.thi.ng.compare)}(this,(function(e,t){"use strict";class s{constructor(e,s){s=Object.assign({compare:t.compare},s),this.compare=s.compare,this.values=[],e&&this.into(e)}static parentIndex(e){return e>0?e-1>>1:-1}static childIndex(e){return e>=0?1+(e<<1):-1}*[Symbol.iterator](){yield*this.min()}get length(){return this.values.length}copy(){const e=this.empty();return e.values=this.values.slice(),e}clear(){this.values.length=0}empty(){return new s(null,{compare:this.compare})}peek(){return this.values[0]}push(e){return this.values.push(e),this.percolateUp(this.values.length-1),this}pop(){const e=this.values,t=e.pop();let s;return e.length>0?(s=e[0],e[0]=t,this.percolateDown(0)):s=t,s}pushPop(e,t=this.values){const s=t[0];return t.length>0&&this.compare(s,e)<0&&(t[0]=e,e=s,this.percolateDown(0,t)),e}into(e){for(let t of e)this.push(t);return this}pushPopAll(e){let t;for(let s of e)t=this.pushPop(s);return t}replaceHead(e){const t=this.values[0];return this.values[0]=e,this.percolateDown(0),t}heapify(e=this.values){for(var t=e.length-1>>1;t>=0;t--)this.percolateDown(t,e)}max(e=this.values.length){const t=this.values,s=this.compare,r=t.slice(0,e);if(!e)return r;this.heapify(r);for(let s=t.length;e<s;e++)this.pushPop(t[e],r);return r.sort((e,t)=>s(t,e))}min(e=this.values.length){const t=this.values,s=this.compare,n=t.slice(0,e).sort(s);if(!e)return n;let i,h=n[e-1];for(let o=e,l=t.length;o<l;o++)s(i=t[o],h)<0&&(n.splice(r(i,n,0,e,s),0,i),n.pop(),h=n[e-1]);return n}parent(e){return(e=s.parentIndex(e))>=0?this.values[e]:void 0}children(e){e=s.childIndex(e);const t=this.values,r=t.length;if(!(e>=r))return e===r-1?[t[e]]:[t[e],t[e+1]]}leaves(){const e=this.values;return e.length?e.slice(s.parentIndex(e.length-1)+1):[]}percolateUp(e,t=this.values){const s=t[e],r=this.compare;for(;e>0;){const n=e-1>>1,i=t[n];if(r(s,i)>=0)break;t[n]=s,t[e]=i,e=n}}percolateDown(e,t=this.values){const s=t.length,r=t[e],n=this.compare;let i=1+(e<<1);for(;i<s;){const h=i+1;if(h<s&&n(t[i],t[h])>=0&&(i=h),!(n(t[i],r)<0))break;t[e]=t[i],i=1+((e=i)<<1)}t[e]=r}}const r=(e,t,s,r,n)=>{let i;for(;s<r;)n(e,t[i=s+r>>>1])<0?r=i:s=i+1;return s};class n extends s{constructor(e,s){super(void 0,Object.assign({compare:t.compare},s)),this.d=s&&s.d||4,this.values=[],e&&this.into(e)}static parentIndex(e,t=4){return e>0?(e-1)/t|0:-1}static childIndex(e,t=4){return e>=0?e*t+1:-1}copy(){return super.copy()}empty(){return new n(null,{compare:this.compare,d:this.d})}parent(e){return(e=n.parentIndex(e,this.d))>=0?this.values[e]:void 0}children(e){e=n.childIndex(e,this.d);const t=this.values;if(!(e>=t.length))return t.slice(e,e+this.d)}leaves(){const e=this.values;return e.length?e.slice(n.parentIndex(e.length-1,this.d)+1):[]}heapify(e=this.values){for(var t=(e.length-1)/this.d|0;t>=0;t--)this.percolateDown(t,e)}percolateUp(e,t=this.values){const s=t[e],r=this.d,n=this.compare;for(;e>0;){const i=(e-1)/r|0,h=t[i];if(n(s,h)>=0)break;t[i]=s,t[e]=h,e=i}}percolateDown(e,t=this.values){const s=t.length,r=this.d,n=t[e],i=this.compare;let h,o=e*r+1;for(;o<s;){h=o;for(let e=o+1,n=o+r;e<n;e++)e<s&&i(t[e],t[h])<0&&(h=e);if(!(i(t[h],n)<0))break;t[e]=t[h],o=(e=h)*r+1}t[e]=n}}e.DHeap=n,e.Heap=s,Object.defineProperty(e,"__esModule",{value:!0})}));
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@thi.ng/compare")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/compare"],e):e(((t=t||self).thi=t.thi||{},t.thi.ng=t.thi.ng||{},t.thi.ng.heaps={}),t.thi.ng.compare)}(this,(function(t,e){"use strict";class s{constructor(t,s){s=Object.assign({compare:e.compare},s),this.compare=s.compare,this.values=[],t&&this.into(t)}static parentIndex(t){return t>0?t-1>>1:-1}static childIndex(t){return t>=0?1+(t<<1):-1}*[Symbol.iterator](){yield*this.min()}get length(){return this.values.length}copy(){const t=this.empty();return t.values=this.values.slice(),t}clear(){this.values.length=0}empty(){return new s(null,{compare:this.compare})}peek(){return this.values[0]}push(t){return this.values.push(t),this.percolateUp(this.values.length-1),this}pop(){const t=this.values,e=t.pop();let s;return t.length>0?(s=t[0],t[0]=e,this.percolateDown(0)):s=e,s}pushPop(t,e=this.values){const s=e[0];return e.length>0&&this.compare(s,t)<0&&(e[0]=t,t=s,this.percolateDown(0,e)),t}into(t){for(let e of t)this.push(e);return this}pushPopAll(t){let e;for(let s of t)e=this.pushPop(s);return e}replaceHead(t){const e=this.values[0];return this.values[0]=t,this.percolateDown(0),e}heapify(t=this.values){for(var e=t.length-1>>1;e>=0;e--)this.percolateDown(e,t)}max(t=this.values.length){const e=this.values,s=this.compare,i=e.slice(0,t);if(!t)return i;this.heapify(i);for(let s=e.length;t<s;t++)this.pushPop(e[t],i);return i.sort((t,e)=>s(e,t))}min(t=this.values.length){const e=this.values,s=this.compare,r=e.slice(0,t).sort(s);if(!t)return r;let o,h=r[t-1];for(let n=t,l=e.length;n<l;n++)o=e[n],s(o,h)<0&&(r.splice(i(o,r,0,t,s),0,o),r.pop(),h=r[t-1]);return r}parent(t){return(t=s.parentIndex(t))>=0?this.values[t]:void 0}children(t){t=s.childIndex(t);const e=this.values,i=e.length;if(!(t>=i))return t===i-1?[e[t]]:[e[t],e[t+1]]}leaves(){const t=this.values;return t.length?t.slice(s.parentIndex(t.length-1)+1):[]}percolateUp(t,e=this.values){const s=e[t],i=this.compare;for(;t>0;){const r=t-1>>1,o=e[r];if(i(s,o)>=0)break;e[r]=s,e[t]=o,t=r}}percolateDown(t,e=this.values){const s=e.length,i=e[t],r=this.compare;let o=1+(t<<1);for(;o<s;){const h=o+1;if(h<s&&r(e[o],e[h])>=0&&(o=h),!(r(e[o],i)<0))break;e[t]=e[o],o=1+((t=o)<<1)}e[t]=i}}const i=(t,e,s,i,r)=>{let o;for(;s<i;)o=s+i>>>1,r(t,e[o])<0?i=o:s=o+1;return s};class r extends s{constructor(t,s){super(void 0,Object.assign({compare:e.compare},s)),this.d=s&&s.d||4,this.values=[],t&&this.into(t)}static parentIndex(t,e=4){return t>0?(t-1)/e|0:-1}static childIndex(t,e=4){return t>=0?t*e+1:-1}copy(){return super.copy()}empty(){return new r(null,{compare:this.compare,d:this.d})}parent(t){return(t=r.parentIndex(t,this.d))>=0?this.values[t]:void 0}children(t){t=r.childIndex(t,this.d);const e=this.values;if(!(t>=e.length))return e.slice(t,t+this.d)}leaves(){const t=this.values;return t.length?t.slice(r.parentIndex(t.length-1,this.d)+1):[]}heapify(t=this.values){for(var e=(t.length-1)/this.d|0;e>=0;e--)this.percolateDown(e,t)}percolateUp(t,e=this.values){const s=e[t],i=this.d,r=this.compare;for(;t>0;){const o=(t-1)/i|0,h=e[o];if(r(s,h)>=0)break;e[o]=s,e[t]=h,t=o}}percolateDown(t,e=this.values){const s=e.length,i=this.d,r=e[t],o=this.compare;let h,n=t*i+1;for(;n<s;){h=n;for(let t=n+1,r=n+i;t<r;t++)t<s&&o(e[t],e[h])<0&&(h=t);if(!(o(e[h],r)<0))break;e[t]=e[h],n=(t=h)*i+1}e[t]=r}}class o{constructor(t,s){s=Object.assign({compare:e.compare},s),this.compare=s.compare,this.clear(),t&&this.into(t)}get length(){return this._size}*[Symbol.iterator](){const t=[];this.visit(e=>(t.push(e),!0)),yield*t}clear(){this.root={v:void 0,p:void 0,c:[]},this._size=0}empty(){return new o(void 0,{compare:e.compare})}copy(){return this.empty()}push(t){return this.root=this.merge(this.root,{v:t,p:void 0,c:[]}),this._size++,this}pop(){const t=this.root.v;if(void 0!==t){const t=this.root.c;t.length?this.root=this.mergePairs(t):this.root.v=void 0,this._size--}return t}pushPop(t){if(!this._size||this.compare(t,this.root.v)<0)return t;const e=this.pop();return this.push(t),e}peek(){return this._size?this.root.v:void 0}into(t){for(let e of t)this.push(e);return this}meld(t){return t._size?(this._size?(this._size+=t._size,this.root=this.compare(this.peek(),t.peek())<=0?this.merge(this.root,t.root):this.merge(t.root,this.root)):(this.root=t.root,this._size=t._size),t.clear(),this):this}visit(t){this._size&&this.doVisit(t,this.root)}doVisit(t,e){if(t(e.v)){const s=e.c;let i=!0;for(let e=s.length;i&&--e>=0;)i=this.doVisit(t,s[e]);return i}return!1}merge(t,e){return void 0===t.v?e:this.compare(t.v,e.v)<0?(t.c.push(e),e.p=t,t):(e.c.push(t),t.p=e,e)}mergePairs(t){const e=t.length-1;let s=t[e];if(e>0)for(let i=e;--i>=0;)s=this.merge(s,t[i]);return s.p=void 0,s}}t.DHeap=r,t.Heap=s,t.PairingHeap=o,Object.defineProperty(t,"__esModule",{value:!0})}));
{
"name": "@thi.ng/heaps",
"version": "1.1.6",
"description": "Generic binary heap & d-ary heap implementations with customizable ordering",
"version": "1.2.0",
"description": "Various heap implementations for arbitrary values and with customizable ordering",
"module": "./index.js",

@@ -21,2 +21,3 @@ "main": "./lib/index.js",

"build:test": "rimraf build && tsc -p test/tsconfig.json",
"bench": "ts-node -P bench/tsconfig.json bench/index.ts",
"test": "mocha test",

@@ -27,17 +28,19 @@ "cover": "nyc mocha test && nyc report --reporter=lcov",

"doc": "node_modules/.bin/typedoc --mode modules --out doc src",
"doc:ae": "mkdir -p .ae/doc .ae/temp && node_modules/.bin/api-extractor run --local --verbose",
"pub": "yarn build:release && yarn publish --access public"
},
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^0.1.3",
"@types/mocha": "^5.2.6",
"@types/node": "^12.12.11",
"mocha": "^6.2.2",
"nyc": "^14.0.0",
"ts-node": "^8.5.2",
"typedoc": "^0.15.2",
"typescript": "^3.7.2"
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@microsoft/api-extractor": "^7.7.7",
"@types/mocha": "^5.2.7",
"@types/node": "^13.5.0",
"mocha": "^7.0.0",
"nyc": "^15.0.0",
"ts-node": "^8.6.2",
"typedoc": "^0.16.8",
"typescript": "^3.7.5"
},
"dependencies": {
"@thi.ng/api": "^6.6.0",
"@thi.ng/compare": "^1.1.0"
"@thi.ng/api": "^6.7.0",
"@thi.ng/compare": "^1.1.1"
},

@@ -49,2 +52,3 @@ "keywords": [

"heap",
"pairing heap",
"priority queue",

@@ -61,3 +65,3 @@ "sort",

},
"gitHead": "36c4d9e967bd80ccdbfa0f4a42f594080f95f105"
"gitHead": "93d8af817724c1c5b06d80ffa2492fe5b4fb7bc4"
}

@@ -22,3 +22,3 @@ <!-- This file is generated - DO NOT EDIT! -->

Generic binary heap & d-ary heap implementations with customizable ordering.
Various heap implementations for arbitrary values and with customizable ordering.

@@ -40,6 +40,8 @@ Type agnostic binary heap & d-ary heap implementations with customizable

Package sizes (gzipped): ESM: 1.5KB / CJS: 1.6KB / UMD: 1.7KB
## Dependencies
- [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/master/packages/api)
- [@thi.ng/compare](https://github.com/thi-ng/umbrella/tree/master/packages/compare)
- [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api)
- [@thi.ng/compare](https://github.com/thi-ng/umbrella/tree/develop/packages/compare)

@@ -83,2 +85,2 @@ ## API

&copy; 2017 - 2019 Karsten Schmidt // Apache Software License 2.0
&copy; 2017 - 2020 Karsten Schmidt // Apache Software License 2.0

@@ -35,2 +35,4 @@ # ${pkg.name}

${pkg.size}
## Dependencies

@@ -37,0 +39,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc