Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Change depthFirstOf to use List instead of Vector #323

Merged
merged 1 commit into from
Oct 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object Producer {
case _ => dependenciesOf(in)
}
}
val above = graph.depthFirstOf(p)(parentFn).toList
val above = graph.depthFirstOf(p)(parentFn)
p :: above
}

Expand Down Expand Up @@ -74,7 +74,7 @@ object Producer {
*/
def transitiveDependenciesOf[P <: Platform[P]](p: Producer[P, Any]): List[Producer[P, Any]] = {
val nfn = dependenciesOf[P](_)
graph.depthFirstOf(p)(nfn).toList
graph.depthFirstOf(p)(nfn)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ abstract class DependantGraph[T] {
* Return all dependendants of a given node.
* Does not include itself
*/
def transitiveDependantsOf(p: T): List[T] = depthFirstOf(p)(graph).toList
def transitiveDependantsOf(p: T): List[T] = depthFirstOf(p)(graph)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ package object graph {

/** Return the depth first enumeration of reachable nodes,
* NOT INCLUDING INPUT, unless it can be reached via neighbors */
def depthFirstOf[T](t: T)(nf: NeighborFn[T]): IndexedSeq[T] = {
def depthFirstOf[T](t: T)(nf: NeighborFn[T]): List[T] = {
@annotation.tailrec
def loop(stack: List[T], deps: Vector[T], acc: Set[T]): (Vector[T], Set[T]) = {
def loop(stack: List[T], deps: List[T], acc: Set[T]): List[T] = {
stack match {
case Nil => (deps, acc)
case Nil => deps
case h::tail =>
val newStack = nf(h).filterNot(acc).foldLeft(tail) { (s, it) => it :: s }
val newDeps = if(acc(h)) deps else (deps :+ h)
val newDeps = if (acc(h)) deps else h :: deps
loop(newStack, newDeps, acc + h)
}
}
val start = nf(t)
loop(start.toList, Vector(start.toSeq.distinct : _*), start.toSet)._1
val start = nf(t).toList
loop(start, start.distinct, start.toSet).reverse
}

/** Return a NeighborFn for the graph of reversed edges defined by
Expand Down