diff options
Diffstat (limited to 'src/main/scala/firrtl/Utils.scala')
| -rw-r--r-- | src/main/scala/firrtl/Utils.scala | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/main/scala/firrtl/Utils.scala b/src/main/scala/firrtl/Utils.scala index d187ea5f..72884e25 100644 --- a/src/main/scala/firrtl/Utils.scala +++ b/src/main/scala/firrtl/Utils.scala @@ -5,6 +5,7 @@ package firrtl import firrtl.ir._ import firrtl.PrimOps._ import firrtl.Mappers._ +import firrtl.traversals.Foreachers._ import firrtl.WrappedExpression._ import scala.collection.mutable @@ -210,6 +211,24 @@ object Utils extends LazyLogging { case _ => false } + /** Selects all the elements of this list ignoring the duplicates as determined by == after + * applying the transforming function f + * + * @note In Scala Standard Library starting in 2.13 + */ + def distinctBy[A, B](xs: List[A])(f: A => B): List[A] = { + val buf = new mutable.ListBuffer[A] + val seen = new mutable.HashSet[B] + for (x <- xs) { + val y = f(x) + if (!seen(y)) { + buf += x + seen += y + } + } + buf.toList + } + /** Provide a nice name to create a temporary * */ def niceName(e: Expression): String = niceName(1)(e) def niceName(depth: Int)(e: Expression): String = { @@ -649,6 +668,19 @@ object Utils extends LazyLogging { case _ => NoInfo } + /** Finds all root References in a nested Expression */ + def getAllRefs(expr: Expression): Seq[Reference] = { + val refs = mutable.ListBuffer.empty[Reference] + def rec(e: Expression): Unit = { + e match { + case ref: Reference => refs += ref + case other => other.foreach(rec) + } + } + rec(expr) + refs.toList + } + /** Splits an Expression into root Ref and tail * * @example |
