Skip to content

KarpelesLab/bitmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Test Go Reference Coverage Status

Bitmap

Simple, efficient bitmaps in Go with atomic operation support.

Installation

go get github.com/KarpelesLab/bitmap

API

// Create a new bitmap with capacity for n bits
bitmap.New(n int) Bitmap

// Non-atomic operations
Get(bit int) bool
Set(bit int, value bool)
Toggle(bit int)

// Atomic operations (thread-safe)
GetAtomic(bit int) bool
SetAtomic(bit int, value bool)
ToggleAtomic(bit int)

Example

package main

import "github.com/KarpelesLab/bitmap"

func main() {
    m := bitmap.New(128)

    m.Set(42, true)

    if m.Get(42) {
        // bit 42 is set
    }

    // Thread-safe operations
    m.SetAtomic(100, true)
    m.ToggleAtomic(100)
}

Why?

There are already a few bitmap implementations in Go available out there, however they have some shortfalls and/or are too complex for what a bitmap should do.

  • boljen's go-bitmap has separate implementations for Bitmap/Concurrent/Threadsafe, and complexifies a lot what should be very simple.
  • ShawnMilo's bitmap has a nice feel but lacks atomic methods and adds string methods using json/gzip/base64 (?)
  • Roaring Bitmaps are simply too complex for what I need bitmaps for. You should however definitely use that if you store more than 200k bits or so in total.

About

Bitmap manipulation in Go

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •