Skip to content

Commit

Permalink
Add groupByIntoSeq from firrtl utils
Browse files Browse the repository at this point in the history
  • Loading branch information
adkian-sifive committed Jul 6, 2022
1 parent 8d41a85 commit 8aa6275
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/main/scala/chisel3/util/experimental/decode/TruthTable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package chisel3.util.experimental.decode

import chisel3.util.BitPat
import firrtl.Utils.groupByIntoSeq
import scala.collection.mutable

sealed class TruthTable private (val table: Seq[(BitPat, BitPat)], val default: BitPat, val sort: Boolean) {
def inputWidth = table.head._1.getWidth
Expand Down Expand Up @@ -190,4 +190,15 @@ object TruthTable {
bitPat(tables.flatMap { case (table, indexes) => table.default.rawString.zip(indexes) })
)
}

/** Similar to Seq.groupBy except that it preserves ordering of elements within each group */
private def groupByIntoSeq[A, K](xs: Iterable[A])(f: A => K): Seq[(K, Seq[A])] = {
val map = mutable.LinkedHashMap.empty[K, mutable.ListBuffer[A]]
for (x <- xs) {
val key = f(x)
val l = map.getOrElseUpdate(key, mutable.ListBuffer.empty[A])
l += x
}
map.view.map({ case (k, vs) => k -> vs.toList }).toList
}
}

0 comments on commit 8aa6275

Please sign in to comment.