Giter Site home page Giter Site logo

bitset's Introduction

Bi-endianess Bit Vector

Build Status GoDoc

bitset is Bit Vector (Array) library supporting both Little Endian and Big Endian for Golang.

bitset write bit vector to byte array with specified endianness (Little Endian or Big Endian) regardless of host endianness. It enables to transfer a file holding bit vector to different endianness machine without any conversion process.

bitset calculate each bit in uint64. bitset switches optimized bit vector operation (SetUnsetGet etc...) by each host endianness.

日本語の README はこちら

Features

  • Zero Copy : Cast []byte provided by user to []uint64 without any memory copy using unsafe package.
  • Bi-Endianness : Switches bit vector operation by host endianness (Little Endian or Big Endian)
  • Compatibility : Ready to transfer a file holding bit vector to different endianness machine without any conversion process

Example

func main() {
	// in memory usage
	buf := make([]byte, 2*8)
	b, _ := bitset.New(buf, binary.LittleEndian, true)
	for _, v := range []uint{0, 1, 3, 6, 10, 64, 127, 128} {
		b.Set(v) // when v == 128, auto extend vector and it returns true
	}
	fmt.Println(buf) // [75 4 0 0 0 0 0 0 1 0 0 0 0 0 0 128]

	b.Unset(127)
	fmt.Println(b.Get(127)) // false

	for v, ok := b.FindFirstOne(0); ok; v, ok = b.FindFirstOne(v + 1) {
		fmt.Println(v) // 0 1 3 6 10 64
	}

	// File + mmap usage
	const pageSize = 4 * 1024
	f, _ := os.OpenFile("example.bit", os.O_RDWR|os.O_CREATE, 0666)
	f.Truncate(pageSize)
	buf, _ = syscall.Mmap(int(f.Fd()), 0, pageSize,
		syscall.PROT_READ|syscall.PROT_WRITE,
		syscall.MAP_SHARED)
	defer func() {
		f.Sync()
		syscall.Munmap(buf)
		f.Close()
	}()

	b, _ = bitset.New(buf, binary.BigEndian, false)
	for v, ok := b.FindFirstOne(0); ok; v, ok = b.FindFirstOne(v + 1) {
		fmt.Println(v) // 0 1 3 6 10 64  if executed twice
	}
	for _, v := range []uint{0, 1, 3, 6, 10, 64, 127, 128} {
		b.Set(v) // when v == 128, returns false because overflow. not extend vector
	}
	fmt.Println(buf) // [0 0 0 0 0 0 4 75 128 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 ....
}

Installation

go get github.com/kawasin73/bitset

Notices

  • Length of the buffer ([]byte) provided by user MUST be a multiple of 8. (or New() returns error bitset.ErrInvalidLength)
  • bitset supports only Little Endian and Big Endian, not middle endian or other endianness.
  • bitset does not auto expand provided buffer if extend (New() 3rd argument) is false. If you need to expand bit vector then re-create bitset.BitVec with expanded buffer by user.
  • Supports Go Version > 1.9. using math/bits package.

LICENSE

MIT

bitset's People

Contributors

kawasin73 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.