Skip to content

Easy to use arrays in Zig, just dont use this lib cause its literal hot garbage

Notifications You must be signed in to change notification settings

mikkurogue/libarr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

libarr - what?

Why? Because im a midwit. This is a "library" I am building to learn some Zig constructs.

This project is not at all usable for any production environment or product, so if you do decide that the features/functions here are somewhat usable then do use this at your own risk.

As this is not a package or anything official, you can just copy and paste the code. It is "unlicensed" and therefore I do not request any type of commendation for usage.

You can however feel free to link to this repository if you feel so inclined, but again; that's completely optional and in all honesty not important to me.

I am however looking for pointers, tips, tricks or any resource to help me make this somewhat usable maybe in the future, or at least as a reference to upcoming projects.

Docs

Array

A Array is an instance of an Array that has a fixed size.

This is essentially a fixed length array, but in size depending on the type of array it is.

There are ways to re-allocate more capacity for this array.

struct Array(T)

An array is a generic struct that takes in a comptime T of the type of array it is.

Members

allocator: std.mem.Allocator

An allocator must be provided to allocate memory.

items: []T

A slice of type T provided by the generic.

len: usize

The length of the amount of items in the array, also accessible by items.len.

capacity: u8

The capacity of memory, required by the allocator.

Member methods

init(allocator: Allocator, capacity: usize) !Array(T)

Function to initialise the array, this allocates and sets the basic default values. Capacity here is the maximum capacity of the array in memory.

deinit() !void

Destruct function for the allocator to free the memory. Recommended to always defer this once initialised.

resize_dynamic() !void

Resize the array dynamically by doubling the capacity.

resize_abs(add_to_capacity: usize) !void

Resize the array based on new capacity to add to existing capacity.

push(val: T) !void

Push a new value to the end of the array. If this operation would exceed capacity, then return an error.

push_head(val: T) !void

Push a new value to the head (start) of the array. If this operation would exceed capacity, then return an error.

pop() !void

Pop the last item out of the array.

replace_at(idx: u8, val: T) !void

Replace an item in the array at a given index.

remove_at(idx: u8) !void

Remove an item in the array at a given index.

left_shift() !void

Move all items 1 place to the left in the array. (experimental)

iterator() ArrIterator(T)

Instantiate a new ArrIterator of given type. See ArrIterator for more.

peek_next(index: usize) !T

Given an index, return the value that comes AFTER the given index. Can error NextIndexOutOfBounds

peek_prev(index: usize) !T

Given an index, return the value that comes BEFORE the given index. Can error PrevIndexOutOfBounds

reverse() !void

Reverse the array. Its just that simple.

ArrIterator

An ArrIterator is a helper struct to help iterate over the arrays. This iterator does not mutate the array in anyway.

Members

items: []const T

A constant of the given type of items.

index: usize = 0

The current index we are at in the iterator. This is essentially a private member.

Member methods

next() ?T

Iterate to the next index. Returns null if we are at the end of the array.

About

Easy to use arrays in Zig, just dont use this lib cause its literal hot garbage

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages