2 Commits

Author SHA1 Message Date
9d07656277 even an array doesn't make it faster... 2024-07-17 20:09:59 +02:00
a680cca30c always calculate neighbor count 2024-07-16 19:33:06 +02:00
2 changed files with 90 additions and 20 deletions

View File

@@ -19,12 +19,15 @@ type Neighbor struct {
X, Y int X, Y int
} }
const max int = 2250000
type Grid struct { type Grid struct {
Data []uint8 Data [max]uint8
NeighborCount []int NeighborCount [max]int
Neighbors [][]Neighbor Neighbors [max][]Neighbor
Empty bool Empty bool
Config *Config Config *Config
Counter func(x, y int) uint8
} }
// Create new empty grid and allocate Data according to provided dimensions // Create new empty grid and allocate Data according to provided dimensions
@@ -34,28 +37,34 @@ func NewGrid(config *Config) *Grid {
STRIDE = config.Width STRIDE = config.Width
} }
size := STRIDE * STRIDE //size := STRIDE * STRIDE
grid := &Grid{ grid := &Grid{
Data: make([]uint8, size), //Data: make([]uint8, size),
NeighborCount: make([]int, size), //NeighborCount: make([]int, size),
Neighbors: make([][]Neighbor, size), //Neighbors: make([][]Neighbor, size),
Empty: config.Empty, Empty: config.Empty,
Config: config, Config: config,
} }
// first setup the cells // first setup the cells
for y := 0; y < config.Height; y++ { // for y := 0; y < config.Height; y++ {
for x := 0; x < config.Width; x++ { // for x := 0; x < config.Width; x++ {
grid.Data[y+STRIDE*x] = 0 // grid.Data[y+STRIDE*x] = 0
} // }
} // }
// in a second pass, collect positions to the neighbors of each cell // in a second pass, collect positions to the neighbors of each cell
for y := 0; y < config.Height; y++ { // for y := 0; y < config.Height; y++ {
for x := 0; x < config.Width; x++ { // for x := 0; x < config.Width; x++ {
grid.SetupNeighbors(x, y) // grid.SetupNeighbors(x, y)
} // }
// }
if grid.Config.Wrap {
grid.Counter = grid.CountNeighborsWrap
} else {
grid.Counter = grid.CountNeighbors
} }
return grid return grid
@@ -101,8 +110,69 @@ func (grid *Grid) SetupNeighbors(x, y int) {
grid.Neighbors[y+STRIDE*x] = neighbors grid.Neighbors[y+STRIDE*x] = neighbors
} }
// count the living neighbors of a cell func (grid *Grid) CountNeighborsWrap(x, y int) uint8 {
var sum uint8
var col, row int
width := grid.Config.Width
height := grid.Config.Height
for nbgX := -1; nbgX < 2; nbgX++ {
for nbgY := -1; nbgY < 2; nbgY++ {
// In wrap mode we look at all the 8 neighbors surrounding us.
// In case we are on an edge we'll look at the neighbor on the
// other side of the grid, thus wrapping lookahead around
// using the mod() function.
col = (x + nbgX + width) % width
row = (y + nbgY + height) % height
p := row + STRIDE*col
sum += grid.Data[p]
}
}
// don't count ourselfes though
sum -= grid.Data[y+STRIDE*x]
return sum
}
func (grid *Grid) CountNeighbors(x, y int) uint8 { func (grid *Grid) CountNeighbors(x, y int) uint8 {
var sum uint8
var val uint8
width := grid.Config.Width
height := grid.Config.Height
for nbgX := -1; nbgX < 2; nbgX++ {
for nbgY := -1; nbgY < 2; nbgY++ {
var col, row int
xnbgX := x + nbgX
ynbgY := y + nbgY
// In traditional grid mode the edges are deadly
if xnbgX < 0 || xnbgX >= width || ynbgY < 0 || ynbgY >= height {
continue
}
col = xnbgX
row = ynbgY
p := row + STRIDE*col
val = grid.Data[p]
sum += val // this uses 18% of the whole running time!
}
}
// don't count ourselfes though
sum -= grid.Data[y+STRIDE*x]
return sum
}
// count the living neighbors of a cell
func (grid *Grid) _CountNeighbors(x, y int) uint8 {
var count uint8 var count uint8
pos := y + STRIDE*x pos := y + STRIDE*x

View File

@@ -167,7 +167,7 @@ func (scene *ScenePlay) UpdateCells() {
for x := 0; x < width; x++ { for x := 0; x < width; x++ {
state := scene.Grids[scene.Index].Data[y+STRIDE*x] // 0|1 == dead or alive state := scene.Grids[scene.Index].Data[y+STRIDE*x] // 0|1 == dead or alive
neighbors := scene.Grids[scene.Index].CountNeighbors(x, y) neighbors := scene.Grids[scene.Index].Counter(x, y)
// actually apply the current rules // actually apply the current rules
nextstate := scene.RuleCheckFunc(state, neighbors) nextstate := scene.RuleCheckFunc(state, neighbors)