vendor: update 'google/btree'

This commit is contained in:
Gyu-Ho Lee 2016-09-23 12:54:25 -07:00
parent 15d268709e
commit ecb577d40c
3 changed files with 112 additions and 45 deletions

View File

@ -22,7 +22,7 @@
// See some discussion on the matter here: // See some discussion on the matter here:
// http://google-opensource.blogspot.com/2013/01/c-containers-that-save-memory-and-time.html // http://google-opensource.blogspot.com/2013/01/c-containers-that-save-memory-and-time.html
// Note, though, that this project is in no way related to the C++ B-Tree // Note, though, that this project is in no way related to the C++ B-Tree
// implmentation written about there. // implementation written about there.
// //
// Within this tree, each node contains a slice of items and a (possibly nil) // Within this tree, each node contains a slice of items and a (possibly nil)
// slice of children. For basic numeric values or raw structs, this can cause // slice of children. For basic numeric values or raw structs, this can cause
@ -44,7 +44,7 @@
// widely used ordered tree implementation in the Go ecosystem currently. // widely used ordered tree implementation in the Go ecosystem currently.
// Its functions, therefore, exactly mirror those of // Its functions, therefore, exactly mirror those of
// llrb.LLRB where possible. Unlike gollrb, though, we currently don't // llrb.LLRB where possible. Unlike gollrb, though, we currently don't
// support storing multiple equivalent values or backwards iteration. // support storing multiple equivalent values.
package btree package btree
import ( import (
@ -138,8 +138,8 @@ func (s *items) insertAt(index int, item Item) {
// back. // back.
func (s *items) removeAt(index int) Item { func (s *items) removeAt(index int) Item {
item := (*s)[index] item := (*s)[index]
(*s)[index] = nil
copy((*s)[index:], (*s)[index+1:]) copy((*s)[index:], (*s)[index+1:])
(*s)[len(*s)-1] = nil
*s = (*s)[:len(*s)-1] *s = (*s)[:len(*s)-1]
return item return item
} }
@ -183,8 +183,8 @@ func (s *children) insertAt(index int, n *node) {
// back. // back.
func (s *children) removeAt(index int) *node { func (s *children) removeAt(index int) *node {
n := (*s)[index] n := (*s)[index]
(*s)[index] = nil
copy((*s)[index:], (*s)[index+1:]) copy((*s)[index:], (*s)[index+1:])
(*s)[len(*s)-1] = nil
*s = (*s)[:len(*s)-1] *s = (*s)[:len(*s)-1]
return n return n
} }
@ -419,34 +419,77 @@ func (n *node) growChildAndRemove(i int, item Item, minItems int, typ toRemove)
return n.remove(item, minItems, typ) return n.remove(item, minItems, typ)
} }
type direction int
const (
descend = direction(-1)
ascend = direction(+1)
)
// iterate provides a simple method for iterating over elements in the tree. // iterate provides a simple method for iterating over elements in the tree.
// It could probably use some work to be extra-efficient (it calls from() a
// little more than it should), but it works pretty well for now.
// //
// It requires that 'from' and 'to' both return true for values we should hit // When ascending, the 'start' should be less than 'stop' and when descending,
// with the iterator. It should also be the case that 'from' returns true for // the 'start' should be greater than 'stop'. Setting 'includeStart' to true
// values less than or equal to values 'to' returns true for, and 'to' // will force the iterator to include the first item when it equals 'start',
// returns true for values greater than or equal to those that 'from' // thus creating a "greaterOrEqual" or "lessThanEqual" rather than just a
// does. // "greaterThan" or "lessThan" queries.
func (n *node) iterate(from, to func(Item) bool, iter ItemIterator) bool { func (n *node) iterate(dir direction, start, stop Item, includeStart bool, hit bool, iter ItemIterator) (bool, bool) {
for i, item := range n.items { var ok bool
if !from(item) { switch dir {
case ascend:
for i := 0; i < len(n.items); i++ {
if start != nil && n.items[i].Less(start) {
continue continue
} }
if len(n.children) > 0 && !n.children[i].iterate(from, to, iter) { if len(n.children) > 0 {
return false if hit, ok = n.children[i].iterate(dir, start, stop, includeStart, hit, iter); !ok {
return hit, false
} }
if !to(item) {
return false
} }
if !iter(item) { if !includeStart && !hit && start != nil && !start.Less(n.items[i]) {
return false hit = true
continue
}
hit = true
if stop != nil && !n.items[i].Less(stop) {
return hit, false
}
if !iter(n.items[i]) {
return hit, false
} }
} }
if len(n.children) > 0 { if len(n.children) > 0 {
return n.children[len(n.children)-1].iterate(from, to, iter) if hit, ok = n.children[len(n.children)-1].iterate(dir, start, stop, includeStart, hit, iter); !ok {
return hit, false
} }
return true }
case descend:
for i := len(n.items) - 1; i >= 0; i-- {
if start != nil && !n.items[i].Less(start) {
if !includeStart || hit || start.Less(n.items[i]) {
continue
}
}
if len(n.children) > 0 {
if hit, ok = n.children[i+1].iterate(dir, start, stop, includeStart, hit, iter); !ok {
return hit, false
}
}
if stop != nil && !stop.Less(n.items[i]) {
return hit, false // continue
}
hit = true
if !iter(n.items[i]) {
return hit, false
}
}
if len(n.children) > 0 {
if hit, ok = n.children[0].iterate(dir, start, stop, includeStart, hit, iter); !ok {
return hit, false
}
}
}
return hit, true
} }
// Used for testing/debugging purposes. // Used for testing/debugging purposes.
@ -569,10 +612,7 @@ func (t *BTree) AscendRange(greaterOrEqual, lessThan Item, iterator ItemIterator
if t.root == nil { if t.root == nil {
return return
} }
t.root.iterate( t.root.iterate(ascend, greaterOrEqual, lessThan, true, false, iterator)
func(a Item) bool { return !a.Less(greaterOrEqual) },
func(a Item) bool { return a.Less(lessThan) },
iterator)
} }
// AscendLessThan calls the iterator for every value in the tree within the range // AscendLessThan calls the iterator for every value in the tree within the range
@ -581,10 +621,7 @@ func (t *BTree) AscendLessThan(pivot Item, iterator ItemIterator) {
if t.root == nil { if t.root == nil {
return return
} }
t.root.iterate( t.root.iterate(ascend, nil, pivot, false, false, iterator)
func(a Item) bool { return true },
func(a Item) bool { return a.Less(pivot) },
iterator)
} }
// AscendGreaterOrEqual calls the iterator for every value in the tree within // AscendGreaterOrEqual calls the iterator for every value in the tree within
@ -593,10 +630,7 @@ func (t *BTree) AscendGreaterOrEqual(pivot Item, iterator ItemIterator) {
if t.root == nil { if t.root == nil {
return return
} }
t.root.iterate( t.root.iterate(ascend, pivot, nil, true, false, iterator)
func(a Item) bool { return !a.Less(pivot) },
func(a Item) bool { return true },
iterator)
} }
// Ascend calls the iterator for every value in the tree within the range // Ascend calls the iterator for every value in the tree within the range
@ -605,10 +639,43 @@ func (t *BTree) Ascend(iterator ItemIterator) {
if t.root == nil { if t.root == nil {
return return
} }
t.root.iterate( t.root.iterate(ascend, nil, nil, false, false, iterator)
func(a Item) bool { return true }, }
func(a Item) bool { return true },
iterator) // DescendRange calls the iterator for every value in the tree within the range
// [lessOrEqual, greaterThan), until iterator returns false.
func (t *BTree) DescendRange(lessOrEqual, greaterThan Item, iterator ItemIterator) {
if t.root == nil {
return
}
t.root.iterate(descend, lessOrEqual, greaterThan, true, false, iterator)
}
// DescendLessOrEqual calls the iterator for every value in the tree within the range
// [pivot, first], until iterator returns false.
func (t *BTree) DescendLessOrEqual(pivot Item, iterator ItemIterator) {
if t.root == nil {
return
}
t.root.iterate(descend, pivot, nil, true, false, iterator)
}
// DescendGreaterThan calls the iterator for every value in the tree within
// the range (pivot, last], until iterator returns false.
func (t *BTree) DescendGreaterThan(pivot Item, iterator ItemIterator) {
if t.root == nil {
return
}
t.root.iterate(descend, nil, pivot, false, false, iterator)
}
// Descend calls the iterator for every value in the tree within the range
// [last, first], until iterator returns false.
func (t *BTree) Descend(iterator ItemIterator) {
if t.root == nil {
return
}
t.root.iterate(descend, nil, nil, false, false, iterator)
} }
// Get looks for the key item in the tree, returning it. It returns nil if // Get looks for the key item in the tree, returning it. It returns nil if

