Skip to content

Commit

Permalink
Make bitarray display nicer progress patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekjarosik committed Nov 4, 2024
1 parent 7acae35 commit cd1b2fc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
25 changes: 25 additions & 0 deletions pkg/bitarray/bitarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,35 @@ func (b *BitArray) GetByte(pos int) byte {
return b.data[pos]
}

// swapBits swaps the bits at positions pos1 and pos2 in byte b.
func swapBits(b byte, pos1 uint, pos2 uint) byte {
// Ensure positions are within the range [0,7]
if pos1 > 7 || pos2 > 7 {
// Positions out of range, return b unchanged or handle error
return b
}

// Extract the bits at positions pos1 and pos2
bit1 := (b >> pos1) & 1
bit2 := (b >> pos2) & 1

// If the bits are different, swap them
if bit1 != bit2 {
// Create a bitmask where bits at pos1 and pos2 are set
mask := (uint8(1) << pos1) | (uint8(1) << pos2)
// Toggle the bits at pos1 and pos2 using XOR
b ^= mask
}
return b
}

// braillePattern returns the Unicode Braille Pattern character for a given byte mask.
func braillePattern(mask byte) rune {
// Base Unicode point for Braille Patterns (all dots blank)
base := rune(0x2800)
mask = swapBits(mask, 3, 4)
mask = swapBits(mask, 4, 5)
mask = swapBits(mask, 5, 6)
return base + rune(mask)
}

Expand Down
12 changes: 10 additions & 2 deletions pkg/bitarray/bitarray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ func TestBitArray_GetByte(t *testing.T) {
}

func TestBitArray_String(t *testing.T) {
ba := New(64)
for idx := 0; idx < 64; idx++ {
ba := New(16)
for idx := 0; idx < 16; idx++ {
ba.Set(idx)
fmt.Printf("bitarray: %s\n", ba)
}
}

func TestBitArray_SingleCharacter(t *testing.T) {
ba := New(8)
for idx := 0; idx < 8; idx++ {
ba.Set(idx)
fmt.Printf("bitarray: %s\n", ba)
}
Expand Down

0 comments on commit cd1b2fc

Please sign in to comment.