Skip to content

Commit

Permalink
Type
Browse files Browse the repository at this point in the history
  • Loading branch information
teivah committed Jul 16, 2019
1 parent ddcfce5 commit cb7a3a6
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 302 deletions.
28 changes: 10 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@

A bit vector is an array data structure that compactly stores bits.

This library is based on 4 different data structures:
This library is statically based on 4 different data structures:
* 8-bit vector: relies on an internal `uint8`
* 16-bit vector: relies on an internal `uint16`
* 32-bit vector: relies on an internal `uint32`
* 64-bit vector: relies on an internal `uint64`

The rationale compared to being based on a `[]byte` is to save memory if you need a static bit vector structure. Hence, you might be interested in this library for memory-bound computation.

Moreover,

## Installation

```
Expand Down Expand Up @@ -51,8 +55,8 @@ bv := bitvector.New64()
* Set ith bit:

```go
bv.Set(i, true)
bv.Set(i, false)
bv = bv.Set(i, true)
bv = bv.Set(i, false)
```

* Get ith bit:
Expand All @@ -64,13 +68,13 @@ b := bv.Get(i) // bool
* Toggle (flip) ith bit:

```go
bv.Toggle(i)
bv = bv.Toggle(i)
```

* Clear bits from index i (included) to index j (excluded):

```go
bv.Clear(i, j)
bv = bv.Clear(i, j)
```

* Count the number of bits set to 1:
Expand All @@ -79,20 +83,8 @@ bv.Clear(i, j)
i := bv.Count() // uint8
```

* Reset the internal bit vector:

```go
bv.Reset()
```

* Copy the current bit vector to another data structure:

```go
bv2 := bv.Copy() // bitvector.Handler
```

* Convert the internal bit vector structure to a string:

```go
s := bv.String()
s := bv.String() // string
```
24 changes: 0 additions & 24 deletions bitvector.go

This file was deleted.

57 changes: 23 additions & 34 deletions bitvector16.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,28 @@ import (
"math"
)

type bitVector16 struct {
n uint16
}
// Len16 is a 16-bit vector
type Len16 uint16

