-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgwc.go
More file actions
38 lines (31 loc) · 741 Bytes
/
gwc.go
File metadata and controls
38 lines (31 loc) · 741 Bytes
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
package gwc
import (
"math/rand"
)
func New(rnd *rand.Rand, mode CollapseOrderFn, nodes Nodes) *GraphWaveCollapse {
return &GraphWaveCollapse{
rnd: rnd,
mode: mode,
nodes: nodes,
}
}
type GraphWaveCollapse struct {
rnd *rand.Rand
mode CollapseOrderFn
nodes Nodes
}
func (gwc *GraphWaveCollapse) Collapse() NodeEnvironment {
env := *NewNodeEnvironment(gwc.nodes)
for {
// Retrieve next NodeIndex according to mode.
next := gwc.mode(gwc.rnd, env)
if _, exists := env.NodesMap[next]; !exists {
break
}
// Collapse the chosen Node and mark it as such.
env.Current = next
env.StatesMap[next] = env.NodesMap[next].Collapse(gwc.rnd, env)
env.CollapsedMap[next] = len(env.CollapsedMap)
}
return env
}