Skip to content

Commit

Permalink
Merge pull request #113 from Workiva/inclusive_rangetree_ranges
Browse files Browse the repository at this point in the history
Make rangetree ranges inclusive.
  • Loading branch information
dustinhiatt-wf committed Oct 9, 2015
2 parents b6b2f46 + 029fc16 commit 98261e2
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
5 changes: 3 additions & 2 deletions rangetree/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ type Entry interface {
ValueAtDimension(dimension uint64) int64
}

// Interval describes the methods required to query the rangetree.
// Interval describes the methods required to query the rangetree. Note that
// all ranges are inclusive.
type Interval interface {
// LowAtDimension returns an integer representing the lower bound
// at the requested dimension.
Expand All @@ -60,7 +61,7 @@ type RangeTree interface {
// a nil is returned for that entry's index in the provided cells.
Delete(entries ...Entry) Entries
// Query will return a list of entries that fall within
// the provided interval.
// the provided interval. The values at dimensions are inclusive.
Query(interval Interval) Entries
// Apply will call the provided function with each entry that exists
// within the provided range, in order. Return false at any time to
Expand Down
2 changes: 1 addition & 1 deletion rangetree/ordered.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (nodes orderedNodes) apply(low, high int64, fn func(*node) bool) bool {
}

for ; index < len(nodes); index++ {
if nodes[index].value >= high {
if nodes[index].value > high {
break
}

Expand Down
6 changes: 3 additions & 3 deletions rangetree/ordered_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestApply(t *testing.T) {

results := make(nodes, 0, 2)

ns.apply(1, 2, func(n *node) bool {
ns.apply(1, 1, func(n *node) bool {
results = append(results, n)
return true
})
Expand All @@ -90,15 +90,15 @@ func TestApply(t *testing.T) {

results = results[:0]

ns.apply(0, 1, func(n *node) bool {
ns.apply(0, 0, func(n *node) bool {
results = append(results, n)
return true
})

assert.Len(t, results, 0)
results = results[:0]

ns.apply(2, 4, func(n *node) bool {
ns.apply(2, 3, func(n *node) bool {
results = append(results, n)
return true
})
Expand Down
1 change: 0 additions & 1 deletion rangetree/orderedtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rangetree

func isLastDimension(value, test uint64) bool {
Expand Down
12 changes: 6 additions & 6 deletions rangetree/orderedtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestOTRootAddMultipleDimensions(t *testing.T) {

assert.Equal(t, uint64(1), tree.Len())

result := tree.Query(constructMockInterval(dimension{0, 1}, dimension{0, 1}))
result := tree.Query(constructMockInterval(dimension{0, 0}, dimension{0, 0}))
assert.Equal(t, Entries{entries[0]}, result)
}

Expand All @@ -52,7 +52,7 @@ func TestOTMultipleAddMultipleDimensions(t *testing.T) {

assert.Equal(t, uint64(4), tree.Len())

result := tree.Query(constructMockInterval(dimension{0, 1}, dimension{0, 1}))
result := tree.Query(constructMockInterval(dimension{0, 0}, dimension{0, 0}))
assert.Equal(t, Entries{entries[0]}, result)

result = tree.Query(constructMockInterval(dimension{3, 4}, dimension{3, 4}))
Expand All @@ -61,7 +61,7 @@ func TestOTMultipleAddMultipleDimensions(t *testing.T) {
result = tree.Query(constructMockInterval(dimension{0, 4}, dimension{0, 4}))
assert.Equal(t, entries, result)

result = tree.Query(constructMockInterval(dimension{1, 3}, dimension{1, 3}))
result = tree.Query(constructMockInterval(dimension{1, 2}, dimension{1, 2}))
assert.Equal(t, Entries{entries[1], entries[2]}, result)

result = tree.Query(constructMockInterval(dimension{0, 2}, dimension{10, 20}))
Expand All @@ -70,10 +70,10 @@ func TestOTMultipleAddMultipleDimensions(t *testing.T) {
result = tree.Query(constructMockInterval(dimension{10, 20}, dimension{0, 2}))
assert.Len(t, result, 0)

result = tree.Query(constructMockInterval(dimension{0, 2}, dimension{0, 1}))
result = tree.Query(constructMockInterval(dimension{0, 1}, dimension{0, 0}))
assert.Equal(t, Entries{entries[0]}, result)

result = tree.Query(constructMockInterval(dimension{0, 1}, dimension{0, 2}))
result = tree.Query(constructMockInterval(dimension{0, 0}, dimension{0, 1}))
assert.Equal(t, Entries{entries[0]}, result)
}

Expand Down Expand Up @@ -218,7 +218,7 @@ func TestOTDeleteMultiDimensions(t *testing.T) {
result = tree.Query(constructMockInterval(dimension{3, 4}, dimension{3, 4}))
assert.Equal(t, Entries{entries[3]}, result)

result = tree.Query(constructMockInterval(dimension{0, 3}, dimension{0, 3}))
result = tree.Query(constructMockInterval(dimension{0, 2}, dimension{0, 2}))
assert.Equal(t, Entries{entries[0], entries[1]}, result)
}

Expand Down

0 comments on commit 98261e2

Please sign in to comment.