From 07b41b253e55263d06351b777e384bb749e40134 Mon Sep 17 00:00:00 2001 From: Graeme Connell Date: Thu, 31 Jul 2014 15:46:25 -0600 Subject: [PATCH] Documentation fixes, small insert optimization. Most of this is finicky documentation/comment fixes. However, we've also changed slice insertion to use copy() instead of a manual for-loop approach. That's sped up the insert benchmark by over 15%, which is pretty nice :) --- README.md | 4 ++-- btree.go | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 88a584a..f319fc3 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # BTree implementation for Go -This package provides an in-memory BTree implementation for Go, useful as a +This package provides an in-memory B-Tree implementation for Go, useful as a an ordered, mutable data structure. The API is based off of the wonderful http://godoc.org/github.com/petar/GoLLRB/llrb, and is meant to allow btree to -act as a drop-in replacement for llrb trees. +act as a drop-in replacement for gollrb trees. See http://godoc.org/github.com/google/btree for documentation. diff --git a/btree.go b/btree.go index 69be0bd..d50af3f 100644 --- a/btree.go +++ b/btree.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package btree implements arbitrary degree B-trees. +// Package btree implements B-Trees of arbitrary degree. // // btree implements an in-memory B-Tree for use as an ordered data structure. // It is not meant for persistent storage solutions. @@ -58,8 +58,8 @@ type items []Item // forward. func (s *items) insertAt(index int, item Item) { *s = append(*s, nil) - for i := len(*s) - 2; i >= index; i-- { - (*s)[i+1] = (*s)[i] + if index < len(*s) { + copy((*s)[index+1:], (*s)[index:]) } (*s)[index] = item } @@ -100,8 +100,8 @@ type children []*node // forward. func (s *children) insertAt(index int, n *node) { *s = append(*s, nil) - for i := len(*s) - 2; i >= index; i-- { - (*s)[i+1] = (*s)[i] + if index < len(*s) { + copy((*s)[index+1:], (*s)[index:]) } (*s)[index] = n } @@ -125,7 +125,7 @@ func (s *children) pop() (out *node) { // node is an internal node in a tree. // // It must at all times maintain the invariant that either -// * len(children) == 0 +// * len(children) == 0, len(items) unconstrained // * len(children) == len(items) + 1 type node struct { items items @@ -319,8 +319,8 @@ func (n *node) growChildAndRemove(i int, item Item, minItems int, typ toRemove) // // It requires that 'from' and 'to' both return true for values we should hit // with the iterator. It should also be the case that 'from' returns true for -// values strictly less than or equal to values 'to' returns true for, and 'to' -// returns true for values strictly greater than or equal to those that 'from' +// values less than or equal to values 'to' returns true for, and 'to' +// returns true for values greater than or equal to those that 'from' // does. func (n *node) iterate(from, to func(Item) bool, iter ItemIterator) bool { for i, item := range n.items { @@ -435,8 +435,8 @@ func (t *BTree) DeleteMin() Item { return t.deleteItem(nil, removeMin) } -// DeleteMax removes an item equal to the passed in item from the tree, returning -// it. If no such item exists, returns nil. +// DeleteMax removes the largest item in the tree and returns it. +// If no such item exists, returns nil. func (t *BTree) DeleteMax() Item { return t.deleteItem(nil, removeMax) }