Skip to content

Commit bf70bef

Browse files
committed
Clone Go 1.23 iterator support for roaring64.
This builds on the previous work in #475.
1 parent 3ef13b7 commit bf70bef

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

roaring64/iter.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package roaring64
2+
3+
func Values(b *Bitmap) func(func(uint64) bool) {
4+
return func(yield func(uint64) bool) {
5+
it := b.Iterator()
6+
for it.HasNext() {
7+
if !yield(it.Next()) {
8+
return
9+
}
10+
}
11+
}
12+
}
13+
14+
func Backward(b *Bitmap) func(func(uint64) bool) {
15+
return func(yield func(uint64) bool) {
16+
it := b.ReverseIterator()
17+
for it.HasNext() {
18+
if !yield(it.Next()) {
19+
return
20+
}
21+
}
22+
}
23+
}

roaring64/iter_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package roaring64
2+
3+
import (
4+
"math"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestBackwardCount(t *testing.T) {
11+
array := []int{2, 63, 64, 65, 4095, 4096, 4097, 4159, 4160, 4161, 5000, 20000, 66666}
12+
for _, testSize := range array {
13+
b := New()
14+
for i := uint64(0); i < uint64(testSize); i++ {
15+
b.Add(i)
16+
}
17+
it := Values(b)
18+
19+
count := 0
20+
it(func(_ uint64) bool {
21+
count++
22+
return true
23+
})
24+
25+
assert.Equal(t, testSize, count)
26+
}
27+
}
28+
29+
func TestBackward(t *testing.T) {
30+
t.Run("#1", func(t *testing.T) {
31+
values := []uint64{0, 2, 15, 16, 31, 32, 33, 9999, math.MaxUint16}
32+
b := New()
33+
for n := 0; n < len(values); n++ {
34+
b.Add(values[n])
35+
}
36+
it := Backward(b)
37+
n := len(values) - 1
38+
39+
it(func(val uint64) bool {
40+
assert.EqualValues(t, val, values[n])
41+
n--
42+
return true
43+
})
44+
45+
it = Backward(b)
46+
n = len(values) - 1
47+
it(func(val uint64) bool {
48+
assert.EqualValues(t, val, values[n])
49+
assert.True(t, n >= 0)
50+
n--
51+
return true
52+
})
53+
})
54+
55+
t.Run("#2", func(t *testing.T) {
56+
b := New()
57+
it := Backward(b)
58+
59+
count := 0
60+
it(func(_ uint64) bool {
61+
count++
62+
return true
63+
})
64+
65+
assert.Equal(t, 0, count)
66+
})
67+
68+
t.Run("#3", func(t *testing.T) {
69+
b := New()
70+
b.AddInt(0)
71+
it := Backward(b)
72+
73+
// only one value zero
74+
it(func(val uint64) bool {
75+
assert.EqualValues(t, 0, val)
76+
return true
77+
})
78+
})
79+
80+
t.Run("#4", func(t *testing.T) {
81+
b := New()
82+
b.AddInt(9999)
83+
it := Backward(b)
84+
85+
// only one value 9999
86+
it(func(val uint64) bool {
87+
assert.EqualValues(t, 9999, val)
88+
return true
89+
})
90+
})
91+
92+
t.Run("#5", func(t *testing.T) {
93+
b := New()
94+
b.AddInt(math.MaxUint16)
95+
it := Values(b)
96+
97+
// only one value MaxUint16
98+
it(func(val uint64) bool {
99+
assert.EqualValues(t, math.MaxUint16, val)
100+
return true
101+
})
102+
})
103+
}
104+
105+
func TestValues(t *testing.T) {
106+
b := New()
107+
108+
testSize := 5000
109+
for i := 0; i < testSize; i++ {
110+
b.AddInt(i)
111+
}
112+
113+
it := Values(b)
114+
n := 0
115+
it(func(val uint64) bool {
116+
assert.Equal(t, uint64(n), val)
117+
n++
118+
return true
119+
})
120+
121+
assert.Equal(t, testSize, n)
122+
}

0 commit comments

Comments
 (0)