mongodb-livedata-server 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/livedata_server.d.ts +4 -4
- package/dist/livedata_server.js +11 -11
- package/dist/meteor/binary-heap/max_heap.d.ts +31 -31
- package/dist/meteor/binary-heap/max_heap.js +186 -186
- package/dist/meteor/binary-heap/min_heap.d.ts +6 -6
- package/dist/meteor/binary-heap/min_heap.js +17 -17
- package/dist/meteor/binary-heap/min_max_heap.d.ts +11 -11
- package/dist/meteor/binary-heap/min_max_heap.js +48 -48
- package/dist/meteor/callback-hook/hook.d.ts +11 -11
- package/dist/meteor/callback-hook/hook.js +78 -78
- package/dist/meteor/ddp/crossbar.d.ts +15 -15
- package/dist/meteor/ddp/crossbar.js +136 -136
- package/dist/meteor/ddp/heartbeat.d.ts +19 -19
- package/dist/meteor/ddp/heartbeat.js +77 -77
- package/dist/meteor/ddp/livedata_server.d.ts +141 -142
- package/dist/meteor/ddp/livedata_server.js +403 -403
- package/dist/meteor/ddp/method-invocation.d.ts +35 -35
- package/dist/meteor/ddp/method-invocation.js +72 -72
- package/dist/meteor/ddp/random-stream.d.ts +8 -8
- package/dist/meteor/ddp/random-stream.js +100 -100
- package/dist/meteor/ddp/session-collection-view.d.ts +20 -20
- package/dist/meteor/ddp/session-collection-view.js +106 -106
- package/dist/meteor/ddp/session-document-view.d.ts +8 -8
- package/dist/meteor/ddp/session-document-view.js +82 -82
- package/dist/meteor/ddp/session.d.ts +75 -75
- package/dist/meteor/ddp/session.js +590 -590
- package/dist/meteor/ddp/stream_server.d.ts +20 -21
- package/dist/meteor/ddp/stream_server.js +181 -181
- package/dist/meteor/ddp/subscription.d.ts +94 -94
- package/dist/meteor/ddp/subscription.js +370 -370
- package/dist/meteor/ddp/utils.d.ts +8 -8
- package/dist/meteor/ddp/utils.js +104 -104
- package/dist/meteor/ddp/writefence.d.ts +20 -20
- package/dist/meteor/ddp/writefence.js +111 -111
- package/dist/meteor/diff-sequence/diff.d.ts +17 -17
- package/dist/meteor/diff-sequence/diff.js +257 -257
- package/dist/meteor/ejson/ejson.d.ts +82 -82
- package/dist/meteor/ejson/ejson.js +568 -569
- package/dist/meteor/ejson/stringify.d.ts +2 -2
- package/dist/meteor/ejson/stringify.js +119 -119
- package/dist/meteor/ejson/utils.d.ts +12 -12
- package/dist/meteor/ejson/utils.js +42 -42
- package/dist/meteor/mongo/caching_change_observer.d.ts +16 -16
- package/dist/meteor/mongo/caching_change_observer.js +63 -63
- package/dist/meteor/mongo/doc_fetcher.d.ts +7 -7
- package/dist/meteor/mongo/doc_fetcher.js +53 -53
- package/dist/meteor/mongo/geojson_utils.d.ts +3 -3
- package/dist/meteor/mongo/geojson_utils.js +40 -41
- package/dist/meteor/mongo/live_connection.d.ts +28 -28
- package/dist/meteor/mongo/live_connection.js +264 -264
- package/dist/meteor/mongo/live_cursor.d.ts +25 -25
- package/dist/meteor/mongo/live_cursor.js +60 -60
- package/dist/meteor/mongo/minimongo_common.d.ts +84 -84
- package/dist/meteor/mongo/minimongo_common.js +1998 -1998
- package/dist/meteor/mongo/minimongo_matcher.d.ts +23 -23
- package/dist/meteor/mongo/minimongo_matcher.js +283 -283
- package/dist/meteor/mongo/minimongo_sorter.d.ts +16 -16
- package/dist/meteor/mongo/minimongo_sorter.js +268 -268
- package/dist/meteor/mongo/observe_driver_utils.d.ts +9 -9
- package/dist/meteor/mongo/observe_driver_utils.js +72 -73
- package/dist/meteor/mongo/observe_multiplexer.d.ts +46 -46
- package/dist/meteor/mongo/observe_multiplexer.js +203 -203
- package/dist/meteor/mongo/oplog-observe-driver.d.ts +68 -68
- package/dist/meteor/mongo/oplog-observe-driver.js +918 -918
- package/dist/meteor/mongo/oplog_tailing.d.ts +35 -35
- package/dist/meteor/mongo/oplog_tailing.js +352 -352
- package/dist/meteor/mongo/oplog_v2_converter.d.ts +1 -1
- package/dist/meteor/mongo/oplog_v2_converter.js +125 -126
- package/dist/meteor/mongo/polling_observe_driver.d.ts +30 -30
- package/dist/meteor/mongo/polling_observe_driver.js +216 -221
- package/dist/meteor/mongo/synchronous-cursor.d.ts +17 -17
- package/dist/meteor/mongo/synchronous-cursor.js +261 -261
- package/dist/meteor/mongo/synchronous-queue.d.ts +13 -13
- package/dist/meteor/mongo/synchronous-queue.js +110 -110
- package/dist/meteor/ordered-dict/ordered_dict.d.ts +31 -31
- package/dist/meteor/ordered-dict/ordered_dict.js +198 -198
- package/dist/meteor/random/AbstractRandomGenerator.d.ts +42 -42
- package/dist/meteor/random/AbstractRandomGenerator.js +92 -92
- package/dist/meteor/random/AleaRandomGenerator.d.ts +13 -13
- package/dist/meteor/random/AleaRandomGenerator.js +90 -90
- package/dist/meteor/random/NodeRandomGenerator.d.ts +16 -16
- package/dist/meteor/random/NodeRandomGenerator.js +42 -42
- package/dist/meteor/random/createAleaGenerator.d.ts +2 -2
- package/dist/meteor/random/createAleaGenerator.js +32 -32
- package/dist/meteor/random/createRandom.d.ts +1 -1
- package/dist/meteor/random/createRandom.js +22 -22
- package/dist/meteor/random/main.d.ts +1 -1
- package/dist/meteor/random/main.js +12 -12
- package/dist/meteor/types.d.ts +1 -1
- package/dist/meteor/types.js +2 -2
- package/package.json +5 -5
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { DDPServer } from "./meteor/ddp/livedata_server";
|
|
2
|
-
export { ddpError } from "./meteor/ddp/livedata_server";
|
|
3
|
-
export { LiveMongoConnection } from "./meteor/mongo/live_connection";
|
|
4
|
-
export { LiveCursor } from "./meteor/mongo/live_cursor";
|
|
1
|
+
export { DDPServer } from "./meteor/ddp/livedata_server";
|
|
2
|
+
export { ddpError } from "./meteor/ddp/livedata_server";
|
|
3
|
+
export { LiveMongoConnection } from "./meteor/mongo/live_connection";
|
|
4
|
+
export { LiveCursor } from "./meteor/mongo/live_cursor";
|
package/dist/livedata_server.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LiveCursor = exports.LiveMongoConnection = exports.ddpError = exports.DDPServer = void 0;
|
|
4
|
-
var livedata_server_1 = require("./meteor/ddp/livedata_server");
|
|
5
|
-
Object.defineProperty(exports, "DDPServer", { enumerable: true, get: function () { return livedata_server_1.DDPServer; } });
|
|
6
|
-
var livedata_server_2 = require("./meteor/ddp/livedata_server");
|
|
7
|
-
Object.defineProperty(exports, "ddpError", { enumerable: true, get: function () { return livedata_server_2.ddpError; } });
|
|
8
|
-
var live_connection_1 = require("./meteor/mongo/live_connection");
|
|
9
|
-
Object.defineProperty(exports, "LiveMongoConnection", { enumerable: true, get: function () { return live_connection_1.LiveMongoConnection; } });
|
|
10
|
-
var live_cursor_1 = require("./meteor/mongo/live_cursor");
|
|
11
|
-
Object.defineProperty(exports, "LiveCursor", { enumerable: true, get: function () { return live_cursor_1.LiveCursor; } });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LiveCursor = exports.LiveMongoConnection = exports.ddpError = exports.DDPServer = void 0;
|
|
4
|
+
var livedata_server_1 = require("./meteor/ddp/livedata_server");
|
|
5
|
+
Object.defineProperty(exports, "DDPServer", { enumerable: true, get: function () { return livedata_server_1.DDPServer; } });
|
|
6
|
+
var livedata_server_2 = require("./meteor/ddp/livedata_server");
|
|
7
|
+
Object.defineProperty(exports, "ddpError", { enumerable: true, get: function () { return livedata_server_2.ddpError; } });
|
|
8
|
+
var live_connection_1 = require("./meteor/mongo/live_connection");
|
|
9
|
+
Object.defineProperty(exports, "LiveMongoConnection", { enumerable: true, get: function () { return live_connection_1.LiveMongoConnection; } });
|
|
10
|
+
var live_cursor_1 = require("./meteor/mongo/live_cursor");
|
|
11
|
+
Object.defineProperty(exports, "LiveCursor", { enumerable: true, get: function () { return live_cursor_1.LiveCursor; } });
|
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
export interface MaxHeapOptions {
|
|
2
|
-
initData?: {
|
|
3
|
-
id: string;
|
|
4
|
-
value: any;
|
|
5
|
-
}[];
|
|
6
|
-
IdMap?: any;
|
|
7
|
-
}
|
|
8
|
-
export declare class MaxHeap {
|
|
9
|
-
protected _comparator: (a: any, b: any) => number;
|
|
10
|
-
protected _heap: any[];
|
|
11
|
-
private _heapIdx;
|
|
12
|
-
constructor(comparator: (a: any, b: any) => number, options?: MaxHeapOptions);
|
|
13
|
-
_initFromData(data: any): void;
|
|
14
|
-
_downHeap(idx: any): void;
|
|
15
|
-
_upHeap(idx: any): void;
|
|
16
|
-
_maxIndex(idxA: any, idxB: any): any;
|
|
17
|
-
_get(idx: any): any;
|
|
18
|
-
_swap(idxA: any, idxB: any): void;
|
|
19
|
-
get(id: any): any;
|
|
20
|
-
set(id: any, value: any): void;
|
|
21
|
-
delete(id: any): void;
|
|
22
|
-
has(id: any): any;
|
|
23
|
-
empty(): boolean;
|
|
24
|
-
clear(): void;
|
|
25
|
-
forEach(iterator: any): void;
|
|
26
|
-
get size(): number;
|
|
27
|
-
setDefault(id: any, def: any): any;
|
|
28
|
-
clone(): MaxHeap;
|
|
29
|
-
maxElementId(): any;
|
|
30
|
-
_selfCheck(): void;
|
|
31
|
-
}
|
|
1
|
+
export interface MaxHeapOptions {
|
|
2
|
+
initData?: {
|
|
3
|
+
id: string;
|
|
4
|
+
value: any;
|
|
5
|
+
}[];
|
|
6
|
+
IdMap?: any;
|
|
7
|
+
}
|
|
8
|
+
export declare class MaxHeap {
|
|
9
|
+
protected _comparator: (a: any, b: any) => number;
|
|
10
|
+
protected _heap: any[];
|
|
11
|
+
private _heapIdx;
|
|
12
|
+
constructor(comparator: (a: any, b: any) => number, options?: MaxHeapOptions);
|
|
13
|
+
_initFromData(data: any): void;
|
|
14
|
+
_downHeap(idx: any): void;
|
|
15
|
+
_upHeap(idx: any): void;
|
|
16
|
+
_maxIndex(idxA: any, idxB: any): any;
|
|
17
|
+
_get(idx: any): any;
|
|
18
|
+
_swap(idxA: any, idxB: any): void;
|
|
19
|
+
get(id: any): any;
|
|
20
|
+
set(id: any, value: any): void;
|
|
21
|
+
delete(id: any): void;
|
|
22
|
+
has(id: any): any;
|
|
23
|
+
empty(): boolean;
|
|
24
|
+
clear(): void;
|
|
25
|
+
forEach(iterator: any): void;
|
|
26
|
+
get size(): number;
|
|
27
|
+
setDefault(id: any, def: any): any;
|
|
28
|
+
clone(): MaxHeap;
|
|
29
|
+
maxElementId(): any;
|
|
30
|
+
_selfCheck(): void;
|
|
31
|
+
}
|
|
@@ -1,186 +1,186 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MaxHeap = void 0;
|
|
4
|
-
// Constructor of Heap
|
|
5
|
-
// - comparator - Function - given two items returns a number
|
|
6
|
-
// - options:
|
|
7
|
-
// - initData - Array - Optional - the initial data in a format:
|
|
8
|
-
// Object:
|
|
9
|
-
// - id - String - unique id of the item
|
|
10
|
-
// - value - Any - the data value
|
|
11
|
-
// each value is retained
|
|
12
|
-
// - IdMap - Constructor - Optional - custom IdMap class to store id->index
|
|
13
|
-
// mappings internally. Standard IdMap is used by default.
|
|
14
|
-
class MaxHeap {
|
|
15
|
-
constructor(comparator, options = {}) {
|
|
16
|
-
if (typeof comparator !== 'function') {
|
|
17
|
-
throw new Error('Passed comparator is invalid, should be a comparison function');
|
|
18
|
-
}
|
|
19
|
-
// a C-style comparator that is given two values and returns a number,
|
|
20
|
-
// negative if the first value is less than the second, positive if the second
|
|
21
|
-
// value is greater than the first and zero if they are equal.
|
|
22
|
-
this._comparator = comparator;
|
|
23
|
-
if (!options.IdMap) {
|
|
24
|
-
options.IdMap = Map;
|
|
25
|
-
}
|
|
26
|
-
// _heapIdx maps an id to an index in the Heap array the corresponding value
|
|
27
|
-
// is located on.
|
|
28
|
-
this._heapIdx = new options.IdMap;
|
|
29
|
-
// The Heap data-structure implemented as a 0-based contiguous array where
|
|
30
|
-
// every item on index idx is a node in a complete binary tree. Every node can
|
|
31
|
-
// have children on indexes idx*2+1 and idx*2+2, except for the leaves. Every
|
|
32
|
-
// node has a parent on index (idx-1)/2;
|
|
33
|
-
this._heap = [];
|
|
34
|
-
// If the initial array is passed, we can build the heap in linear time
|
|
35
|
-
// complexity (O(N)) compared to linearithmic time complexity (O(nlogn)) if
|
|
36
|
-
// we push elements one by one.
|
|
37
|
-
if (Array.isArray(options.initData)) {
|
|
38
|
-
this._initFromData(options.initData);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
// Builds a new heap in-place in linear time based on passed data
|
|
42
|
-
_initFromData(data) {
|
|
43
|
-
this._heap = data.map(({ id, value }) => ({ id, value }));
|
|
44
|
-
data.forEach(({ id }, i) => this._heapIdx.set(id, i));
|
|
45
|
-
if (!data.length) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
// start from the first non-leaf - the parent of the last leaf
|
|
49
|
-
for (let i = parentIdx(data.length - 1); i >= 0; i--) {
|
|
50
|
-
this._downHeap(i);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
_downHeap(idx) {
|
|
54
|
-
while (leftChildIdx(idx) < this.size) {
|
|
55
|
-
const left = leftChildIdx(idx);
|
|
56
|
-
const right = rightChildIdx(idx);
|
|
57
|
-
let largest = idx;
|
|
58
|
-
if (left < this.size) {
|
|
59
|
-
largest = this._maxIndex(largest, left);
|
|
60
|
-
}
|
|
61
|
-
if (right < this.size) {
|
|
62
|
-
largest = this._maxIndex(largest, right);
|
|
63
|
-
}
|
|
64
|
-
if (largest === idx) {
|
|
65
|
-
break;
|
|
66
|
-
}
|
|
67
|
-
this._swap(largest, idx);
|
|
68
|
-
idx = largest;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
_upHeap(idx) {
|
|
72
|
-
while (idx > 0) {
|
|
73
|
-
const parent = parentIdx(idx);
|
|
74
|
-
if (this._maxIndex(parent, idx) === idx) {
|
|
75
|
-
this._swap(parent, idx);
|
|
76
|
-
idx = parent;
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
_maxIndex(idxA, idxB) {
|
|
84
|
-
const valueA = this._get(idxA);
|
|
85
|
-
const valueB = this._get(idxB);
|
|
86
|
-
return this._comparator(valueA, valueB) >= 0 ? idxA : idxB;
|
|
87
|
-
}
|
|
88
|
-
// Internal: gets raw data object placed on idxth place in heap
|
|
89
|
-
_get(idx) {
|
|
90
|
-
return this._heap[idx].value;
|
|
91
|
-
}
|
|
92
|
-
_swap(idxA, idxB) {
|
|
93
|
-
const recA = this._heap[idxA];
|
|
94
|
-
const recB = this._heap[idxB];
|
|
95
|
-
this._heapIdx.set(recA.id, idxB);
|
|
96
|
-
this._heapIdx.set(recB.id, idxA);
|
|
97
|
-
this._heap[idxA] = recB;
|
|
98
|
-
this._heap[idxB] = recA;
|
|
99
|
-
}
|
|
100
|
-
get(id) {
|
|
101
|
-
return this.has(id) ?
|
|
102
|
-
this._get(this._heapIdx.get(id)) :
|
|
103
|
-
null;
|
|
104
|
-
}
|
|
105
|
-
set(id, value) {
|
|
106
|
-
if (this.has(id)) {
|
|
107
|
-
if (this.get(id) === value) {
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
const idx = this._heapIdx.get(id);
|
|
111
|
-
this._heap[idx].value = value;
|
|
112
|
-
// Fix the new value's position
|
|
113
|
-
// Either bubble new value up if it is greater than its parent
|
|
114
|
-
this._upHeap(idx);
|
|
115
|
-
// or bubble it down if it is smaller than one of its children
|
|
116
|
-
this._downHeap(idx);
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
this._heapIdx.set(id, this._heap.length);
|
|
120
|
-
this._heap.push({ id, value });
|
|
121
|
-
this._upHeap(this._heap.length - 1);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
delete(id) {
|
|
125
|
-
if (this.has(id)) {
|
|
126
|
-
const last = this._heap.length - 1;
|
|
127
|
-
const idx = this._heapIdx.get(id);
|
|
128
|
-
if (idx !== last) {
|
|
129
|
-
this._swap(idx, last);
|
|
130
|
-
this._heap.pop();
|
|
131
|
-
this._heapIdx.remove(id);
|
|
132
|
-
// Fix the swapped value's position
|
|
133
|
-
this._upHeap(idx);
|
|
134
|
-
this._downHeap(idx);
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
this._heap.pop();
|
|
138
|
-
this._heapIdx.remove(id);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
has(id) {
|
|
143
|
-
return this._heapIdx.has(id);
|
|
144
|
-
}
|
|
145
|
-
empty() {
|
|
146
|
-
return this.size === 0;
|
|
147
|
-
}
|
|
148
|
-
clear() {
|
|
149
|
-
this._heap = [];
|
|
150
|
-
this._heapIdx.clear();
|
|
151
|
-
}
|
|
152
|
-
// iterate over values in no particular order
|
|
153
|
-
forEach(iterator) {
|
|
154
|
-
this._heap.forEach(obj => iterator(obj.value, obj.id));
|
|
155
|
-
}
|
|
156
|
-
get size() {
|
|
157
|
-
return this._heap.length;
|
|
158
|
-
}
|
|
159
|
-
setDefault(id, def) {
|
|
160
|
-
if (this.has(id)) {
|
|
161
|
-
return this.get(id);
|
|
162
|
-
}
|
|
163
|
-
this.set(id, def);
|
|
164
|
-
return def;
|
|
165
|
-
}
|
|
166
|
-
clone() {
|
|
167
|
-
const clone = new MaxHeap(this._comparator, { initData: this._heap });
|
|
168
|
-
return clone;
|
|
169
|
-
}
|
|
170
|
-
maxElementId() {
|
|
171
|
-
return this.size > 0 ? this._heap[0].id : null;
|
|
172
|
-
}
|
|
173
|
-
_selfCheck() {
|
|
174
|
-
for (let i = 1; i < this._heap.length; i++) {
|
|
175
|
-
if (this._maxIndex(parentIdx(i), i) !== parentIdx(i)) {
|
|
176
|
-
throw new Error(`An item with id ${this._heap[i].id}` +
|
|
177
|
-
" has a parent younger than it: " +
|
|
178
|
-
this._heap[parentIdx(i)].id);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
exports.MaxHeap = MaxHeap;
|
|
184
|
-
const leftChildIdx = i => i * 2 + 1;
|
|
185
|
-
const rightChildIdx = i => i * 2 + 2;
|
|
186
|
-
const parentIdx = i => (i - 1) >> 1;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MaxHeap = void 0;
|
|
4
|
+
// Constructor of Heap
|
|
5
|
+
// - comparator - Function - given two items returns a number
|
|
6
|
+
// - options:
|
|
7
|
+
// - initData - Array - Optional - the initial data in a format:
|
|
8
|
+
// Object:
|
|
9
|
+
// - id - String - unique id of the item
|
|
10
|
+
// - value - Any - the data value
|
|
11
|
+
// each value is retained
|
|
12
|
+
// - IdMap - Constructor - Optional - custom IdMap class to store id->index
|
|
13
|
+
// mappings internally. Standard IdMap is used by default.
|
|
14
|
+
class MaxHeap {
|
|
15
|
+
constructor(comparator, options = {}) {
|
|
16
|
+
if (typeof comparator !== 'function') {
|
|
17
|
+
throw new Error('Passed comparator is invalid, should be a comparison function');
|
|
18
|
+
}
|
|
19
|
+
// a C-style comparator that is given two values and returns a number,
|
|
20
|
+
// negative if the first value is less than the second, positive if the second
|
|
21
|
+
// value is greater than the first and zero if they are equal.
|
|
22
|
+
this._comparator = comparator;
|
|
23
|
+
if (!options.IdMap) {
|
|
24
|
+
options.IdMap = Map;
|
|
25
|
+
}
|
|
26
|
+
// _heapIdx maps an id to an index in the Heap array the corresponding value
|
|
27
|
+
// is located on.
|
|
28
|
+
this._heapIdx = new options.IdMap;
|
|
29
|
+
// The Heap data-structure implemented as a 0-based contiguous array where
|
|
30
|
+
// every item on index idx is a node in a complete binary tree. Every node can
|
|
31
|
+
// have children on indexes idx*2+1 and idx*2+2, except for the leaves. Every
|
|
32
|
+
// node has a parent on index (idx-1)/2;
|
|
33
|
+
this._heap = [];
|
|
34
|
+
// If the initial array is passed, we can build the heap in linear time
|
|
35
|
+
// complexity (O(N)) compared to linearithmic time complexity (O(nlogn)) if
|
|
36
|
+
// we push elements one by one.
|
|
37
|
+
if (Array.isArray(options.initData)) {
|
|
38
|
+
this._initFromData(options.initData);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Builds a new heap in-place in linear time based on passed data
|
|
42
|
+
_initFromData(data) {
|
|
43
|
+
this._heap = data.map(({ id, value }) => ({ id, value }));
|
|
44
|
+
data.forEach(({ id }, i) => this._heapIdx.set(id, i));
|
|
45
|
+
if (!data.length) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// start from the first non-leaf - the parent of the last leaf
|
|
49
|
+
for (let i = parentIdx(data.length - 1); i >= 0; i--) {
|
|
50
|
+
this._downHeap(i);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
_downHeap(idx) {
|
|
54
|
+
while (leftChildIdx(idx) < this.size) {
|
|
55
|
+
const left = leftChildIdx(idx);
|
|
56
|
+
const right = rightChildIdx(idx);
|
|
57
|
+
let largest = idx;
|
|
58
|
+
if (left < this.size) {
|
|
59
|
+
largest = this._maxIndex(largest, left);
|
|
60
|
+
}
|
|
61
|
+
if (right < this.size) {
|
|
62
|
+
largest = this._maxIndex(largest, right);
|
|
63
|
+
}
|
|
64
|
+
if (largest === idx) {
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
this._swap(largest, idx);
|
|
68
|
+
idx = largest;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
_upHeap(idx) {
|
|
72
|
+
while (idx > 0) {
|
|
73
|
+
const parent = parentIdx(idx);
|
|
74
|
+
if (this._maxIndex(parent, idx) === idx) {
|
|
75
|
+
this._swap(parent, idx);
|
|
76
|
+
idx = parent;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
_maxIndex(idxA, idxB) {
|
|
84
|
+
const valueA = this._get(idxA);
|
|
85
|
+
const valueB = this._get(idxB);
|
|
86
|
+
return this._comparator(valueA, valueB) >= 0 ? idxA : idxB;
|
|
87
|
+
}
|
|
88
|
+
// Internal: gets raw data object placed on idxth place in heap
|
|
89
|
+
_get(idx) {
|
|
90
|
+
return this._heap[idx].value;
|
|
91
|
+
}
|
|
92
|
+
_swap(idxA, idxB) {
|
|
93
|
+
const recA = this._heap[idxA];
|
|
94
|
+
const recB = this._heap[idxB];
|
|
95
|
+
this._heapIdx.set(recA.id, idxB);
|
|
96
|
+
this._heapIdx.set(recB.id, idxA);
|
|
97
|
+
this._heap[idxA] = recB;
|
|
98
|
+
this._heap[idxB] = recA;
|
|
99
|
+
}
|
|
100
|
+
get(id) {
|
|
101
|
+
return this.has(id) ?
|
|
102
|
+
this._get(this._heapIdx.get(id)) :
|
|
103
|
+
null;
|
|
104
|
+
}
|
|
105
|
+
set(id, value) {
|
|
106
|
+
if (this.has(id)) {
|
|
107
|
+
if (this.get(id) === value) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const idx = this._heapIdx.get(id);
|
|
111
|
+
this._heap[idx].value = value;
|
|
112
|
+
// Fix the new value's position
|
|
113
|
+
// Either bubble new value up if it is greater than its parent
|
|
114
|
+
this._upHeap(idx);
|
|
115
|
+
// or bubble it down if it is smaller than one of its children
|
|
116
|
+
this._downHeap(idx);
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
this._heapIdx.set(id, this._heap.length);
|
|
120
|
+
this._heap.push({ id, value });
|
|
121
|
+
this._upHeap(this._heap.length - 1);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
delete(id) {
|
|
125
|
+
if (this.has(id)) {
|
|
126
|
+
const last = this._heap.length - 1;
|
|
127
|
+
const idx = this._heapIdx.get(id);
|
|
128
|
+
if (idx !== last) {
|
|
129
|
+
this._swap(idx, last);
|
|
130
|
+
this._heap.pop();
|
|
131
|
+
this._heapIdx.remove(id);
|
|
132
|
+
// Fix the swapped value's position
|
|
133
|
+
this._upHeap(idx);
|
|
134
|
+
this._downHeap(idx);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
this._heap.pop();
|
|
138
|
+
this._heapIdx.remove(id);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
has(id) {
|
|
143
|
+
return this._heapIdx.has(id);
|
|
144
|
+
}
|
|
145
|
+
empty() {
|
|
146
|
+
return this.size === 0;
|
|
147
|
+
}
|
|
148
|
+
clear() {
|
|
149
|
+
this._heap = [];
|
|
150
|
+
this._heapIdx.clear();
|
|
151
|
+
}
|
|
152
|
+
// iterate over values in no particular order
|
|
153
|
+
forEach(iterator) {
|
|
154
|
+
this._heap.forEach(obj => iterator(obj.value, obj.id));
|
|
155
|
+
}
|
|
156
|
+
get size() {
|
|
157
|
+
return this._heap.length;
|
|
158
|
+
}
|
|
159
|
+
setDefault(id, def) {
|
|
160
|
+
if (this.has(id)) {
|
|
161
|
+
return this.get(id);
|
|
162
|
+
}
|
|
163
|
+
this.set(id, def);
|
|
164
|
+
return def;
|
|
165
|
+
}
|
|
166
|
+
clone() {
|
|
167
|
+
const clone = new MaxHeap(this._comparator, { initData: this._heap });
|
|
168
|
+
return clone;
|
|
169
|
+
}
|
|
170
|
+
maxElementId() {
|
|
171
|
+
return this.size > 0 ? this._heap[0].id : null;
|
|
172
|
+
}
|
|
173
|
+
_selfCheck() {
|
|
174
|
+
for (let i = 1; i < this._heap.length; i++) {
|
|
175
|
+
if (this._maxIndex(parentIdx(i), i) !== parentIdx(i)) {
|
|
176
|
+
throw new Error(`An item with id ${this._heap[i].id}` +
|
|
177
|
+
" has a parent younger than it: " +
|
|
178
|
+
this._heap[parentIdx(i)].id);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
exports.MaxHeap = MaxHeap;
|
|
184
|
+
const leftChildIdx = i => i * 2 + 1;
|
|
185
|
+
const rightChildIdx = i => i * 2 + 2;
|
|
186
|
+
const parentIdx = i => (i - 1) >> 1;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { MaxHeap, MaxHeapOptions } from './max_heap';
|
|
2
|
-
export declare class MinHeap extends MaxHeap {
|
|
3
|
-
constructor(comparator: any, options?: MaxHeapOptions);
|
|
4
|
-
maxElementId(): void;
|
|
5
|
-
minElementId(): any;
|
|
6
|
-
}
|
|
1
|
+
import { MaxHeap, MaxHeapOptions } from './max_heap';
|
|
2
|
+
export declare class MinHeap extends MaxHeap {
|
|
3
|
+
constructor(comparator: any, options?: MaxHeapOptions);
|
|
4
|
+
maxElementId(): void;
|
|
5
|
+
minElementId(): any;
|
|
6
|
+
}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MinHeap = void 0;
|
|
4
|
-
const max_heap_1 = require("./max_heap");
|
|
5
|
-
class MinHeap extends max_heap_1.MaxHeap {
|
|
6
|
-
constructor(comparator, options) {
|
|
7
|
-
super((a, b) => -comparator(a, b), options);
|
|
8
|
-
}
|
|
9
|
-
maxElementId() {
|
|
10
|
-
throw new Error("Cannot call maxElementId on MinHeap");
|
|
11
|
-
}
|
|
12
|
-
minElementId() {
|
|
13
|
-
return super.maxElementId();
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
exports.MinHeap = MinHeap;
|
|
17
|
-
;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MinHeap = void 0;
|
|
4
|
+
const max_heap_1 = require("./max_heap");
|
|
5
|
+
class MinHeap extends max_heap_1.MaxHeap {
|
|
6
|
+
constructor(comparator, options) {
|
|
7
|
+
super((a, b) => -comparator(a, b), options);
|
|
8
|
+
}
|
|
9
|
+
maxElementId() {
|
|
10
|
+
throw new Error("Cannot call maxElementId on MinHeap");
|
|
11
|
+
}
|
|
12
|
+
minElementId() {
|
|
13
|
+
return super.maxElementId();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.MinHeap = MinHeap;
|
|
17
|
+
;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { MaxHeap, MaxHeapOptions } from './max_heap';
|
|
2
|
-
export declare class MinMaxHeap extends MaxHeap {
|
|
3
|
-
private _minHeap;
|
|
4
|
-
constructor(comparator: any, options?: MaxHeapOptions);
|
|
5
|
-
set(id: string, value: any): void;
|
|
6
|
-
delete(id: string): void;
|
|
7
|
-
clear(): void;
|
|
8
|
-
setDefault(id: string, def: any): any;
|
|
9
|
-
clone(): MinMaxHeap;
|
|
10
|
-
minElementId(): any;
|
|
11
|
-
}
|
|
1
|
+
import { MaxHeap, MaxHeapOptions } from './max_heap';
|
|
2
|
+
export declare class MinMaxHeap extends MaxHeap {
|
|
3
|
+
private _minHeap;
|
|
4
|
+
constructor(comparator: any, options?: MaxHeapOptions);
|
|
5
|
+
set(id: string, value: any): void;
|
|
6
|
+
delete(id: string): void;
|
|
7
|
+
clear(): void;
|
|
8
|
+
setDefault(id: string, def: any): any;
|
|
9
|
+
clone(): MinMaxHeap;
|
|
10
|
+
minElementId(): any;
|
|
11
|
+
}
|
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MinMaxHeap = void 0;
|
|
4
|
-
const max_heap_1 = require("./max_heap");
|
|
5
|
-
const min_heap_1 = require("./min_heap");
|
|
6
|
-
// This implementation of Min/Max-Heap is just a subclass of Max-Heap
|
|
7
|
-
// with a Min-Heap as an encapsulated property.
|
|
8
|
-
//
|
|
9
|
-
// Most of the operations are just proxy methods to call the same method on both
|
|
10
|
-
// heaps.
|
|
11
|
-
//
|
|
12
|
-
// This implementation takes 2*N memory but is fairly simple to write and
|
|
13
|
-
// understand. And the constant factor of a simple Heap is usually smaller
|
|
14
|
-
// compared to other two-way priority queues like Min/Max Heaps
|
|
15
|
-
// (http://www.cs.otago.ac.nz/staffpriv/mike/Papers/MinMaxHeaps/MinMaxHeaps.pdf)
|
|
16
|
-
// and Interval Heaps
|
|
17
|
-
// (http://www.cise.ufl.edu/~sahni/dsaac/enrich/c13/double.htm)
|
|
18
|
-
class MinMaxHeap extends max_heap_1.MaxHeap {
|
|
19
|
-
constructor(comparator, options) {
|
|
20
|
-
super(comparator, options);
|
|
21
|
-
this._minHeap = new min_heap_1.MinHeap(comparator, options);
|
|
22
|
-
}
|
|
23
|
-
set(id, value) {
|
|
24
|
-
super.set(id, value);
|
|
25
|
-
this._minHeap.set(id, value);
|
|
26
|
-
}
|
|
27
|
-
delete(id) {
|
|
28
|
-
super.delete(id);
|
|
29
|
-
this._minHeap.delete(id);
|
|
30
|
-
}
|
|
31
|
-
clear() {
|
|
32
|
-
super.clear();
|
|
33
|
-
this._minHeap.clear();
|
|
34
|
-
}
|
|
35
|
-
setDefault(id, def) {
|
|
36
|
-
super.setDefault(id, def);
|
|
37
|
-
return this._minHeap.setDefault(id, def);
|
|
38
|
-
}
|
|
39
|
-
clone() {
|
|
40
|
-
const clone = new MinMaxHeap(this._comparator, { initData: this._heap });
|
|
41
|
-
return clone;
|
|
42
|
-
}
|
|
43
|
-
minElementId() {
|
|
44
|
-
return this._minHeap.minElementId();
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
exports.MinMaxHeap = MinMaxHeap;
|
|
48
|
-
;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MinMaxHeap = void 0;
|
|
4
|
+
const max_heap_1 = require("./max_heap");
|
|
5
|
+
const min_heap_1 = require("./min_heap");
|
|
6
|
+
// This implementation of Min/Max-Heap is just a subclass of Max-Heap
|
|
7
|
+
// with a Min-Heap as an encapsulated property.
|
|
8
|
+
//
|
|
9
|
+
// Most of the operations are just proxy methods to call the same method on both
|
|
10
|
+
// heaps.
|
|
11
|
+
//
|
|
12
|
+
// This implementation takes 2*N memory but is fairly simple to write and
|
|
13
|
+
// understand. And the constant factor of a simple Heap is usually smaller
|
|
14
|
+
// compared to other two-way priority queues like Min/Max Heaps
|
|
15
|
+
// (http://www.cs.otago.ac.nz/staffpriv/mike/Papers/MinMaxHeaps/MinMaxHeaps.pdf)
|
|
16
|
+
// and Interval Heaps
|
|
17
|
+
// (http://www.cise.ufl.edu/~sahni/dsaac/enrich/c13/double.htm)
|
|
18
|
+
class MinMaxHeap extends max_heap_1.MaxHeap {
|
|
19
|
+
constructor(comparator, options) {
|
|
20
|
+
super(comparator, options);
|
|
21
|
+
this._minHeap = new min_heap_1.MinHeap(comparator, options);
|
|
22
|
+
}
|
|
23
|
+
set(id, value) {
|
|
24
|
+
super.set(id, value);
|
|
25
|
+
this._minHeap.set(id, value);
|
|
26
|
+
}
|
|
27
|
+
delete(id) {
|
|
28
|
+
super.delete(id);
|
|
29
|
+
this._minHeap.delete(id);
|
|
30
|
+
}
|
|
31
|
+
clear() {
|
|
32
|
+
super.clear();
|
|
33
|
+
this._minHeap.clear();
|
|
34
|
+
}
|
|
35
|
+
setDefault(id, def) {
|
|
36
|
+
super.setDefault(id, def);
|
|
37
|
+
return this._minHeap.setDefault(id, def);
|
|
38
|
+
}
|
|
39
|
+
clone() {
|
|
40
|
+
const clone = new MinMaxHeap(this._comparator, { initData: this._heap });
|
|
41
|
+
return clone;
|
|
42
|
+
}
|
|
43
|
+
minElementId() {
|
|
44
|
+
return this._minHeap.minElementId();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.MinMaxHeap = MinMaxHeap;
|
|
48
|
+
;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export declare class Hook {
|
|
2
|
-
private nextCallbackId;
|
|
3
|
-
private callbacks;
|
|
4
|
-
private exceptionHandler;
|
|
5
|
-
constructor(options?: any);
|
|
6
|
-
register(callback: any): {
|
|
7
|
-
callback: any;
|
|
8
|
-
stop: () => void;
|
|
9
|
-
};
|
|
10
|
-
each(iterator: (callback: Function) => boolean): void;
|
|
11
|
-
}
|
|
1
|
+
export declare class Hook {
|
|
2
|
+
private nextCallbackId;
|
|
3
|
+
private callbacks;
|
|
4
|
+
private exceptionHandler;
|
|
5
|
+
constructor(options?: any);
|
|
6
|
+
register(callback: any): {
|
|
7
|
+
callback: any;
|
|
8
|
+
stop: () => void;
|
|
9
|
+
};
|
|
10
|
+
each(iterator: (callback: Function) => boolean): void;
|
|
11
|
+
}
|