6
glide.lock generated
View File

@ -1,5 +1,5 @@
hash: 9d37c5aecd92dfa9b789162e389d87564aa7e7a18f9c1f14ea57d06c0165a1cf hash: 0e7d50c7716873a35f2d14542747525a35af1e5bb734b3696bc1ff78d30107b4
updated: 2016-08-18T20:16:50.783927627-07:00 updated: 2016-09-23T12:52:52.300796603-07:00
imports: imports:
- name: bitbucket.org/ww/goautoneg - name: bitbucket.org/ww/goautoneg
version: 75cd24fc2f2c2a2088577d12123ddee5f54e0675 version: 75cd24fc2f2c2a2088577d12123ddee5f54e0675
@ -54,7 +54,7 @@ imports:
- jsonpb - jsonpb
- proto - proto
- name: github.com/google/btree - name: github.com/google/btree
version: 7d79101e329e5a3adf994758c578dab82b90c017 version: 7364763242911ab6d418d2722e237194938ebad0
- name: github.com/grpc-ecosystem/grpc-gateway - name: github.com/grpc-ecosystem/grpc-gateway
version: f52d055dc48aec25854ed7d31862f78913cf17d1 version: f52d055dc48aec25854ed7d31862f78913cf17d1
subpackages: subpackages:

View File

@ -52,7 +52,7 @@ import:
- jsonpb - jsonpb
- proto - proto
- package: github.com/google/btree - package: github.com/google/btree
version: 7d79101e329e5a3adf994758c578dab82b90c017 version: 7364763242911ab6d418d2722e237194938ebad0
- package: github.com/grpc-ecosystem/grpc-gateway - package: github.com/grpc-ecosystem/grpc-gateway
version: f52d055dc48aec25854ed7d31862f78913cf17d1 version: f52d055dc48aec25854ed7d31862f78913cf17d1
subpackages: subpackages: