Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

jakubgiesler/VecZig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VecZig

Zig implementation of Vectors.

Used Zig version v0.10.1
Library version 0.2.1
> About
  • Storing any Zig and C variables inside Vec including Structs, Unions and Enums on heap
  • Super simple iteration
  • Automatic memory allocations, reallocations
  • Uses c allocator by default, no need to pass allocator
> Note

This library is not meant to be a replacement for ArrayList in STD, but rather an alternative. Also, this library is young (see lib version above) and not meant to be in production yet.

Usage example

// build.zig
exe.linkLibC();
exe.addPackage(std.build.Pkg { .name = "vec", .source = .{ .path = "path_to_pkg/VecZig/src/main.zig" } });
  • Then you can anywhere use
// main.zig
const Vec = @import("vec").Vec;

fn main() !void {
	var vec = Vec.new();
	defer vec.delete();
	// ...
}

Docs?

Creating new Vec examples

var vector0 = Vec(type).init();
var vector1 = Vec(i32).init();

// or can be initalized with
var vector2 = Vec(f32).new();

const MY_STRUCT = struct {
	data: i32,
};

var vector3 = Vec(MY_STRUCT).new();

Freeing Vec

defer vec0.deinit();

// or can be freed with
defer vec1.delete();

Pushing new elements

try vector1.push(1);
try vector3.push(MY_STRUCT { .data = 13, });

Push same value n times

// n times --------------\
// value ------------\   |
//                   |   |
try vector1.populate(10, 10);

Print entire content of Vec into Terminal

vector1.debug_print();

Iterate through elements

for (try  vec.iterate()) |element| {
	print("{}", .{ element });
}

// or by pointer which you can edit
for (try  vec.iterate()) |*element| {
	element.* += 1;
	
	print("{}", .{ element });
}

Getting length of Vec

var length = vector1.len;

Check if Vec is empty with

if (vector1.is_empty()) {
	print("Yes its empty", .{});
}

Removing last element

try vector1.pop();

Removing element by index

try vector1.remove(3);

Edit value by index

// new value -------\
// index -------\   |
//              |   |
try vector1.set(10, 100);

Getting value by index

try vector1.get(10);

Getting pointer to value by index

try vector1.get_ptr(10);

Clearing entire array

vector1.clear();

Checking if Vec contains value

var result = vector1.contains(13);

Pushing slices

try vector1.insert(&[_] i32 { 1, 2, 3, 4, 5 });

Concat another arrays with same type

try vector1.append(another_vector);

Reversing order

try vector1.reverse();

Truncate -> shortens the vector

try vector1.truncate(5);

Drain -> Removes range from array and returns it as slice

var slice = try vector1.drain(5, 7);

Swaping values by indexes

try vector1.swap(0, 3);

Getting slice -> slices are returned as Vec(T)

// end at -------------------\
// begin at --------------\  |
//                        |  |
var slice = vector1.slice(0, 5);
defer slice.delete();

Cloning Vec

var new_vector = vector1.clone();

Getting last / first values

var first = try vector1.first();
var last = try vector1.last();

Getting last / first pointers

var first = try vector1.first_ptr();
var last = try vector1.last_ptr();

About

Vector implementation in Zig

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages