Skip to content

Commit

Permalink
Merge pull request #8 from harry0000/fix
Browse files Browse the repository at this point in the history
Fix code
  • Loading branch information
harry0000 authored Nov 12, 2023
2 parents 70ca4f2 + 4cdad82 commit 5b7cafd
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/io/github/acl4s/FenwickTree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.github.acl4s

import scala.reflect.ClassTag

import io.github.acl4s.internal.rightOpenInterval
import io.github.acl4s.internal.{rightOpenInterval, IPair}

/**
* Reference: https://en.wikipedia.org/wiki/Fenwick_tree
Expand Down Expand Up @@ -34,7 +34,7 @@ case class FenwickTree[T: ClassTag](n: Int)(using m: AddSub[T]) {
}

def sum(range: Range): T = {
val (l, r) = rightOpenInterval(range)
val IPair(l, r) = rightOpenInterval(range)
sum(l, r)
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/io/github/acl4s/LazySegtree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.github.acl4s

import scala.reflect.ClassTag

import io.github.acl4s.internal.{ceilPow2, rightOpenInterval}
import io.github.acl4s.internal.{ceilPow2, rightOpenInterval, IPair}

final case class LazySegtree[S, F](
n: Int
Expand Down Expand Up @@ -48,7 +48,7 @@ final case class LazySegtree[S, F](
}

def prod(range: Range): S = {
val (l, r) = rightOpenInterval(range)
val IPair(l, r) = rightOpenInterval(range)
prod(l, r)
}

Expand Down Expand Up @@ -89,7 +89,7 @@ final case class LazySegtree[S, F](
}

def applyRange(range: Range, f: F): Unit = {
val (l, r) = rightOpenInterval(range)
val IPair(l, r) = rightOpenInterval(range)
applyRange(l, r, f)
}

Expand Down
14 changes: 8 additions & 6 deletions src/main/scala/io/github/acl4s/ModInt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import scala.annotation.targetName

import io.github.acl4s.internal.{Barrett, LPair}

private[acl4s] inline def applyIntImpl(value: Int, mod: Int): Long = {
applyLongImpl(value.toLong, mod)
private[acl4s] inline def applyIntImpl(value: Int, mod: Int): Int = {
var x = value % mod
if (x < 0) { x += mod }
x
}

private[acl4s] inline def applyLongImpl(value: Long, mod: Int): Long = {
var x = value % mod.toLong
if (x < 0L) { x += mod.toLong }
var x = value % mod
if (x < 0L) { x += mod }
x
}

Expand Down Expand Up @@ -174,7 +176,7 @@ object StaticModInt {

def apply[T <: Int](value: Int)(using m: Modulus[T]): StaticModInt[T] = {
val x = applyIntImpl(value, m.value)
new StaticModInt(x.toInt)
new StaticModInt(x)
}

def apply[T <: Int](value: Long)(using m: Modulus[T]): StaticModInt[T] = {
Expand Down Expand Up @@ -285,7 +287,7 @@ object DynamicModInt {

def apply(value: Int): DynamicModInt = {
val x = applyIntImpl(value, bt.m)
new DynamicModInt(x.toInt)
new DynamicModInt(x)
}

def apply(value: Long): DynamicModInt = {
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/io/github/acl4s/Segtree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.github.acl4s

import scala.reflect.ClassTag

import io.github.acl4s.internal.{ceilPow2, rightOpenInterval}
import io.github.acl4s.internal.{ceilPow2, rightOpenInterval, IPair}

final case class Segtree[T](
n: Int
Expand Down Expand Up @@ -30,7 +30,7 @@ final case class Segtree[T](
}

def prod(range: Range): T = {
val (l, r) = rightOpenInterval(range)
val IPair(l, r) = rightOpenInterval(range)
prod(l, r)
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/scala/io/github/acl4s/internal/Range.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.github.acl4s.internal

private[acl4s] def rightOpenInterval(range: Range): (Int, Int) = {
private[acl4s] def rightOpenInterval(range: Range): IPair = {
assert(range.step == 1 || range.step == -1)
if (range.isEmpty) {
(range.start, range.start)
IPair(range.start, range.start)
} else if (range.step > 0) {
(range.start, range.last + 1)
IPair(range.start, range.last + 1)
} else {
(range.last, range.start + 1)
IPair(range.last, range.start + 1)
}
}
1 change: 1 addition & 0 deletions src/main/scala/io/github/acl4s/internal/Tuple.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package io.github.acl4s.internal

final case class IPair(a: Int, b: Int)
final case class LPair(a: Long, b: Long)
18 changes: 9 additions & 9 deletions src/test/scala/io/github/acl4s/internal/RangeSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package io.github.acl4s.internal
class RangeSuite extends munit.FunSuite {

test("rightOpenInterval()") {
assertEquals(rightOpenInterval(0 to 10), (0, 11))
assertEquals(rightOpenInterval((0 to 10).reverse), (0, 11))
assertEquals(rightOpenInterval(10 to 0 by -1), (0, 11))
assertEquals(rightOpenInterval(0 to 10), IPair(0, 11))
assertEquals(rightOpenInterval((0 to 10).reverse), IPair(0, 11))
assertEquals(rightOpenInterval(10 to 0 by -1), IPair(0, 11))

assertEquals(rightOpenInterval(0 until 10), (0, 10))
assertEquals(rightOpenInterval((0 until 10).reverse), (0, 10))
assertEquals(rightOpenInterval(10 until 0 by -1), (1, 11))
assertEquals(rightOpenInterval(0 until 10), IPair(0, 10))
assertEquals(rightOpenInterval((0 until 10).reverse), IPair(0, 10))
assertEquals(rightOpenInterval(10 until 0 by -1), IPair(1, 11))

assertEquals(rightOpenInterval(0 to 0), (0, 1))
assertEquals(rightOpenInterval((0 until 0).reverse), (0, 0))
assertEquals(rightOpenInterval(0 until 0 by -1), (0, 0))
assertEquals(rightOpenInterval(0 to 0), IPair(0, 1))
assertEquals(rightOpenInterval((0 until 0).reverse), IPair(0, 0))
assertEquals(rightOpenInterval(0 until 0 by -1), IPair(0, 0))
}

}

0 comments on commit 5b7cafd

Please sign in to comment.