1
1
package org.example
2
2
3
- import kotlinx.coroutines.sync.Mutex
4
-
5
3
class OptimisticTree <K : Comparable <K >, V >: Tree <K , V >() {
6
4
private var root: TreeNode <K , V >? = null
7
- private val treeLock = Mutex ()
8
5
9
6
private suspend fun searchAux (key : K ): Pair <TreeNode <K , V >? , TreeNode<K, V>?> {
10
7
while (true ) {
@@ -28,15 +25,15 @@ class OptimisticTree<K : Comparable<K>, V>: Tree<K, V>() {
28
25
29
26
var validationParent: TreeNode <K , V >? = root
30
27
while (parent != validationParent) {
31
- val key = checkNotNull(validationParent?.key)
32
- validationParent = if (key < parent.key) validationParent?.right else validationParent?.left
33
- // if (validationParent == null) {
34
- // // again
35
- // validationParent = root
36
- // }
28
+ val validationParentKey = checkNotNull(validationParent?.key)
29
+ validationParent = if (validationParentKey < parent.key) validationParent?.right else validationParent?.left
30
+ if (validationParent == null ) {
31
+ // again
32
+ break
33
+ }
37
34
}
38
35
39
- if (current == validationParent.left || current == validationParent.right) {
36
+ if (current == validationParent? .left || current == validationParent? .right) {
40
37
return current to parent
41
38
} else {
42
39
// retry
@@ -90,7 +87,7 @@ class OptimisticTree<K : Comparable<K>, V>: Tree<K, V>() {
90
87
private suspend fun deleteRec (node : TreeNode <K , V >? , parent : TreeNode <K , V >? ): TreeNode <K , V >? {
91
88
when {
92
89
node == null -> {
93
- parent?.unlock() ? : treeLock.unlock()
90
+ parent?.unlock()
94
91
return parent
95
92
}
96
93
@@ -108,7 +105,7 @@ class OptimisticTree<K : Comparable<K>, V>: Tree<K, V>() {
108
105
}
109
106
}
110
107
node.unlock()
111
- parent?.unlock() ? : treeLock.unlock()
108
+ parent?.unlock()
112
109
}
113
110
114
111
else -> {
@@ -136,7 +133,7 @@ class OptimisticTree<K : Comparable<K>, V>: Tree<K, V>() {
136
133
137
134
right.unlock()
138
135
node.unlock()
139
- parent?.unlock() ? : treeLock.unlock()
136
+ parent?.unlock()
140
137
}
141
138
}
142
139
return parent
0 commit comments