@@ -12,12 +12,11 @@ import "C"
1212import (
1313 "bytes"
1414 "errors"
15- "strconv"
1615 "runtime"
16+ "strconv"
17+ "unsafe"
1718)
1819
19- import "unsafe"
20-
2120const CRoaringMajor = C .ROARING_VERSION_MAJOR
2221const CRoaringMinor = C .ROARING_VERSION_MINOR
2322const CRoaringRevision = C .ROARING_VERSION_REVISION
@@ -185,6 +184,53 @@ func (rb *Bitmap) SerializedSizeInBytes() int {
185184 return int (C .roaring_bitmap_portable_size_in_bytes (rb .cpointer ))
186185}
187186
187+ // IntIterable allows you to iterate over the values in a Bitmap
188+ type IntIterable interface {
189+ HasNext () bool
190+ Next () uint32
191+ }
192+
193+ type intIterator struct {
194+ pointertonext * C.roaring_uint32_iterator_t
195+ current uint32
196+ has_next bool
197+ }
198+
199+ // Iterator creates a new IntIterable to iterate over the integers contained in the bitmap, in sorted order
200+ func (rb * Bitmap ) Iterator () IntIterable {
201+ return newIntIterator (rb )
202+ }
203+
204+ // HasNext returns true if there are more integers to iterate over
205+ func (ii * intIterator ) HasNext () bool {
206+ return ii .has_next
207+ }
208+
209+ // Next returns the next integer
210+ func (ii * intIterator ) Next () uint32 {
211+ answer := ii .current
212+ ii .has_next = bool (ii .pointertonext .has_value )
213+ ii .current = uint32 (ii .pointertonext .current_value )
214+ C .roaring_advance_uint32_iterator (ii .pointertonext )
215+ return answer
216+ }
217+
218+ func freeIntIterator (a * intIterator ) {
219+ C .roaring_free_uint32_iterator (a .pointertonext )
220+ }
221+
222+ func newIntIterator (a * Bitmap ) * intIterator {
223+ p := new (intIterator )
224+ p .pointertonext = C .roaring_create_iterator (a .cpointer )
225+ p .has_next = bool (p .pointertonext .has_value )
226+ p .current = uint32 (p .pointertonext .current_value )
227+ if p .has_next {
228+ C .roaring_advance_uint32_iterator (p .pointertonext )
229+ }
230+ runtime .SetFinalizer (p , freeIntIterator )
231+ return p
232+ }
233+
188234// Write writes a serialized version of this bitmap to stream (you should have enough space)
189235func (rb * Bitmap ) Write (b []byte ) error {
190236 if len (b ) < rb .SerializedSizeInBytes () {
@@ -204,19 +250,19 @@ func (rb *Bitmap) ToArray() []uint32 {
204250
205251// String creates a string representation of the Bitmap
206252func (rb * Bitmap ) String () string {
207- arr := rb .ToArray () // todo: replace with an iterator
208- var buffer bytes.Buffer
253+ arr := rb .ToArray () // todo: replace with an iterator
254+ var buffer bytes.Buffer
209255 start := []byte ("{" )
210256 buffer .Write (start )
211- l := len (arr )
212- for counter ,i := range arr {
257+ l := len (arr )
258+ for counter , i := range arr {
213259 // to avoid exhausting the memory
214260 if counter > 0x40000 {
215261 buffer .WriteString ("..." )
216262 break
217263 }
218264 buffer .WriteString (strconv .FormatInt (int64 (i ), 10 ))
219- if counter + 1 < l { // there is more
265+ if counter + 1 < l { // there is more
220266 buffer .WriteString ("," )
221267 }
222268 }
0 commit comments