<commit_before>package cook.actors

import scala.collection.mutable.HashSet
import scala.collection.mutable.Stack
import scala.collection.immutable.VectorBuilder

import cook.target._
import cook.util._

object Analysis {

  def analyze(targetLabel: TargetLabel): Seq[Target] = {

    val targets = new VectorBuilder[Target]
    val labelsProccessing = new Stack[String]
    val labelsProccessingSet = new HashSet[String]
    val labelsProccessed = new HashSet[String]

    analyzeDeps(targetLabel, targets, labelsProccessing, labelsProccessingSet, labelsProccessed)

    targets.result.reverse
  }

  def analyzeDeps(
      targetLabel: TargetLabel,
      targets: VectorBuilder[Target],
      labelsProccessing: Stack[String],
      labelsProccessingSet: HashSet[String],
      labelsProccessed: HashSet[String]) {

    val target = TargetManager.getTarget(targetLabel)
    targets += target
    labelsProccessing.push(target.targetName)
    labelsProccessingSet += target.targetName
    for (dep <- target.depTargets) {
      if (labelsProccessingSet.contains(dep.targetName)) {
        // TODO(timgreen): better error message
        labelsProccessing.push(dep.targetName)
        throw new CookBaseException(
            "Found dependence circle: %s", labelsProccessing.mkString(" -> "))
      }
      if (!labelsProccessed.contains(dep.targetName)) {
        analyzeDeps(dep, targets, labelsProccessing, labelsProccessingSet, labelsProccessed)
      }
    }

    labelsProccessingSet -= labelsProccessing.pop
    labelsProccessed += target.targetName
  }
}<commit_msg>Impl topological sort in Analysis<commit_after>package cook.actors

import scala.collection.mutable.HashMap
import scala.collection.mutable.HashSet
import scala.collection.mutable.Stack
import scala.collection.immutable.VectorBuilder

import cook.target._
import cook.util._

object Analysis {

  def analyze(targetLabel: TargetLabel): Seq[Target] = {

    val labelsProccessing = new Stack[String]
    val labelsProccessingSet = new HashSet[String]
    val labelsProccessed = new HashSet[String]

    val outDeg = new HashMap[String, Int]
    val inEdges = new HashMap[String, VectorBuilder[String]]

    analyzeDeps(
        targetLabel,
        labelsProccessing,
        labelsProccessingSet,
        labelsProccessed,
        outDeg,
        inEdges)

    val readyToBuild = new Stack[String]
    val buildOrder = new VectorBuilder[Target]
    for ((t, i) <- outDeg) {
      if (i == 0) {
        readyToBuild.push(t)
      }
    }

    while (readyToBuild.nonEmpty) {
      val t = readyToBuild.pop
      buildOrder += TargetManager.getTarget(new TargetLabel("", t))

      for (tt <- inEdges.getOrElse(t, new VectorBuilder[String]).result) {
        val deg = outDeg(tt) - 1
        if (deg == 0) {
          readyToBuild.push(tt)
        } else {
          outDeg(tt) = deg
        }
      }
    }

    buildOrder.result
  }

  def analyzeDeps(
      targetLabel: TargetLabel,
      labelsProccessing: Stack[String],
      labelsProccessingSet: HashSet[String],
      labelsProccessed: HashSet[String],
      outDeg: HashMap[String, Int],
      inEdges: HashMap[String, VectorBuilder[String]]) {

    val target = TargetManager.getTarget(targetLabel)
    labelsProccessing.push(target.targetName)
    labelsProccessingSet += target.targetName

    val depTargets = target.depTargets
    outDeg.put(target.targetName, depTargets.length)

    for (dep <- depTargets) {
      inEdges.getOrElseUpdate(dep.targetName, new VectorBuilder[String]) += target.targetName

      if (labelsProccessingSet.contains(dep.targetName)) {
        // TODO(timgreen): better error message
        labelsProccessing.push(dep.targetName)
        throw new CookBaseException(
            "Found dependence circle: %s", labelsProccessing.mkString(" -> "))
      }
      if (!labelsProccessed.contains(dep.targetName)) {
        analyzeDeps(dep, labelsProccessing, labelsProccessingSet, labelsProccessed, outDeg, inEdges)
      }
    }

    labelsProccessingSet -= labelsProccessing.pop
    labelsProccessed += target.targetName
  }
}
{'fraction_non_alphanumeric': 0.09484233683695918, 'fraction_numerical': 0.0007333170373991689, 'mean_word_length': 3.3164556962025316, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4601839', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1206, 'n_words': 245}