btree.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. // Copyright 2014 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package btree implements in-memory B-Trees of arbitrary degree.
  15. //
  16. // btree implements an in-memory B-Tree for use as an ordered data structure.
  17. // It is not meant for persistent storage solutions.
  18. //
  19. // It has a flatter structure than an equivalent red-black or other binary tree,
  20. // which in some cases yields better memory usage and/or performance.
  21. // See some discussion on the matter here:
  22. // http://google-opensource.blogspot.com/2013/01/c-containers-that-save-memory-and-time.html
  23. // Note, though, that this project is in no way related to the C++ B-Tree
  24. // implementation written about there.
  25. //
  26. // Within this tree, each node contains a slice of items and a (possibly nil)
  27. // slice of children. For basic numeric values or raw structs, this can cause
  28. // efficiency differences when compared to equivalent C++ template code that
  29. // stores values in arrays within the node:
  30. // * Due to the overhead of storing values as interfaces (each
  31. // value needs to be stored as the value itself, then 2 words for the
  32. // interface pointing to that value and its type), resulting in higher
  33. // memory use.
  34. // * Since interfaces can point to values anywhere in memory, values are
  35. // most likely not stored in contiguous blocks, resulting in a higher
  36. // number of cache misses.
  37. // These issues don't tend to matter, though, when working with strings or other
  38. // heap-allocated structures, since C++-equivalent structures also must store
  39. // pointers and also distribute their values across the heap.
  40. //
  41. // This implementation is designed to be a drop-in replacement to gollrb.LLRB
  42. // trees, (http://github.com/petar/gollrb), an excellent and probably the most
  43. // widely used ordered tree implementation in the Go ecosystem currently.
  44. // Its functions, therefore, exactly mirror those of
  45. // llrb.LLRB where possible. Unlike gollrb, though, we currently don't
  46. // support storing multiple equivalent values.
  47. package btree
  48. import (
  49. "fmt"
  50. "io"
  51. "sort"
  52. "strings"
  53. )
  54. // Item represents a single object in the tree.
  55. type Item interface {
  56. // Less tests whether the current item is less than the given argument.
  57. //
  58. // This must provide a strict weak ordering.
  59. // If !a.Less(b) && !b.Less(a), we treat this to mean a == b (i.e. we can only
  60. // hold one of either a or b in the tree).
  61. Less(than Item) bool
  62. }
  63. const (
  64. DefaultFreeListSize = 32
  65. )
  66. // FreeList represents a free list of btree nodes. By default each
  67. // BTree has its own FreeList, but multiple BTrees can share the same
  68. // FreeList.
  69. // Two Btrees using the same freelist are not safe for concurrent write access.
  70. type FreeList struct {
  71. freelist []*node
  72. }
  73. // NewFreeList creates a new free list.
  74. // size is the maximum size of the returned free list.
  75. func NewFreeList(size int) *FreeList {
  76. return &FreeList{freelist: make([]*node, 0, size)}
  77. }
  78. func (f *FreeList) newNode() (n *node) {
  79. index := len(f.freelist) - 1
  80. if index < 0 {
  81. return new(node)
  82. }
  83. f.freelist, n = f.freelist[:index], f.freelist[index]
  84. return
  85. }
  86. func (f *FreeList) freeNode(n *node) {
  87. if len(f.freelist) < cap(f.freelist) {
  88. f.freelist = append(f.freelist, n)
  89. }
  90. }
  91. // ItemIterator allows callers of Ascend* to iterate in-order over portions of
  92. // the tree. When this function returns false, iteration will stop and the
  93. // associated Ascend* function will immediately return.
  94. type ItemIterator func(i Item) bool
  95. // New creates a new B-Tree with the given degree.
  96. //
  97. // New(2), for example, will create a 2-3-4 tree (each node contains 1-3 items
  98. // and 2-4 children).
  99. func New(degree int) *BTree {
  100. return NewWithFreeList(degree, NewFreeList(DefaultFreeListSize))
  101. }
  102. // NewWithFreeList creates a new B-Tree that uses the given node free list.
  103. func NewWithFreeList(degree int, f *FreeList) *BTree {
  104. if degree <= 1 {
  105. panic("bad degree")
  106. }
  107. return &BTree{
  108. degree: degree,
  109. freelist: f,
  110. }
  111. }
  112. // items stores items in a node.
  113. type items []Item
  114. // insertAt inserts a value into the given index, pushing all subsequent values
  115. // forward.
  116. func (s *items) insertAt(index int, item Item) {
  117. *s = append(*s, nil)
  118. if index < len(*s) {
  119. copy((*s)[index+1:], (*s)[index:])
  120. }
  121. (*s)[index] = item
  122. }
  123. // removeAt removes a value at a given index, pulling all subsequent values
  124. // back.
  125. func (s *items) removeAt(index int) Item {
  126. item := (*s)[index]
  127. copy((*s)[index:], (*s)[index+1:])
  128. (*s)[len(*s)-1] = nil
  129. *s = (*s)[:len(*s)-1]
  130. return item
  131. }
  132. // pop removes and returns the last element in the list.
  133. func (s *items) pop() (out Item) {
  134. index := len(*s) - 1
  135. out = (*s)[index]
  136. (*s)[index] = nil
  137. *s = (*s)[:index]
  138. return
  139. }
  140. // find returns the index where the given item should be inserted into this
  141. // list. 'found' is true if the item already exists in the list at the given
  142. // index.
  143. func (s items) find(item Item) (index int, found bool) {
  144. i := sort.Search(len(s), func(i int) bool {
  145. return item.Less(s[i])
  146. })
  147. if i > 0 && !s[i-1].Less(item) {
  148. return i - 1, true
  149. }
  150. return i, false
  151. }
  152. // children stores child nodes in a node.
  153. type children []*node
  154. // insertAt inserts a value into the given index, pushing all subsequent values
  155. // forward.
  156. func (s *children) insertAt(index int, n *node) {
  157. *s = append(*s, nil)
  158. if index < len(*s) {
  159. copy((*s)[index+1:], (*s)[index:])
  160. }
  161. (*s)[index] = n
  162. }
  163. // removeAt removes a value at a given index, pulling all subsequent values
  164. // back.
  165. func (s *children) removeAt(index int) *node {
  166. n := (*s)[index]
  167. copy((*s)[index:], (*s)[index+1:])
  168. (*s)[len(*s)-1] = nil
  169. *s = (*s)[:len(*s)-1]
  170. return n
  171. }
  172. // pop removes and returns the last element in the list.
  173. func (s *children) pop() (out *node) {
  174. index := len(*s) - 1
  175. out = (*s)[index]
  176. (*s)[index] = nil
  177. *s = (*s)[:index]
  178. return
  179. }
  180. // node is an internal node in a tree.
  181. //
  182. // It must at all times maintain the invariant that either
  183. // * len(children) == 0, len(items) unconstrained
  184. // * len(children) == len(items) + 1
  185. type node struct {
  186. items items
  187. children children
  188. t *BTree
  189. }
  190. // split splits the given node at the given index. The current node shrinks,
  191. // and this function returns the item that existed at that index and a new node
  192. // containing all items/children after it.
  193. func (n *node) split(i int) (Item, *node) {
  194. item := n.items[i]
  195. next := n.t.newNode()
  196. next.items = append(next.items, n.items[i+1:]...)
  197. n.items = n.items[:i]
  198. if len(n.children) > 0 {
  199. next.children = append(next.children, n.children[i+1:]...)
  200. n.children = n.children[:i+1]
  201. }
  202. return item, next
  203. }
  204. // maybeSplitChild checks if a child should be split, and if so splits it.
  205. // Returns whether or not a split occurred.
  206. func (n *node) maybeSplitChild(i, maxItems int) bool {
  207. if len(n.children[i].items) < maxItems {
  208. return false
  209. }
  210. first := n.children[i]
  211. item, second := first.split(maxItems / 2)
  212. n.items.insertAt(i, item)
  213. n.children.insertAt(i+1, second)
  214. return true
  215. }
  216. // insert inserts an item into the subtree rooted at this node, making sure
  217. // no nodes in the subtree exceed maxItems items. Should an equivalent item be
  218. // be found/replaced by insert, it will be returned.
  219. func (n *node) insert(item Item, maxItems int) Item {
  220. i, found := n.items.find(item)
  221. if found {
  222. out := n.items[i]
  223. n.items[i] = item
  224. return out
  225. }
  226. if len(n.children) == 0 {
  227. n.items.insertAt(i, item)
  228. return nil
  229. }
  230. if n.maybeSplitChild(i, maxItems) {
  231. inTree := n.items[i]
  232. switch {
  233. case item.Less(inTree):
  234. // no change, we want first split node
  235. case inTree.Less(item):
  236. i++ // we want second split node
  237. default:
  238. out := n.items[i]
  239. n.items[i] = item
  240. return out
  241. }
  242. }
  243. return n.children[i].insert(item, maxItems)
  244. }
  245. // get finds the given key in the subtree and returns it.
  246. func (n *node) get(key Item) Item {
  247. i, found := n.items.find(key)
  248. if found {
  249. return n.items[i]
  250. } else if len(n.children) > 0 {
  251. return n.children[i].get(key)
  252. }
  253. return nil
  254. }
  255. // min returns the first item in the subtree.
  256. func min(n *node) Item {
  257. if n == nil {
  258. return nil
  259. }
  260. for len(n.children) > 0 {
  261. n = n.children[0]
  262. }
  263. if len(n.items) == 0 {
  264. return nil
  265. }
  266. return n.items[0]
  267. }
  268. // max returns the last item in the subtree.
  269. func max(n *node) Item {
  270. if n == nil {
  271. return nil
  272. }
  273. for len(n.children) > 0 {
  274. n = n.children[len(n.children)-1]
  275. }
  276. if len(n.items) == 0 {
  277. return nil
  278. }
  279. return n.items[len(n.items)-1]
  280. }
  281. // toRemove details what item to remove in a node.remove call.
  282. type toRemove int
  283. const (
  284. removeItem toRemove = iota // removes the given item
  285. removeMin // removes smallest item in the subtree
  286. removeMax // removes largest item in the subtree
  287. )
  288. // remove removes an item from the subtree rooted at this node.
  289. func (n *node) remove(item Item, minItems int, typ toRemove) Item {
  290. var i int
  291. var found bool
  292. switch typ {
  293. case removeMax:
  294. if len(n.children) == 0 {
  295. return n.items.pop()
  296. }
  297. i = len(n.items)
  298. case removeMin:
  299. if len(n.children) == 0 {
  300. return n.items.removeAt(0)
  301. }
  302. i = 0
  303. case removeItem:
  304. i, found = n.items.find(item)
  305. if len(n.children) == 0 {
  306. if found {
  307. return n.items.removeAt(i)
  308. }
  309. return nil
  310. }
  311. default:
  312. panic("invalid type")
  313. }
  314. // If we get to here, we have children.
  315. child := n.children[i]
  316. if len(child.items) <= minItems {
  317. return n.growChildAndRemove(i, item, minItems, typ)
  318. }
  319. // Either we had enough items to begin with, or we've done some
  320. // merging/stealing, because we've got enough now and we're ready to return
  321. // stuff.
  322. if found {
  323. // The item exists at index 'i', and the child we've selected can give us a
  324. // predecessor, since if we've gotten here it's got > minItems items in it.
  325. out := n.items[i]
  326. // We use our special-case 'remove' call with typ=maxItem to pull the
  327. // predecessor of item i (the rightmost leaf of our immediate left child)
  328. // and set it into where we pulled the item from.
  329. n.items[i] = child.remove(nil, minItems, removeMax)
  330. return out
  331. }
  332. // Final recursive call. Once we're here, we know that the item isn't in this
  333. // node and that the child is big enough to remove from.
  334. return child.remove(item, minItems, typ)
  335. }
  336. // growChildAndRemove grows child 'i' to make sure it's possible to remove an
  337. // item from it while keeping it at minItems, then calls remove to actually
  338. // remove it.
  339. //
  340. // Most documentation says we have to do two sets of special casing:
  341. // 1) item is in this node
  342. // 2) item is in child
  343. // In both cases, we need to handle the two subcases:
  344. // A) node has enough values that it can spare one
  345. // B) node doesn't have enough values
  346. // For the latter, we have to check:
  347. // a) left sibling has node to spare
  348. // b) right sibling has node to spare
  349. // c) we must merge
  350. // To simplify our code here, we handle cases #1 and #2 the same:
  351. // If a node doesn't have enough items, we make sure it does (using a,b,c).
  352. // We then simply redo our remove call, and the second time (regardless of
  353. // whether we're in case 1 or 2), we'll have enough items and can guarantee
  354. // that we hit case A.
  355. func (n *node) growChildAndRemove(i int, item Item, minItems int, typ toRemove) Item {
  356. child := n.children[i]
  357. if i > 0 && len(n.children[i-1].items) > minItems {
  358. // Steal from left child
  359. stealFrom := n.children[i-1]
  360. stolenItem := stealFrom.items.pop()
  361. child.items.insertAt(0, n.items[i-1])
  362. n.items[i-1] = stolenItem
  363. if len(stealFrom.children) > 0 {
  364. child.children.insertAt(0, stealFrom.children.pop())
  365. }
  366. } else if i < len(n.items) && len(n.children[i+1].items) > minItems {
  367. // steal from right child
  368. stealFrom := n.children[i+1]
  369. stolenItem := stealFrom.items.removeAt(0)
  370. child.items = append(child.items, n.items[i])
  371. n.items[i] = stolenItem
  372. if len(stealFrom.children) > 0 {
  373. child.children = append(child.children, stealFrom.children.removeAt(0))
  374. }
  375. } else {
  376. if i >= len(n.items) {
  377. i--
  378. child = n.children[i]
  379. }
  380. // merge with right child
  381. mergeItem := n.items.removeAt(i)
  382. mergeChild := n.children.removeAt(i + 1)
  383. child.items = append(child.items, mergeItem)
  384. child.items = append(child.items, mergeChild.items...)
  385. child.children = append(child.children, mergeChild.children...)
  386. n.t.freeNode(mergeChild)
  387. }
  388. return n.remove(item, minItems, typ)
  389. }
  390. type direction int
  391. const (
  392. descend = direction(-1)
  393. ascend = direction(+1)
  394. )
  395. // iterate provides a simple method for iterating over elements in the tree.
  396. //
  397. // When ascending, the 'start' should be less than 'stop' and when descending,
  398. // the 'start' should be greater than 'stop'. Setting 'includeStart' to true
  399. // will force the iterator to include the first item when it equals 'start',
  400. // thus creating a "greaterOrEqual" or "lessThanEqual" rather than just a
  401. // "greaterThan" or "lessThan" queries.
  402. func (n *node) iterate(dir direction, start, stop Item, includeStart bool, hit bool, iter ItemIterator) (bool, bool) {
  403. var ok bool
  404. switch dir {
  405. case ascend:
  406. for i := 0; i < len(n.items); i++ {
  407. if start != nil && n.items[i].Less(start) {
  408. continue
  409. }
  410. if len(n.children) > 0 {
  411. if hit, ok = n.children[i].iterate(dir, start, stop, includeStart, hit, iter); !ok {
  412. return hit, false
  413. }
  414. }
  415. if !includeStart && !hit && start != nil && !start.Less(n.items[i]) {
  416. hit = true
  417. continue
  418. }
  419. hit = true
  420. if stop != nil && !n.items[i].Less(stop) {
  421. return hit, false
  422. }
  423. if !iter(n.items[i]) {
  424. return hit, false
  425. }
  426. }
  427. if len(n.children) > 0 {
  428. if hit, ok = n.children[len(n.children)-1].iterate(dir, start, stop, includeStart, hit, iter); !ok {
  429. return hit, false
  430. }
  431. }
  432. case descend:
  433. for i := len(n.items) - 1; i >= 0; i-- {
  434. if start != nil && !n.items[i].Less(start) {
  435. if !includeStart || hit || start.Less(n.items[i]) {
  436. continue
  437. }
  438. }
  439. if len(n.children) > 0 {
  440. if hit, ok = n.children[i+1].iterate(dir, start, stop, includeStart, hit, iter); !ok {
  441. return hit, false
  442. }
  443. }
  444. if stop != nil && !stop.Less(n.items[i]) {
  445. return hit, false // continue
  446. }
  447. hit = true
  448. if !iter(n.items[i]) {
  449. return hit, false
  450. }
  451. }
  452. if len(n.children) > 0 {
  453. if hit, ok = n.children[0].iterate(dir, start, stop, includeStart, hit, iter); !ok {
  454. return hit, false
  455. }
  456. }
  457. }
  458. return hit, true
  459. }
  460. // Used for testing/debugging purposes.
  461. func (n *node) print(w io.Writer, level int) {
  462. fmt.Fprintf(w, "%sNODE:%v\n", strings.Repeat(" ", level), n.items)
  463. for _, c := range n.children {
  464. c.print(w, level+1)
  465. }
  466. }
  467. // BTree is an implementation of a B-Tree.
  468. //
  469. // BTree stores Item instances in an ordered structure, allowing easy insertion,
  470. // removal, and iteration.
  471. //
  472. // Write operations are not safe for concurrent mutation by multiple
  473. // goroutines, but Read operations are.
  474. type BTree struct {
  475. degree int
  476. length int
  477. root *node
  478. freelist *FreeList
  479. }
  480. // maxItems returns the max number of items to allow per node.
  481. func (t *BTree) maxItems() int {
  482. return t.degree*2 - 1
  483. }
  484. // minItems returns the min number of items to allow per node (ignored for the
  485. // root node).
  486. func (t *BTree) minItems() int {
  487. return t.degree - 1
  488. }
  489. func (t *BTree) newNode() (n *node) {
  490. n = t.freelist.newNode()
  491. n.t = t
  492. return
  493. }
  494. func (t *BTree) freeNode(n *node) {
  495. for i := range n.items {
  496. n.items[i] = nil // clear to allow GC
  497. }
  498. n.items = n.items[:0]
  499. for i := range n.children {
  500. n.children[i] = nil // clear to allow GC
  501. }
  502. n.children = n.children[:0]
  503. n.t = nil // clear to allow GC
  504. t.freelist.freeNode(n)
  505. }
  506. // ReplaceOrInsert adds the given item to the tree. If an item in the tree
  507. // already equals the given one, it is removed from the tree and returned.
  508. // Otherwise, nil is returned.
  509. //
  510. // nil cannot be added to the tree (will panic).
  511. func (t *BTree) ReplaceOrInsert(item Item) Item {
  512. if item == nil {
  513. panic("nil item being added to BTree")
  514. }
  515. if t.root == nil {
  516. t.root = t.newNode()
  517. t.root.items = append(t.root.items, item)
  518. t.length++
  519. return nil
  520. } else if len(t.root.items) >= t.maxItems() {
  521. item2, second := t.root.split(t.maxItems() / 2)
  522. oldroot := t.root
  523. t.root = t.newNode()
  524. t.root.items = append(t.root.items, item2)
  525. t.root.children = append(t.root.children, oldroot, second)
  526. }
  527. out := t.root.insert(item, t.maxItems())
  528. if out == nil {
  529. t.length++
  530. }
  531. return out
  532. }
  533. // Delete removes an item equal to the passed in item from the tree, returning
  534. // it. If no such item exists, returns nil.
  535. func (t *BTree) Delete(item Item) Item {
  536. return t.deleteItem(item, removeItem)
  537. }
  538. // DeleteMin removes the smallest item in the tree and returns it.
  539. // If no such item exists, returns nil.
  540. func (t *BTree) DeleteMin() Item {
  541. return t.deleteItem(nil, removeMin)
  542. }
  543. // DeleteMax removes the largest item in the tree and returns it.
  544. // If no such item exists, returns nil.
  545. func (t *BTree) DeleteMax() Item {
  546. return t.deleteItem(nil, removeMax)
  547. }
  548. func (t *BTree) deleteItem(item Item, typ toRemove) Item {
  549. if t.root == nil || len(t.root.items) == 0 {
  550. return nil
  551. }
  552. out := t.root.remove(item, t.minItems(), typ)
  553. if len(t.root.items) == 0 && len(t.root.children) > 0 {
  554. oldroot := t.root
  555. t.root = t.root.children[0]
  556. t.freeNode(oldroot)
  557. }
  558. if out != nil {
  559. t.length--
  560. }
  561. return out
  562. }
  563. // AscendRange calls the iterator for every value in the tree within the range
  564. // [greaterOrEqual, lessThan), until iterator returns false.
  565. func (t *BTree) AscendRange(greaterOrEqual, lessThan Item, iterator ItemIterator) {
  566. if t.root == nil {
  567. return
  568. }
  569. t.root.iterate(ascend, greaterOrEqual, lessThan, true, false, iterator)
  570. }
  571. // AscendLessThan calls the iterator for every value in the tree within the range
  572. // [first, pivot), until iterator returns false.
  573. func (t *BTree) AscendLessThan(pivot Item, iterator ItemIterator) {
  574. if t.root == nil {
  575. return
  576. }
  577. t.root.iterate(ascend, nil, pivot, false, false, iterator)
  578. }
  579. // AscendGreaterOrEqual calls the iterator for every value in the tree within
  580. // the range [pivot, last], until iterator returns false.
  581. func (t *BTree) AscendGreaterOrEqual(pivot Item, iterator ItemIterator) {
  582. if t.root == nil {
  583. return
  584. }
  585. t.root.iterate(ascend, pivot, nil, true, false, iterator)
  586. }
  587. // Ascend calls the iterator for every value in the tree within the range
  588. // [first, last], until iterator returns false.
  589. func (t *BTree) Ascend(iterator ItemIterator) {
  590. if t.root == nil {
  591. return
  592. }
  593. t.root.iterate(ascend, nil, nil, false, false, iterator)
  594. }
  595. // DescendRange calls the iterator for every value in the tree within the range
  596. // [lessOrEqual, greaterThan), until iterator returns false.
  597. func (t *BTree) DescendRange(lessOrEqual, greaterThan Item, iterator ItemIterator) {
  598. if t.root == nil {
  599. return
  600. }
  601. t.root.iterate(descend, lessOrEqual, greaterThan, true, false, iterator)
  602. }
  603. // DescendLessOrEqual calls the iterator for every value in the tree within the range
  604. // [pivot, first], until iterator returns false.
  605. func (t *BTree) DescendLessOrEqual(pivot Item, iterator ItemIterator) {
  606. if t.root == nil {
  607. return
  608. }
  609. t.root.iterate(descend, pivot, nil, true, false, iterator)
  610. }
  611. // DescendGreaterThan calls the iterator for every value in the tree within
  612. // the range (pivot, last], until iterator returns false.
  613. func (t *BTree) DescendGreaterThan(pivot Item, iterator ItemIterator) {
  614. if t.root == nil {
  615. return
  616. }
  617. t.root.iterate(descend, nil, pivot, false, false, iterator)
  618. }
  619. // Descend calls the iterator for every value in the tree within the range
  620. // [last, first], until iterator returns false.
  621. func (t *BTree) Descend(iterator ItemIterator) {
  622. if t.root == nil {
  623. return
  624. }
  625. t.root.iterate(descend, nil, nil, false, false, iterator)
  626. }
  627. // Get looks for the key item in the tree, returning it. It returns nil if
  628. // unable to find that item.
  629. func (t *BTree) Get(key Item) Item {
  630. if t.root == nil {
  631. return nil
  632. }
  633. return t.root.get(key)
  634. }
  635. // Min returns the smallest item in the tree, or nil if the tree is empty.
  636. func (t *BTree) Min() Item {
  637. return min(t.root)
  638. }
  639. // Max returns the largest item in the tree, or nil if the tree is empty.
  640. func (t *BTree) Max() Item {
  641. return max(t.root)
  642. }
  643. // Has returns true if the given key is in the tree.
  644. func (t *BTree) Has(key Item) bool {
  645. return t.Get(key) != nil
  646. }
  647. // Len returns the number of items currently in the tree.
  648. func (t *BTree) Len() int {
  649. return t.length
  650. }
  651. // Int implements the Item interface for integers.
  652. type Int int
  653. // Less returns true if int(a) < int(b).
  654. func (a Int) Less(b Item) bool {
  655. return a < b.(Int)
  656. }