-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticMethods.go
More file actions
128 lines (117 loc) · 2.59 KB
/
StaticMethods.go
File metadata and controls
128 lines (117 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package omap
import (
"cmp"
"iter"
"slices"
)
func KvIter[K any, V any](set []KvSet[K, V]) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for _, row := range set {
if !yield(row.Key, row.Value) {
return
}
}
}
}
func (s *SliceTree[K, V]) rangedel(a, b int) {
s.Slices = slices.Delete(s.Slices, a, b+1)
}
func reverse(a, b int) int {
return cmp.Compare(b, a)
}
// Returns the index and offset of a given key.
//
// The index is the current relative postion in the slice.
//
// The offset represents where the item would be placed:
// - offset of 0, at index value.
// - offset of 1, expand the slice after the inddex and put the value to the right of the index
// - offset of -1, expand the slice before the index and put the value to left of the current postion
//
// Complexity: o(log n)
func GetIndex[K any, V any](k K, Cmp func(a, b K) int, Slices []KvSet[K, V]) (index, offset int) {
end := len(Slices)
switch end {
case 0:
return 0, 0
case 1:
return 0, Cmp(k, Slices[0].Key)
}
mid := getMid(end)
offset = Cmp(k, Slices[mid].Key)
end--
begin, diff := 0, 0
for {
switch offset {
case -1:
// moving left
end = mid - 1
diff = end - begin
case 1:
begin = mid + 1
diff = end - begin
default:
return mid, 0
}
switch diff {
case -1:
return begin, -1
case 0:
return begin, Cmp(k, Slices[begin].Key)
}
diff += 1
mid = begin + getMid(diff)
offset = Cmp(k, Slices[mid].Key)
}
}
func getMid(size int) int {
// shift right 1 same as divide by 2, bitwise is always faster
// size&1 is the same as size%2, but biwise is faster
return (size-2)>>1 + size&1
}
// Merges a map into an OrderedMap instance.
func Merge[K comparable, V any](dst OrderedMap[K, V], src map[K]V) int {
count := 0
// is this worth trying to optimize?
for k, v := range src {
count++
dst.Put(k, v)
}
return count
}
func MergeKvSet[K any, V any](left, right, dst []KvSet[K, V], start, finish int, Cmp func(a, b K) int, OnOverwrite func(K, V, V)) (res []KvSet[K, V], end int) {
res = dst
pos := 0
f := len(right)
dp := start
i := start
cmp := 0
for i <= finish && pos < f {
cmp = Cmp(left[i].Key, right[pos].Key)
switch cmp {
case -1:
dst[dp] = left[i]
i++
case 1:
dst[dp] = right[pos]
pos++
default:
if OnOverwrite != nil {
OnOverwrite(left[i].Key, left[i].Value, right[pos].Value)
}
dst[dp] = right[pos]
i++
pos++
}
end = dp
dp++
}
if i <= finish {
res = append(res[0:end+1], left[i:finish+1]...)
end += finish - i + 1
} else if pos < f {
res = append(res[0:end+1], right[pos:f]...)
end += f - pos
}
return
}