-
Notifications
You must be signed in to change notification settings - Fork 2
/
array.go
70 lines (55 loc) · 1.25 KB
/
array.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
67
68
69
70
package chd
import "reflect"
// CompactArray represents a
// compressed integer array.
type CompactArray interface {
Add(value int)
Get(index int) int
Len() int
Size() int
}
// array is the CompactArray used
// to store hash indices. If this
// is nil, then an integer array is
// used.
var arrayType reflect.Type
// SetCompactArray sets the compressed
// array used to store hash indices. It
// is important that the compact array
// used when building and reading a map
// is exactly the same.
func SetCompactArray(a CompactArray) {
arrayType = indirect(reflect.TypeOf(a))
}
// newCompactArray returns a new instance
// of CompactArray with type arrayType.
func newCompactArray() CompactArray {
if arrayType == nil {
return newIntArray(0)
}
va := reflect.New(arrayType)
return va.Interface().(CompactArray)
}
func indirect(t reflect.Type) reflect.Type {
if t.Kind() != reflect.Ptr {
return t
}
return t.Elem()
}
type intArray []int32
func newIntArray(size int) *intArray {
a := make(intArray, 0, size)
return &a
}
func (a *intArray) Add(value int) {
*a = append(*a, int32(value))
}
func (a intArray) Get(index int) int {
return int(a[index])
}
func (a intArray) Len() int {
return len(a)
}
func (a intArray) Size() int {
return len(a) * 4
}