Skip to content

Commit

Permalink
Use IPair instead of Tuple2
Browse files Browse the repository at this point in the history
  • Loading branch information
harry0000 committed Nov 12, 2023
1 parent 84fca28 commit 4cdad82
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 20 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
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 4cdad82

Please sign in to comment.