Zig implementation of Vectors.
> 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.
// 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();
// ...
}
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();
defer vec0.deinit();
// or can be freed with
defer vec1.delete();
try vector1.push(1);
try vector3.push(MY_STRUCT { .data = 13, });
// n times --------------\
// value ------------\ |
// | |
try vector1.populate(10, 10);
vector1.debug_print();
for (try vec.iterate()) |element| {
print("{}", .{ element });
}
// or by pointer which you can edit
for (try vec.iterate()) |*element| {
element.* += 1;
print("{}", .{ element });
}
var length = vector1.len;
if (vector1.is_empty()) {
print("Yes its empty", .{});
}
try vector1.pop();
try vector1.remove(3);
// new value -------\
// index -------\ |
// | |
try vector1.set(10, 100);
try vector1.get(10);
try vector1.get_ptr(10);
vector1.clear();
var result = vector1.contains(13);
try vector1.insert(&[_] i32 { 1, 2, 3, 4, 5 });
try vector1.append(another_vector);
try vector1.reverse();
try vector1.truncate(5);
var slice = try vector1.drain(5, 7);
try vector1.swap(0, 3);
// end at -------------------\
// begin at --------------\ |
// | |
var slice = vector1.slice(0, 5);
defer slice.delete();
var new_vector = vector1.clone();
var first = try vector1.first();
var last = try vector1.last();
var first = try vector1.first_ptr();
var last = try vector1.last_ptr();