-
Notifications
You must be signed in to change notification settings - Fork 11
/
Dag.scala
160 lines (138 loc) · 4.87 KB
/
Dag.scala
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package org.bykn.bosatsu.graph
import cats.data.NonEmptyList
import scala.collection.immutable.SortedSet
import scala.collection.mutable.{Map => MMap}
import org.bykn.bosatsu.ListOrdering
sealed trait Dag[A] {
def nodes: SortedSet[A]
def deps(a: A): SortedSet[A]
private val layerCache: MMap[A, Int] = MMap.empty[A, Int]
// 0: no deps, 1: only depend on layer 0 and less, 2: layers 1 and less ...
def layerOf(a: A): Int =
// we know we can't infinitely loop on a dag, so this cache is safe
layerCache.getOrElseUpdate(
a, {
val d = deps(a)
if (d.isEmpty) 0
else (d.iterator.map(layerOf(_)).max + 1)
}
)
// the exclusive upper bound of layerOf
lazy val layers: Int =
if (nodes.isEmpty) 0
else nodes.iterator.map(layerOf(_)).max + 1
def toToposorted: Toposort.Success[A] = {
val layerMap: Map[Int, SortedSet[A]] = nodes.groupBy(layerOf(_))
val ls = (0 until layers).iterator
.map { idx =>
// by construction all layers have at least 1 item
NonEmptyList.fromListUnsafe(layerMap(idx).toList)
}
.to(Vector)
Toposort.Success(ls, deps(_).toList)
}
override def equals(that: Any) =
that match {
case thatDag: Dag[_] =>
def eqDag[B](bs: Dag[B]): Boolean =
(nodes == bs.nodes) && {
nodes.iterator.zip(bs.nodes.iterator).forall { case (a, b) =>
deps(a) == bs.deps(b)
}
}
eqDag(thatDag)
case _ => false
}
override lazy val hashCode: Int =
nodes.hashCode
}
object Dag {
implicit class Dagified[A: Ordering](dag: Dag[SortedSet[A]]) {
def unsingleton: Option[Dag[A]] =
if (dag.nodes.forall(_.size == 1)) Some {
new Dag[A] {
val nodes = dag.nodes.map(_.head)
val depsCache = MMap.empty[A, SortedSet[A]]
def deps(a: A): SortedSet[A] =
depsCache.getOrElseUpdate(a, dag.deps(SortedSet(a)).map(_.head))
}
}
else None
}
def dagify[A: Ordering](nodes: Iterable[A])(
nfn: A => IterableOnce[A]
): (A => Option[SortedSet[A]], Dag[SortedSet[A]]) = {
def allReachable(nfn: A => IterableOnce[A]): Map[A, SortedSet[A]] = {
def step(m: Map[A, SortedSet[A]]): Map[A, SortedSet[A]] =
m.iterator
.map { case (a, current) =>
val next = nfn(a).iterator.foldLeft(SortedSet.empty[A]) { (s, a) =>
s | (m.get(a) match {
case Some(value) => value
case None => SortedSet.empty
})
}
a -> (current | next)
}
.to(Map)
@annotation.tailrec
def loop(m: Map[A, SortedSet[A]]): Map[A, SortedSet[A]] = {
val m1 = step(m)
if (m1 == m) m
else loop(m1)
}
loop(nodes.iterator.map(a => (a, SortedSet.empty[A] + a)).to(Map))
}
// two nodes are joined into one cluster if a can reach b and vice-versa
// all edges are either within or between clusters
val originalAR = allReachable(nfn)
val internalNfn: A => IterableOnce[A] = { a =>
nfn(a).iterator.filter { b =>
originalAR(a).contains(b) && originalAR(b).contains(a)
}
}
val externalNfn: A => Iterator[A] = { a =>
nfn(a).iterator.filterNot { b =>
originalAR(a).contains(b) && originalAR(b).contains(a)
}
}
val clusterMap: Map[A, SortedSet[A]] = allReachable(internalNfn)
implicit val ordSortedSet: Ordering[SortedSet[A]] =
ListOrdering.byIterator[SortedSet[A], A]
val dag: Dag[SortedSet[A]] =
new Dag[SortedSet[A]] {
val nodes = clusterMap.iterator.map(_._2).to(SortedSet)
val depCache = MMap.empty[SortedSet[A], SortedSet[SortedSet[A]]]
def deps(cluster: SortedSet[A]): SortedSet[SortedSet[A]] =
depCache.getOrElseUpdate(
cluster,
// if any node in the set is linked, the set is:
cluster.iterator
.flatMap { a =>
externalNfn(a).map(clusterMap)
}
.to(SortedSet)
)
}
(clusterMap.get(_), dag)
}
def fromToposorted[A: Ordering](s: Toposort.Success[A]): Dag[A] =
new Dag[A] {
val nodes = s.layers.iterator.flatMap(nel => nel.toList).to(SortedSet)
val depCache = MMap.empty[A, SortedSet[A]]
def deps(a: A): SortedSet[A] =
depCache.getOrElseUpdate(a, s.nfn(a).to(SortedSet))
}
def transitiveSet[A: Ordering](nodes: List[A])(nfn: A => Iterable[A]): SortedSet[A] = {
def loop(stack: List[A], inStack: SortedSet[A], reached: SortedSet[A]): SortedSet[A] =
stack match {
case head :: tail =>
val next = nfn(head).iterator.filterNot { n => inStack(n) || reached(n) }
.toList
.sorted
loop(next ::: tail, inStack ++ next, reached + head)
case Nil => reached
}
loop(nodes, SortedSet.empty, SortedSet.empty)
}
}