3 Commits

2 changed files with 28 additions and 37 deletions

View File

@@ -19,12 +19,10 @@ type Neighbor struct {
X, Y int X, Y int
} }
const max int = 2250000
type Grid struct { type Grid struct {
Data [max]uint8 Data []uint8
NeighborCount [max]int NeighborCount []int
Neighbors [max][]Neighbor Neighbors [][]Neighbor
Empty bool Empty bool
Config *Config Config *Config
Counter func(x, y int) uint8 Counter func(x, y int) uint8
@@ -37,29 +35,29 @@ 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 { if grid.Config.Wrap {
grid.Counter = grid.CountNeighborsWrap grid.Counter = grid.CountNeighborsWrap
@@ -112,23 +110,19 @@ func (grid *Grid) SetupNeighbors(x, y int) {
func (grid *Grid) CountNeighborsWrap(x, y int) uint8 { func (grid *Grid) CountNeighborsWrap(x, y int) uint8 {
var sum uint8 var sum uint8
var col, row int
width := grid.Config.Width
height := grid.Config.Height
for nbgX := -1; nbgX < 2; nbgX++ { for nbgX := -1; nbgX < 2; nbgX++ {
for nbgY := -1; nbgY < 2; nbgY++ { for nbgY := -1; nbgY < 2; nbgY++ {
var col, row int
// In wrap mode we look at all the 8 neighbors surrounding us. // 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 // In case we are on an edge we'll look at the neighbor on the
// other side of the grid, thus wrapping lookahead around // other side of the grid, thus wrapping lookahead around
// using the mod() function. // using the mod() function.
col = (x + nbgX + width) % width col = (x + nbgX + grid.Config.Width) % grid.Config.Width
row = (y + nbgY + height) % height row = (y + nbgY + grid.Config.Height) % grid.Config.Height
p := row + STRIDE*col sum += grid.Data[row+STRIDE*col]
sum += grid.Data[p]
} }
} }
@@ -140,7 +134,6 @@ func (grid *Grid) CountNeighborsWrap(x, y int) uint8 {
func (grid *Grid) CountNeighbors(x, y int) uint8 { func (grid *Grid) CountNeighbors(x, y int) uint8 {
var sum uint8 var sum uint8
var val uint8
width := grid.Config.Width width := grid.Config.Width
height := grid.Config.Height height := grid.Config.Height
@@ -159,9 +152,7 @@ func (grid *Grid) CountNeighbors(x, y int) uint8 {
col = xnbgX col = xnbgX
row = ynbgY row = ynbgY
p := row + STRIDE*col sum += grid.Data[row+STRIDE*col]
val = grid.Data[p]
sum += val // this uses 18% of the whole running time!
} }
} }

View File

@@ -42,10 +42,10 @@ var THEMES = map[string]ThemeDef{
dead: "5a5a5a", dead: "5a5a5a",
old: "ff1e1e", old: "ff1e1e",
grid: "808080", grid: "808080",
age1: "735f52", age3: "6c6059",
age2: "6c6059", age2: "735f52",
age3: "635d59", age1: "7b5e4b",
age4: "7b5e4b", age4: "635d59",
}, },
"dark": { "dark": {
life: "c8c8c8", life: "c8c8c8",