File: grid.go

package info (click to toggle)
golang-github-badgerodon-collections 0.0~git20130729.604e922-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 164 kB
  • sloc: makefile: 5
file content (52 lines) | stat: -rw-r--r-- 843 bytes parent folder | download | duplicates (2)
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{}) {

}