///|
test {
let _map1 : @sorted_map.SortedMap[Int32, String] = SortedMap([])
let _map2 = @sorted_map.from_array([(1, "one"), (2, "two"), (3, "three")])
}///|
test {
let map = @sorted_map.from_array([(1, "one"), (2, "two")])
map.set(3, "three")
@test.assert_eq(map.length(), 3)
}///|
test {
let map = @sorted_map.SortedMap([])
map[1] = "one"
map[2] = "two"
@test.assert_eq(map.length(), 2)
}///|
test {
let map = @sorted_map.from_array([(1, "one"), (2, "two"), (3, "three")])
map.remove(2)
@test.assert_eq(map.length(), 2)
@test.assert_eq(map.contains(2), false)
}///|
test {
let map = @sorted_map.from_array([(1, "one"), (2, "two"), (3, "three")])
assert_true(map.get(2) == Some("two"))
assert_true(map.get(4) == None)
}///|
test {
let map = @sorted_map.from_array([(1, "one"), (2, "two")])
let key = 3
debug_inspect(map.get(key), content="None")
}///|
test {
let map = @sorted_map.from_array([(1, "one"), (2, "two"), (3, "three")])
@test.assert_eq(map.contains(2), true)
@test.assert_eq(map.contains(4), false)
}///|
test {
let map = @sorted_map.from_array([(3, "three"), (1, "one"), (2, "two")])
let keys = []
let values = []
map.each((k, v) => {
keys.push(k)
values.push(v)
})
@debug.assert_eq(keys, [1, 2, 3])
@debug.assert_eq(values, ["one", "two", "three"])
}///|
test {
let map = @sorted_map.from_array([(3, "three"), (1, "one"), (2, "two")])
let result = []
map.eachi((i, k, v) => result.push((i, k, v)))
@debug.assert_eq(result, [(0, 1, "one"), (1, 2, "two"), (2, 3, "three")])
}///|
test {
let map = @sorted_map.from_array([(1, "one"), (2, "two"), (3, "three")])
@test.assert_eq(map.length(), 3)
}///|
test {
let map : @sorted_map.SortedMap[Int32, String] = SortedMap([])
@test.assert_eq(map.is_empty(), true)
}///|
test {
let map = @sorted_map.from_array([(1, "one"), (2, "two"), (3, "three")])
map.clear()
@test.assert_eq(map.is_empty(), true)
}///|
test {
let map = @sorted_map.from_array([(10, "a"), (20, "b"), (30, "c")])
@debug.assert_eq(map.first(), Some((10, "a")))
@debug.assert_eq(map.last(), Some((30, "c")))
@debug.assert_eq(map.first_ge(15), Some((20, "b")))
@debug.assert_eq(map.first_gt(20), Some((30, "c")))
@debug.assert_eq(map.last_le(25), Some((20, "b")))
@debug.assert_eq(map.last_lt(10), None)
@debug.assert_eq(map.pop_first(), Some((10, "a")))
@debug.assert_eq(map.pop_last(), Some((30, "c")))
@debug.assert_eq(map.to_array(), [(20, "b")])
}///|
test {
let map = @sorted_map.from_array([(1, "a"), (2, "b"), (3, "c")])
@debug.assert_eq(map.rev_keys().to_array(), [3, 2, 1])
@debug.assert_eq(map.rev_iter().take(1).to_array(), [(3, "c")])
}///|
priv struct IndexKey {
cols : Array[Int32]
descending : Array[Bool] // shared by every key in the map
}
///|
impl Eq for IndexKey with fn equal(a, b) {
a.cols == b.cols
}
///|
impl Compare for IndexKey with fn compare(a, b) {
for i in 0..<a.cols.length() {
let c = a.cols[i].compare(b.cols[i])
if c != 0 {
return if a.descending[i] { -c } else { c }
}
}
0
}
///|
test {
// Descending order with @cmp.Reverse
let desc = @sorted_map.from_array([(@cmp.Reverse(1), "a"), (Reverse(3), "c")])
@debug.assert_eq(desc.keys().map(k => k.0).to_array(), [3, 1])
// Order chosen at runtime: first column ascending, second descending
let descending = [false, true]
let index : @sorted_map.SortedMap[IndexKey, String] = @sorted_map.from_array([])
index.set({ cols: [1, 5], descending, }, "x")
index.set({ cols: [1, 9], descending, }, "y")
index.set({ cols: [0, 7], descending, }, "z")
@debug.assert_eq(index.values().to_array(), ["z", "y", "x"])
}///|
test {
let map = @sorted_map.from_array([(3, "three"), (1, "one"), (2, "two")])
@debug.assert_eq(map.keys().collect(), [1, 2, 3])
@debug.assert_eq(map.values().collect(), ["one", "two", "three"])
}///|
test {
let map = @sorted_map.from_array([(3, "three"), (1, "one"), (2, "two")])
@debug.assert_eq(map.to_array(), [(1, "one"), (2, "two"), (3, "three")])
}///|
test {
let map = @sorted_map.from_array([
(1, "one"),
(2, "two"),
(3, "three"),
(4, "four"),
(5, "five"),
])
let range_items = []
map.range(2, 4).each((k, v) => range_items.push((k, v)))
@debug.assert_eq(range_items, [(2, "two"), (3, "three"), (4, "four")])
}///|
/// Example with out-of-bounds range
test {
let map = @sorted_map.from_array([(1, "one"), (2, "two"), (3, "three")])
let range_items = []
map.range(0, 10).each((k, v) => range_items.push((k, v)))
@debug.assert_eq(range_items, [(1, "one"), (2, "two"), (3, "three")])
// Example with invalid range
let empty_range : Array[(Int32, String)] = []
map.range(10, 5).each((k, v) => empty_range.push((k, v)))
@debug.assert_eq(empty_range, [])
}///|
test {
let map = @sorted_map.from_array([(1, "a"), (2, "b"), (3, "c"), (4, "d")])
// 1 < key <= 3
let mid = map.range_bounds(Excluded(1), Included(3)).iter().map(p => p.0)
@debug.assert_eq(mid.to_array(), [2, 3])
// key < 4, largest first
let below = map.rev_range_bounds(Unbounded, Excluded(4)).iter().map(p => p.0)
@debug.assert_eq(below.to_array(), [3, 2, 1])
// choose the bound kind at runtime, e.g. for `x > 2` versus `x >= 2`
let strict = true
let lower : @cmp.Bound[Int32] = if strict { Excluded(2) } else { Included(2) }
let tail = map.range_bounds(lower, Unbounded).iter().map(p => p.0)
@debug.assert_eq(tail.to_array(), [3, 4])
}///|
test {
let pairs = [|(1, "one"), (2, "two"), (3, "three")|]
let map = @sorted_map.from_iter(pairs)
@test.assert_eq(map.length(), 3)
}///|
test {
let map = @sorted_map.from_array([(3, "three"), (1, "one"), (2, "two")])
let pairs = map.iter().to_array()
@debug.assert_eq(pairs, [(1, "one"), (2, "two"), (3, "three")])
}///|
test {
let map = @sorted_map.from_array([(3, "three"), (1, "one"), (2, "two")])
let transformed = []
map.iter2().each((k, v) => transformed.push(k.to_string() + ": " + v))
@debug.assert_eq(transformed, ["1: one", "2: two", "3: three"])
}///|
test {
let map1 = @sorted_map.from_array([(1, "one"), (2, "two")])
let map2 = @sorted_map.from_array([(2, "two"), (1, "one")])
@test.assert_eq(map1 == map2, true)
}///|
test {
let map = @sorted_map.from_array([(1, "one"), (2, "two")])
@test.assert_eq(map[1], "one")
@test.assert_eq(map[2], "two")
}///|
test {
let map = @sorted_map.from_array([(1, "one")])
@test.assert_eq(map.get_or_default(1, "???"), "one")
@test.assert_eq(map.get_or_default(2, "???"), "???")
// get_or_init inserts the value if missing
let val = map.get_or_init(3, fn() { "three" })
@test.assert_eq(val, "three")
@test.assert_eq(map.contains(3), true) // now in the map
}///|
test {
let map = @sorted_map.from_array([(1, "a"), (2, "b")])
let cloned = map.copy()
cloned.set(3, "c")
@test.assert_eq(map.contains(3), false) // original unchanged
@test.assert_eq(cloned.contains(3), true)
}///|
test {
let m1 = @sorted_map.from_array([(1, "a"), (2, "b")])
let m2 = @sorted_map.from_array([(2, "B"), (3, "c")])
let merged = m1.merge(m2)
assert_true(merged.get(2) == Some("B")) // right wins
assert_true(merged.get(3) == Some("c"))
// merge_in_place
let m3 = @sorted_map.from_array([(1, "x")])
let m4 = @sorted_map.from_array([(2, "y")])
m3.merge_in_place(m4)
@test.assert_eq(m3.contains(2), true)
}///|
fn get_score(
scores : @sorted_map.SortedMap[Int32, Int32],
student_id : Int32,
) -> Int32 {
match scores.get(student_id) {
Some(score) => score
None =>
// println(
// "Student ID " +
// student_id.to_string() +
// " does not exist, returning default score",
// )
0 // Default score
}
}
///|
test "safe_key_access" {
// Create a mapping storing student IDs and their scores
let scores = @sorted_map.from_array([(1001, 85), (1002, 92), (1003, 78)])
// Access an existing key
@test.assert_eq(get_score(scores, 1001), 85)
// Access a non-existent key, returning the default value
@test.assert_eq(get_score(scores, 9999), 0)
}type SortedMap[K, V]test {
let map = @sorted_map.SortedMap([(1, "one"), (2, "two")])
assert_true(map.get(1) == Some("one"))
}test {
let map1 = @sorted_map.from_array([(1, "a"), (2, "b"), (3, "c")])
let map2 = map1.copy()
map2.set(4, "d")
inspect(map1.length(), content="3")
inspect(map2.length(), content="4")
}test {
let map = @sorted_map.from_array([(3, "c"), (1, "a"), (2, "b")])
@debug.debug_inspect(map.first(), content="Some((1, \"a\"))")
}test {
let map = @sorted_map.from_array([(10, "a"), (20, "b"), (30, "c")])
@debug.debug_inspect(map.first_ge(20), content="Some((20, \"b\"))")
@debug.debug_inspect(map.first_ge(21), content="Some((30, \"c\"))")
@debug.debug_inspect(map.first_ge(31), content="None")
}test {
let map = @sorted_map.from_array([(10, "a"), (20, "b"), (30, "c")])
@debug.debug_inspect(map.first_gt(20), content="Some((30, \"c\"))")
@debug.debug_inspect(map.first_gt(30), content="None")
}test {
let map = @sorted_map.from_array([(3, "c"), (1, "a"), (2, "b")])
@debug.debug_inspect(map.last(), content="Some((3, \"c\"))")
}test {
let map = @sorted_map.from_array([(10, "a"), (20, "b"), (30, "c")])
@debug.debug_inspect(map.last_le(20), content="Some((20, \"b\"))")
@debug.debug_inspect(map.last_le(19), content="Some((10, \"a\"))")
@debug.debug_inspect(map.last_le(9), content="None")
}test {
let map = @sorted_map.from_array([(10, "a"), (20, "b"), (30, "c")])
@debug.debug_inspect(map.last_lt(20), content="Some((10, \"a\"))")
@debug.debug_inspect(map.last_lt(10), content="None")
}test {
let map1 = @sorted_map.from_array([(1, "a"), (2, "b")])
let map2 = @sorted_map.from_array([(2, "c"), (3, "d")])
let merged = map1.merge(map2)
debug_inspect(merged.get(1), content="Some(\"a\")")
debug_inspect(merged.get(2), content="Some(\"c\")")
debug_inspect(merged.get(3), content="Some(\"d\")")
}test {
let map1 = @sorted_map.from_array([(1, "a"), (2, "b")])
let map2 = @sorted_map.from_array([(2, "c"), (3, "d")])
map1.merge_in_place(map2)
debug_inspect(map1.get(1), content="Some(\"a\")")
debug_inspect(map1.get(2), content="Some(\"c\")")
debug_inspect(map1.get(3), content="Some(\"d\")")
}test {
let map = @sorted_map.from_array([(3, "c"), (1, "a"), (2, "b")])
@debug.debug_inspect(map.pop_first(), content="Some((1, \"a\"))")
@debug.debug_inspect(map.to_array(), content="[(2, \"b\"), (3, \"c\")]")
}test {
let map = @sorted_map.from_array([(3, "c"), (1, "a"), (2, "b")])
@debug.debug_inspect(map.pop_last(), content="Some((3, \"c\"))")
@debug.debug_inspect(map.to_array(), content="[(1, \"a\"), (2, \"b\")]")
}test {
let map = @sorted_map.from_array([(1, "a"), (2, "b"), (3, "c"), (4, "d")])
let keys = map.range_bounds(Excluded(1), Included(3)).iter().map(p => p.0)
@debug.debug_inspect(keys.to_array(), content="[2, 3]")
let tail = map.range_bounds(Included(3), Unbounded).iter().map(p => p.0)
@debug.debug_inspect(tail.to_array(), content="[3, 4]")
let empty = map.range_bounds(Included(2), Excluded(2))
@debug.debug_inspect(empty.iter().to_array(), content="[]")
}test {
let map = @sorted_map.from_array([(1, "a"), (3, "c"), (2, "b")])
@debug.debug_inspect(
map.rev_iter().to_array(),
content="[(3, \"c\"), (2, \"b\"), (1, \"a\")]",
)
}test {
let map = @sorted_map.from_array([(1, "a"), (3, "c"), (2, "b")])
let entries = []
for key, value in map.rev_iter2() {
entries.push((key, value))
}
@debug.debug_inspect(entries, content="[(3, \"c\"), (2, \"b\"), (1, \"a\")]")
}test {
let map = @sorted_map.from_array([(1, "a"), (2, "b"), (3, "c"), (4, "d")])
let keys = map.rev_range(2, 3).iter().map(p => p.0)
@debug.debug_inspect(keys.to_array(), content="[3, 2]")
}test {
let map = @sorted_map.from_array([(1, "a"), (2, "b"), (3, "c"), (4, "d")])
let keys = map.rev_range_bounds(Unbounded, Excluded(4)).iter().map(p => p.0)
@debug.debug_inspect(keys.to_array(), content="[3, 2, 1]")
}test {
let map = @sorted_map.SortedMap([])
map.set(1, "a")
map.set(2, "b")
map.set(2, "c") // updates value for key 2
debug_inspect(map.get(1), content="Some(\"a\")")
debug_inspect(map.get(2), content="Some(\"c\")")
}test {
let counts : @sorted_map.SortedMap[String, Int32] = SortedMap([])
counts.update_or_default("a", 1, x => x + 1)
counts.update_or_default("a", 1, x => x + 1)
counts.update_or_default("b", 1, x => x + 1)
debug_inspect(counts.get("a"), content="Some(2)")
debug_inspect(counts.get("b"), content="Some(1)")
}Install
Installed by default