Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1264 Add .range() method to sorted_map and sorted_set #1359

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions sorted_map/map.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ pub impl[K : @quickcheck.Arbitrary + Compare, V : @quickcheck.Arbitrary] @quickc
@quickcheck.Arbitrary::arbitrary(size, rs) |> T::from_iter
}

///|Returns a new array of key-value pairs that are within the specified range [low, high).
pub fn range[K : Compare, V](
self : T[K, V],
low : K,
high : K
) -> Array[(K, V)] {
let result = []
self.each(fn(k, v) { if k >= low && k <= high { result.push((k, v)) } })
result
}

// AVL tree operations

///|
Expand Down
11 changes: 11 additions & 0 deletions sorted_map/map_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,14 @@ test "iter" {
}
inspect!(buf, content="[1a][2b][3c]")
}

test "range" {
let map = @sorted_map.of([(1, "a"), (2, "b"), (3, "c"), (4, "d"), (5, "e")])
let result = map.range(2, 4)
inspect!(
result,
content=
#|[(2, "b"), (3, "c"), (4, "d")]
,
)
}
1 change: 1 addition & 0 deletions sorted_map/sorted_map.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl T {
op_equal[K : Eq, V : Eq](Self[K, V], Self[K, V]) -> Bool
op_get[K : Compare, V](Self[K, V], K) -> V?
op_set[K : Compare, V](Self[K, V], K, V) -> Unit
range[K : Compare, V](Self[K, V], K, K) -> Array[(K, V)]
remove[K : Compare, V](Self[K, V], K) -> Unit
size[K, V](Self[K, V]) -> Int
to_array[K, V](Self[K, V]) -> Array[(K, V)]
Expand Down
7 changes: 7 additions & 0 deletions sorted_set/set.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,13 @@ fn to_array[T](self : Node[T]) -> Array[T] {
aux(Some(self))
}

///|
pub fn range[V : Compare](self : T[V], low : V, high : V) -> Array[V] {
let result = []
self.each(fn(x) { if x >= low && x <= high { result.push(x) } })
result
}

// AVL tree operations

///|
Expand Down
6 changes: 6 additions & 0 deletions sorted_set/set_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,9 @@ test "from_iter empty iter" {
let pq : @sorted_set.T[Int] = @sorted_set.T::from_iter(Iter::empty())
inspect!(pq, content="@sorted_set.of([])")
}

test "range" {
let set = @sorted_set.of([1, 2, 3, 4, 5])
let result = set.range(2, 4)
inspect!(result, content="[2, 3, 4]")
}
1 change: 1 addition & 0 deletions sorted_set/sorted_set.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl T {
is_empty[V : Compare](Self[V]) -> Bool
iter[V](Self[V]) -> Iter[V]
op_equal[V : Compare](Self[V], Self[V]) -> Bool
range[V : Compare](Self[V], V, V) -> Array[V]
remove[V : Compare](Self[V], V) -> Unit
size[V : Compare](Self[V]) -> Int64
subset[V : Compare](Self[V], Self[V]) -> Bool
Expand Down
Loading