Skip to content

Commit

Permalink
Merge pull request #1 from dangrier/main
Browse files Browse the repository at this point in the history
Change import for constraints module
  • Loading branch information
Jeadie authored Aug 29, 2022
2 parents f11185d + af70b18 commit 8a9e43b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module github.com/Jeadie/Go-L

go 1.18

require golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
require github.com/Jeadie/DateDiff v0.0.0-20220326045009-6a7a8d44b728

require github.com/teamortix/golang-wasm/wasm v0.0.0-20220630082535-f3e518b544db // indirect
require github.com/teamortix/golang-wasm/wasm v0.0.0-20220630082535-f3e518b544db // indirect
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA=
github.com/Jeadie/DateDiff v0.0.0-20220326045009-6a7a8d44b728 h1:XdIwmtIDRwVhRLLanNfnM+nQP3tVdoyWYqBVwtNHEbA=
github.com/Jeadie/DateDiff v0.0.0-20220326045009-6a7a8d44b728/go.mod h1:iZgMbqnBTrwxyQKXr+17N1z5LPDoCWWc32V+s8OEYsc=
github.com/teamortix/golang-wasm/wasm v0.0.0-20220630082535-f3e518b544db h1:WjuAaAU0DojriAHY/TNV+Km18cImbaOu1K8j1PPrD1M=
github.com/teamortix/golang-wasm/wasm v0.0.0-20220630082535-f3e518b544db/go.mod h1:nskvTyoGIaAsC+664SkRitVI1ft6dm1xerCr50YZsnY=
github.com/teamortix/golang-wasm/wasm v0.0.0-20220630082535-f3e518b544db/go.mod h1:nskvTyoGIaAsC+664SkRitVI1ft6dm1xerCr50YZsnY=
34 changes: 21 additions & 13 deletions pkg/gol/lattice.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package gol

import (
"constraints"
"math"
"math/rand"

"golang.org/x/exp/constraints"
)

// Node can be used in Lattice method signatures
type Node constraints.Ordered
type IntPair = [2]int


type Lattice[T Node] struct {
grid []T
topologyFn TopologyTransformation
Expand All @@ -31,23 +31,25 @@ func (l *Lattice[T]) Size() uint {

// GetValue on lattice at coordinate (x, y)
func (l *Lattice[T]) GetValue(x int, y int) T {
nx, ny := l.topologyFn(x, y, 0,0, int(l.n))
if nx == -1 || ny == -1 { return l.null }
return l.grid[(nx * int(l.n)) + ny]
nx, ny := l.topologyFn(x, y, 0, 0, int(l.n))
if nx == -1 || ny == -1 {
return l.null
}
return l.grid[(nx*int(l.n))+ny]
}

// SetValue on lattice at coordinate (x, y) to v
func (l *Lattice[T]) SetValue(x int, y int, v T) {
nx, ny := l.topologyFn(x, y, 0,0, int(l.n))
func (l *Lattice[T]) SetValue(x int, y int, v T) {
nx, ny := l.topologyFn(x, y, 0, 0, int(l.n))
l.grid[(nx*int(l.n))+ny] = v
}

// GetValuesAround returns all values around a coordinate within an L1 distance of w.
func (l *Lattice[T]) GetValuesAround(x int, y int, w int) [][]T {
rows := make([][]T, 2*w+1)

for i := 0; i < 2*w + 1; i++ {
rows[i] = make([]T, 2*w + 1)
for i := 0; i < 2*w+1; i++ {
rows[i] = make([]T, 2*w+1)
for j := 0; j < 2*w+1; j++ {

rows[i][j] = l.GetValue(x+i-w, y+j-w)
Expand All @@ -58,7 +60,7 @@ func (l *Lattice[T]) GetValuesAround(x int, y int, w int) [][]T {

// Copy a Lattice struct, and all its references, to a new instance (i.e. deep copy).
func (l *Lattice[T]) Copy() *Lattice[T] {
readG := make([]T, len(l.grid))
readG := make([]T, len(l.grid))
copy(readG, l.grid)
return &Lattice[T]{
grid: readG,
Expand Down Expand Up @@ -107,7 +109,9 @@ func (l *Lattice[T]) SingleIteration() bool {
readL := l.Copy()
for i := range l.GetLatticeCoordinates() {
updated := l.UpdatePair(i, readL)
if updated { isUpdated = true }
if updated {
isUpdated = true
}
}
return isUpdated
}
Expand Down Expand Up @@ -164,13 +168,17 @@ func ConstructUintGrid(n uint, binaryProb float64) []uint {
u := make(chan uint)
go func(out chan uint, size uint) {
for i := 0; i < int(size); i++ {
if rand.Float64() < binaryProb { u <- 1 } else { u <- 0 }
if rand.Float64() < binaryProb {
u <- 1
} else {
u <- 0
}
}
}(u, n*n)

rows := make([]uint, n*n)
for i := 0; i < int(n*n); i++ {
rows[i] = <- u
rows[i] = <-u
}
return rows
}

0 comments on commit 8a9e43b

Please sign in to comment.