Skip to content

Commit

Permalink
remove debugs
Browse files Browse the repository at this point in the history
  • Loading branch information
hexsprite committed Jan 20, 2024
1 parent e481909 commit a1b16e3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 60 deletions.
2 changes: 0 additions & 2 deletions src/IntervalHashSet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Interval } from './Interval'
import { debug } from './debug'

const intervalHash = (iv: Interval) => {
return `${iv.start},${iv.end},${iv.data}`
Expand All @@ -19,7 +18,6 @@ export class IntervalHashSet {

add(interval: Interval) {
const hash = intervalHash(interval)
debug('add', interval, hash)
this.intervalsMap.set(hash, interval)
}

Expand Down
26 changes: 5 additions & 21 deletions src/IntervalTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -63,7 +62,6 @@ export class IntervalTree {
}

public add(interval: Interval) {
debug('tree/add', interval)
if (this.allIntervals.has(interval)) {
return
}
Expand Down Expand Up @@ -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][]) {
Expand All @@ -125,7 +114,6 @@ export class IntervalTree {
*/
public update(intervals: HashSet<Interval> | Interval[] | IntervalHashSet) {
intervals.forEach((iv: Interval) => {
debug('update', iv)
this.add(iv)
})
}
Expand Down Expand Up @@ -155,7 +143,6 @@ export class IntervalTree {
Completes in O(log n) time.
*/
debug('remove', interval)
if (!this.allIntervals.has(interval)) {
if (ignoreMissing) {
return
Expand All @@ -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) {
Expand Down Expand Up @@ -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])
Expand All @@ -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)
}

Expand Down
71 changes: 34 additions & 37 deletions src/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -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)) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -534,24 +531,24 @@ 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
const promotees = rotatedNode
.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))
}
Expand All @@ -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')

0 comments on commit a1b16e3

Please sign in to comment.