From cb7a3a66b25d796f05d162700845ee4fa182efc7 Mon Sep 17 00:00:00 2001 From: teivah Date: Tue, 16 Jul 2019 01:25:13 +0100 Subject: [PATCH] Type --- README.md | 28 ++++++++-------------- bitvector.go | 24 ------------------- bitvector16.go | 57 ++++++++++++++++++--------------------------- bitvector16_test.go | 53 +++++++++++++++++------------------------ bitvector32.go | 57 ++++++++++++++++++--------------------------- bitvector32_test.go | 53 +++++++++++++++++------------------------ bitvector64.go | 57 ++++++++++++++++++--------------------------- bitvector64_test.go | 53 +++++++++++++++++------------------------ bitvector8.go | 57 ++++++++++++++++++--------------------------- bitvector8_test.go | 52 +++++++++++++++++------------------------ 10 files changed, 189 insertions(+), 302 deletions(-) delete mode 100644 bitvector.go diff --git a/README.md b/README.md index 3bf8511..133513d 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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: @@ -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: @@ -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 ``` \ No newline at end of file diff --git a/bitvector.go b/bitvector.go deleted file mode 100644 index 4060385..0000000 --- a/bitvector.go +++ /dev/null @@ -1,24 +0,0 @@ -package bitvector - -import ( - "fmt" -) - -// Handler represents an 8, 16, 32 or 64 bit vector -type Handler interface { - fmt.Stringer - // Clear bits from index i (included) to index j (excluded) - Clear(i, j uint8) - // Copy creates another bit vector structure based on the same length - Copy() Handler - // Count the number of bits set to 1 - Count() uint8 - // Get ith bit - Get(i uint8) bool - // Reset the bit vector to 0 - Reset() - // Set ith bit - Set(i uint8, b bool) - // Toggle ith bit - Toggle(i uint8) -} diff --git a/bitvector16.go b/bitvector16.go index 29de864..aa7318d 100644 --- a/bitvector16.go +++ b/bitvector16.go @@ -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 { - return - } - bv.n = math.MaxUint32< j { - return - } - bv.n = math.MaxUint64< j { - return - } - bv.n = math.MaxUint8<