mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 07:08:48 +00:00
container/queue: Add shrink
This commit is contained in:
@@ -121,6 +121,33 @@ reserve :: proc(q: ^$Q/Queue($T), capacity: int) -> runtime.Allocator_Error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Shrink a queue's dynamically allocated array.
|
||||||
|
|
||||||
|
This has no effect if the queue was initialized with a backing slice.
|
||||||
|
*/
|
||||||
|
shrink :: proc(q: ^$Q/Queue($T), temp_allocator := context.temp_allocator, loc := #caller_location) {
|
||||||
|
if q.data.allocator.procedure == runtime.nil_allocator_proc {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if q.len > 0 && q.offset > 0 {
|
||||||
|
// Make the array contiguous again.
|
||||||
|
buffer := make([]T, q.len, temp_allocator)
|
||||||
|
defer delete(buffer, temp_allocator)
|
||||||
|
|
||||||
|
right := uint(builtin.len(q.data)) - q.offset
|
||||||
|
copy(buffer[:], q.data[q.offset:])
|
||||||
|
copy(buffer[right:], q.data[:q.offset])
|
||||||
|
|
||||||
|
copy(q.data[:], buffer[:])
|
||||||
|
|
||||||
|
q.offset = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
builtin.shrink(&q.data, q.len, loc)
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Get the element at index `i`.
|
Get the element at index `i`.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user