Skip to content

Commit f13c372

Browse files
authored
Add BitSet union iterator (#308)
1 parent d288250 commit f13c372

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

core/js/src/main/scala/cats/parse/BitSet.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,14 @@ object BitSetUtil {
4646

4747
def isSingleton(t: Tpe): Boolean = t.size == 1
4848

49+
def union(bs: List[(Int, BitSet)]): Iterable[Char] =
50+
union(bs.iterator)
51+
4952
// what are all the Chars in these bitsets
50-
def union(bs: List[(Int, BitSet)]): Iterable[Char] = {
53+
def union(bs: Iterator[(Int, BitSet)]): Iterable[Char] = {
5154
def toIter(m: Int, bs: BitSet): Iterator[Char] =
5255
bs.iterator.map { i => (i + m).toChar } ++ Iterator.single(m.toChar)
5356

54-
bs.iterator.flatMap { case (m, bs) => toIter(m, bs) }.toSet
57+
bs.flatMap { case (m, bs) => toIter(m, bs) }.toSet
5558
}
5659
}

core/jvm/src/main/scala/cats/parse/BitSet.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,16 @@ object BitSetUtil {
4949
def isSingleton(t: Tpe): Boolean = t.cardinality() == 1
5050

5151
// what are all the Chars in these bitsets
52-
def union(bs: List[(Int, BitSet)]): Iterable[Char] = {
52+
def union(bs: List[(Int, BitSet)]): Iterable[Char] =
53+
union(bs.iterator)
54+
55+
def union(bs: Iterator[(Int, BitSet)]): Iterable[Char] = {
5356
def toIter(m: Int, bs: BitSet): Iterator[Char] =
5457
Iterator
5558
.iterate(0) { m => bs.nextSetBit(m + 1) }
5659
.takeWhile(_ >= 0)
5760
.map { i => (m + i).toChar }
5861

59-
bs.iterator.flatMap { case (m, bs) => toIter(m, bs) }.toSet
62+
bs.flatMap { case (m, bs) => toIter(m, bs) }.toSet
6063
}
6164
}

core/shared/src/main/scala/cats/parse/Parser.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2675,7 +2675,7 @@ object Parser {
26752675
case one :: Nil => Chain.one(one.asInstanceOf[P0])
26762676
case many =>
26772677
// we need to union
2678-
val minBs: List[(Int, BitSetUtil.Tpe)] = many.map { case CharIn(m, bs, _) => (m, bs) }
2678+
val minBs = many.iterator.map { case CharIn(m, bs, _) => (m, bs) }
26792679
Chain.one(Parser.charIn(BitSetUtil.union(minBs)).asInstanceOf[P0])
26802680
}
26812681

core/shared/src/test/scala/cats/parse/BitSetTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import org.scalacheck.Prop.forAll
2626
class BitSetTest extends munit.ScalaCheckSuite {
2727
property("BitSetUtil union works") {
2828
forAll { (cs: List[List[Char]]) =>
29-
val arys = cs.filter(_.nonEmpty).map(_.toArray.sorted)
29+
val arys = cs.iterator.filter(_.nonEmpty).map(_.toArray.sorted)
3030
val bs = arys.map { ary => (ary(0).toInt, BitSetUtil.bitSetFor(ary)) }
3131
val sortedFlat = BitSetUtil.union(bs)
3232
assertEquals(sortedFlat.toSet, cs.flatten.toSet)

0 commit comments

Comments
 (0)