This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inmem64.go
114 lines (98 loc) · 2.04 KB
/
inmem64.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package bigarray
import (
"fmt"
"io"
)
type inMemoryArray64 struct {
data []uint64
max uint64
ro bool
}
func (ba *inMemoryArray64) Frozen() bool {
return ba.ro
}
func (ba *inMemoryArray64) MaxValue() uint64 {
return ba.max
}
func (ba *inMemoryArray64) Len() uint64 {
return uint64(len(ba.data))
}
func (ba *inMemoryArray64) ValueAt(index uint64) (uint64, error) {
if index >= ba.Len() {
return ^uint64(0), io.EOF
}
return ba.data[index], nil
}
func (ba *inMemoryArray64) SetValueAt(index uint64, value uint64) error {
if ba.ro {
panic("BigArray is read-only")
}
if value > ba.MaxValue() {
panic(fmt.Sprintf("value out of range: value %d vs max %d", value, ba.MaxValue()))
}
if index >= ba.Len() {
return io.EOF
}
ba.data[index] = value
return nil
}
func (ba *inMemoryArray64) Iterate(i, j uint64) Iterator {
if i > j {
panic(fmt.Errorf("inMemoryArray64.Iterate: i > j: i=%d j=%d", i, j))
}
return &inMemoryIterator{
ba: ba,
base: i,
num: (j - i),
val: ^uint64(0),
}
}
func (ba *inMemoryArray64) ReverseIterate(i, j uint64) Iterator {
if i > j {
panic(fmt.Errorf("inMemoryArray64.ReverseIterate: i > j: i=%d j=%d", i, j))
}
return &inMemoryIterator{
ba: ba,
base: i,
num: (j - i),
val: ^uint64(0),
down: true,
}
}
func (ba *inMemoryArray64) CopyFrom(src BigArray) error {
if ba.ro {
panic("BigArray is read-only")
}
if src.Len() != ba.Len() {
panic("big arrays are not equal in size")
}
if x, ok := src.(*inMemoryArray64); ok {
copy(ba.data, x.data)
return nil
}
return copyFromImpl(ba, src)
}
func (ba *inMemoryArray64) Truncate(n uint64) error {
if ba.ro {
panic("BigArray is read-only")
}
if n > ba.Len() {
panic("cannot grow a big array")
}
ba.data = ba.data[0:n]
return nil
}
func (ba *inMemoryArray64) Freeze() error {
ba.ro = true
return nil
}
func (ba *inMemoryArray64) Flush() error {
return nil
}
func (ba *inMemoryArray64) Close() error {
return nil
}
func (ba *inMemoryArray64) Debug() string {
return debugImpl(ba)
}
var _ BigArray = (*inMemoryArray64)(nil)