-
Notifications
You must be signed in to change notification settings - Fork 114
/
array_map.go
96 lines (89 loc) · 2.03 KB
/
array_map.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
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
package antnet
type ArrayMap struct {
cap int32
rawCap int32
genArray []interface{}
delArray []int32
generArray []int32
genIndex int32
delIndex int32
}
func (r *ArrayMap) Clone() *ArrayMap {
return &ArrayMap{
cap: r.cap,
rawCap: r.rawCap,
genArray: append([]interface{}{}, r.genArray...),
delArray: append([]int32{}, r.delArray...),
generArray: append([]int32{}, r.generArray...),
genIndex: r.genIndex,
delIndex: r.delIndex,
}
}
func NewArrayMap(cap int32, fixedArray bool) *ArrayMap {
m := &ArrayMap{
cap: cap,
rawCap: cap,
genArray: make([]interface{}, cap),
delArray: make([]int32, cap),
generArray: make([]int32, cap),
genIndex: -1,
delIndex: -1,
}
if fixedArray {
m.genIndex = cap - 1
}
return m
}
func (r *ArrayMap) Add(value interface{}) int32 {
var index int32 = -1
if r.delIndex >= 0 {
index = r.delArray[r.delIndex]
r.delIndex--
}
if index == -1 {
index = r.genIndex + 1
r.genIndex++
if index >= r.cap {
newArray := make([]interface{}, r.rawCap)
r.genArray = append(r.genArray, newArray...)
newDelArray := make([]int32, r.rawCap)
r.delArray = append(r.delArray, newDelArray...)
newDelArray = make([]int32, r.rawCap)
r.generArray = append(r.generArray, newDelArray...)
r.cap += r.rawCap
}
}
r.genArray[index] = value
return index + (r.generArray[index] << 16)
}
func (r *ArrayMap) Set(key int32, value interface{}) {
index := key & 0x0000FFFF
r.genArray[index] = value
}
func (r *ArrayMap) Del(key int32) {
index := key & 0x0000FFFF
r.delArray[r.delIndex+1] = index
r.genArray[index] = nil
r.generArray[index]++
r.delIndex++
}
func (r *ArrayMap) RawLen() int32 {
return r.genIndex + 1
}
func (r *ArrayMap) RawGet(key int32) interface{} {
if key >= r.cap {
return nil
}
return r.genArray[key]
}
func (r *ArrayMap) Get(key int32) interface{} {
index := key & 0x0000FFFF
gener := key >> 16
if index >= r.cap {
return nil
}
if gener != r.generArray[index] {
return nil
}
return r.genArray[index]
}