martin-riedl / top

Topological Sorting and Processing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TOP - Topological Sorting and Processing

This project provides an algorithm to sort (and process) a given list of objects that have loop-free interdependencies.

Usage Example

val unsorted = List(1,2,3,4)
val dependencies = HashMap[Int,Set[Int]](
  1->Set(3),
  3->Set(4,2),
  2->Set(4),
  4->Set()
)

Top.sortAndProcess(
  unsorted, (e:Int)=> e,
  (e:Int) => dependencies(e),
  (source: Int, targets : List[Int]) => {
    targets.foldLeft(source)(_ + _)
  }
)

should result

List(4,6,13,14)

About

Topological Sorting and Processing

License:Apache License 2.0


Languages

Language:Scala 100.0%