Skip to content

Commit

Permalink
rearrange param and receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jan 28, 2023
1 parent af02e01 commit 25a1408
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 125 deletions.
14 changes: 7 additions & 7 deletions comparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ package interval
// | B1----B2 | | | | | |
// -------------------------|----------------------------------------
//
func (t Tree[T]) compare(a, b T) int {
func (t *Tree[T]) compare(a, b T) int {
ll, rr, _, _ := t.cmp(a, b)
switch {
case ll == 0:
Expand All @@ -80,7 +80,7 @@ func (t Tree[T]) compare(a, b T) int {
}
}

// covers, returns true if a covers b.
// cmpCovers, returns true if a cmpCovers b.
//
// =================================================================|
// | visualization | ll | rr | lr | rl | description |
Expand All @@ -100,12 +100,12 @@ func (t Tree[T]) compare(a, b T) int {
// | B1----B2 | | | | | |
// -------------------------|----------------------------------------
//
func (t Tree[T]) covers(a, b T) bool {
func (t *Tree[T]) cmpCovers(a, b T) bool {
ll, rr, _, _ := t.cmp(a, b)
return ll <= 0 && rr >= 0
}

// intersects, returns false if the intervals does precede each other.
// cmpIntersects, returns false if the intervals does precede each other.
//
// =================================================================|
// | visualization | ll | rr | lr | rl | description |
Expand All @@ -119,19 +119,19 @@ func (t Tree[T]) covers(a, b T) bool {
// | B1---B2 | | | | | |
// -------------------------|---------------------------------------|
//
func (t Tree[T]) intersects(a, b T) bool {
func (t *Tree[T]) cmpIntersects(a, b T) bool {
ll, rr, lr, rl := t.cmp(a, b)
return !((ll == -1 && rr == -1 && lr == -1 && rl == -1) || (ll == 1 && rr == 1 && lr == 1 && rl == 1))
}

// cmpRR, compares just the right point of the intervals.
func (t Tree[T]) cmpRR(a, b T) int {
func (t *Tree[T]) cmpRR(a, b T) int {
_, rr, _, _ := t.cmp(a, b)
return rr
}

// cmpLR, compares just the left point from a with right point from b.
func (t Tree[T]) cmpLR(a, b T) int {
func (t *Tree[T]) cmpLR(a, b T) int {
_, _, lr, _ := t.cmp(a, b)
return lr
}
20 changes: 9 additions & 11 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (n *node[T]) pcmForNode(pcm parentChildsMap[T], t *Tree[T]) parentChildsMap
for j := len(pcm.stack) - 1; j >= 0; j-- {

that := pcm.stack[j]
if t.covers(that.item, n.item) {
if t.cmpCovers(that.item, n.item) {
// item in node j is parent to item
pcm.pcMap[that] = append(pcm.pcMap[that], n)
break
Expand Down Expand Up @@ -383,10 +383,10 @@ func (t Tree[T]) Visit(start, stop T, visitFn func(item T) bool) {
}

// treaps are really cool datastructures!!!
_, mid1, r := t.root.split(start, true, &t)
l, mid2, _ := r.split(stop, true, &t)
_, mid1, r := t.split(t.root, start, true)
l, mid2, _ := t.split(r, stop, true)

span := join(mid1, join(l, mid2, true, &t), true, &t)
span := (&t).join(mid1, (&t).join(l, mid2, true), true)

span.traverse(order, 0, func(n *node[T], _ int) bool {
return visitFn(n.item)
Expand All @@ -396,25 +396,23 @@ func (t Tree[T]) Visit(start, stop T, visitFn func(item T) bool) {
// Clone, deep cloning of the tree structure.
func (t Tree[T]) Clone() Tree[T] {
if t.root != nil {
t.root = t.root.clone(t)
t.root = t.clone(t.root)
}
return t
}

// clone rec-descent
//
// The parameter t is needed to access the compare function.
func (n *node[T]) clone(t Tree[T]) *node[T] {
func (t *Tree[T]) clone(n *node[T]) *node[T] {
n = n.copyNode()

if n.left != nil {
n.left = n.left.clone(t)
n.left = t.clone(n.left)
}

if n.right != nil {
n.right = n.right.clone(t)
n.right = t.clone(n.right)
}

n.recalc(&t)
t.recalc(n)
return n
}
Loading

0 comments on commit 25a1408

Please sign in to comment.