Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
d2e9a6636f | |||
e8109737af | |||
e553da2c61 | |||
3e2be025b3 | |||
157e4f302d |
36
buffer.go
36
buffer.go
@ -7,6 +7,7 @@ import (
|
|||||||
// Buffer is a 2-dimensional buffer
|
// Buffer is a 2-dimensional buffer
|
||||||
type Buffer[T any] struct {
|
type Buffer[T any] struct {
|
||||||
data [][]T
|
data [][]T
|
||||||
|
x, y int
|
||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
parent *Buffer[T]
|
parent *Buffer[T]
|
||||||
@ -24,6 +25,7 @@ func NewBuffer[T any](width, height int, defaultValue T) *Buffer[T] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &Buffer[T]{
|
return &Buffer[T]{
|
||||||
|
x: 0, y: 0,
|
||||||
data: b,
|
data: b,
|
||||||
width: width,
|
width: width,
|
||||||
height: height,
|
height: height,
|
||||||
@ -32,17 +34,19 @@ func NewBuffer[T any](width, height int, defaultValue T) *Buffer[T] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Buffer[T]) x(x int) int {
|
func (b *Buffer[T]) limX(x int) int {
|
||||||
return limit(x, 0, b.width-1)
|
return limit(x, 0, b.width-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Buffer[T]) y(y int) int {
|
func (b *Buffer[T]) limY(y int) int {
|
||||||
return limit(y, 0, b.height-1)
|
return limit(y, 0, b.height-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets the value at position (x,y) to c
|
// Set sets the value at position (x,y) to c
|
||||||
func (b *Buffer[T]) Set(x, y int, v T) {
|
func (b *Buffer[T]) Set(x, y int, v T) {
|
||||||
b.data[b.y(y)][b.x(x)] = v
|
if b.width > 0 && b.height > 0 {
|
||||||
|
b.data[b.limY(y)][b.limX(x)] = v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the value at position (x,y)
|
// Get returns the value at position (x,y)
|
||||||
@ -65,7 +69,26 @@ func (b *Buffer[T]) Height() int {
|
|||||||
return b.height
|
return b.height
|
||||||
}
|
}
|
||||||
|
|
||||||
// ForEach calls f for every value in this buffer
|
// OffsetX returns the horizontal offset of b relative to its parent buffer.
|
||||||
|
// OffsetX returns 0 if b has no parent
|
||||||
|
func (b *Buffer[T]) OffsetX() int {
|
||||||
|
return b.x
|
||||||
|
}
|
||||||
|
|
||||||
|
// OffsetY returns the vertical offset of b relative to its parent buffer.
|
||||||
|
// OffsetY returns 0 if b has no parent
|
||||||
|
func (b *Buffer[T]) OffsetY() int {
|
||||||
|
return b.y
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForEachLine calls f for each line in b
|
||||||
|
func (b *Buffer[T]) ForEachLine(f func(line int, content []T)) {
|
||||||
|
for line, content := range b.data {
|
||||||
|
f(line, content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForEach calls f for every value in b
|
||||||
func (b *Buffer[T]) ForEach(f func(x, y int, v T)) {
|
func (b *Buffer[T]) ForEach(f func(x, y int, v T)) {
|
||||||
for y, col := range b.data {
|
for y, col := range b.data {
|
||||||
for x, v := range col {
|
for x, v := range col {
|
||||||
@ -95,8 +118,8 @@ func (b *Buffer[T]) Sub(x, y, w, h int) *Buffer[T] {
|
|||||||
// sanitize inputs
|
// sanitize inputs
|
||||||
x = limit(x, 0, b.width-1)
|
x = limit(x, 0, b.width-1)
|
||||||
y = limit(y, 0, b.height-1)
|
y = limit(y, 0, b.height-1)
|
||||||
w = limit(w, 1, b.width-x)
|
w = limit(w, 0, b.width-x)
|
||||||
h = limit(h, 1, b.height-y)
|
h = limit(h, 0, b.height-y)
|
||||||
|
|
||||||
// make slice references
|
// make slice references
|
||||||
data := make([][]T, h)
|
data := make([][]T, h)
|
||||||
@ -107,6 +130,7 @@ func (b *Buffer[T]) Sub(x, y, w, h int) *Buffer[T] {
|
|||||||
|
|
||||||
// make buffer
|
// make buffer
|
||||||
return &Buffer[T]{
|
return &Buffer[T]{
|
||||||
|
x: x, y: y,
|
||||||
data: data,
|
data: data,
|
||||||
width: w,
|
width: w,
|
||||||
height: h,
|
height: h,
|
||||||
|
@ -17,3 +17,14 @@ func TestSub(t *testing.T) {
|
|||||||
fmt.Println(strings.Repeat("-", 10))
|
fmt.Println(strings.Repeat("-", 10))
|
||||||
fmt.Println(s)
|
fmt.Println(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSet(t *testing.T) {
|
||||||
|
b := NewBuffer(0, 0, ' ')
|
||||||
|
b.Set(0, 0, 'a')
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOffset(t *testing.T) {
|
||||||
|
b := NewBuffer(10, 10, ' ')
|
||||||
|
b = b.Sub(3, 3, 3, 3)
|
||||||
|
fmt.Println(b.OffsetX(), b.OffsetY())
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user