chore: refactor structure

This commit is contained in:
Ilia Denisov
2025-11-21 21:40:15 +03:00
parent 126f381b04
commit 269de2184c
72 changed files with 512 additions and 393 deletions
@@ -0,0 +1,6 @@
░░██████░░░░
██░░░░░░██░░
██░░░░░░██░░
██░░░░░░██░░
░░██████░░░░
░░░░░░░░░░░░
@@ -0,0 +1,6 @@
████░░░░░░██
██████░░████
██████░░████
██████░░████
████░░░░░░██
░░░░░░░░░░░░
@@ -0,0 +1,12 @@
░░░░░░██████████░░░░░░░░
░░░░██████████████░░░░░░
░░██████████████████░░░░
██████████████████████░░
██████████████████████░░
██████████████████████░░
██████████████████████░░
██████████████████████░░
░░██████████████████░░░░
░░░░██████████████░░░░░░
░░░░░░██████████░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░
@@ -0,0 +1,12 @@
██░░░░░░░░░░░░░░░░░░██░░
██░░░░░░░░░░░░░░░░░░██░░
██░░░░░░░░░░░░░░░░░░██░░
░░██░░░░░░░░░░░░░░██░░░░
░░░░██░░░░░░░░░░██░░░░░░
░░░░░░██████████░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░██████████░░░░░░░░
░░░░██░░░░░░░░░░██░░░░░░
░░██░░░░░░░░░░░░░░██░░░░
██░░░░░░░░░░░░░░░░░░██░░
██░░░░░░░░░░░░░░░░░░██░░
@@ -0,0 +1,15 @@
░░░░░░░░░░██████████░░░░░░░░░░
░░░░░░████░░░░░░░░░░████░░░░░░
░░░░██░░░░░░░░░░░░░░░░░░██░░░░
░░██░░░░░░░░░░░░░░░░░░░░░░██░░
░░██░░░░░░░░░░░░░░░░░░░░░░██░░
██░░░░░░░░░░░░░░░░░░░░░░░░░░██
██░░░░░░░░░░░░░░░░░░░░░░░░░░██
██░░░░░░░░░░░░░░░░░░░░░░░░░░██
██░░░░░░░░░░░░░░░░░░░░░░░░░░██
██░░░░░░░░░░░░░░░░░░░░░░░░░░██
░░██░░░░░░░░░░░░░░░░░░░░░░██░░
░░██░░░░░░░░░░░░░░░░░░░░░░██░░
░░░░██░░░░░░░░░░░░░░░░░░██░░░░
░░░░░░████░░░░░░░░░░████░░░░░░
░░░░░░░░░░██████████░░░░░░░░░░
+173
View File
@@ -0,0 +1,173 @@
package bitmap
import (
"errors"
"fmt"
"math"
"math/bits"
)
const intSize = 32
type Bitmap struct {
width uint32
height uint32
bitVector []uint32
}
func NewBitmap(width uint32, height uint32) Bitmap {
return Bitmap{width: width, height: height, bitVector: make([]uint32, int(math.Ceil(float64(width*height)/intSize)))}
}
func (p Bitmap) Set(x, y int) {
boundX := (p.width + uint32(x)) % p.width
boundY := (p.height + uint32(y)) % p.height
p.set(boundX + boundY*p.width)
}
func (p Bitmap) set(number uint32) {
p.bitVector[number/intSize] |= (0b1 << (number % intSize))
}
func (p Bitmap) IsSet(x, y int) bool {
return p.isSet(uint32(x) + uint32(y)*p.width)
}
func (p Bitmap) isSet(number uint32) bool {
return p.bitVector[number/intSize]&(0b1<<(number%intSize)) > 0
}
func (p Bitmap) FreeCount() (result int) {
result = int(p.width) * int(p.height)
for i := range p.bitVector {
result -= bits.OnesCount32(p.bitVector[i])
}
return
}
func (p Bitmap) GetFreeN(number int) (int, int, error) {
if p.FreeCount() == 0 {
return 0, 0, errors.New("no free pixels left")
}
fc := 0
n := 0
for ; n < int(p.width)*int(p.height); n++ {
if p.isSet(uint32(n)) {
continue
}
if fc == number {
y := n / int(p.height)
x := n - int(p.height)*y
return x, y, nil
}
fc++
}
return 0, 0, fmt.Errorf("get free pixel: no such number=%d, max=%d", number, n)
}
func (p Bitmap) SetFreeN(number int) error {
if p.FreeCount() == 0 {
return errors.New("no free pixels left")
}
fc := 0
n := 0
for ; n < int(p.width)*int(p.height); n++ {
if p.isSet(uint32(n)) {
continue
}
if fc == number {
p.set(uint32(n))
return nil
}
fc++
}
return fmt.Errorf("set free pixel: no such number=%d, max=%d", number, n)
}
func (p Bitmap) Circle(x, y int, r float64, fill bool) {
plotX := 0
plotY := int(math.Ceil(r))
delta := 3 - 2*plotY
lastY := plotY
for plotX <= plotY {
p.octant(x, y, plotX, plotY)
if fill && plotY < lastY {
for lineX := 0; lineX < plotX; lineX++ {
p.octant(x, y, lineX, plotY)
}
lastY = plotY
}
if delta < 0 {
delta += 4*plotX + 6
} else {
delta += 4*(plotX-plotY) + 10
plotY -= 1
}
plotX += 1
}
if !fill {
return
}
for fillX := 0; fillX < plotX; fillX++ {
for fillY := 0; fillY <= fillX; fillY++ {
p.octant(x, y, fillX, fillY)
}
}
}
func (p Bitmap) circleAdjacent(x, y int, r float64) {
plotX := 0
plotY := int(math.Ceil(r))
delta := 1 - 2*plotY
err := 0
for plotX <= plotY {
p.octant(x, y, plotX, plotY)
err = 2*(delta+plotY) - 1
if delta < 0 && err <= 0 {
plotX += 1
delta += 2*plotX + 1
continue
}
if delta > 0 && err > 0 {
plotY -= 1
delta -= 2*plotY + 1
continue
}
plotX += 1
plotY -= 1
delta += 2 * (plotX - plotY)
}
}
func (p Bitmap) octant(x, y int, plotX, plotY int) {
p.Set(x+plotX, y+plotY)
p.Set(x+plotX, y-plotY)
p.Set(x-plotX, y+plotY)
p.Set(x-plotX, y-plotY)
p.Set(x+plotY, y+plotX)
p.Set(x+plotY, y-plotX)
p.Set(x-plotY, y+plotX)
p.Set(x-plotY, y-plotX)
}
func (p Bitmap) Clear() {
for i := range p.bitVector {
p.bitVector[i] &= 0
}
}
func (p Bitmap) String() string {
px := map[bool]string{true: "██", false: "░░"}
var result string
cnt := 0
for i := 0; i < len(p.bitVector); i++ {
for bit := 0; bit < intSize && cnt < int(p.width*p.height); bit++ {
result += px[p.bitVector[i]&(0b1<<bit) > 0]
cnt++
if cnt%int(p.width) == 0 {
result += "\n"
}
}
}
return result
}
+249
View File
@@ -0,0 +1,249 @@
package bitmap_test
import (
"fmt"
"math/rand"
"os"
"strings"
"testing"
"github.com/iliadenisov/galaxy/internal/bitmap"
)
func TestBitVectorSize(t *testing.T) {
type testCase struct {
width, height uint32
expectSize int
}
for _, tc := range []testCase{
{
width: 10,
height: 10,
expectSize: 4,
},
{
width: 1,
height: 1,
expectSize: 1,
},
{
width: 32,
height: 32,
expectSize: 32,
},
} {
t.Run(fmt.Sprintf("w=%d h=%d s=%d", tc.width, tc.height, tc.expectSize), func(t *testing.T) {
bm := bitmap.NewBitmap(tc.width, tc.height)
l := len(bitmap.Value(bm))
if tc.expectSize != l {
t.Errorf("expected bitmap size: %d, got: %d", tc.expectSize, l)
}
})
}
}
func TestSetPixel(t *testing.T) {
type coord struct {
x, y int
}
type testCase struct {
width, height uint32
pixels []coord
bits []int
}
asMap := func(bits []int) map[int]bool {
result := make(map[int]bool)
for i := range bits {
if _, ok := result[bits[i]]; ok {
t.Fatalf("source bits duplicate at idx=%d", i)
} else {
result[bits[i]] = true
}
}
return result
}
asUint32 := func(v bool) uint32 { return map[bool]uint32{true: 1, false: 0}[v] }
for i, tc := range []testCase{
{
width: 5,
height: 5,
pixels: []coord{{0, 0}},
bits: []int{0},
},
{
width: 5,
height: 5,
pixels: []coord{{1, 0}},
bits: []int{1},
},
{
width: 5,
height: 5,
pixels: []coord{{2, 0}},
bits: []int{2},
},
{
width: 5,
height: 5,
pixels: []coord{{0, 1}},
bits: []int{5},
},
{
width: 5,
height: 5,
pixels: []coord{{4, 4}},
bits: []int{24},
},
{
width: 8,
height: 8,
pixels: []coord{{7, 7}},
bits: []int{63},
},
} {
t.Run(fmt.Sprintf("tc#%d", i), func(t *testing.T) {
bm := bitmap.NewBitmap(tc.width, tc.height)
for _, c := range tc.pixels {
if bm.IsSet(c.x, c.y) {
t.Errorf("expected pixel to be clear at x=%d y=%d", c.x, c.y)
}
bm.Set(c.x, c.y)
if !bm.IsSet(c.x, c.y) {
t.Errorf("expected pixel to be set at x=%d y=%d", c.x, c.y)
}
}
bitVector := bitmap.Value(bm)
bitNum := 0
expected := asMap(tc.bits)
for bi := range bitVector {
for ; bitNum < (bi+1)*32; bitNum++ {
if (bitVector[bi]>>(bitNum%32))&1 != asUint32(expected[bitNum]) {
t.Errorf("expected: bit #%d to be %t, got %v", bitNum, expected[bitNum], uint32(1<<(bitNum%32)))
}
}
}
})
}
}
func TestFreeCount(t *testing.T) {
bm := bitmap.NewBitmap(10, 10)
type testCase struct {
x, y, expect int
}
for _, tc := range []testCase{
{0, 0, 99},
{5, 5, 98},
{9, 9, 97},
{10, 10, 97},
{15, 6, 96},
{9, 9, 96},
{3, 8, 95},
} {
t.Run(fmt.Sprintf("x=%d,y=%d", tc.x, tc.y), func(t *testing.T) {
bm.Set(tc.x, tc.y)
count := bm.FreeCount()
if tc.expect != count {
t.Errorf("expected: %d, actual %d", tc.expect, count)
}
})
}
}
func TestSetFreeN(t *testing.T) {
bm := bitmap.NewBitmap(5, 5)
type testCase struct {
x, y, number int
}
for i, tc := range []testCase{
{0, 0, 0},
{1, 0, 0},
{4, 0, 2},
{2, 2, 9},
{1, 1, 3},
{3, 1, 4},
{3, 2, 7},
{4, 4, 17},
{3, 0, 1},
} {
t.Run(fmt.Sprintf("tc#%d", i), func(t *testing.T) {
if x, y, err := bm.GetFreeN(tc.number); err != nil {
t.Errorf("get free by number=%d: %s", tc.number, err)
} else if tc.x != x || tc.y != y {
t.Fatalf("expected: x=%d, y=%d by number=%d, got: x=%d, y=%d", tc.x, tc.y, tc.number, x, y)
}
if err := bm.SetFreeN(tc.number); err != nil {
t.Errorf("set free by number=%d: %s", tc.number, err)
} else if !bm.IsSet(tc.x, tc.y) {
t.Errorf("expected to be set: free_number=%d @ x=%d,y=%d bitmap:\n%s", tc.number, tc.x, tc.y, bm)
}
})
}
bm = bitmap.NewBitmap(2, 2)
bm.Set(0, 0)
bm.Set(1, 0)
bm.Set(0, 1)
if err := bm.SetFreeN(1); err == nil {
t.Errorf("expected: error when free pixel number greater than free pixels count")
}
if _, _, err := bm.GetFreeN(1); err == nil {
t.Errorf("expected: error when free pixel number greater than free pixels count")
}
bm.Set(1, 1)
if err := bm.SetFreeN(0); err == nil {
t.Errorf("expected: error when no free pixels left")
}
}
func TestClear(t *testing.T) {
var bm = bitmap.NewBitmap(10, 10)
for range 50 {
bm.Set(rand.Intn(10), rand.Intn(10))
}
var acc uint32
for _, holder := range bitmap.Value(bm) {
acc |= holder
}
if acc == 0 {
t.Errorf("some pixels should be set")
}
bm.Clear()
acc = 0
for _, holder := range bitmap.Value(bm) {
acc |= holder
}
if acc != 0 {
t.Errorf("all pixels should be clear")
}
}
func TestCircle(t *testing.T) {
type testCase struct {
x, y int
r float64
filled bool
}
for i, tc := range []testCase{
{2, 2, 2, false},
{0, 2, 2, true},
{5, 5, 5, true},
{5, 0, 5, false},
{7, 7, 6.6, false},
} {
exampleFile := fmt.Sprintf("assets_test/circle_case_%02d.txt", i)
t.Run(exampleFile, func(t *testing.T) {
size := uint32(tc.r*2) + 2
bm := bitmap.NewBitmap(size, size)
bm.Circle(tc.x, tc.y, tc.r, tc.filled)
b, err := os.ReadFile(exampleFile)
if err != nil {
t.Fatal(err)
}
expect := strings.TrimSpace(string(b))
if expect != strings.TrimSpace(bm.String()) {
t.Errorf("expect:\n%s\ngot:\n%s\n", expect, bm)
}
})
}
}
+9
View File
@@ -0,0 +1,9 @@
package bitmap
import "slices"
func (p Bitmap) value() []uint32 {
return slices.Clone(p.bitVector)
}
var Value = (Bitmap).value