stlib arraylist
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
mem :: import "@std/mem"
|
||||
|
||||
ArrayList func($T type) type {
|
||||
return struct {
|
||||
items []mut T
|
||||
capacity usize
|
||||
allocator mem.Allocator
|
||||
}
|
||||
}
|
||||
|
||||
init func($T type, allocator mem.Allocator) ArrayList(T) {
|
||||
return ArrayList(T) {
|
||||
items = mem.empty(T),
|
||||
capacity = 0,
|
||||
allocator = allocator,
|
||||
}
|
||||
}
|
||||
|
||||
deinit func($T type, list @mut ArrayList(T)) void {
|
||||
allocation []mut T :: list.items.ptr[..list.capacity]
|
||||
mem.free(T, list.allocator, allocation)
|
||||
list.items = mem.empty(T)
|
||||
list.capacity = 0
|
||||
}
|
||||
|
||||
reserve func($T type, list @mut ArrayList(T), minimum_capacity usize) void ! mem.AllocError {
|
||||
if minimum_capacity <= list.capacity {
|
||||
return _
|
||||
}
|
||||
|
||||
new_capacity usize = 8
|
||||
if list.capacity >= 8 {
|
||||
half usize :: list.capacity / 2
|
||||
if list.capacity > max_value(usize) - half {
|
||||
new_capacity = minimum_capacity
|
||||
} else {
|
||||
new_capacity = list.capacity + half
|
||||
}
|
||||
}
|
||||
if new_capacity < minimum_capacity {
|
||||
new_capacity = minimum_capacity
|
||||
}
|
||||
|
||||
length usize :: list.items.len
|
||||
allocation []mut T :: list.items.ptr[..list.capacity]
|
||||
grown []mut T :: mem.realloc(T, list.allocator, allocation, new_capacity) catch |_| {
|
||||
return .out_of_memory
|
||||
}
|
||||
list.items = grown.ptr[..length]
|
||||
list.capacity = new_capacity
|
||||
return _
|
||||
}
|
||||
|
||||
append func($T type, list @mut ArrayList(T), value T) void ! mem.AllocError {
|
||||
length usize :: list.items.len
|
||||
if length == max_value(usize) {
|
||||
return .out_of_memory
|
||||
}
|
||||
try reserve(T, list, length + 1)
|
||||
list.items = list.items.ptr[..length + 1]
|
||||
list.items[length] = value
|
||||
return _
|
||||
}
|
||||
|
||||
clear func($T type, list @mut ArrayList(T)) void {
|
||||
list.items = list.items.ptr[..0]
|
||||
}
|
||||
@@ -34,6 +34,10 @@ _empty_slice func($T type, count usize) []mut T {
|
||||
return pointer[..count]
|
||||
}
|
||||
|
||||
empty func($T type) []mut T {
|
||||
return _empty_slice(T, 0)
|
||||
}
|
||||
|
||||
alloc func($T type, allocator Allocator, count usize) []mut T ! AllocError {
|
||||
if count == 0 {
|
||||
return _empty_slice(T, 0)
|
||||
@@ -55,6 +59,43 @@ alloc func($T type, allocator Allocator, count usize) []mut T ! AllocError {
|
||||
return .out_of_memory
|
||||
}
|
||||
|
||||
realloc func($T type, allocator Allocator, memory []mut T, new_count usize) []mut T ! AllocError {
|
||||
if new_count == memory.len {
|
||||
return memory
|
||||
}
|
||||
if new_count == 0 {
|
||||
free(T, allocator, memory)
|
||||
return _empty_slice(T, 0)
|
||||
}
|
||||
|
||||
element_size usize :: size_of(T)
|
||||
if element_size == 0 {
|
||||
return _empty_slice(T, new_count)
|
||||
}
|
||||
if new_count > max_value(usize) / element_size {
|
||||
return .out_of_memory
|
||||
}
|
||||
|
||||
old_memory ?*mut u8 = none
|
||||
old_size usize = 0
|
||||
if memory.len != 0 {
|
||||
old_memory = ptr_cast(u8, memory.ptr)
|
||||
old_size = memory.len * element_size
|
||||
}
|
||||
resized ?*mut u8 = raw_realloc(
|
||||
allocator,
|
||||
old_memory,
|
||||
old_size,
|
||||
new_count * element_size,
|
||||
align_of(T),
|
||||
)
|
||||
if resized |bytes| {
|
||||
pointer *mut T :: ptr_cast(T, bytes)
|
||||
return pointer[..new_count]
|
||||
}
|
||||
return .out_of_memory
|
||||
}
|
||||
|
||||
free func($T type, allocator Allocator, memory []mut T) void {
|
||||
if memory.len != 0 and size_of(T) != 0 {
|
||||
raw_free(allocator, ptr_cast(u8, memory.ptr), memory.len * size_of(T), align_of(T))
|
||||
|
||||
Reference in New Issue
Block a user