forked from marcetin/parallelcoin
/
uint64.go
67 lines (62 loc) · 1.07 KB
/
uint64.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
package ring
type BufferUint64 struct {
Buf []uint64
Cursor int
Full bool
}
func NewBufferUint64(size int) *BufferUint64 {
return &BufferUint64{
Buf: make([]uint64, size),
Cursor: -1,
}
}
// Get returns the value at the given index or nil if nothing
func (b *BufferUint64) Get(index int) (out *uint64) {
bl := len(b.Buf)
if index < bl {
cursor := b.Cursor + index
if cursor > bl {
cursor = cursor - bl
}
return &b.Buf[cursor]
}
return
}
func (b *BufferUint64) Add(value uint64) {
b.Cursor++
if b.Cursor == len(b.Buf) {
b.Cursor = 0
if !b.Full {
b.Full = true
}
}
b.Buf[b.Cursor] = value
}
func (b *BufferUint64) ForEach(fn func(v uint64) error) (e error) {
c := b.Cursor
i := c + 1
if i == len(b.Buf) {
// D.Ln("hit the end")
i = 0
}
if !b.Full {
// D.Ln("buffer not yet full")
i = 0
}
// D.Ln(b.Buf)
for ; ; i++ {
if i == len(b.Buf) {
// D.Ln("passed the end")
i = 0
}
if i == c {
// D.Ln("reached cursor again")
break
}
// D.Ln(i, b.Cursor)
if e = fn(b.Buf[i]); E.Chk(e) {
break
}
}
return
}