-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
146 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2018-2024 by Rob Norris and Contributors | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package skunk.data | ||
|
||
import scala.collection.immutable.SortedMap | ||
import scala.math.Ordering | ||
|
||
/** Immutable bi-directional map that is sorted by key. */ | ||
sealed abstract case class SortedBiMap[K: Ordering, V](entries: SortedMap[K, V], inverse: Map[V, K]) { | ||
assert(entries.size == inverse.size) | ||
|
||
def size: Int = entries.size | ||
|
||
def head: (K, V) = entries.head | ||
|
||
def get(k: K): Option[V] = entries.get(k) | ||
|
||
def put(k: K, v: V): SortedBiMap[K, V] = | ||
SortedBiMap( | ||
inverse.get(v).fold(entries)(entries - _) + (k -> v), | ||
entries.get(k).fold(inverse)(inverse - _) + (v -> k)) | ||
|
||
def +(kv: (K, V)): SortedBiMap[K, V] = put(kv._1, kv._2) | ||
|
||
def -(k: K): SortedBiMap[K, V] = | ||
get(k) match { | ||
case Some(v) => SortedBiMap(entries - k, inverse - v) | ||
case None => this | ||
} | ||
|
||
def inverseGet(v: V): Option[K] = inverse.get(v) | ||
|
||
override def toString: String = | ||
entries.iterator.map { case (k, v) => s"$k <-> $v" }.mkString("SortedBiMap(", ", ", ")") | ||
} | ||
|
||
object SortedBiMap { | ||
private def apply[K: Ordering, V](entries: SortedMap[K, V], inverse: Map[V, K]): SortedBiMap[K, V] = | ||
new SortedBiMap[K, V](entries, inverse) {} | ||
|
||
def empty[K: Ordering, V]: SortedBiMap[K, V] = apply(SortedMap.empty, Map.empty) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
modules/tests/shared/src/test/scala/data/SortedBiMapTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) 2018-2024 by Rob Norris and Contributors | ||
// This software is licensed under the MIT License (MIT). | ||
// For more information see LICENSE or https://opensource.org/licenses/MIT | ||
|
||
package skunk.data | ||
|
||
import munit.ScalaCheckSuite | ||
import org.scalacheck.Prop | ||
|
||
class SortedBiMapTest extends ScalaCheckSuite { | ||
|
||
test("put handles overwrites") { | ||
val m = SortedBiMap.empty[Int, Int].put(1, 2) | ||
assertEquals(m.size, 1) | ||
assertEquals(m.get(1), Some(2)) | ||
assertEquals(m.inverseGet(2), Some(1)) | ||
|
||
val m2 = m.put(3, 2) | ||
assertEquals(m2.size, 1) | ||
assertEquals(m2.get(3), Some(2)) | ||
assertEquals(m2.inverseGet(2), Some(3)) | ||
assertEquals(m2.get(1), None) | ||
|
||
val m3 = m2.put(3, 4) | ||
assertEquals(m3.size, 1) | ||
assertEquals(m3.get(3), Some(4)) | ||
assertEquals(m3.inverseGet(4), Some(3)) | ||
assertEquals(m3.inverseGet(2), None) | ||
} | ||
|
||
test("entries are sorted") { | ||
Prop.forAll { (s: Set[Int]) => | ||
val m = s.foldLeft(SortedBiMap.empty[Int, String])((acc, i) => acc.put(i, i.toString)) | ||
assertEquals(m.size, s.size) | ||
assertEquals(m.entries.keySet.toList, s.toList.sorted) | ||
} | ||
} | ||
} |