Skip to content

Commit

Permalink
💄 work on code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
cbarrick committed Nov 28, 2015
1 parent f22306c commit 19e7e21
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions pop/gen/generational.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,14 @@ func (pop *population) Evolve(suiters ...evo.Genome) evo.Genome {
// The main goroutine.
func (pop *population) run() {
var (
mate <-chan time.Time
// receives when the next iteration should start
mate <-chan time.Time

// holds the next generation as it is being built
nextgen = make([]evo.Genome, len(pop.members))
pos = 0

// the number of updates until the next generation is complete
wait int
)

for i := range pop.members {
Expand All @@ -121,8 +126,9 @@ func (pop *population) run() {
}

cross := func() {
pos = 0
wait = 0
for i := range pop.members {
wait++
go func(i int, members []evo.Genome) {
pop.updates <- members[i].Evolve(members...)
}(i, pop.members)
Expand Down Expand Up @@ -154,9 +160,11 @@ func (pop *population) run() {
runtime.SetFinalizer(val, func(val evo.Genome) {
val.Close()
})
nextgen[pos] = val
pos++
if pos == len(nextgen) {

wait--
nextgen[wait] = val

if wait == 0 {
pop.members, nextgen = nextgen, pop.members
for i := range nextgen {
nextgen[i] = nil
Expand All @@ -165,9 +173,9 @@ func (pop *population) run() {
}

case ch := <-pop.closec:
for pos < len(nextgen) {
for 0 < wait {
<-pop.updates
pos++
wait--
}
ch <- struct{}{}
return
Expand Down

0 comments on commit 19e7e21

Please sign in to comment.