From a1b16e3bae15e33a4fa409606dacd1d2466e9e42 Mon Sep 17 00:00:00 2001 From: Jordan Baker Date: Fri, 19 Jan 2024 17:33:01 -0700 Subject: [PATCH] remove debugs --- src/IntervalHashSet.ts | 2 -- src/IntervalTree.ts | 26 +++------------- src/Node.ts | 71 ++++++++++++++++++++---------------------- 3 files changed, 39 insertions(+), 60 deletions(-) diff --git a/src/IntervalHashSet.ts b/src/IntervalHashSet.ts index b71e294..bd78e94 100644 --- a/src/IntervalHashSet.ts +++ b/src/IntervalHashSet.ts @@ -1,5 +1,4 @@ import { Interval } from './Interval' -import { debug } from './debug' const intervalHash = (iv: Interval) => { return `${iv.start},${iv.end},${iv.data}` @@ -19,7 +18,6 @@ export class IntervalHashSet { add(interval: Interval) { const hash = intervalHash(interval) - debug('add', interval, hash) this.intervalsMap.set(hash, interval) } diff --git a/src/IntervalTree.ts b/src/IntervalTree.ts index a637400..57382a1 100644 --- a/src/IntervalTree.ts +++ b/src/IntervalTree.ts @@ -3,7 +3,6 @@ import * as crypto from 'crypto' import * as lodash from 'lodash' import { bisectLeft } from './bisect' -import { debug } from './debug' import { Node } from './Node' import { Interval } from './Interval' import { SortedMap, HashSet } from '@rimbu/core' @@ -63,7 +62,6 @@ export class IntervalTree { } public add(interval: Interval) { - debug('tree/add', interval) if (this.allIntervals.has(interval)) { return } @@ -98,19 +96,10 @@ export class IntervalTree { endHits.forEach((iv) => { insertions.add(new Interval(end, iv.end, iv.data)) }) - debug(() => ({ - insertions: insertions.toString(), - start, - end, - endHits: endHits, - startHits: startHits, - })) - debug(() => `chop: before=${this.allIntervals.toArray()}`) this.removeEnveloped(start, end) this.differenceUpdate(startHits) this.differenceUpdate(endHits) this.update(insertions.toArray()) - debug(() => `chop: after=${this.allIntervals.toArray()}`) } chopAll(intervals: [number, number][]) { @@ -125,7 +114,6 @@ export class IntervalTree { */ public update(intervals: HashSet | Interval[] | IntervalHashSet) { intervals.forEach((iv: Interval) => { - debug('update', iv) this.add(iv) }) } @@ -155,7 +143,6 @@ export class IntervalTree { Completes in O(log n) time. */ - debug('remove', interval) if (!this.allIntervals.has(interval)) { if (ignoreMissing) { return @@ -176,10 +163,8 @@ export class IntervalTree { * m = number of matches * r = size of the search range (this is 1 for a point) */ - debug(`removeEnveloped: start=${start} end=${end}`) const hitlist = this.search(start, end, true) hitlist.forEach((iv) => { - debug('removing', iv) try { this.remove(iv) } catch (err) { @@ -264,11 +249,11 @@ badInterval=${iv} const keysArray = this.boundaryTable.streamKeys().toArray() const boundStart = bisectLeft(keysArray, start) const boundEnd = bisectLeft(keysArray, end) // exclude final end bound - debug( - () => - `search: start=${start} end=${end} strict=${strict} boundaryTable=${keysArray}` - ) - debug(() => `search: boundStart=${boundStart} boundEnd=${boundEnd}`) + // debug( + // () => + // `search: start=${start} end=${end} strict=${strict} boundaryTable=${keysArray}` + // ) + // debug(() => `search: boundStart=${boundStart} boundEnd=${boundEnd}`) result = result.concat( this.topNode.searchOverlap( lodash.range(boundStart, boundEnd).map((index) => keysArray[index]) @@ -279,7 +264,6 @@ badInterval=${iv} if (strict) { result = result.filter((iv) => iv.start >= start && iv.end <= end) } - debug('search: result=', result) return new IntervalHashSet(result) } diff --git a/src/Node.ts b/src/Node.ts index 3edd832..987b531 100644 --- a/src/Node.ts +++ b/src/Node.ts @@ -3,7 +3,6 @@ import assert from 'assert' import { Interval } from './Interval' import { compareIntervals } from './IntervalSortedSet' import { HashSet } from '@rimbu/core' -import { debug } from './debug' import { IntervalHashSet } from './IntervalHashSet' export class Node { @@ -116,11 +115,11 @@ export class Node { // balance > 0 is the heavy side const myHeavy = this.balance > 0 const childHeavy = this.getBranch(myHeavy).balance > 0 - debug( - `rotate: myHeavy=${branchStr(myHeavy)} childHeavy=${branchStr( - childHeavy - )} this.balance=${this.balance}` - ) + // debug( + // `rotate: myHeavy=${branchStr(myHeavy)} childHeavy=${branchStr( + // childHeavy + // )} this.balance=${this.balance}` + // ) // const struct = this.printStructure(0, true) let result: Node if (myHeavy === childHeavy || this.getBranch(myHeavy).balance === 0) { @@ -157,14 +156,14 @@ export class Node { * / \ * 2 ... */ - debug('rotate: doing singleRotate') + // debug('rotate: doing singleRotate') result = this.singleRotate() - debug('rotate: done singleRotate', result.toString()) + // debug('rotate: done singleRotate', result.toString()) } else { - debug('rotate: doing doubleRotate') + // debug('rotate: doing doubleRotate') result = this.doubleRotate() - debug('rotate: done doubleRotate', result.toString()) - debug(() => result.printStructure(0, true)) + // debug('rotate: done doubleRotate', result.toString()) + // debug(() => result.printStructure(0, true)) } // try { result.verify() @@ -198,22 +197,21 @@ export class Node { } public add(interval: Interval) { - debug('add', interval) if (this.centerHit(interval)) { - debug('add: center hit', interval) + // debug('add: center hit', interval) this.sCenter.add(interval) return this } else { const direction = this.hitBranch(interval) const branchNode = this.getBranch(direction) - debug('add: on branch', interval, direction) + // debug('add: on branch', interval, direction) if (!this.getBranch(direction)) { this.setBranch(direction, Node.fromInterval(interval)) this.refreshBalance() return this } else { this.setBranch(direction, branchNode.add(interval)) - debug('existing branch, rotating') + // debug('existing branch, rotating') return this.rotate() } } @@ -249,7 +247,6 @@ export class Node { public searchPoint(point: number, result: Interval[]) { // Returns all intervals that contain point. - debug('searchPoint', point) this.sCenter .filter((interval) => interval.start <= point && point < interval.end) .forEach((interval) => result.push(interval)) @@ -292,10 +289,10 @@ export class Node { See Eternally Confuzzled's jsw_remove_r function (lines 1-32) in his AVL tree article for reference. */ - debug(`removeIntervalHelper: ${this.toString()}`) + // debug(`removeIntervalHelper: ${this.toString()}`) if (this.centerHit(interval)) { - debug('removeIntervalHelper: center hit') + // debug('removeIntervalHelper: center hit') if (!shouldRaiseError && !this.sCenter.has(interval)) { done.push(1) return this @@ -305,23 +302,23 @@ export class Node { // desired. this.sCenter = this.sCenter.remove(interval) } catch (e) { - debug(() => this.printStructure(0, true)) + // debug(() => this.printStructure(0, true)) throw new TypeError(interval.toString()) } if (this.sCenter.size) { // keep this node done.push(1) // no rebalancing necessary - debug('removeIntervalHelper: Removed, no rebalancing.') + // debug('removeIntervalHelper: Removed, no rebalancing.') return this } else { // If we reach here, no intervals are left in this.sCenter // So, prune self. - debug('removeIntervalHelper: pruning self') + // debug('removeIntervalHelper: pruning self') return this.prune() } } else { // interval not in sCenter - debug('removeIntervalHelper: not in center') + // debug('removeIntervalHelper: not in center') const direction = this.hitBranch(interval) let branch = this.getBranch(direction) if (!this.getBranch(direction)) { @@ -331,12 +328,12 @@ export class Node { done.push(1) return this } - debug(`removeIntervalHelper: Descending to ${direction} branch`) + // debug(`removeIntervalHelper: Descending to ${direction} branch`) branch = branch.removeIntervalHelper(interval, done, shouldRaiseError) this.setBranch(direction, branch) // Clean up if (!done.length) { - debug(`removeIntervalHelper: rotating ${this.xCenter}`) + // debug(`removeIntervalHelper: rotating ${this.xCenter}`) return this.rotate() } return this @@ -354,7 +351,7 @@ export class Node { if (!leftBranch || !rightBranch) { // if I have an empty branch const direction = !leftBranch // graft the other branch here - debug(`prune: Grafting ${direction ? 'right' : 'left'} branch`) + // debug(`prune: Grafting ${direction ? 'right' : 'left'} branch`) const result = this.getBranch(direction) result?.verify() return result @@ -365,7 +362,7 @@ export class Node { const newBranch = result[1] this.setBranch(0, newBranch) - debug(`prune: Replacing ${this} with ${heir}`) + // debug(`prune: Replacing ${this} with ${heir}`) this.leftNode?.verify() this.rightNode?.verify() @@ -534,16 +531,16 @@ export class Node { const light = !heavy const rotatedNode = this.getBranch(heavy) // this.verify(new IntervalSet([])) - debug( - 'singleRotate', - this.toString(), - `balance=${this.balance}, ${rotatedNode.balance}`, - `heavy=${heavy ? 'right' : 'left'}` - ) + // debug( + // 'singleRotate', + // this.toString(), + // `balance=${this.balance}, ${rotatedNode.balance}`, + // `heavy=${heavy ? 'right' : 'left'}` + // ) // assert(save.getBranch(light)) this.setBranch(heavy, rotatedNode.getBranch(light)) rotatedNode.setBranch(light, this.rotate()) // Needed to ensure the 2 and 3 are balanced under new subnode - debug(() => this.printStructure(0, true)) + // debug(() => this.printStructure(0, true)) // Some intervals may overlap both this.xCenter and save.xCenter // Promote those to the new tip of the tree @@ -551,7 +548,7 @@ export class Node { .getBranch(light) .sCenter.filter((iv) => rotatedNode.centerHit(iv)) if (promotees.length) { - debug('have promotees', promotees.toString()) + // debug('have promotees', promotees.toString()) for (const iv of promotees) { rotatedNode.setBranch(light, rotatedNode.getBranch(light).remove(iv)) } @@ -567,11 +564,11 @@ export class Node { const myHeavy = this.balance > 0 this.setBranch(myHeavy, this.getBranch(myHeavy).singleRotate()) this.refreshBalance() - debug('doubleRotate: after first rotate') - debug(() => this.printStructure(0, true)) + // debug('doubleRotate: after first rotate') + // debug(() => this.printStructure(0, true)) // Second rotation return this.singleRotate() } } -const branchStr = (branch: boolean | number) => (branch ? 'right' : 'left') +// const branchStr = (branch: boolean | number) => (branch ? 'right' : 'left')