-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
66 lines (55 loc) · 1.01 KB
/
util.go
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
//go:build go1.18
// +build go1.18
package cog
// minArrayCap is smallest capacity that array may have.
const minArrayCap = 16
// doubleup grow up capcity by c *= 2 until c >= n
func doubleup(c, n int) int {
if c < minArrayCap {
c = minArrayCap
}
for c < n {
c <<= 1
}
return c
}
// roundup round up size by the block size r
func roundup(n, r int) int {
r--
if (n & r) == 0 {
return n
}
return (n + r) & (^r)
}
func contains[T any](a []T, v T) bool {
return index(a, v) >= 0
}
func index[T any](a []T, v T) int {
for i, e := range a {
if any(e) == any(v) {
return i
}
}
return -1
}
func setMapPairs[K any, V any](m Map[K, V], ps ...P[K, V]) {
for _, p := range ps {
m.Set(p.Key, p.Value)
}
}
// CopyMap copy map 'src' to map 'des'
func CopyMap[K any, V any](dst, src Map[K, V]) {
if dst == src {
return
}
if im, ok := src.(Iterable2[K, V]); ok {
it := im.Iterator()
for it.Next() {
dst.Set(it.Key(), it.Value())
}
return
}
src.Each(func(k K, v V) {
dst.Set(k, v)
})
}