Skip to content

Commit

Permalink
Add some missing APIs for set/map
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaquraish committed Dec 4, 2023
1 parent fafca3b commit 3aa8604
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions std/compact_map.oc
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ def Map::at(&this, key: K): V {
return item.value
}

def Map::contains(&this, key: K): bool {
let hash = key.hash()
let index = .get_index(key, hash)
return .indices[index] != INDEX_FREE
}

def Map::clear(&this) {
.items.clear()
for let i = 0; i < .capacity; i++ {
.indices[i] = INDEX_FREE
}
}

def Map::is_empty(&this): bool => .items.size == 0

def Map::iter(&this): Iterator<Item<K, V>> => .items.iter()
Expand Down
11 changes: 11 additions & 0 deletions std/map.oc
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ def Map::free(&this) {
.buckets[i].free_list()
}
free(.buckets)
free(this)
}

//* Clears the map
def Map::clear(&this) {
for let i = 0; i < .num_buckets; i += 1 {
.buckets[i].free_list()
.buckets[i] = null
}
.num_items = 0
.num_collisions = 0
}

//* Returns an iterator over the map
Expand Down
4 changes: 4 additions & 0 deletions std/set.oc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def Set::size(&this): u32 {
return .map.num_items
}

def Set::clear(&this) {
.map.clear()
}

def Set::extend(&this, other: &Set<T>) {
.map.extend(other.map)
}
Expand Down

0 comments on commit 3aa8604

Please sign in to comment.