vec
Growable array of i64 values. Doubles capacity automatically when full. No external dependencies.
Memory Layout
Each vec is a pointer to a 3-word header (&i64):
| Offset | Field | Description |
|---|---|---|
[0] | length | Number of elements currently stored |
[1] | capacity | Number of allocated slots |
[2] | data_ptr | &i64 pointer to the backing array |
Usage
import vec
fn main() {
let v = vec_new(16)
// Add elements
vec_push(v, 10)
vec_push(v, 20)
vec_push(v, 30)
print(vec_len(v)) // 3
print(vec_get(v, 1)) // 20
// Modify in place
vec_set(v, 1, 25)
print(vec_get(v, 1)) // 25
// Remove last element
let last = vec_pop(v)
print(last) // 30
print(vec_len(v)) // 2
}Function Reference
| Function | Signature | Description |
|---|---|---|
vec_new | (cap: i64) -> &i64 | Create a new vec with initial capacity |
vec_len | (v: &i64) -> i64 | Get number of elements |
vec_cap | (v: &i64) -> i64 | Get current capacity |
vec_push | (v: &i64, val: i64) | Append an element |
vec_pop | (v: &i64) -> i64 | Remove and return last element |
vec_get | (v: &i64, idx: i64) -> i64 | Get element at index |
vec_set | (v: &i64, idx: i64, val: i64) | Set element at index |
vec_clear | (v: &i64) | Reset length to 0 |
vec_last | (v: &i64) -> i64 | Get last element |
vec_contains | (v: &i64, val: i64) -> i64 | Check if value exists |
vec_remove | (v: &i64, idx: i64) -> i64 | Remove at index, shift left |
Detailed API
vec_new
fn vec_new(cap: i64) -> &i64Create a new vec with the given initial capacity. The minimum capacity is 16 – values below 16 are rounded up.
let v = vec_new(64)
print(vec_len(v)) // 0
print(vec_cap(v)) // 64Returns: Pointer to a new vec header.
vec_push
fn vec_push(v: &i64, val: i64)Append a value to the end of the vec. If the vec is full, the backing array is automatically doubled in size and all existing elements are copied to the new array.
let v = vec_new(16)
vec_push(v, 42)
vec_push(v, 99)
print(vec_len(v)) // 2
print(vec_get(v, 0)) // 42vec_pop
fn vec_pop(v: &i64) -> i64Remove and return the last element. If the vec is empty, returns 0.
let v = vec_new(16)
vec_push(v, 10)
vec_push(v, 20)
let val = vec_pop(v)
print(val) // 20
print(vec_len(v)) // 1Returns: The removed element, or 0 if empty.
vec_get
fn vec_get(v: &i64, idx: i64) -> i64Get the element at the given index. No bounds checking is performed.
let v = vec_new(16)
vec_push(v, 100)
vec_push(v, 200)
print(vec_get(v, 0)) // 100
print(vec_get(v, 1)) // 200Returns: The value at position idx.
vec_set
fn vec_set(v: &i64, idx: i64, val: i64)Set the element at the given index. No bounds checking is performed.
let v = vec_new(16)
vec_push(v, 0)
vec_push(v, 0)
vec_set(v, 0, 42)
vec_set(v, 1, 99)
print(vec_get(v, 0)) // 42vec_len
fn vec_len(v: &i64) -> i64Get the number of elements currently stored.
let v = vec_new(16)
print(vec_len(v)) // 0
vec_push(v, 1)
print(vec_len(v)) // 1vec_cap
fn vec_cap(v: &i64) -> i64Get the current capacity (number of slots allocated).
let v = vec_new(32)
print(vec_cap(v)) // 32vec_clear
fn vec_clear(v: &i64)Reset the length to 0. The backing memory is not freed – the capacity remains unchanged. Useful for reusing a vec without reallocation.
let v = vec_new(16)
vec_push(v, 1)
vec_push(v, 2)
vec_clear(v)
print(vec_len(v)) // 0
print(vec_cap(v)) // 16 (unchanged)vec_last
fn vec_last(v: &i64) -> i64Get the last element without removing it. Returns 0 if the vec is empty.
let v = vec_new(16)
vec_push(v, 10)
vec_push(v, 20)
print(vec_last(v)) // 20
print(vec_len(v)) // 2 (unchanged)vec_contains
fn vec_contains(v: &i64, val: i64) -> i64Check whether the vec contains a given value. Performs a linear scan.
let v = vec_new(16)
vec_push(v, 10)
vec_push(v, 20)
vec_push(v, 30)
print(vec_contains(v, 20)) // 1
print(vec_contains(v, 99)) // 0Returns: 1 if found, 0 otherwise.
vec_remove
fn vec_remove(v: &i64, idx: i64) -> i64Remove the element at index idx and shift all subsequent elements left by one position. The vec length decreases by 1.
let v = vec_new(16)
vec_push(v, 10)
vec_push(v, 20)
vec_push(v, 30)
let removed = vec_remove(v, 1)
print(removed) // 20
print(vec_len(v)) // 2
print(vec_get(v, 0)) // 10
print(vec_get(v, 1)) // 30Returns: The removed value.
vec_pages_for
fn vec_pages_for(n: i64) -> i64Internal: Calculate the number of 4KB memory pages required to store n i64 elements, including space for the vec header and data pointer.
vec_grow
fn vec_grow(v: &i64)Internal: Double the capacity of the vec. Allocates a new backing array, copies all existing elements, and updates the vec header’s capacity and data pointer.
Internal Functions
| Function | Signature | Description |
|---|---|---|
vec_pages_for | (n: i64) -> i64 | Calculate pages needed for n i64 elements |
vec_grow | (v: &i64) | Double the backing array capacity and copy elements |