Skip to content

Commit 9351add

Browse files
committed
sorted queue
1 parent a73caed commit 9351add

File tree

3 files changed

+96
-34
lines changed

3 files changed

+96
-34
lines changed

scripts/recursive-generator.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
3+
const r = function* (n){
4+
if(n > 50){
5+
return n;
6+
}
7+
yield n;
8+
yield * r(n+1);
9+
};
10+
11+
12+
for(const v of r(20)){
13+
console.log(v);
14+
}

scripts/slice.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
console.log('12345'.slice(-3)); // I get '345'
4+
5+
console.log('12345'.slice(2, -2)); // I get ''

src/sorted-queue.ts

100644100755
Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/usr/bin/env ts-node
2+
'use strict';
3+
14
export interface KeyVal<V, K> {
25
key?: K,
36
val: V,
@@ -187,7 +190,7 @@ class SortedQueue<V, K = any> {
187190

188191
}
189192

190-
* inOr3der(node: SortedQueueNode<any, any>): any {
193+
* inOr3der(node: SortedQueueNode<any, any> = this.rootNode): any {
191194

192195
if (node === null) {
193196
return;
@@ -205,7 +208,7 @@ class SortedQueue<V, K = any> {
205208

206209
}
207210

208-
[Symbol.iterator](node: SortedQueueNode<any, any>): any {
211+
[Symbol('foo')](node: SortedQueueNode<any, any>): any {
209212

210213
const sq = this;
211214
let currentNode = this.rootNode;
@@ -250,34 +253,7 @@ class SortedQueue<V, K = any> {
250253
}
251254

252255
iterator() {
253-
254-
const sq = this;
255-
256-
return {
257-
258-
[Symbol.iterator](node: SortedQueueNode<any, any>): any {
259-
260-
return {
261-
next() {
262-
263-
for (const n of sq.iterator()) {
264-
265-
}
266-
267-
if (node.right) {
268-
return
269-
}
270-
271-
return {
272-
done: false,
273-
value: <unknown>null
274-
}
275-
276-
}
277-
}
278-
279-
}
280-
}
256+
return this;
281257
}
282258

283259
iterateAll(cb: (v: number) => void) {
@@ -305,6 +281,53 @@ class SortedQueue<V, K = any> {
305281
}
306282
}
307283

284+
[Symbol('iterator')]() {
285+
const stack: any[] = [this.rootNode];
286+
let currentNode = this.rootNode;
287+
288+
return {
289+
next() {
290+
while (currentNode !== null || stack.length > 0) {
291+
if (currentNode !== null) {
292+
stack.push(currentNode);
293+
currentNode = currentNode.left || null;
294+
} else {
295+
const node = stack.pop();
296+
// currentNode = node.right;
297+
// currentNode = stack.pop();
298+
currentNode = node.right || null;
299+
return {done: false, value: node.val};
300+
}
301+
}
302+
return {done: true, value: null};
303+
}
304+
};
305+
}
306+
307+
[Symbol.iterator]() {
308+
const stack: any[] = [];
309+
let currentNode = this.rootNode;
310+
311+
return {
312+
next() {
313+
314+
while (currentNode !== null) {
315+
stack.push(currentNode);
316+
currentNode = currentNode.left || null;
317+
}
318+
319+
if (stack.length < 1) {
320+
return {done: true, value: null};
321+
}
322+
323+
const node = stack.pop();
324+
currentNode = node.right || null;
325+
return {done: false, value: node.val};
326+
327+
}
328+
};
329+
}
330+
308331
find(val: V) {
309332

310333
let currentNode = this.rootNode;
@@ -414,6 +437,7 @@ const rootNode = getNode(0.5, 0.25, 1);
414437
console.timeEnd('foo');
415438
// process.exit(0);
416439

440+
417441
const rootNode2 = new SortedQueueNode<number, number>(
418442
{val: 0.5},
419443

@@ -468,6 +492,8 @@ const sq = new SortedQueue(rootNode, {
468492
compareByNum: ((a, b) => (a as number) - (b as number)),
469493
});
470494

495+
496+
471497
const vals = [];
472498

473499
console.time('start');
@@ -483,13 +509,29 @@ for (let i = 0; i < 1000; i++) {
483509
console.timeEnd('start');
484510

485511

512+
const runLog0 = () => {
513+
let count = 0;
514+
let prev: any = 0;
515+
console.time('bar0')
516+
for (const v of sq) {
517+
if (v <= prev) {
518+
console.log(count++, v);
519+
throw 'smaller 2';
520+
}
521+
prev = v;
522+
// console.log(count++, v);
523+
}
524+
console.timeEnd('bar0');
525+
526+
}
527+
486528
const runLog1 = () => {
487529

488530
let previous = 0;
489531
let count = 0;
490532

491533
console.time('bar1')
492-
for (const z of sq.inOr3der(sq.rootNode)) {
534+
for (const z of sq.inOr3der()) {
493535
if (z < previous) {
494536
throw new Error('smaller.');
495537
}
@@ -540,6 +582,7 @@ const runLog3 = () => {
540582
console.log({count});
541583
}
542584

585+
runLog0();
543586
runLog1();
544587
runLog2();
545588
runLog3();
@@ -578,9 +621,9 @@ doRecurse(rootNode, 0);
578621
// console.log(sq);
579622

580623

581-
for (const v of vals) {
582-
console.log(sq.find(v).numOfSearches);
583-
}
624+
// for (const v of vals) {
625+
// console.log(sq.find(v).numOfSearches);
626+
// }
584627

585628
console.log(sq.find(0.5).numOfSearches);
586629
console.log(sq.find(0.25).numOfSearches);

0 commit comments

Comments
 (0)