diff --git a/build.sbt b/build.sbt index 0c5b0f1..3b2ee7f 100644 --- a/build.sbt +++ b/build.sbt @@ -13,7 +13,8 @@ lazy val root = project scalacOptions ++= List( "-deprecation", "-feature", - "-unchecked" + "-unchecked", + "-Wunused:all" ), libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test ) diff --git a/src/main/scala/io/github/acl4s/LazySegtree.scala b/src/main/scala/io/github/acl4s/LazySegtree.scala index bf2956c..559e834 100644 --- a/src/main/scala/io/github/acl4s/LazySegtree.scala +++ b/src/main/scala/io/github/acl4s/LazySegtree.scala @@ -4,13 +4,13 @@ import scala.reflect.ClassTag import io.github.acl4s.internal.{ceilPow2, rightOpenInterval} -final case class LazySegtree[S: Monoid, F]( - n: Int, - size: Int, - log: Int, - d: Array[S], - lz: Array[F] -)(using m: MapMonoid[S, F]) { +final case class LazySegtree[S, F]( + n: Int +)(using m: Monoid[S], mm: MapMonoid[S, F], tagS: ClassTag[S], tagF: ClassTag[F]) { + val log: Int = ceilPow2(n) + val size: Int = 1 << log + val d: Array[S] = Array.fill(2 * size)(m.e()) + val lz: Array[F] = Array.fill(size)(mm.id()) private val _1_to_log = 1 to log private val _1_to_log_rev = _1_to_log.reverse @@ -20,16 +20,16 @@ final case class LazySegtree[S: Monoid, F]( } private def applyAll(k: Int, f: F): Unit = { - d(k) = m.mapping(f, d(k)) + d(k) = mm.mapping(f, d(k)) if (k < size) { - lz(k) = m.composition(f, lz(k)) + lz(k) = mm.composition(f, lz(k)) } } private def push(k: Int): Unit = { applyAll(2 * k, lz(k)) applyAll(2 * k + 1, lz(k)) - lz(k) = m.id() + lz(k) = mm.id() } def set(index: Int, x: S): Unit = { @@ -84,7 +84,7 @@ final case class LazySegtree[S: Monoid, F]( assert(0 <= index && index < n) val p = index + size _1_to_log_rev.foreach(i => { push(p >> i) }) - d(p) = m.mapping(f, d(p)) + d(p) = mm.mapping(f, d(p)) _1_to_log.foreach(i => { update(p >> i) }) } @@ -192,15 +192,11 @@ final case class LazySegtree[S: Monoid, F]( object LazySegtree { - def apply[S: Monoid, F](n: Int)(using m: MapMonoid[S, F], tagS: ClassTag[S], tagF: ClassTag[F]): LazySegtree[S, F] = { - val log = ceilPow2(n) - val size = 1 << log - val d = Array.fill(2 * size)(m.e()) - val lz = Array.fill(size)(m.id()) - new LazySegtree(n, size, log, d, lz) + def apply[S, F](n: Int)(using Monoid[S], MapMonoid[S, F], ClassTag[S], ClassTag[F]): LazySegtree[S, F] = { + new LazySegtree(n) } - def apply[S: Monoid, F](array: Array[S])(using MapMonoid[S, F], ClassTag[S], ClassTag[F]): LazySegtree[S, F] = { + def apply[S, F](array: Array[S])(using Monoid[S], MapMonoid[S, F], ClassTag[S], ClassTag[F]): LazySegtree[S, F] = { val ret = LazySegtree(array.length) (0 until ret.n).foreach(i => { ret.d(ret.size + i) = array(i) }) (1 until ret.size).reverse.foreach(ret.update) diff --git a/src/main/scala/io/github/acl4s/MapMonoid.scala b/src/main/scala/io/github/acl4s/MapMonoid.scala index a897cc0..9e60b0f 100644 --- a/src/main/scala/io/github/acl4s/MapMonoid.scala +++ b/src/main/scala/io/github/acl4s/MapMonoid.scala @@ -1,9 +1,6 @@ package io.github.acl4s trait MapMonoid[S: Monoid, F] { - def e(): S = Monoid[S].e() - def combine(a: S, b: S): S = Monoid[S].combine(a, b) - def id(): F def mapping(f: F, s: S): S def composition(a: F, b: F): F diff --git a/src/test/scala/io/github/acl4s/LazySegtreeSuite.scala b/src/test/scala/io/github/acl4s/LazySegtreeSuite.scala index 2bd10b3..82c9ded 100644 --- a/src/test/scala/io/github/acl4s/LazySegtreeSuite.scala +++ b/src/test/scala/io/github/acl4s/LazySegtreeSuite.scala @@ -1,7 +1,5 @@ package io.github.acl4s -import io.github.acl4s.{LazySegtree, MapMonoid, Monoid} - class LazySegtreeSuite extends munit.FunSuite { /** @@ -23,13 +21,13 @@ class LazySegtreeSuite extends munit.FunSuite { } given m: MapMonoid[S, Pair[Int]] with { - def id(): Pair[Int] = Pair(1, 0) - def mapping(f: Pair[Int], s: S): S = { + final override def id(): Pair[Int] = Pair(1, 0) + final override def mapping(f: Pair[Int], s: S): S = { val Pair(b, c) = f s.a = ((s.a.toLong * b.toLong + s.size.toLong * c.toLong) % MOD).toInt s } - def composition(l: Pair[Int], r: Pair[Int]): Pair[Int] = { + final override def composition(l: Pair[Int], r: Pair[Int]): Pair[Int] = { Pair( ((l.b.toLong * r.b.toLong) % MOD).toInt, ((l.b.toLong * r.c.toLong + l.c.toLong) % MOD).toInt diff --git a/src/test/scala/io/github/acl4s/SegtreeSuite.scala b/src/test/scala/io/github/acl4s/SegtreeSuite.scala index 533f69f..ee70bbc 100644 --- a/src/test/scala/io/github/acl4s/SegtreeSuite.scala +++ b/src/test/scala/io/github/acl4s/SegtreeSuite.scala @@ -1,7 +1,5 @@ package io.github.acl4s -import io.github.acl4s.{Monoid, Segtree} - class SegtreeSuite extends munit.FunSuite { /**