node.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. // Copyright 2018 The Cockroach Authors.
  2. // Copyright 2021 Andrew Werner.
  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
  13. // implied. See the License for the specific language governing
  14. // permissions and limitations under the License.
  15. package abstract
  16. import (
  17. "fmt"
  18. "strings"
  19. "sync/atomic"
  20. )
  21. // Node represents a node in the tree.
  22. type Node[K, V, A any] struct {
  23. ref int32
  24. count int16
  25. aug A
  26. keys [MaxEntries]K
  27. values [MaxEntries]V
  28. children *[MaxEntries + 1]*Node[K, V, A]
  29. }
  30. type interiorNode[K, V, A any] struct {
  31. Node[K, V, A]
  32. children [MaxEntries + 1]*Node[K, V, A]
  33. }
  34. func (n *Node[K, V, A]) GetA() *A {
  35. return &n.aug
  36. }
  37. func (n *Node[K, V, A]) IsLeaf() bool {
  38. return n.children == nil
  39. }
  40. func (n *Node[K, V, A]) Count() int16 {
  41. return n.count
  42. }
  43. func (n *Node[K, V, A]) GetKey(i int16) K {
  44. return n.keys[i]
  45. }
  46. func (n *Node[K, V, A]) GetChild(i int16) *A {
  47. if !n.IsLeaf() && n.children[i] != nil {
  48. return &n.children[i].aug
  49. }
  50. return nil
  51. }
  52. // mut creates and returns a mutable node reference. If the node is not shared
  53. // with any other trees then it can be modified in place. Otherwise, it must be
  54. // cloned to ensure unique ownership. In this way, we enforce a copy-on-write
  55. // policy which transparently incorporates the idea of local mutations, like
  56. // Clojure's transients or Haskell's ST monad, where nodes are only copied
  57. // during the first time that they are modified between Clone operations.
  58. //
  59. // When a node is cloned, the provided pointer will be redirected to the new
  60. // mutable node.
  61. func mut[K, V, A any](
  62. np *nodePool[K, V, A],
  63. n **Node[K, V, A],
  64. ) *Node[K, V, A] {
  65. if atomic.LoadInt32(&(*n).ref) == 1 {
  66. // Exclusive ownership. Can mutate in place.
  67. return *n
  68. }
  69. // If we do not have unique ownership over the node then we
  70. // clone it to gain unique ownership. After doing so, we can
  71. // release our reference to the old node. We pass recursive
  72. // as true because even though we just observed the node's
  73. // reference count to be greater than 1, we might be racing
  74. // with another call to decRef on this node.
  75. c := (*n).clone(np)
  76. (*n).decRef(np, true /* recursive */)
  77. *n = c
  78. return *n
  79. }
  80. // incRef acquires a reference to the node.
  81. func (n *Node[K, V, A]) incRef() {
  82. atomic.AddInt32(&n.ref, 1)
  83. }
  84. // decRef releases a reference to the node. If requested, the method
  85. // will recurse into child nodes and decrease their refcounts as well.
  86. func (n *Node[K, V, A]) decRef(
  87. np *nodePool[K, V, A], recursive bool,
  88. ) {
  89. if atomic.AddInt32(&n.ref, -1) > 0 {
  90. // Other references remain. Can't free.
  91. return
  92. }
  93. // Clear and release node into memory pool.
  94. if n.IsLeaf() {
  95. np.putLeafNode(n)
  96. } else {
  97. // Release child references first, if requested.
  98. if recursive {
  99. for i := int16(0); i <= n.count; i++ {
  100. n.children[i].decRef(np, true /* recursive */)
  101. }
  102. }
  103. np.putInteriorNode(n)
  104. }
  105. }
  106. // clone creates a clone of the receiver with a single reference count.
  107. func (n *Node[K, V, A]) clone(
  108. np *nodePool[K, V, A],
  109. ) *Node[K, V, A] {
  110. var c *Node[K, V, A]
  111. if n.IsLeaf() {
  112. c = np.getLeafNode()
  113. } else {
  114. c = np.getInteriorNode()
  115. }
  116. // NB: copy field-by-field without touching N.N.ref to avoid
  117. // triggering the race detector and looking like a data race.
  118. c.count = n.count
  119. n.aug = c.aug
  120. c.keys = n.keys
  121. if !c.IsLeaf() {
  122. // Copy children and increase each refcount.
  123. *c.children = *n.children
  124. for i := int16(0); i <= c.count; i++ {
  125. c.children[i].incRef()
  126. }
  127. }
  128. return c
  129. }
  130. func (n *Node[K, V, A]) insertAt(index int, item K, value V, nd *Node[K, V, A]) {
  131. if index < int(n.count) {
  132. copy(n.keys[index+1:n.count+1], n.keys[index:n.count])
  133. copy(n.values[index+1:n.count+1], n.values[index:n.count])
  134. if !n.IsLeaf() {
  135. copy(n.children[index+2:n.count+2], n.children[index+1:n.count+1])
  136. }
  137. }
  138. n.keys[index] = item
  139. n.values[index] = value
  140. if !n.IsLeaf() {
  141. n.children[index+1] = nd
  142. }
  143. n.count++
  144. }
  145. func (n *Node[K, V, A]) pushBack(item K, value V, nd *Node[K, V, A]) {
  146. n.keys[n.count] = item
  147. n.values[n.count] = value
  148. if !n.IsLeaf() {
  149. n.children[n.count+1] = nd
  150. }
  151. n.count++
  152. }
  153. func (n *Node[K, V, A]) pushFront(item K, value V, nd *Node[K, V, A]) {
  154. if !n.IsLeaf() {
  155. copy(n.children[1:n.count+2], n.children[:n.count+1])
  156. n.children[0] = nd
  157. }
  158. copy(n.keys[1:n.count+1], n.keys[:n.count])
  159. copy(n.values[1:n.count+1], n.values[:n.count])
  160. n.keys[0] = item
  161. n.values[0] = value
  162. n.count++
  163. }
  164. // removeAt removes a value at a given index, pulling all subsequent values
  165. // back.
  166. func (n *Node[K, V, A]) removeAt(index int) (K, V, *Node[K, V, A]) {
  167. var child *Node[K, V, A]
  168. if !n.IsLeaf() {
  169. child = n.children[index+1]
  170. copy(n.children[index+1:n.count], n.children[index+2:n.count+1])
  171. n.children[n.count] = nil
  172. }
  173. n.count--
  174. outK := n.keys[index]
  175. outV := n.values[index]
  176. copy(n.keys[index:n.count], n.keys[index+1:n.count+1])
  177. copy(n.values[index:n.count], n.values[index+1:n.count+1])
  178. var rk K
  179. var rv V
  180. n.keys[n.count] = rk
  181. n.values[n.count] = rv
  182. return outK, outV, child
  183. }
  184. // popBack removes and returns the last element in the list.
  185. func (n *Node[K, V, A]) popBack() (K, V, *Node[K, V, A]) {
  186. n.count--
  187. outK := n.keys[n.count]
  188. outV := n.values[n.count]
  189. var rK K
  190. var rV V
  191. n.keys[n.count] = rK
  192. n.values[n.count] = rV
  193. if n.IsLeaf() {
  194. return outK, outV, nil
  195. }
  196. child := n.children[n.count+1]
  197. n.children[n.count+1] = nil
  198. return outK, outV, child
  199. }
  200. // popFront removes and returns the first element in the list.
  201. func (n *Node[K, V, A]) popFront() (K, V, *Node[K, V, A]) {
  202. n.count--
  203. var child *Node[K, V, A]
  204. if !n.IsLeaf() {
  205. child = n.children[0]
  206. copy(n.children[:n.count+1], n.children[1:n.count+2])
  207. n.children[n.count+1] = nil
  208. }
  209. outK := n.keys[0]
  210. outV := n.values[0]
  211. copy(n.keys[:n.count], n.keys[1:n.count+1])
  212. copy(n.values[:n.count], n.values[1:n.count+1])
  213. var rK K
  214. var rV V
  215. n.keys[n.count] = rK
  216. n.values[n.count] = rV
  217. return outK, outV, child
  218. }
  219. // find returns the index where the given item should be inserted into this
  220. // list. 'found' is true if the item already exists in the list at the given
  221. // index.
  222. func (n *Node[K, V, A]) find(cmp func(K, K) int, item K) (index int, found bool) {
  223. // Logic copied from sort.Search. Inlining this gave
  224. // an 11% speedup on BenchmarkBTreeDeleteInsert.
  225. i, j := 0, int(n.count)
  226. for i < j {
  227. h := int(uint(i+j) >> 1) // avoid overflow when computing h
  228. // i ≤ h < j
  229. c := cmp(item, n.keys[h])
  230. if c < 0 {
  231. j = h
  232. } else if c > 0 {
  233. i = h + 1
  234. } else {
  235. return h, true
  236. }
  237. }
  238. return i, false
  239. }
  240. // split splits the given node at the given index. The current node shrinks,
  241. // and this function returns the item that existed at that index and a new
  242. // node containing all keys/children after it.
  243. //
  244. // Before:
  245. //
  246. // +-----------+
  247. // | x y z |
  248. // +--/-/-\-\--+
  249. //
  250. // After:
  251. //
  252. // +-----------+
  253. // | y |
  254. // +----/-\----+
  255. // / \
  256. // v v
  257. // +-----------+ +-----------+
  258. // | x | | z |
  259. // +-----------+ +-----------+
  260. //
  261. func (n *Node[K, V, A]) split(cfg *config[K, V, A], i int) (K, V, *Node[K, V, A]) {
  262. outK := n.keys[i]
  263. outV := n.values[i]
  264. var next *Node[K, V, A]
  265. if n.IsLeaf() {
  266. next = cfg.np.getLeafNode()
  267. } else {
  268. next = cfg.np.getInteriorNode()
  269. }
  270. next.count = n.count - int16(i+1)
  271. copy(next.keys[:], n.keys[i+1:n.count])
  272. copy(next.values[:], n.values[i+1:n.count])
  273. var rK K
  274. var rV V
  275. for j := int16(i); j < n.count; j++ {
  276. n.keys[j] = rK
  277. n.values[j] = rV
  278. }
  279. if !n.IsLeaf() {
  280. copy(next.children[:], n.children[i+1:n.count+1])
  281. for j := int16(i + 1); j <= n.count; j++ {
  282. n.children[j] = nil
  283. }
  284. }
  285. n.count = int16(i)
  286. next.update(&cfg.Config)
  287. n.updateOn(&cfg.Config, Split, outK, next)
  288. return outK, outV, next
  289. }
  290. func (n *Node[K, V, A]) update(cfg *Config[K, V, A]) bool {
  291. return n.updateWithMeta(cfg, UpdateInfo[K, A]{})
  292. }
  293. func (n *Node[K, V, A]) updateWithMeta(cfg *Config[K, V, A], md UpdateInfo[K, A]) bool {
  294. if cfg.Updater == nil {
  295. return false
  296. }
  297. return cfg.Updater.Update(n, md)
  298. }
  299. func (n *Node[K, V, A]) updateOn(cfg *Config[K, V, A], action Action, k K, affected *Node[K, V, A]) bool {
  300. if cfg.Updater == nil {
  301. return false
  302. }
  303. var a *A
  304. if affected != nil {
  305. a = &affected.aug
  306. }
  307. return n.updateWithMeta(cfg, UpdateInfo[K, A]{
  308. Action: action,
  309. RelevantKey: k,
  310. ModifiedOther: a,
  311. })
  312. }
  313. // insert inserts an item into the suAugBTree rooted at this node, making sure no
  314. // nodes in the suAugBTree exceed MaxEntries keys. Returns true if an existing item
  315. // was replaced and false if an item was inserted. Also returns whether the
  316. // node's upper bound changes.
  317. func (n *Node[K, V, A]) insert(cfg *config[K, V, A], item K, value V) (replacedK K, replacedV V, replaced, newBound bool) {
  318. i, found := n.find(cfg.cmp, item)
  319. if found {
  320. replacedV = n.values[i]
  321. replacedK = n.keys[i]
  322. n.keys[i] = item
  323. n.values[i] = value
  324. return replacedK, replacedV, true, false
  325. }
  326. if n.IsLeaf() {
  327. n.insertAt(i, item, value, nil)
  328. return replacedK, replacedV, false, n.updateOn(&cfg.Config, Insertion, item, nil)
  329. }
  330. if n.children[i].count >= MaxEntries {
  331. splitLK, splitLV, splitNode := mut(cfg.np, &n.children[i]).
  332. split(cfg, MaxEntries/2)
  333. n.insertAt(i, splitLK, splitLV, splitNode)
  334. if c := cfg.cmp(item, n.keys[i]); c < 0 {
  335. // no change, we want first split node
  336. } else if c > 0 {
  337. i++ // we want second split node
  338. } else {
  339. // TODO(ajwerner): add something to the augmentation api to
  340. // deal with replacement.
  341. replacedV = n.values[i]
  342. replacedK = n.keys[i]
  343. n.keys[i] = item
  344. n.values[i] = value
  345. return replacedK, replacedV, true, false
  346. }
  347. }
  348. replacedK, replacedV, replaced, newBound =
  349. mut(cfg.np, &n.children[i]).insert(cfg, item, value)
  350. if newBound {
  351. newBound = n.updateOn(&cfg.Config, Insertion, item, nil)
  352. }
  353. return replacedK, replacedV, replaced, newBound
  354. }
  355. // removeMax removes and returns the maximum item from the suAugBTree rooted at
  356. // this node.
  357. func (n *Node[K, V, A]) removeMax(cfg *config[K, V, A]) (K, V) {
  358. if n.IsLeaf() {
  359. n.count--
  360. outK := n.keys[n.count]
  361. outV := n.values[n.count]
  362. var rK K
  363. var rV V
  364. n.keys[n.count] = rK
  365. n.values[n.count] = rV
  366. n.updateOn(&cfg.Config, Removal, outK, nil)
  367. return outK, outV
  368. }
  369. // Recurse into max child.
  370. i := int(n.count)
  371. if n.children[i].count <= MinEntries {
  372. // Child not large enough to remove from.
  373. n.rebalanceOrMerge(cfg, i)
  374. return n.removeMax(cfg) // redo
  375. }
  376. child := mut(cfg.np, &n.children[i])
  377. outK, outV := child.removeMax(cfg)
  378. n.updateOn(&cfg.Config, Removal, outK, nil)
  379. return outK, outV
  380. }
  381. // rebalanceOrMerge grows child 'i' to ensure it has sufficient room to remove
  382. // an item from it while keeping it at or above MinItems.
  383. func (n *Node[K, V, A]) rebalanceOrMerge(
  384. cfg *config[K, V, A], i int,
  385. ) {
  386. switch {
  387. case i > 0 && n.children[i-1].count > MinEntries:
  388. // Rebalance from left sibling.
  389. //
  390. // +-----------+
  391. // | y |
  392. // +----/-\----+
  393. // / \
  394. // v v
  395. // +-----------+ +-----------+
  396. // | x | | |
  397. // +----------\+ +-----------+
  398. // \
  399. // v
  400. // a
  401. //
  402. // After:
  403. //
  404. // +-----------+
  405. // | x |
  406. // +----/-\----+
  407. // / \
  408. // v v
  409. // +-----------+ +-----------+
  410. // | | | y |
  411. // +-----------+ +/----------+
  412. // /
  413. // v
  414. // a
  415. //
  416. left := mut(cfg.np, &n.children[i-1])
  417. child := mut(cfg.np, &n.children[i])
  418. xLaK, xLaV, grandChild := left.popBack()
  419. yLaK, yLaV := n.keys[i-1], n.values[i-1]
  420. child.pushFront(yLaK, yLaV, grandChild)
  421. n.keys[i-1], n.values[i-1] = xLaK, xLaV
  422. left.updateOn(&cfg.Config, Removal, xLaK, grandChild)
  423. child.updateOn(&cfg.Config, Insertion, yLaK, grandChild)
  424. case i < int(n.count) && n.children[i+1].count > MinEntries:
  425. // Rebalance from right sibling.
  426. //
  427. // +-----------+
  428. // | y |
  429. // +----/-\----+
  430. // / \
  431. // v v
  432. // +-----------+ +-----------+
  433. // | | | x |
  434. // +-----------+ +/----------+
  435. // /
  436. // v
  437. // a
  438. //
  439. // After:
  440. //
  441. // +-----------+
  442. // | x |
  443. // +----/-\----+
  444. // / \
  445. // v v
  446. // +-----------+ +-----------+
  447. // | y | | |
  448. // +----------\+ +-----------+
  449. // \
  450. // v
  451. // a
  452. //
  453. right := mut(cfg.np, &n.children[i+1])
  454. child := mut(cfg.np, &n.children[i])
  455. xLaK, xLaV, grandChild := right.popFront()
  456. yLaK, yLaV := n.keys[i], n.values[i]
  457. child.pushBack(yLaK, yLaV, grandChild)
  458. n.keys[i], n.values[i] = xLaK, xLaV
  459. right.updateOn(&cfg.Config, Removal, xLaK, grandChild)
  460. child.updateOn(&cfg.Config, Insertion, yLaK, grandChild)
  461. default:
  462. // Merge with either the left or right sibling.
  463. //
  464. // +-----------+
  465. // | u y v |
  466. // +----/-\----+
  467. // / \
  468. // v v
  469. // +-----------+ +-----------+
  470. // | x | | z |
  471. // +-----------+ +-----------+
  472. //
  473. // After:
  474. //
  475. // +-----------+
  476. // | u v |
  477. // +-----|-----+
  478. // |
  479. // v
  480. // +-----------+
  481. // | x y z |
  482. // +-----------+
  483. //
  484. if i >= int(n.count) {
  485. i = int(n.count - 1)
  486. }
  487. child := mut(cfg.np, &n.children[i])
  488. // Make mergeChild mutable, bumping the refcounts on its children if necessary.
  489. _ = mut(cfg.np, &n.children[i+1])
  490. mergeLaK, mergeLaV, mergeChild := n.removeAt(i)
  491. child.keys[child.count] = mergeLaK
  492. child.values[child.count] = mergeLaV
  493. copy(child.keys[child.count+1:], mergeChild.keys[:mergeChild.count])
  494. copy(child.values[child.count+1:], mergeChild.values[:mergeChild.count])
  495. if !child.IsLeaf() {
  496. copy(child.children[child.count+1:], mergeChild.children[:mergeChild.count+1])
  497. }
  498. child.count += mergeChild.count + 1
  499. child.updateOn(&cfg.Config, Insertion, mergeLaK, mergeChild)
  500. mergeChild.decRef(cfg.np, false /* recursive */)
  501. }
  502. }
  503. // remove removes an item from the suAugBTree rooted at this node. Returns the item
  504. // that was removed or nil if no matching item was found. Also returns whether
  505. // the node's upper bound changes.
  506. func (n *Node[K, V, A]) remove(
  507. cfg *config[K, V, A], item K,
  508. ) (outK K, outV V, found, newBound bool) {
  509. i, found := n.find(cfg.cmp, item)
  510. if n.IsLeaf() {
  511. if found {
  512. outK, outV, _ = n.removeAt(i)
  513. return outK, outV, true, n.updateOn(&cfg.Config, Removal, outK, nil)
  514. }
  515. var rK K
  516. var rV V
  517. return rK, rV, false, false
  518. }
  519. if n.children[i].count <= MinEntries {
  520. // Child not large enough to remove from.
  521. n.rebalanceOrMerge(cfg, i)
  522. return n.remove(cfg, item) // redo
  523. }
  524. child := mut(cfg.np, &n.children[i])
  525. if found {
  526. // Replace the item being removed with the max item in our left child.
  527. outK = n.keys[i]
  528. outV = n.values[i]
  529. n.keys[i], n.values[i] = child.removeMax(cfg)
  530. return outK, outV, true, n.updateOn(&cfg.Config, Removal, outK, nil)
  531. }
  532. // Latch is not in this node and child is large enough to remove from.
  533. outK, outV, found, newBound = child.remove(cfg, item)
  534. if newBound {
  535. newBound = n.updateOn(&cfg.Config, Removal, outK, nil)
  536. }
  537. return outK, outV, found, newBound
  538. }
  539. func (n *Node[K, V, A]) writeString(b *strings.Builder) {
  540. if n.IsLeaf() {
  541. for i := int16(0); i < n.count; i++ {
  542. if i != 0 {
  543. b.WriteString(",")
  544. }
  545. fmt.Fprintf(b, "%v:%v", n.keys[i], n.values[i])
  546. }
  547. return
  548. }
  549. for i := int16(0); i <= n.count; i++ {
  550. b.WriteString("(")
  551. n.children[i].writeString(b)
  552. b.WriteString(")")
  553. if i < n.count {
  554. fmt.Fprintf(b, "%v:%v", n.keys[i], n.values[i])
  555. }
  556. }
  557. }