Skip to content

Commit

Permalink
Make interval tree inclusive.
Browse files Browse the repository at this point in the history
  • Loading branch information
dzyp committed Dec 7, 2015
1 parent 6e7864c commit 2dca854
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion augmentedtree/atree.go
Expand Up @@ -37,7 +37,7 @@ func intervalOverlaps(n *node, low, high int64, interval Interval, maxDimension
}

func overlaps(high, otherHigh, low, otherLow int64) bool {
return high > otherLow && low < otherHigh
return high >= otherLow && low <= otherHigh
}

// compare returns an int indicating which direction the node
Expand Down
18 changes: 9 additions & 9 deletions augmentedtree/atree_test.go
Expand Up @@ -305,7 +305,7 @@ func constructSingleDimensionQueryTestTree() (

it := newTree(1)

iv1 := constructSingleDimensionInterval(5, 10, 0)
iv1 := constructSingleDimensionInterval(6, 10, 0)
it.Add(iv1)

iv2 := constructSingleDimensionInterval(4, 5, 1)
Expand All @@ -329,7 +329,7 @@ func TestSimpleQuery(t *testing.T) {
func TestRightQuery(t *testing.T) {
it, iv1, _, iv3 := constructSingleDimensionQueryTestTree()

result := it.Query(constructSingleDimensionInterval(5, 8, 0))
result := it.Query(constructSingleDimensionInterval(6, 8, 0))

expected := Intervals{iv1, iv3}
assert.Equal(t, expected, result)
Expand All @@ -356,7 +356,7 @@ func TestMatchingQuery(t *testing.T) {
func TestNoMatchLeft(t *testing.T) {
it, _, _, _ := constructSingleDimensionQueryTestTree()

result := it.Query(constructSingleDimensionInterval(1, 4, 0))
result := it.Query(constructSingleDimensionInterval(1, 3, 0))

expected := Intervals{}
assert.Equal(t, expected, result)
Expand All @@ -365,7 +365,7 @@ func TestNoMatchLeft(t *testing.T) {
func TestNoMatchRight(t *testing.T) {
it, _, _, _ := constructSingleDimensionQueryTestTree()

result := it.Query(constructSingleDimensionInterval(12, 13, 0))
result := it.Query(constructSingleDimensionInterval(13, 13, 0))

expected := Intervals{}
assert.Equal(t, expected, result)
Expand Down Expand Up @@ -605,7 +605,7 @@ func TestInsertSingleAtDimension(t *testing.T) {
assert.Len(t, deleted, 0)
assert.Equal(t, ivs[1:], modified)

result := tree.Query(constructSingleDimensionInterval(10, 20, 0))
result := tree.Query(constructSingleDimensionInterval(11, 20, 0))
assert.Equal(t, ivs[1:], result)
checkRedBlack(t, tree.root, 1)

Expand Down Expand Up @@ -635,7 +635,7 @@ func TestInsertAtLowestIndex(t *testing.T) {
assert.Equal(t, ivs[0:], modified)
assert.Len(t, deleted, 0)

result := tree.Query(constructSingleDimensionInterval(0, 1, 0))
result := tree.Query(constructSingleDimensionInterval(0, 0, 0))
assert.Len(t, result, 0)

result = tree.Query(constructSingleDimensionInterval(1, 4, 0))
Expand All @@ -654,7 +654,7 @@ func TestDeleteSingleAtDimension(t *testing.T) {
assert.Equal(t, ivs[1:], modified)
assert.Len(t, deleted, 0)

result := tree.Query(constructSingleDimensionInterval(10, 20, 0))
result := tree.Query(constructSingleDimensionInterval(11, 20, 0))
assert.Equal(t, ivs[2:], result)

result = tree.Query(constructSingleDimensionInterval(9, 20, 0))
Expand All @@ -681,7 +681,7 @@ func TestDeleteBelowLowestIndex(t *testing.T) {
assert.Equal(t, ivs, modified)
assert.Len(t, deleted, 0)

result := tree.Query(constructSingleDimensionInterval(0, 1, 0))
result := tree.Query(constructSingleDimensionInterval(0, 0, 0))
assert.Equal(t, ivs[:1], result)

result = tree.Query(constructSingleDimensionInterval(0, 10, 0))
Expand All @@ -699,7 +699,7 @@ func TestInsertDeletesInterval(t *testing.T) {
assert.Equal(t, ivs[1:], modified)
assert.Equal(t, ivs[:1], deleted)

result := tree.Query(constructSingleDimensionInterval(2, 10, 0))
result := tree.Query(constructSingleDimensionInterval(3, 10, 0))
assert.Len(t, result, 0)

result = tree.Query(constructSingleDimensionInterval(0, 2, 0))
Expand Down
19 changes: 19 additions & 0 deletions sort/sort.go
Expand Up @@ -70,3 +70,22 @@ func chunk(comparators Comparators, numParts int64) []Comparators {
}
return parts
}

func returnInts() []int {
numCalls := 10
var wg sync.WaitGroup
wg.Add(numCalls)
ints := make([]int, numCalls)

for i := 0; i < numCalls; i++ {
go func(i int) {
result := someRandomDatastoreCall()
ints[i] = result
wg.Done()
}(i)
}

wg.Wait()

return ints
}

0 comments on commit 2dca854

Please sign in to comment.