btree.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // Copyright 2020 Joshua J Baker. All rights reserved.
  2. // Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. package btree
  5. type BTree struct {
  6. base *BTreeG[any]
  7. }
  8. // New returns a new BTree
  9. func New(less func(a, b any) bool) *BTree {
  10. if less == nil {
  11. panic("nil less")
  12. }
  13. return &BTree{base: NewBTreeG(less)}
  14. }
  15. // NewNonConcurrent returns a new BTree which is not safe for concurrent
  16. // write operations by multiple goroutines.
  17. //
  18. // This is useful for when you do not need the BTree to manage the locking,
  19. // but would rather do it yourself.
  20. //
  21. // Deprecated: use NewOptions
  22. func NewNonConcurrent(less func(a, b any) bool) *BTree {
  23. if less == nil {
  24. panic("nil less")
  25. }
  26. return &BTree{base: NewBTreeGOptions(less, Options{NoLocks: true})}
  27. }
  28. // NewOptions returns a new BTree
  29. func NewOptions(less func(a, b any) bool, opts Options) *BTree {
  30. if less == nil {
  31. panic("nil less")
  32. }
  33. return &BTree{base: NewBTreeGOptions(less, opts)}
  34. }
  35. // Less is a convenience function that performs a comparison of two items
  36. // using the same "less" function provided to New.
  37. func (tr *BTree) Less(a, b any) bool {
  38. return tr.base.Less(a, b)
  39. }
  40. // Set or replace a value for a key
  41. // Returns the value for the replaced item or nil if the key was not found.
  42. func (tr *BTree) Set(item any) (prev any) {
  43. return tr.SetHint(item, nil)
  44. }
  45. // SetHint sets or replace a value for a key using a path hint
  46. // Returns the value for the replaced item or nil if the key was not found.
  47. func (tr *BTree) SetHint(item any, hint *PathHint) (prev any) {
  48. if item == nil {
  49. panic("nil item")
  50. }
  51. v, ok := tr.base.SetHint(item, hint)
  52. if !ok {
  53. return nil
  54. }
  55. return v
  56. }
  57. // Get a value for key.
  58. // Returns nil if the key was not found.
  59. func (tr *BTree) Get(key any) any {
  60. return tr.getHintMut(key, nil, false)
  61. }
  62. func (tr *BTree) GetMut(key any) any {
  63. return tr.getHintMut(key, nil, true)
  64. }
  65. func (tr *BTree) GetHint(key any, hint *PathHint) any {
  66. return tr.getHintMut(key, hint, false)
  67. }
  68. func (tr *BTree) GetHintMut(key any, hint *PathHint) any {
  69. return tr.getHintMut(key, hint, true)
  70. }
  71. // GetHint gets a value for key using a path hint.
  72. // Returns nil if the item was not found.
  73. func (tr *BTree) getHintMut(key any, hint *PathHint, mut bool) (value any) {
  74. if key == nil {
  75. return nil
  76. }
  77. var v any
  78. var ok bool
  79. if mut {
  80. v, ok = tr.base.GetHintMut(key, hint)
  81. } else {
  82. v, ok = tr.base.GetHint(key, hint)
  83. }
  84. if !ok {
  85. return nil
  86. }
  87. return v
  88. }
  89. // Len returns the number of items in the tree
  90. func (tr *BTree) Len() int {
  91. return tr.base.Len()
  92. }
  93. // Delete an item for a key.
  94. // Returns the deleted value or nil if the key was not found.
  95. func (tr *BTree) Delete(key any) (prev any) {
  96. return tr.DeleteHint(key, nil)
  97. }
  98. // DeleteHint deletes a value for a key using a path hint
  99. // Returns the deleted value or nil if the key was not found.
  100. func (tr *BTree) DeleteHint(key any, hint *PathHint) (prev any) {
  101. if key == nil {
  102. return nil
  103. }
  104. v, ok := tr.base.DeleteHint(key, nil)
  105. if !ok {
  106. return nil
  107. }
  108. return v
  109. }
  110. // Ascend the tree within the range [pivot, last]
  111. // Pass nil for pivot to scan all item in ascending order
  112. // Return false to stop iterating
  113. func (tr *BTree) Ascend(pivot any, iter func(item any) bool) {
  114. if pivot == nil {
  115. tr.base.Scan(iter)
  116. } else {
  117. tr.base.Ascend(pivot, iter)
  118. }
  119. }
  120. func (tr *BTree) AscendMut(pivot any, iter func(item any) bool) {
  121. if pivot == nil {
  122. tr.base.ScanMut(iter)
  123. } else {
  124. tr.base.AscendMut(pivot, iter)
  125. }
  126. }
  127. // Descend the tree within the range [pivot, first]
  128. // Pass nil for pivot to scan all item in descending order
  129. // Return false to stop iterating
  130. func (tr *BTree) Descend(pivot any, iter func(item any) bool) {
  131. if pivot == nil {
  132. tr.base.Reverse(iter)
  133. } else {
  134. tr.base.Descend(pivot, iter)
  135. }
  136. }
  137. func (tr *BTree) DescendMut(pivot any, iter func(item any) bool) {
  138. if pivot == nil {
  139. tr.base.ReverseMut(iter)
  140. } else {
  141. tr.base.DescendMut(pivot, iter)
  142. }
  143. }
  144. // Load is for bulk loading pre-sorted items
  145. // If the load replaces and existing item then the value for the replaced item
  146. // is returned.
  147. func (tr *BTree) Load(item any) (prev any) {
  148. if item == nil {
  149. panic("nil item")
  150. }
  151. v, ok := tr.base.Load(item)
  152. if !ok {
  153. return nil
  154. }
  155. return v
  156. }
  157. // Min returns the minimum item in tree.
  158. // Returns nil if the tree has no items.
  159. func (tr *BTree) Min() any {
  160. v, ok := tr.base.Min()
  161. if !ok {
  162. return nil
  163. }
  164. return v
  165. }
  166. func (tr *BTree) MinMut() any {
  167. v, ok := tr.base.MinMut()
  168. if !ok {
  169. return nil
  170. }
  171. return v
  172. }
  173. // Max returns the maximum item in tree.
  174. // Returns nil if the tree has no items.
  175. func (tr *BTree) Max() any {
  176. v, ok := tr.base.Max()
  177. if !ok {
  178. return nil
  179. }
  180. return v
  181. }
  182. func (tr *BTree) MaxMut() any {
  183. v, ok := tr.base.Max()
  184. if !ok {
  185. return nil
  186. }
  187. return v
  188. }
  189. // PopMin removes the minimum item in tree and returns it.
  190. // Returns nil if the tree has no items.
  191. func (tr *BTree) PopMin() any {
  192. v, ok := tr.base.PopMin()
  193. if !ok {
  194. return nil
  195. }
  196. return v
  197. }
  198. // PopMax removes the maximum item in tree and returns it.
  199. // Returns nil if the tree has no items.
  200. func (tr *BTree) PopMax() any {
  201. v, ok := tr.base.PopMax()
  202. if !ok {
  203. return nil
  204. }
  205. return v
  206. }
  207. // GetAt returns the value at index.
  208. // Return nil if the tree is empty or the index is out of bounds.
  209. func (tr *BTree) GetAt(index int) any {
  210. v, ok := tr.base.GetAt(index)
  211. if !ok {
  212. return nil
  213. }
  214. return v
  215. }
  216. func (tr *BTree) GetAtMut(index int) any {
  217. v, ok := tr.base.GetAtMut(index)
  218. if !ok {
  219. return nil
  220. }
  221. return v
  222. }
  223. // DeleteAt deletes the item at index.
  224. // Return nil if the tree is empty or the index is out of bounds.
  225. func (tr *BTree) DeleteAt(index int) any {
  226. v, ok := tr.base.DeleteAt(index)
  227. if !ok {
  228. return nil
  229. }
  230. return v
  231. }
  232. // Height returns the height of the tree.
  233. // Returns zero if tree has no items.
  234. func (tr *BTree) Height() int {
  235. return tr.base.Height()
  236. }
  237. // Walk iterates over all items in tree, in order.
  238. // The items param will contain one or more items.
  239. func (tr *BTree) Walk(iter func(items []any)) {
  240. tr.base.Walk(func(items []any) bool {
  241. iter(items)
  242. return true
  243. })
  244. }
  245. func (tr *BTree) WalkMut(iter func(items []any)) {
  246. tr.base.WalkMut(func(items []any) bool {
  247. iter(items)
  248. return true
  249. })
  250. }
  251. // Copy the tree. This is a copy-on-write operation and is very fast because
  252. // it only performs a shadowed copy.
  253. func (tr *BTree) Copy() *BTree {
  254. return &BTree{base: tr.base.Copy()}
  255. }
  256. func (tr *BTree) IsoCopy() *BTree {
  257. return &BTree{base: tr.base.IsoCopy()}
  258. }
  259. // Clear will delete all items.
  260. func (tr *BTree) Clear() {
  261. tr.base.Clear()
  262. }
  263. // Iter is an iterator for
  264. type Iter struct {
  265. base IterG[any]
  266. }
  267. // Iter returns a read-only iterator.
  268. // The Release method must be called finished with iterator.
  269. func (tr *BTree) Iter() Iter {
  270. return Iter{tr.base.Iter()}
  271. }
  272. func (tr *BTree) IterMut() Iter {
  273. return Iter{tr.base.IterMut()}
  274. }
  275. // Seek to item greater-or-equal-to key.
  276. // Returns false if there was no item found.
  277. func (iter *Iter) Seek(key any) bool {
  278. return iter.base.Seek(key)
  279. }
  280. // First moves iterator to first item in tree.
  281. // Returns false if the tree is empty.
  282. func (iter *Iter) First() bool {
  283. return iter.base.First()
  284. }
  285. // Last moves iterator to last item in tree.
  286. // Returns false if the tree is empty.
  287. func (iter *Iter) Last() bool {
  288. return iter.base.Last()
  289. }
  290. // First moves iterator to first item in tree.
  291. // Returns false if the tree is empty.
  292. func (iter *Iter) Release() {
  293. iter.base.Release()
  294. }
  295. // Next moves iterator to the next item in iterator.
  296. // Returns false if the tree is empty or the iterator is at the end of
  297. // the tree.
  298. func (iter *Iter) Next() bool {
  299. return iter.base.Next()
  300. }
  301. // Prev moves iterator to the previous item in iterator.
  302. // Returns false if the tree is empty or the iterator is at the beginning of
  303. // the tree.
  304. func (iter *Iter) Prev() bool {
  305. return iter.base.Prev()
  306. }
  307. // Item returns the current iterator item.
  308. func (iter *Iter) Item() any {
  309. return iter.base.Item()
  310. }