Skip to content

Commit

Permalink
iterative Find()
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jan 11, 2023
1 parent 1b9e8be commit 8869133
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 39 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ goos: linux
goarch: amd64
pkg: github.com/gaissmai/interval
cpu: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
BenchmarkFind/In100-8 11051197 102.8 ns/op 0 B/op 0 allocs/op
BenchmarkFind/In1_000-8 12076317 99.6 ns/op 0 B/op 0 allocs/op
BenchmarkFind/In10_000-8 8264806 145.3 ns/op 0 B/op 0 allocs/op
BenchmarkFind/In100_000-8 2878986 412.3 ns/op 0 B/op 0 allocs/op
BenchmarkFind/In1_000_000-8 4449298 263.9 ns/op 0 B/op 0 allocs/op
BenchmarkFind/In100-8 17299449 63.9 ns/op 0 B/op 0 allocs/op
BenchmarkFind/In1_000-8 17327350 69.4 ns/op 0 B/op 0 allocs/op
BenchmarkFind/In10_000-8 12858908 90.2 ns/op 0 B/op 0 allocs/op
BenchmarkFind/In100_000-8 4696676 256.7 ns/op 0 B/op 0 allocs/op
BenchmarkFind/In1_000_000-8 7131028 163.1 ns/op 0 B/op 0 allocs/op
```
6 changes: 3 additions & 3 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func BenchmarkInsertMutable(b *testing.B) {
}

func BenchmarkDelete(b *testing.B) {
for n := 10; n <= 1_000_000; n *= 10 {
for n := 1; n <= 1_000_000; n *= 10 {
ivals := generateIvals(n)
probe := ivals[rand.Intn(len(ivals))]

Expand All @@ -65,7 +65,7 @@ func BenchmarkDelete(b *testing.B) {
}

func BenchmarkDeleteMutable(b *testing.B) {
for n := 10; n <= 1_000_000; n *= 10 {
for n := 1; n <= 1_000_000; n *= 10 {
ivals := generateIvals(n)
probe := ivals[rand.Intn(len(ivals))]

Expand Down Expand Up @@ -112,7 +112,7 @@ func BenchmarkUnionMutable(b *testing.B) {
}

func BenchmarkFind(b *testing.B) {
for n := 100; n <= 1_000_000; n *= 10 {
for n := 1; n <= 1_000_000; n *= 10 {
ivals := generateIvals(n)
tree := interval.NewTree(ivals...)
probe := ivals[rand.Intn(len(ivals))]
Expand Down
46 changes: 15 additions & 31 deletions treap.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,8 @@ func makeNode[T Interface[T]](item T) *node[T] {

// copyNode, make a shallow copy of the pointers and the item, no recalculation necessary.
func (n *node[T]) copyNode() *node[T] {
if n == nil {
return n
}

m := *n
return &m
c := *n
return &c
}

// Insert elements into the tree, returns the new Tree.
Expand Down Expand Up @@ -293,33 +289,21 @@ func (t *node[T]) split(key T, immutable bool) (left, mid, right *node[T]) {
// otherwise the zero value for item and false.
func (t Tree[T]) Find(item T) (result T, ok bool) {
n := t.root
return n.find(item)
}

func (n *node[T]) find(item T) (result T, ok bool) {
// recursion stop condition(s)
if n == nil {
return
}

// fast exit, node has too small max upper interval value (augmented value)
if item.CompareUpper(n.maxUpper.item) > 0 {
return
}

cmp := compare(item, n.item)
if cmp == 0 {
return n.item, true
}
for {
if n == nil {
return
}

// rec-descent
switch {
case cmp < 0:
return n.left.find(item)
case cmp > 0:
return n.right.find(item)
cmp := compare(item, n.item)
switch {
case cmp == 0:
return n.item, true
case cmp < 0:
n = n.left
case cmp > 0:
n = n.right
}
}
panic("unreachable")
}

// Shortest returns the most specific interval that covers item. ok is true on
Expand Down
4 changes: 4 additions & 0 deletions treap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func TestNewTree(t *testing.T) {
t.Errorf("FprintBST(w) = %v, want \"\"", w.String())
}

if _, ok := zeroTree.Find(zeroItem); ok {
t.Errorf("Find(), got: %v, want: false", ok)
}

if _, ok := zeroTree.Delete(zeroItem); ok {
t.Errorf("Delete(), got: %v, want: false", ok)
}
Expand Down

0 comments on commit 8869133

Please sign in to comment.