xref: /XiangShan/src/main/scala/utils/SeqUtils.scala (revision 039cdc35f5f3b68b6295ec5ace90f22a77322e02)
1package utils
2
3import chisel3._
4import chisel3.util._
5
6import scala.collection.mutable
7
8object SeqUtils {
9  /**
10    * @todo remove it when when xiangshan is updated to 2.13.11
11    */
12  def distinctBy[A, B](seqLike: Seq[B])(f: B => A): Seq[B] = {
13    val seen = new mutable.HashSet[A]()
14    var res = Seq[B]()
15    val it = seqLike.iterator
16    while (it.hasNext) {
17      val next = it.next
18      if (seen.add(f(next))) {
19        res :+= next
20      }
21    }
22    res
23  }
24
25  type Seq2[+T] = Seq[Seq[T]]
26  type Seq3[+T] = Seq2[Seq[T]]
27  type MixedVec2[T <: Data] = MixedVec[MixedVec[T]]
28  type MixedVec3[T <: Data] = MixedVec2[MixedVec[T]]
29
30  def mapToMixedVec[T, A <: Data](in: Seq[T], f: T => A): MixedVec[A] = {
31    MixedVec(in.map(f))
32  }
33
34  def mapToMixedVec2[T, A <: Data](in: Seq2[T], f: T => A): MixedVec2[A] = {
35    MixedVec(in.map(x => mapToMixedVec(x, f)))
36  }
37
38  def mapToMixedVec3[T, A <: Data](in: Seq3[T], f: T => A): MixedVec3[A] = {
39    MixedVec(in.map(x => mapToMixedVec2(x, f)))
40  }
41}
42