forked from RoaringBitmap/roaring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_roaring_test.go
61 lines (50 loc) · 1.22 KB
/
example_roaring_test.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
package roaring_test
import (
"bytes"
"fmt"
"github.com/tgruben/roaring"
)
// Example_roaring demonstrates how to use the roaring library.
func Example_roaring() {
// example inspired by https://github.com/fzandona/goroar
fmt.Println("==roaring==")
rb1 := roaring.BitmapOf(1, 2, 3, 4, 5, 100, 1000)
fmt.Println(rb1.String())
rb2 := roaring.BitmapOf(3, 4, 1000)
fmt.Println(rb2.String())
rb3 := roaring.NewRoaringBitmap()
fmt.Println(rb3.String())
fmt.Println("Cardinality: ", rb1.GetCardinality())
fmt.Println("Contains 3? ", rb1.Contains(3))
rb1.And(rb2)
rb3.Add(1)
rb3.Add(5)
rb3.Or(rb1)
// prints 1, 3, 4, 5, 1000
i := rb3.Iterator()
for i.HasNext() {
fmt.Println(i.Next())
}
fmt.Println()
// next we include an example of serialization
buf := new(bytes.Buffer)
size, err := rb1.WriteTo(buf)
if err != nil {
fmt.Println("Failed writing")
return
} else {
fmt.Println("Wrote ", size, " bytes")
}
newrb := roaring.NewRoaringBitmap()
size, err = newrb.ReadFrom(buf)
if err != nil {
fmt.Println("Failed reading")
return
}
if !rb1.Equals(newrb) {
fmt.Println("I did not get back to original bitmap?")
return
} else {
fmt.Println("I wrote the content to a byte stream and read it back.")
}
}