diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2025-04-17 14:20:03 +0200 |
|---|---|---|
| committer | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2025-04-17 14:20:03 +0200 |
| commit | 5a3901333933777a5d217856d27ca2c07d684be9 (patch) | |
| tree | 6e2840c7778ef5925b97a34301c9630e35ad2cf6 /core/container/priority_queue | |
| parent | 09c8477bd3ed381843f1d157cca6806f335e29ae (diff) | |
Let `core:container/priority_queue` return `runtime.Allocator_Error`
`init`, `reserve` and `push` now return `runtime.Allocator_Error`.
Diffstat (limited to 'core/container/priority_queue')
| -rw-r--r-- | core/container/priority_queue/priority_queue.odin | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/core/container/priority_queue/priority_queue.odin b/core/container/priority_queue/priority_queue.odin index 8a6d77288..7387a8d09 100644 --- a/core/container/priority_queue/priority_queue.odin +++ b/core/container/priority_queue/priority_queue.odin @@ -1,6 +1,7 @@ package container_priority_queue import "base:builtin" +import "base:runtime" Priority_Queue :: struct($T: typeid) { queue: [dynamic]T, @@ -17,13 +18,14 @@ default_swap_proc :: proc($T: typeid) -> proc(q: []T, i, j: int) { } } -init :: proc(pq: ^$Q/Priority_Queue($T), less: proc(a, b: T) -> bool, swap: proc(q: []T, i, j: int), capacity := DEFAULT_CAPACITY, allocator := context.allocator) { +init :: proc(pq: ^$Q/Priority_Queue($T), less: proc(a, b: T) -> bool, swap: proc(q: []T, i, j: int), capacity := DEFAULT_CAPACITY, allocator := context.allocator) -> (err: runtime.Allocator_Error) { if pq.queue.allocator.procedure == nil { pq.queue.allocator = allocator } - reserve(pq, capacity) + reserve(pq, capacity) or_return pq.less = less pq.swap = swap + return .None } init_from_dynamic_array :: proc(pq: ^$Q/Priority_Queue($T), queue: [dynamic]T, less: proc(a, b: T) -> bool, swap: proc(q: []T, i, j: int)) { @@ -41,8 +43,8 @@ destroy :: proc(pq: ^$Q/Priority_Queue($T)) { delete(pq.queue) } -reserve :: proc(pq: ^$Q/Priority_Queue($T), capacity: int) { - builtin.reserve(&pq.queue, capacity) +reserve :: proc(pq: ^$Q/Priority_Queue($T), capacity: int) -> (err: runtime.Allocator_Error) { + return builtin.reserve(&pq.queue, capacity) } clear :: proc(pq: ^$Q/Priority_Queue($T)) { builtin.clear(&pq.queue) @@ -103,9 +105,10 @@ fix :: proc(pq: ^$Q/Priority_Queue($T), i: int) { } } -push :: proc(pq: ^$Q/Priority_Queue($T), value: T) { - append(&pq.queue, value) +push :: proc(pq: ^$Q/Priority_Queue($T), value: T) -> (err: runtime.Allocator_Error) { + append(&pq.queue, value) or_return _shift_up(pq, builtin.len(pq.queue)-1) + return .None } pop :: proc(pq: ^$Q/Priority_Queue($T), loc := #caller_location) -> (value: T) { |