1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
package grid
import (
. "github.com/badgerodon/collections"
)
type (
Grid struct {
values []interface{}
cols, rows int
}
)
func New(cols, rows int) *Grid {
return &Grid{
values: make([]interface{}, cols*rows),
cols: cols,
rows: rows,
}
}
func (this *Grid) Do(f func(p Point, value interface{})) {
for x := 0; x < this.cols; x++ {
for y := 0; y < this.rows; y++ {
f(Point{x, y}, this.values[x*this.cols+y])
}
}
}
func (this *Grid) Get(p Point) interface{} {
if p.X < 0 || p.Y < 0 || p.X >= this.cols || p.Y >= this.rows {
return nil
}
v := this.values[p.X*this.cols+p.Y]
return v
}
func (this *Grid) Rows() int {
return this.rows
}
func (this *Grid) Cols() int {
return this.cols
}
func (this *Grid) Len() int {
return this.rows * this.cols
}
func (this *Grid) Set(p Point, v interface{}) {
}
|