RoaringBitmap数据结构的Go移植

jopen 10年前

 Roaring bitmap数据结构的一个Go语言移植,Java版本可以查看:http://www.open-open.com/lib/view/open1419558335250.html

 Java 和 Go 两个版本的二进制相兼容,因此用Java程序保存的bitmaps,可以用Go加载,反之亦然。

依赖

  • go get github.com/smartystreets/goconvey/convey
  • go get github.com/willf/bitset

Naturally, you also need to grab the roaring code itself:

  • go get github.com/tgruben/roaring

示例

Here is a simplified but complete example:

package main    import (      "fmt"      "github.com/tgruben/roaring"      "bytes"  )      func main() {      // example inspired by https://github.com/fzandona/goroa      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)      rb1.WriteTo(buf) // we omit error handling      newrb:= NewRoaringBitmap()      newrb.ReadFrom(buf)      if rb1.Equals(newrb) {          fmt.Println("I wrote the content to a byte stream and read it back.")      }  }

项目主页:http://www.open-open.com/lib/view/home/1419559030812