std/vec

Vec<T> is a contiguous, growable array type. It is the standard dynamic array used in Zen-C.

Overview

Usage

import "std/vec.zc"
import "std/io.zc"

fn main() {
    let v = Vec<int>::new();
    v.push(10);
    v.push(20);
    
    // Iteration
    for x in &v {
        println(f"{(*x)}");
    }
} // v is freed automatically here

Struct Definition

struct Vec<T> {
    data: T*;
    len: usize;
    cap: usize;
}

Methods

Construction

MethodSignatureDescription
newVec<T>::new() -> Vec<T>Creates a new, empty vector. Does not allocate memory until the first push.
with_capacityVec<T>::with_capacity(cap: usize) -> Vec<T>Creates a new vector with an initial capacity of cap. Useful for optimization if you know the number of elements in advance.

Modification

MethodSignatureDescription
pushpush(self, item: T)Appends an element to the back. Panics if allocation fails.
poppop(self) -> TRemoves the last element and returns it. Panics if empty.
pop_optpop_opt(self) -> Option<T>Removes the last element and returns Some(val). Returns None if empty. Safe usage.
insertinsert(self, idx: usize, item: T)Inserts an element at idx. Shifts elements right. Panics if idx > len.
removeremove(self, idx: usize) -> TRemoves and returns the element at idx. Shifts elements left. Panics if idx >= len.
appendappend(self, other: Vec<T>)Appends all elements from other to self. Consumes other (move semantics).
clearclear(self)Removes all values. Has no effect on allocated capacity.
reversereverse(self)Reverses the order of elements in place.

Access

MethodSignatureDescription
getget(self, idx: usize) -> TReturns a copy of the element at idx. Panics if out of bounds.
get_refget_ref(self, idx: usize) -> T*Returns a pointer to the element at idx. Panics if out of bounds. Useful for avoiding copies.
setset(self, idx: usize, item: T)Overwrites the element at idx. Panics if out of bounds.
firstfirst(self) -> TReturns a copy of the first element. Panics if empty.
lastlast(self) -> TReturns a copy of the last element. Panics if empty.

Utility

MethodSignatureDescription
lengthlength(self) -> usizeReturns the number of elements.
is_emptyis_empty(self) -> boolReturns true if the vector contains no elements.
containscontains(self, item: T) -> boolReturns true if vector contains an element equal to item (byte-wise).
cloneclone(self) -> Vec<T>Returns a new vector with a deep copy of the data.
eqeq(self, other: Vec<T>*) -> boolReturns true if two vectors are equal byte-wise. Takes a pointer to avoid moving other.

Operators

Zen-C supports operator overloading. Vec<T> implements the following:

OperatorMethodDescription
+addv1 + &v2. Returns a new vector (concatenation).
+=add_assignv1 += &v2. Appends v2 to v1.
==eqv1 == &v2. Structural equality check.
!=neqv1 != &v2. Structural inequality check.
<<shlv << item. Pushes item to the back.
>>shrv >> &item. Pops the last element into item.
*mulv * n. Returns a new vector with elements repeated n times.
*=mul_assignv *= n. Repeats elements in-place n times.
[]get / setv[i] and v[i] = x. Standard indexing.

Iteration

MethodSignatureDescription
iteratoriterator(self) -> VecIter<T>Returns an iterator yielding copies. Used by for x in v.
iter_refiter_ref(self) -> VecIterRef<T>Returns an iterator yielding pointers. Used by for x in &v (sugar) or for x in v.iter_ref(). Allows in-place mod.

Iterator Combinators

The VecIter<T> returned by iterator() drains the vector as it is consumed. The methods below operate on the remaining items.

MethodSignatureDescription
nextnext(self) -> Option<T>Yields the next item, or None when exhausted.
countcount(self) -> usizeNumber of items still to be yielded.
collectcollect(self) -> Vec<T>Drains the remaining items into a new Vec.
forEachforEach(self, f: fn(T))Calls f for each remaining item.
findfind(self, pred: fn(T) -> bool) -> Option<T>Returns the first item accepted by pred.
anyany(self, pred: fn(T) -> bool) -> boolReturns true when pred accepts at least one remaining item.
allall(self, pred: fn(T) -> bool) -> boolReturns true when pred accepts every remaining item.
filterfilter(self, pred: fn(T) -> bool) -> Vec<T>Collects the remaining items accepted by pred into a new Vec.
foldfold(self, init: T, f: fn(T, T) -> T) -> TReduces the remaining items with f starting from init.

VecIterRef<T> also provides next, count, and find (yielding item pointers via VecIterResult<T>).

Memory Management

MethodSignatureDescription
Freefree(self)Manually frees memory. Safe to call multiple times.
Forgetforget(self)Detaches the memory buffer from the vector (sets fields to 0). Prevents Drop from freeing memory. Useful for implementing move semantics or transferring ownership.
Traitimpl Drop for VecAutomatically calls free() when Vec goes out of scope.