Skip to content

Commit

Permalink
Add atexit(); Refactor Map::resize
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaquraish committed May 3, 2024
1 parent 7e79a40 commit 37c2575
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
19 changes: 11 additions & 8 deletions std/compact_map.oc
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,11 @@ def Map::remove(&this, key: K) {
.items.pop()
.num_tombstones += 1

// Resize if the number of tombstones is too high
if .num_tombstones + .items.size >= .capacity {
.resize(.capacity * 2)
}
.resize_if_necessary()
}

def Map::resize(&this, new_capacity: u32) {
mem::free(.indices)
let old_indices = .indices
.indices = mem::alloc<i32>(new_capacity)
.capacity = new_capacity
for let i = 0; i < new_capacity; i++ {
Expand All @@ -128,6 +125,13 @@ def Map::resize(&this, new_capacity: u32) {
}
}
.num_tombstones = 0
mem::free(old_indices)
}

def Map::resize_if_necessary(&this) {
if .num_tombstones + .items.size as u32 >= .capacity * 3 / 4 {
.resize(.capacity * 2)
}
}

[operator "[]="]
Expand All @@ -138,9 +142,8 @@ def Map::insert(&this, key: K, value: V) {
if .indices[index] < 0 {
.indices[index] = .items.size as i32
.items.push(Item<K, V>(hash, key, value))
if .items.size as u32 >= .capacity {
.resize(.capacity * 2)
}
.resize_if_necessary()

} else {
if .indices[index] == INDEX_DELETED {
.num_tombstones -= 1
Expand Down
1 change: 1 addition & 0 deletions std/libc.oc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[extern] def free(ptr: untyped_ptr)
[extern] def getenv(name: str): str
[extern] def system(cmd: str): i32
[extern] def atexit(callback: fn())

[extern] [exits] def exit(code: i32)

Expand Down

0 comments on commit 37c2575

Please sign in to comment.