Skip to content

Commit

Permalink
some apis
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaquraish committed Dec 16, 2023
1 parent fedc900 commit 2cd0a1e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions std/compact_map.oc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ def Map::at(&this, key: K): V {
return item.value
}

def Map::size(&this): u32 => .items.size

def Map::contains(&this, key: K): bool {
let hash = key.hash()
let index = .get_index(key, hash)
Expand Down
2 changes: 2 additions & 0 deletions std/map.oc
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ def Map::push_keys(&this, vec: &Vector<K>) {
}
}

def Map::size(&this): u32 => .num_items

//* Checks if the map is empty
def Map::is_empty(&this): bool => .num_items == 0

Expand Down
13 changes: 13 additions & 0 deletions std/vector.oc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ def Vector::pop(&this): T {
return .data[.size]
}

//* Pops an element from the given index and returns it - O(n)
def Vector::pop_at(&this, i: u32): T {
assert .size > 0, "Empty vector in Vector::pop_at"
assert i < .size, "Out of bounds in Vector::pop_at"

let val = .data[i]
for let j = i; j < .size - 1; j += 1 {
.data[j] = .data[j + 1]
}
.size -= 1
return val
}

//* Appends the contents of another vector to this one
def Vector::extend(&this, other: &Vector<T>) {
for val : other.iter() {
Expand Down

0 comments on commit 2cd0a1e

Please sign in to comment.