func (bv *bitVector16) String() string {
return fmt.Sprintf("%b", bv.n)
func (bv Len16) String() string {
return fmt.Sprintf("%b", bv)
}

func (bv *bitVector16) Clear(i, j uint8) {
// Clear bits from index i (included) to index j (excluded)
func (bv Len16) Clear(i, j uint8) Len16 {
if i > j {
return
}
bv.n = math.MaxUint16<<j | ((1<<i)-1)&bv.n
}

func (bv *bitVector16) Copy() Handler {
return &bitVector16{
n: bv.n,
return bv
}
return math.MaxUint16<<j | ((1<<i)-1)&bv
}

func (bv *bitVector16) Count() uint8 {
// Count the number of bits set to 1
func (bv Len16) Count() uint8 {
var count uint8
var index uint16 = 1
var index Len16 = 1
var i uint8
for {
if bv.n&index != 0 {
if bv&index != 0 {
count++
}
index <<= 1
Expand All @@ -43,28 +38,22 @@ func (bv *bitVector16) Count() uint8 {
return count
}

func (bv *bitVector16) Toggle(i uint8) {
bv.n ^= 1 << i
// Toggle ith bit
func (bv Len16) Toggle(i uint8) Len16 {
return bv ^ 1<<i
}

func (bv *bitVector16) Get(i uint8) bool {
return (bv.n & (1 << i)) != 0
// Get ith bit
func (bv Len16) Get(i uint8) bool {
return (bv & (1 << i)) != 0
}

func (bv *bitVector16) Reset() {
bv.n = 0
}

func (bv *bitVector16) Set(i uint8, b bool) {
var value uint16
// Set ith bit
func (bv Len16) Set(i uint8, b bool) Len16 {
var value Len16
if b {
value = 1
}
var mask uint16 = ^(1 << i)
bv.n = (bv.n & mask) | (value << i)
}

// New16 creates a new 16-bit vector
func New16() Handler {
return &bitVector16{}
var mask Len16 = ^(1 << i)
return (bv & mask) | (value << i)
}
53 changes: 22 additions & 31 deletions bitvector16_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import (
"github.com/stretchr/testify/assert"
)

func TestBV16(t *testing.T) {
bv := &bitVector16{}
func Test_Len16(t *testing.T) {
var bv Len16

// Set
assert.Equal(t, uint16(0), bv.n)
bv.Set(0, true)
assert.Equal(t, uint16(1), bv.n)
bv.Set(15, true)
assert.Equal(t, uint16(32769), bv.n)
bv.Set(16, true)
assert.Equal(t, uint16(32769), bv.n)
bv.Set(14, true)
assert.Equal(t, uint16(49153), bv.n)
bv.Set(14, false)
assert.Equal(t, uint16(32769), bv.n)
assert.Equal(t, Len16(0), bv)
bv = bv.Set(0, true)
assert.Equal(t, Len16(1), bv)
bv = bv.Set(15, true)
assert.Equal(t, Len16(32769), bv)
bv = bv.Set(16, true)
assert.Equal(t, Len16(32769), bv)
bv = bv.Set(14, true)
assert.Equal(t, Len16(49153), bv)
bv = bv.Set(14, false)
assert.Equal(t, Len16(32769), bv)

// Count
assert.Equal(t, uint8(2), bv.Count())
Expand All @@ -31,31 +31,22 @@ func TestBV16(t *testing.T) {
assert.True(t, bv.Get(15))
assert.False(t, bv.Get(14))

// Reset
bv.Reset()
assert.Equal(t, uint16(0), bv.n)

// Toggle
bv.Toggle(15)
assert.Equal(t, uint16(32768), bv.n)
bv = 0
bv = bv.Toggle(15)
assert.Equal(t, Len16(32768), bv)

// Clear
bv.Reset()
bv = 0
var i uint8
for ; i < 16; i++ {
bv.Set(i, true)
bv = bv.Set(i, true)
}
bv.Clear(6, 2)
assert.Equal(t, uint16(math.MaxUint16), bv.n)
bv.Clear(2, 6)
assert.Equal(t, uint16(65475), bv.n)

// Copy
assert.Equal(t, uint8(12), bv.Copy().Count())
bv = bv.Clear(6, 2)
assert.Equal(t, Len16(math.MaxUint16), bv)
bv = bv.Clear(2, 6)
assert.Equal(t, Len16(65475), bv)

// String
assert.Equal(t, "1111111111000011", bv.String())

// New
assert.Equal(t, uint8(0), New16().Count())
}
57 changes: 23 additions & 34 deletions bitvector32.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,28 @@ import (
"math"
)

type bitVector32 struct {
n uint32
}
// Len32 is a 32-bit vector
type Len32 uint32

func (bv *bitVector32) String() string {
return fmt.Sprintf("%b", bv.n)
func (bv Len32) String() string {
return fmt.Sprintf("%b", bv)
}

func (bv *bitVector32) Clear(i, j uint8) {
// Clear bits from index i (included) to index j (excluded)
func (bv Len32) Clear(i, j uint8) Len32 {
if i > j {
return
}
bv.n = math.MaxUint32<<j | ((1<<i)-1)&bv.n
}

func (bv *bitVector32) Copy() Handler {
return &bitVector32{
n: bv.n,
return bv
}
return math.MaxUint32<<j | ((1<<i)-1)&bv
}

func (bv *bitVector32) Count() uint8 {
// Count the number of bits set to 1
func (bv Len32) Count() uint8 {
var count uint8
var index uint32 = 1
var index Len32 = 1
var i uint8
for {
if bv.n&index != 0 {
if bv&index != 0 {
count++
}
index <<= 1
Expand All @@ -43,28 +38,22 @@ func (bv *bitVector32) Count() uint8 {
return count
}

func (bv *bitVector32) Toggle(i uint8) {
bv.n ^= 1 << i
// Toggle ith bit
func (bv Len32) Toggle(i uint8) Len32 {
return bv ^ 1<<i
}

func (bv *bitVector32) Get(i uint8) bool {
return (bv.n & (1 << i)) != 0
// Get ith bit
func (bv Len32) Get(i uint8) bool {
return (bv & (1 << i)) != 0
}

func (bv *bitVector32) Reset() {
bv.n = 0
}

func (bv *bitVector32) Set(i uint8, b bool) {
var value uint32
// Set ith bit
func (bv Len32) Set(i uint8, b bool) Len32 {
var value Len32
if b {
value = 1
}
var mask uint32 = ^(1 << i)
bv.n = (bv.n & mask) | (value << i)
}

// New32 creates a new 32-bit vector
func New32() Handler {
return &bitVector32{}
var mask Len32 = ^(1 << i)
return (bv & mask) | (value << i)
}
Loading

0 comments on commit cb7a3a6

Please sign in to comment.