import "libc.osmx" constexpr VECTOR_MIN_CAP = 2 as usize constexpr VECTOR_GROWTH_FACTOR = 2 as usize constexpr VECTOR_SHRINK_TRESHOLD = (1 / 4) as usize constexpr VECTOR_ERROR = -1 constexpr VECTOR_SUCCESS = 0 pub struct Vector { size: usize, capacity: usize, element_size: usize, data: *u8 } pub struct VectorIterator { pointer: *u8, element_size: usize } pub func vector_setup(vector: *Vector,capacity: usize,element_size: usize) i32 { vector.size = 0 as usize; if VECTOR_MIN_CAP > capacity { vector.capacity = VECTOR_MIN_CAP; } else { vector.capacity = capacity; } vector.element_size = element_size; vector.data = malloc(vector.capacity * vector.element_size); return VECTOR_SUCCESS; } pub func vector_push_back(vector: *Vector,element: *u8) void { if _vector_should_grow(vector) { _vector_adjust_capacity(vector); } _vector_assign(vector,vector.size,element); vector.size = vector.size + 1; return; } pub func vector_pop_back(vector: *Vector) *u8 { vector.size = vector.size - 1; return &vector.data[vector.size]; } pub func vector_byte_size(vector: *Vector) usize { return vector.element_size * vector.size; } pub func _vector_should_grow(vector: *Vector) bool { return vector.size == vector.capacity; } pub func _vector_offset(vector: *Vector,index: usize) *u8 { var data = (vector.data) return &(data + (index * vector.element_size)); } pub func _vector_assign(vector: *Vector,index: usize,element: *u8) void { var offset = _vector_offset(vector,index); memcpy(offset,element,vector.element_size); return; } pub func _vector_reallocate(vector: *Vector,new_cap: usize) void { var new_cap_in_bytes: usize; if new_cap < VECTOR_MIN_CAP { if vector.capacity > VECTOR_MIN_CAP { new_cap = VECTOR_MIN_CAP; } else { return; } } new_cap_in_bytes = new_cap * vector.element_size; var old = vector.data; vector.data = malloc(new_cap_in_bytes); memcpy(vector.data,old,vector.size * vector.element_size); return; } pub func _vector_adjust_capacity(vector: *Vector) void { var reloc_size: usize; if vector.size * VECTOR_GROWTH_FACTOR == 1 { reloc_size = 1; } else { vector.size * VECTOR_GROWTH_FACTOR } _vector_reallocate(vector,reloc_size,) return; }