-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLessTree.go
More file actions
62 lines (58 loc) · 1.02 KB
/
LessTree.go
File metadata and controls
62 lines (58 loc) · 1.02 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
package omap
/*
func LessIndex[K any, V any](k K, Less func(a, b K) bool, Slice []KvSet[K, V]) (idx, offset int) {
end := len(Slice)
if end < 3 {
switch end {
case 0:
return 0, 0
case 1:
if Less(k, Slice[0].Key) {
return 0, -1
} else if Less(Slice[0].Key, k) {
return 0, 1
}
return 0, 0
case 2:
// being lazy for just 2 elements
for id := range 2 {
if Less(k, Slice[id].Key) {
return 0, -1
} else if !Less(Slice[id].Key, k) {
return 0, 0
}
}
return 1, 1
}
return
}
mid := getMid(end)
idx = mid
end--
begin := 0
diff := 0
for {
lt := Less(k, Slice[mid].Key)
if lt {
end = mid - 1
diff = end - begin
} else if Less(Slice[mid].Key, k) {
begin = mid + 1
diff = end - begin
} else {
return mid, 0
}
if diff == 0 {
if Less(k, Slice[begin].Key) {
return begin, -1
} else if Less(Slice[begin].Key, k) {
return begin, 1
}
return begin, 0
} else if diff < 0 {
return begin, -1
}
mid = begin + getMid(diff+1)
}
}
*/