You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1019 lines
30 KiB

  1. // Copyright 2014 Google LLC
  2. // Modified 2018 by Jonathan Amsterdam (jbamsterdam@gmail.com)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // Package btree implements in-memory B-Trees of arbitrary degree.
  16. //
  17. // This implementation is based on google/btree (http://github.com/google/btree), and
  18. // much of the code is taken from there. But the API has been changed significantly,
  19. // particularly around iteration, and support for indexing by position has been
  20. // added.
  21. //
  22. // btree implements an in-memory B-Tree for use as an ordered data structure.
  23. // It is not meant for persistent storage solutions.
  24. //
  25. // It has a flatter structure than an equivalent red-black or other binary tree,
  26. // which in some cases yields better memory usage and/or performance.
  27. // See some discussion on the matter here:
  28. // http://google-opensource.blogspot.com/2013/01/c-containers-that-save-memory-and-time.html
  29. // Note, though, that this project is in no way related to the C++ B-Tree
  30. // implementation written about there.
  31. //
  32. // Within this tree, each node contains a slice of items and a (possibly nil)
  33. // slice of children. For basic numeric values or raw structs, this can cause
  34. // efficiency differences when compared to equivalent C++ template code that
  35. // stores values in arrays within the node:
  36. // * Due to the overhead of storing values as interfaces (each
  37. // value needs to be stored as the value itself, then 2 words for the
  38. // interface pointing to that value and its type), resulting in higher
  39. // memory use.
  40. // * Since interfaces can point to values anywhere in memory, values are
  41. // most likely not stored in contiguous blocks, resulting in a higher
  42. // number of cache misses.
  43. // These issues don't tend to matter, though, when working with strings or other
  44. // heap-allocated structures, since C++-equivalent structures also must store
  45. // pointers and also distribute their values across the heap.
  46. package btree
  47. import (
  48. "fmt"
  49. "sort"
  50. "sync"
  51. )
  52. // Key represents a key into the tree.
  53. type Key interface{}
  54. type Value interface{}
  55. // item is a key-value pair.
  56. type item struct {
  57. key Key
  58. value Value
  59. }
  60. type lessFunc func(interface{}, interface{}) bool
  61. // New creates a new B-Tree with the given degree and comparison function.
  62. //
  63. // New(2, less), for example, will create a 2-3-4 tree (each node contains 1-3 items
  64. // and 2-4 children).
  65. //
  66. // The less function tests whether the current item is less than the given argument.
  67. // It must provide a strict weak ordering.
  68. // If !less(a, b) && !less(b, a), we treat this to mean a == b (i.e. the tree
  69. // can hold only one of a or b).
  70. func New(degree int, less func(interface{}, interface{}) bool) *BTree {
  71. if degree <= 1 {
  72. panic("bad degree")
  73. }
  74. return &BTree{
  75. degree: degree,
  76. less: less,
  77. cow: &copyOnWriteContext{},
  78. }
  79. }
  80. // items stores items in a node.
  81. type items []item
  82. // insertAt inserts a value into the given index, pushing all subsequent values
  83. // forward.
  84. func (s *items) insertAt(index int, m item) {
  85. *s = append(*s, item{})
  86. if index < len(*s) {
  87. copy((*s)[index+1:], (*s)[index:])
  88. }
  89. (*s)[index] = m
  90. }
  91. // removeAt removes a value at a given index, pulling all subsequent values
  92. // back.
  93. func (s *items) removeAt(index int) item {
  94. m := (*s)[index]
  95. copy((*s)[index:], (*s)[index+1:])
  96. (*s)[len(*s)-1] = item{}
  97. *s = (*s)[:len(*s)-1]
  98. return m
  99. }
  100. // pop removes and returns the last element in the list.
  101. func (s *items) pop() item {
  102. index := len(*s) - 1
  103. out := (*s)[index]
  104. (*s)[index] = item{}
  105. *s = (*s)[:index]
  106. return out
  107. }
  108. var nilItems = make(items, 16)
  109. // truncate truncates this instance at index so that it contains only the
  110. // first index items. index must be less than or equal to length.
  111. func (s *items) truncate(index int) {
  112. var toClear items
  113. *s, toClear = (*s)[:index], (*s)[index:]
  114. for len(toClear) > 0 {
  115. toClear = toClear[copy(toClear, nilItems):]
  116. }
  117. }
  118. // find returns the index where an item with key should be inserted into this
  119. // list. 'found' is true if the item already exists in the list at the given
  120. // index.
  121. func (s items) find(k Key, less lessFunc) (index int, found bool) {
  122. i := sort.Search(len(s), func(i int) bool { return less(k, s[i].key) })
  123. // i is the smallest index of s for which k.Less(s[i].Key), or len(s).
  124. if i > 0 && !less(s[i-1].key, k) {
  125. return i - 1, true
  126. }
  127. return i, false
  128. }
  129. // children stores child nodes in a node.
  130. type children []*node
  131. // insertAt inserts a value into the given index, pushing all subsequent values
  132. // forward.
  133. func (s *children) insertAt(index int, n *node) {
  134. *s = append(*s, nil)
  135. if index < len(*s) {
  136. copy((*s)[index+1:], (*s)[index:])
  137. }
  138. (*s)[index] = n
  139. }
  140. // removeAt removes a value at a given index, pulling all subsequent values
  141. // back.
  142. func (s *children) removeAt(index int) *node {
  143. n := (*s)[index]
  144. copy((*s)[index:], (*s)[index+1:])
  145. (*s)[len(*s)-1] = nil
  146. *s = (*s)[:len(*s)-1]
  147. return n
  148. }
  149. // pop removes and returns the last element in the list.
  150. func (s *children) pop() (out *node) {
  151. index := len(*s) - 1
  152. out = (*s)[index]
  153. (*s)[index] = nil
  154. *s = (*s)[:index]
  155. return
  156. }
  157. var nilChildren = make(children, 16)
  158. // truncate truncates this instance at index so that it contains only the
  159. // first index children. index must be less than or equal to length.
  160. func (s *children) truncate(index int) {
  161. var toClear children
  162. *s, toClear = (*s)[:index], (*s)[index:]
  163. for len(toClear) > 0 {
  164. toClear = toClear[copy(toClear, nilChildren):]
  165. }
  166. }
  167. // node is an internal node in a tree.
  168. //
  169. // It must at all times maintain the invariant that either
  170. // * len(children) == 0, len(items) unconstrained
  171. // * len(children) == len(items) + 1
  172. type node struct {
  173. items items
  174. children children
  175. size int // number of items in the subtree: len(items) + sum over i of children[i].size
  176. cow *copyOnWriteContext
  177. }
  178. func (n *node) computeSize() int {
  179. sz := len(n.items)
  180. for _, c := range n.children {
  181. sz += c.size
  182. }
  183. return sz
  184. }
  185. func (n *node) checkSize() {
  186. sz := n.computeSize()
  187. if n.size != sz {
  188. panic(fmt.Sprintf("n.size = %d, computed size = %d", n.size, sz))
  189. }
  190. }
  191. func (n *node) mutableFor(cow *copyOnWriteContext) *node {
  192. if n.cow == cow {
  193. return n
  194. }
  195. out := cow.newNode()
  196. if cap(out.items) >= len(n.items) {
  197. out.items = out.items[:len(n.items)]
  198. } else {
  199. out.items = make(items, len(n.items), cap(n.items))
  200. }
  201. copy(out.items, n.items)
  202. // Copy children
  203. if cap(out.children) >= len(n.children) {
  204. out.children = out.children[:len(n.children)]
  205. } else {
  206. out.children = make(children, len(n.children), cap(n.children))
  207. }
  208. copy(out.children, n.children)
  209. out.size = n.size
  210. return out
  211. }
  212. func (n *node) mutableChild(i int) *node {
  213. c := n.children[i].mutableFor(n.cow)
  214. n.children[i] = c
  215. return c
  216. }
  217. // split splits the given node at the given index. The current node shrinks,
  218. // and this function returns the item that existed at that index and a new node
  219. // containing all items/children after it.
  220. func (n *node) split(i int) (item, *node) {
  221. item := n.items[i]
  222. next := n.cow.newNode()
  223. next.items = append(next.items, n.items[i+1:]...)
  224. n.items.truncate(i)
  225. if len(n.children) > 0 {
  226. next.children = append(next.children, n.children[i+1:]...)
  227. n.children.truncate(i + 1)
  228. }
  229. n.size = n.computeSize()
  230. next.size = next.computeSize()
  231. return item, next
  232. }
  233. // maybeSplitChild checks if a child should be split, and if so splits it.
  234. // Returns whether or not a split occurred.
  235. func (n *node) maybeSplitChild(i, maxItems int) bool {
  236. if len(n.children[i].items) < maxItems {
  237. return false
  238. }
  239. first := n.mutableChild(i)
  240. item, second := first.split(maxItems / 2)
  241. n.items.insertAt(i, item)
  242. n.children.insertAt(i+1, second)
  243. // The size of n doesn't change.
  244. return true
  245. }
  246. // insert inserts an item into the subtree rooted at this node, making sure
  247. // no nodes in the subtree exceed maxItems items. Should an equivalent item be
  248. // be found/replaced by insert, its value will be returned.
  249. //
  250. // If computeIndex is true, the third return value is the index of the value with respect to n.
  251. func (n *node) insert(m item, maxItems int, less lessFunc, computeIndex bool) (old Value, present bool, idx int) {
  252. i, found := n.items.find(m.key, less)
  253. if found {
  254. out := n.items[i]
  255. n.items[i] = m
  256. if computeIndex {
  257. idx = n.itemIndex(i)
  258. }
  259. return out.value, true, idx
  260. }
  261. if len(n.children) == 0 {
  262. n.items.insertAt(i, m)
  263. n.size++
  264. return old, false, i
  265. }
  266. if n.maybeSplitChild(i, maxItems) {
  267. inTree := n.items[i]
  268. switch {
  269. case less(m.key, inTree.key):
  270. // no change, we want first split node
  271. case less(inTree.key, m.key):
  272. i++ // we want second split node
  273. default:
  274. out := n.items[i]
  275. n.items[i] = m
  276. if computeIndex {
  277. idx = n.itemIndex(i)
  278. }
  279. return out.value, true, idx
  280. }
  281. }
  282. old, present, idx = n.mutableChild(i).insert(m, maxItems, less, computeIndex)
  283. if !present {
  284. n.size++
  285. }
  286. if computeIndex {
  287. idx += n.partialSize(i)
  288. }
  289. return old, present, idx
  290. }
  291. // get finds the given key in the subtree and returns the corresponding item, along with a boolean reporting
  292. // whether it was found.
  293. // If computeIndex is true, it also returns the index of the key relative to the node's subtree.
  294. func (n *node) get(k Key, computeIndex bool, less lessFunc) (item, bool, int) {
  295. i, found := n.items.find(k, less)
  296. if found {
  297. return n.items[i], true, n.itemIndex(i)
  298. }
  299. if len(n.children) > 0 {
  300. m, found, idx := n.children[i].get(k, computeIndex, less)
  301. if computeIndex && found {
  302. idx += n.partialSize(i)
  303. }
  304. return m, found, idx
  305. }
  306. return item{}, false, -1
  307. }
  308. // itemIndex returns the index w.r.t. n of the ith item in n.
  309. func (n *node) itemIndex(i int) int {
  310. if len(n.children) == 0 {
  311. return i
  312. }
  313. // Get the size of the node up to but not including the child to the right of
  314. // item i. Subtract 1 because the index is 0-based.
  315. return n.partialSize(i+1) - 1
  316. }
  317. // Returns the size of the non-leaf node up to but not including child i.
  318. func (n *node) partialSize(i int) int {
  319. var sz int
  320. for j, c := range n.children {
  321. if j == i {
  322. break
  323. }
  324. sz += c.size + 1
  325. }
  326. return sz
  327. }
  328. // cursorStackForKey returns a stack of cursors for the key, along with whether the key was found and the index.
  329. func (n *node) cursorStackForKey(k Key, cs cursorStack, less lessFunc) (cursorStack, bool, int) {
  330. i, found := n.items.find(k, less)
  331. cs.push(cursor{n, i})
  332. idx := i
  333. if found {
  334. if len(n.children) > 0 {
  335. idx = n.partialSize(i+1) - 1
  336. }
  337. return cs, true, idx
  338. }
  339. if len(n.children) > 0 {
  340. cs, found, idx := n.children[i].cursorStackForKey(k, cs, less)
  341. return cs, found, idx + n.partialSize(i)
  342. }
  343. return cs, false, idx
  344. }
  345. // at returns the item at the i'th position in the subtree rooted at n.
  346. // It assumes i is in range.
  347. func (n *node) at(i int) item {
  348. if len(n.children) == 0 {
  349. return n.items[i]
  350. }
  351. for j, c := range n.children {
  352. if i < c.size {
  353. return c.at(i)
  354. }
  355. i -= c.size
  356. if i == 0 {
  357. return n.items[j]
  358. }
  359. i--
  360. }
  361. panic("impossible")
  362. }
  363. // cursorStackForIndex returns a stack of cursors for the index.
  364. // It assumes i is in range.
  365. func (n *node) cursorStackForIndex(i int, cs cursorStack) cursorStack {
  366. if len(n.children) == 0 {
  367. return cs.push(cursor{n, i})
  368. }
  369. for j, c := range n.children {
  370. if i < c.size {
  371. return c.cursorStackForIndex(i, cs.push(cursor{n, j}))
  372. }
  373. i -= c.size
  374. if i == 0 {
  375. return cs.push(cursor{n, j})
  376. }
  377. i--
  378. }
  379. panic("impossible")
  380. }
  381. // toRemove details what item to remove in a node.remove call.
  382. type toRemove int
  383. const (
  384. removeItem toRemove = iota // removes the given item
  385. removeMin // removes smallest item in the subtree
  386. removeMax // removes largest item in the subtree
  387. )
  388. // remove removes an item from the subtree rooted at this node.
  389. func (n *node) remove(key Key, minItems int, typ toRemove, less lessFunc) (item, bool) {
  390. var i int
  391. var found bool
  392. switch typ {
  393. case removeMax:
  394. if len(n.children) == 0 {
  395. n.size--
  396. return n.items.pop(), true
  397. }
  398. i = len(n.items)
  399. case removeMin:
  400. if len(n.children) == 0 {
  401. n.size--
  402. return n.items.removeAt(0), true
  403. }
  404. i = 0
  405. case removeItem:
  406. i, found = n.items.find(key, less)
  407. if len(n.children) == 0 {
  408. if found {
  409. n.size--
  410. return n.items.removeAt(i), true
  411. }
  412. return item{}, false
  413. }
  414. default:
  415. panic("invalid type")
  416. }
  417. // If we get to here, we have children.
  418. if len(n.children[i].items) <= minItems {
  419. return n.growChildAndRemove(i, key, minItems, typ, less)
  420. }
  421. child := n.mutableChild(i)
  422. // Either we had enough items to begin with, or we've done some
  423. // merging/stealing, because we've got enough now and we're ready to return
  424. // stuff.
  425. if found {
  426. // The item exists at index 'i', and the child we've selected can give us a
  427. // predecessor, since if we've gotten here it's got > minItems items in it.
  428. out := n.items[i]
  429. // We use our special-case 'remove' call with typ=maxItem to pull the
  430. // predecessor of item i (the rightmost leaf of our immediate left child)
  431. // and set it into where we pulled the item from.
  432. n.items[i], _ = child.remove(nil, minItems, removeMax, less)
  433. n.size--
  434. return out, true
  435. }
  436. // Final recursive call. Once we're here, we know that the item isn't in this
  437. // node and that the child is big enough to remove from.
  438. m, removed := child.remove(key, minItems, typ, less)
  439. if removed {
  440. n.size--
  441. }
  442. return m, removed
  443. }
  444. // growChildAndRemove grows child 'i' to make sure it's possible to remove an
  445. // item from it while keeping it at minItems, then calls remove to actually
  446. // remove it.
  447. //
  448. // Most documentation says we have to do two sets of special casing:
  449. // 1) item is in this node
  450. // 2) item is in child
  451. // In both cases, we need to handle the two subcases:
  452. // A) node has enough values that it can spare one
  453. // B) node doesn't have enough values
  454. // For the latter, we have to check:
  455. // a) left sibling has node to spare
  456. // b) right sibling has node to spare
  457. // c) we must merge
  458. // To simplify our code here, we handle cases #1 and #2 the same:
  459. // If a node doesn't have enough items, we make sure it does (using a,b,c).
  460. // We then simply redo our remove call, and the second time (regardless of
  461. // whether we're in case 1 or 2), we'll have enough items and can guarantee
  462. // that we hit case A.
  463. func (n *node) growChildAndRemove(i int, key Key, minItems int, typ toRemove, less lessFunc) (item, bool) {
  464. if i > 0 && len(n.children[i-1].items) > minItems {
  465. // Steal from left child
  466. child := n.mutableChild(i)
  467. stealFrom := n.mutableChild(i - 1)
  468. stolenItem := stealFrom.items.pop()
  469. stealFrom.size--
  470. child.items.insertAt(0, n.items[i-1])
  471. child.size++
  472. n.items[i-1] = stolenItem
  473. if len(stealFrom.children) > 0 {
  474. c := stealFrom.children.pop()
  475. stealFrom.size -= c.size
  476. child.children.insertAt(0, c)
  477. child.size += c.size
  478. }
  479. } else if i < len(n.items) && len(n.children[i+1].items) > minItems {
  480. // steal from right child
  481. child := n.mutableChild(i)
  482. stealFrom := n.mutableChild(i + 1)
  483. stolenItem := stealFrom.items.removeAt(0)
  484. stealFrom.size--
  485. child.items = append(child.items, n.items[i])
  486. child.size++
  487. n.items[i] = stolenItem
  488. if len(stealFrom.children) > 0 {
  489. c := stealFrom.children.removeAt(0)
  490. stealFrom.size -= c.size
  491. child.children = append(child.children, c)
  492. child.size += c.size
  493. }
  494. } else {
  495. if i >= len(n.items) {
  496. i--
  497. }
  498. child := n.mutableChild(i)
  499. // merge with right child
  500. mergeItem := n.items.removeAt(i)
  501. mergeChild := n.children.removeAt(i + 1)
  502. child.items = append(child.items, mergeItem)
  503. child.items = append(child.items, mergeChild.items...)
  504. child.children = append(child.children, mergeChild.children...)
  505. child.size = child.computeSize()
  506. n.cow.freeNode(mergeChild)
  507. }
  508. return n.remove(key, minItems, typ, less)
  509. }
  510. // BTree is an implementation of a B-Tree.
  511. //
  512. // BTree stores item instances in an ordered structure, allowing easy insertion,
  513. // removal, and iteration.
  514. //
  515. // Write operations are not safe for concurrent mutation by multiple
  516. // goroutines, but Read operations are.
  517. type BTree struct {
  518. degree int
  519. less lessFunc
  520. root *node
  521. cow *copyOnWriteContext
  522. }
  523. // copyOnWriteContext pointers determine node ownership. A tree with a cow
  524. // context equivalent to a node's cow context is allowed to modify that node.
  525. // A tree whose write context does not match a node's is not allowed to modify
  526. // it, and must create a new, writable copy (IE: it's a Clone).
  527. //
  528. // When doing any write operation, we maintain the invariant that the current
  529. // node's context is equal to the context of the tree that requested the write.
  530. // We do this by, before we descend into any node, creating a copy with the
  531. // correct context if the contexts don't match.
  532. //
  533. // Since the node we're currently visiting on any write has the requesting
  534. // tree's context, that node is modifiable in place. Children of that node may
  535. // not share context, but before we descend into them, we'll make a mutable
  536. // copy.
  537. type copyOnWriteContext struct{ byte } // non-empty, because empty structs may have same addr
  538. // Clone clones the btree, lazily. Clone should not be called concurrently,
  539. // but the original tree (t) and the new tree (t2) can be used concurrently
  540. // once the Clone call completes.
  541. //
  542. // The internal tree structure of b is marked read-only and shared between t and
  543. // t2. Writes to both t and t2 use copy-on-write logic, creating new nodes
  544. // whenever one of b's original nodes would have been modified. Read operations
  545. // should have no performance degredation. Write operations for both t and t2
  546. // will initially experience minor slow-downs caused by additional allocs and
  547. // copies due to the aforementioned copy-on-write logic, but should converge to
  548. // the original performance characteristics of the original tree.
  549. func (t *BTree) Clone() *BTree {
  550. // Create two entirely new copy-on-write contexts.
  551. // This operation effectively creates three trees:
  552. // the original, shared nodes (old b.cow)
  553. // the new b.cow nodes
  554. // the new out.cow nodes
  555. cow1, cow2 := *t.cow, *t.cow
  556. out := *t
  557. t.cow = &cow1
  558. out.cow = &cow2
  559. return &out
  560. }
  561. // maxItems returns the max number of items to allow per node.
  562. func (t *BTree) maxItems() int {
  563. return t.degree*2 - 1
  564. }
  565. // minItems returns the min number of items to allow per node (ignored for the
  566. // root node).
  567. func (t *BTree) minItems() int {
  568. return t.degree - 1
  569. }
  570. var nodePool = sync.Pool{New: func() interface{} { return new(node) }}
  571. func (c *copyOnWriteContext) newNode() *node {
  572. n := nodePool.Get().(*node)
  573. n.cow = c
  574. return n
  575. }
  576. func (c *copyOnWriteContext) freeNode(n *node) {
  577. if n.cow == c {
  578. // clear to allow GC
  579. n.items.truncate(0)
  580. n.children.truncate(0)
  581. n.cow = nil
  582. nodePool.Put(n)
  583. }
  584. }
  585. // Set sets the given key to the given value in the tree. If the key is present in
  586. // the tree, its value is changed and the old value is returned along with a second
  587. // return value of true. If the key is not in the tree, it is added, and the second
  588. // return value is false.
  589. func (t *BTree) Set(k Key, v Value) (old Value, present bool) {
  590. old, present, _ = t.set(k, v, false)
  591. return old, present
  592. }
  593. func (t *BTree) SetWithIndex(k Key, v Value) (old Value, present bool, index int) {
  594. return t.set(k, v, true)
  595. }
  596. func (t *BTree) set(k Key, v Value, computeIndex bool) (old Value, present bool, idx int) {
  597. if t.root == nil {
  598. t.root = t.cow.newNode()
  599. t.root.items = append(t.root.items, item{k, v})
  600. t.root.size = 1
  601. return old, false, 0
  602. }
  603. t.root = t.root.mutableFor(t.cow)
  604. if len(t.root.items) >= t.maxItems() {
  605. sz := t.root.size
  606. item2, second := t.root.split(t.maxItems() / 2)
  607. oldroot := t.root
  608. t.root = t.cow.newNode()
  609. t.root.items = append(t.root.items, item2)
  610. t.root.children = append(t.root.children, oldroot, second)
  611. t.root.size = sz
  612. }
  613. return t.root.insert(item{k, v}, t.maxItems(), t.less, computeIndex)
  614. }
  615. // Delete removes the item with the given key, returning its value. The second return value
  616. // reports whether the key was found.
  617. func (t *BTree) Delete(k Key) (Value, bool) {
  618. m, removed := t.deleteItem(k, removeItem)
  619. return m.value, removed
  620. }
  621. // DeleteMin removes the smallest item in the tree and returns its key and value.
  622. // If the tree is empty, it returns zero values.
  623. func (t *BTree) DeleteMin() (Key, Value) {
  624. item, _ := t.deleteItem(nil, removeMin)
  625. return item.key, item.value
  626. }
  627. // DeleteMax removes the largest item in the tree and returns its key and value.
  628. // If the tree is empty, it returns zero values.
  629. func (t *BTree) DeleteMax() (Key, Value) {
  630. item, _ := t.deleteItem(nil, removeMax)
  631. return item.key, item.value
  632. }
  633. func (t *BTree) deleteItem(key Key, typ toRemove) (item, bool) {
  634. if t.root == nil || len(t.root.items) == 0 {
  635. return item{}, false
  636. }
  637. t.root = t.root.mutableFor(t.cow)
  638. out, removed := t.root.remove(key, t.minItems(), typ, t.less)
  639. if len(t.root.items) == 0 && len(t.root.children) > 0 {
  640. oldroot := t.root
  641. t.root = t.root.children[0]
  642. t.cow.freeNode(oldroot)
  643. }
  644. return out, removed
  645. }
  646. // Get returns the value for the given key in the tree, or the zero value if the
  647. // key is not in the tree.
  648. //
  649. // To distinguish a zero value from a key that is not present, use GetWithIndex.
  650. func (t *BTree) Get(k Key) Value {
  651. var z Value
  652. if t.root == nil {
  653. return z
  654. }
  655. item, ok, _ := t.root.get(k, false, t.less)
  656. if !ok {
  657. return z
  658. }
  659. return item.value
  660. }
  661. // GetWithIndex returns the value and index for the given key in the tree, or the
  662. // zero value and -1 if the key is not in the tree.
  663. func (t *BTree) GetWithIndex(k Key) (Value, int) {
  664. var z Value
  665. if t.root == nil {
  666. return z, -1
  667. }
  668. item, _, index := t.root.get(k, true, t.less)
  669. return item.value, index
  670. }
  671. // At returns the key and value at index i. The minimum item has index 0.
  672. // If i is outside the range [0, t.Len()), At panics.
  673. func (t *BTree) At(i int) (Key, Value) {
  674. if i < 0 || i >= t.Len() {
  675. panic("btree: index out of range")
  676. }
  677. item := t.root.at(i)
  678. return item.key, item.value
  679. }
  680. // Has reports whether the given key is in the tree.
  681. func (t *BTree) Has(k Key) bool {
  682. if t.root == nil {
  683. return false
  684. }
  685. _, ok, _ := t.root.get(k, false, t.less)
  686. return ok
  687. }
  688. // Min returns the smallest key in the tree and its value. If the tree is empty, it
  689. // returns zero values.
  690. func (t *BTree) Min() (Key, Value) {
  691. var k Key
  692. var v Value
  693. if t.root == nil {
  694. return k, v
  695. }
  696. n := t.root
  697. for len(n.children) > 0 {
  698. n = n.children[0]
  699. }
  700. if len(n.items) == 0 {
  701. return k, v
  702. }
  703. return n.items[0].key, n.items[0].value
  704. }
  705. // Max returns the largest key in the tree and its value. If the tree is empty, both
  706. // return values are zero values.
  707. func (t *BTree) Max() (Key, Value) {
  708. var k Key
  709. var v Value
  710. if t.root == nil {
  711. return k, v
  712. }
  713. n := t.root
  714. for len(n.children) > 0 {
  715. n = n.children[len(n.children)-1]
  716. }
  717. if len(n.items) == 0 {
  718. return k, v
  719. }
  720. m := n.items[len(n.items)-1]
  721. return m.key, m.value
  722. }
  723. // Len returns the number of items currently in the tree.
  724. func (t *BTree) Len() int {
  725. if t.root == nil {
  726. return 0
  727. }
  728. return t.root.size
  729. }
  730. // Before returns an iterator positioned just before k. After the first call to Next,
  731. // the Iterator will be at k, or at the key just greater than k if k is not in the tree.
  732. // Subsequent calls to Next will traverse the tree's items in ascending order.
  733. func (t *BTree) Before(k Key) *Iterator {
  734. if t.root == nil {
  735. return &Iterator{}
  736. }
  737. var cs cursorStack
  738. cs, found, idx := t.root.cursorStackForKey(k, cs, t.less)
  739. // If we found the key, the cursor stack is pointing to it. Since that is
  740. // the first element we want, don't advance the iterator on the initial call to Next.
  741. // If we haven't found the key, then the top of the cursor stack is either pointing at the
  742. // item just after k, in which case we do not want to move the iterator; or the index
  743. // is past the end of the items slice, in which case we do.
  744. var stay bool
  745. top := cs[len(cs)-1]
  746. if found {
  747. stay = true
  748. } else if top.index < len(top.node.items) {
  749. stay = true
  750. } else {
  751. idx--
  752. }
  753. return &Iterator{
  754. cursors: cs,
  755. stay: stay,
  756. descending: false,
  757. Index: idx,
  758. }
  759. }
  760. // After returns an iterator positioned just after k. After the first call to Next,
  761. // the Iterator will be at k, or at the key just less than k if k is not in the tree.
  762. // Subsequent calls to Next will traverse the tree's items in descending order.
  763. func (t *BTree) After(k Key) *Iterator {
  764. if t.root == nil {
  765. return &Iterator{}
  766. }
  767. var cs cursorStack
  768. cs, found, idx := t.root.cursorStackForKey(k, cs, t.less)
  769. // If we found the key, the cursor stack is pointing to it. Since that is
  770. // the first element we want, don't advance the iterator on the initial call to Next.
  771. // If we haven't found the key, the the cursor stack is pointing just after the first item,
  772. // so we do want to advance.
  773. return &Iterator{
  774. cursors: cs,
  775. stay: found,
  776. descending: true,
  777. Index: idx,
  778. }
  779. }
  780. // BeforeIndex returns an iterator positioned just before the item with the given index.
  781. // The iterator will traverse the tree's items in ascending order.
  782. // If i is not in the range [0, tr.Len()], BeforeIndex panics.
  783. // Note that it is not an error to provide an index of tr.Len().
  784. func (t *BTree) BeforeIndex(i int) *Iterator {
  785. return t.indexIterator(i, false)
  786. }
  787. // AfterIndex returns an iterator positioned just after the item with the given index.
  788. // The iterator will traverse the tree's items in descending order.
  789. // If i is not in the range [0, tr.Len()], AfterIndex panics.
  790. // Note that it is not an error to provide an index of tr.Len().
  791. func (t *BTree) AfterIndex(i int) *Iterator {
  792. return t.indexIterator(i, true)
  793. }
  794. func (t *BTree) indexIterator(i int, descending bool) *Iterator {
  795. if i < 0 || i > t.Len() {
  796. panic("btree: index out of range")
  797. }
  798. if i == t.Len() {
  799. return &Iterator{}
  800. }
  801. var cs cursorStack
  802. return &Iterator{
  803. cursors: t.root.cursorStackForIndex(i, cs),
  804. stay: true,
  805. descending: descending,
  806. Index: i,
  807. }
  808. }
  809. // An Iterator supports traversing the items in the tree.
  810. type Iterator struct {
  811. Key Key
  812. Value Value
  813. // Index is the position of the item in the tree viewed as a sequence.
  814. // The minimum item has index zero.
  815. Index int
  816. cursors cursorStack // stack of nodes with indices; last element is the top
  817. stay bool // don't do anything on the first call to Next.
  818. descending bool // traverse the items in descending order
  819. }
  820. // Next advances the Iterator to the next item in the tree. If Next returns true,
  821. // the Iterator's Key, Value and Index fields refer to the next item. If Next returns
  822. // false, there are no more items and the values of Key, Value and Index are undefined.
  823. //
  824. // If the tree is modified during iteration, the behavior is undefined.
  825. func (it *Iterator) Next() bool {
  826. var more bool
  827. switch {
  828. case len(it.cursors) == 0:
  829. more = false
  830. case it.stay:
  831. it.stay = false
  832. more = true
  833. case it.descending:
  834. more = it.dec()
  835. default:
  836. more = it.inc()
  837. }
  838. if !more {
  839. return false
  840. }
  841. top := it.cursors[len(it.cursors)-1]
  842. item := top.node.items[top.index]
  843. it.Key = item.key
  844. it.Value = item.value
  845. return true
  846. }
  847. // When inc returns true, the top cursor on the stack refers to the new current item.
  848. func (it *Iterator) inc() bool {
  849. // Useful invariants for understanding this function:
  850. // - Leaf nodes have zero children, and zero or more items.
  851. // - Nonleaf nodes have one more child than item, and children[i] < items[i] < children[i+1].
  852. // - The current item in the iterator is top.node.items[top.index].
  853. it.Index++
  854. // If we are at a non-leaf node, the current item is items[i], so
  855. // now we want to continue with children[i+1], which must exist
  856. // by the node invariant. We want the minimum item in that child's subtree.
  857. top := it.cursors.incTop(1)
  858. for len(top.node.children) > 0 {
  859. top = cursor{top.node.children[top.index], 0}
  860. it.cursors.push(top)
  861. }
  862. // Here, we are at a leaf node. top.index points to
  863. // the new current item, if it's within the items slice.
  864. for top.index >= len(top.node.items) {
  865. // We've gone through everything in this node. Pop it off the stack.
  866. it.cursors.pop()
  867. // If the stack is now empty,we're past the last item in the tree.
  868. if it.cursors.empty() {
  869. return false
  870. }
  871. top = it.cursors.top()
  872. // The new top's index points to a child, which we've just finished
  873. // exploring. The next item is the one at the same index in the items slice.
  874. }
  875. // Here, the top cursor on the stack points to the new current item.
  876. return true
  877. }
  878. func (it *Iterator) dec() bool {
  879. // See the invariants for inc, above.
  880. it.Index--
  881. top := it.cursors.top()
  882. // If we are at a non-leaf node, the current item is items[i], so
  883. // now we want to continue with children[i]. We want the maximum item in that child's subtree.
  884. for len(top.node.children) > 0 {
  885. c := top.node.children[top.index]
  886. top = cursor{c, len(c.items)}
  887. it.cursors.push(top)
  888. }
  889. top = it.cursors.incTop(-1)
  890. // Here, we are at a leaf node. top.index points to
  891. // the new current item, if it's within the items slice.
  892. for top.index < 0 {
  893. // We've gone through everything in this node. Pop it off the stack.
  894. it.cursors.pop()
  895. // If the stack is now empty,we're past the last item in the tree.
  896. if it.cursors.empty() {
  897. return false
  898. }
  899. // The new top's index points to a child, which we've just finished
  900. // exploring. That child is to the right of the item we want to advance to,
  901. // so decrement the index.
  902. top = it.cursors.incTop(-1)
  903. }
  904. return true
  905. }
  906. // A cursor is effectively a pointer into a node. A stack of cursors identifies an item in the tree,
  907. // and makes it possible to move to the next or previous item efficiently.
  908. //
  909. // If the cursor is on the top of the stack, its index points into the node's items slice, selecting
  910. // the current item. Otherwise, the index points into the children slice and identifies the child
  911. // that is next in the stack.
  912. type cursor struct {
  913. node *node
  914. index int
  915. }
  916. // A cursorStack is a stack of cursors, representing a path of nodes from the root of the tree.
  917. type cursorStack []cursor
  918. func (s *cursorStack) push(c cursor) cursorStack {
  919. *s = append(*s, c)
  920. return *s
  921. }
  922. func (s *cursorStack) pop() cursor {
  923. last := len(*s) - 1
  924. t := (*s)[last]
  925. *s = (*s)[:last]
  926. return t
  927. }
  928. func (s *cursorStack) top() cursor {
  929. return (*s)[len(*s)-1]
  930. }
  931. func (s *cursorStack) empty() bool {
  932. return len(*s) == 0
  933. }
  934. // incTop increments top's index by n and returns it.
  935. func (s *cursorStack) incTop(n int) cursor {
  936. (*s)[len(*s)-1].index += n // Don't call top: modify the original, not a copy.
  937. return s.top()
  938. }