-
Notifications
You must be signed in to change notification settings - Fork 0
/
tdarray.go
48 lines (41 loc) · 827 Bytes
/
tdarray.go
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
package ds
type TDArray struct {
data []float64
height, width int
}
func makeTDArray(h, w int) (t TDArray) {
t.data = make([]float64, h*w)
t.height = h
t.width = w
return
}
func (t TDArray) GetSize() (int, int) {
return t.height, t.width
}
func (t TDArray) checkBounds(y, x int) bool {
//Returns true if in bounds
if (x >= 0 && x < t.width) && (y >= 0 && y < t.height) {
return true
} else {
return false
}
}
func (t TDArray) getElemVal(y, x int) float64 {
if t.checkBounds(y, x) {
return t.data[y*t.width+x]
} else {
panic("out of bounds")
}
}
func (t *TDArray) setElem(y, x int, val float64) {
if t.checkBounds(y, x) {
t.data[y*t.width+x] = val
}
}
func (t *TDArray) getElemRef(y, x int) *float64 {
if t.checkBounds(y, x) {
return &t.data[y*t.width+x]
} else {
return nil
}
}