Skip to content

Commit

Permalink
Merge branch 'main' into buffer-test
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoorkin authored Dec 11, 2024
2 parents 49a7acb + 0b69971 commit fb9d080
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions hashmap/hashmap.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl T {
get_or_default[K : Hash + Eq, V](Self[K, V], K, V) -> V
is_empty[K, V](Self[K, V]) -> Bool
iter[K, V](Self[K, V]) -> Iter[(K, V)]
iter2[K, V](Self[K, V]) -> Iter2[K, V]
new[K, V](capacity~ : Int = ..) -> Self[K, V]
of[K : Eq + Hash, V](FixedArray[(K, V)]) -> Self[K, V]
op_get[K : Hash + Eq, V](Self[K, V], K) -> V?
Expand Down
9 changes: 9 additions & 0 deletions hashmap/hashmap_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ test "iter" {
inspect!(buf, content="[2-two][1-one]")
}

test "iter2" {
let buf = StringBuilder::new(size_hint=20)
let map = @hashmap.of([(1, "one"), (2, "two"), (3, "three")])
for k, v in map {
buf.write_string("[\{k}-\{v}]")
}
inspect!(buf, content="[2-two][1-one][3-three]")
}

test "to_array" {
let map = @hashmap.of([(1, "one"), (2, "two"), (3, "three")])
inspect!(
Expand Down
24 changes: 21 additions & 3 deletions hashmap/utils.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ pub fn clear[K, V](self : T[K, V]) -> Unit {
pub fn iter[K, V](self : T[K, V]) -> Iter[(K, V)] {
Iter::new(
fn(yield_) {
let len = self.entries.length()
for i = 0; i < len; i = i + 1 {
match self.entries[i] {
for entry in self.entries {
match entry {
Some({ key, value, .. }) =>
if yield_((key, value)) == IterEnd {
break IterEnd
Expand All @@ -54,6 +53,25 @@ pub fn iter[K, V](self : T[K, V]) -> Iter[(K, V)] {
)
}

///|
pub fn T::iter2[K, V](self : T[K, V]) -> Iter2[K, V] {
Iter2::new(
fn(yield_) {
for entry in self.entries {
match entry {
Some({ key, value, .. }) =>
if yield_(key, value) == IterEnd {
break IterEnd
}
None => continue
}
} else {
IterContinue
}
},
)
}

///|
pub fn T::from_iter[K : Hash + Eq, V](iter : Iter[(K, V)]) -> T[K, V] {
let m = T::new()
Expand Down
4 changes: 2 additions & 2 deletions hashset/hashset.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ pub fn symmetric_difference[K : Hash + Eq](self : T[K], other : T[K]) -> T[K] {
pub fn iter[K](self : T[K]) -> Iter[K] {
Iter::new(
fn(yield_) {
for i = 0, len = self.entries.length(); i < len; i = i + 1 {
match self.entries[i] {
for entry in self.entries {
match entry {
Some({ key, .. }) => if yield_(key) == IterEnd { break IterEnd }
None => continue
}
Expand Down

0 comments on commit fb9d080

Please sign in to comment.