Skip to content
This repository was archived by the owner on Mar 7, 2026. It is now read-only.

archaistvolts/multi-bounded-array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

Similar to std.MultiArrayList() but backed by fixed size arrays.

Each field of T becomes an array with length buffer_capacity. To access the arrays as slices use items(.<field>) or constItems(.<field>).

Useful when a struct of small arrays is desired with capacity that is known at compile time. Like std.BoundedArray, MultiBoundedArrays are only values and thus may be copied.

Usage

$ zig fetch --save=multi_bounded_array git+https://github.com/travisstaloch/multi-bounded-array#main"
// build.zig
const dep = b.dependency("multi_bounded_array", .{ .optimize = optimize, .target = target });
exe.root_module.addImport("multi_bounded_array", dep.module("multi_bounded_array"));
// main.zig
const MultiBoundedArray = @import("multi_bounded_array").MultiBoundedArray;
var a = MultiBoundedArray(struct { int: u8, float: f32 }, 64){};
// ...

Example

From tests in lib.zig which also contains more tests.

test "basic" {
    const T = struct { int: u8, float: f32 };

    var a = MultiBoundedArray(T, 64){};
    try testing.expectEqual(0, a.len);
    try a.append(.{ .int = 1, .float = 1 });
    const mut_ints = a.items(.int);
    try testing.expectEqual([]u8, @TypeOf(mut_ints)); // items() slice constness is inherited
    try testing.expectEqual(1, mut_ints.len);
    try testing.expectEqual(1, mut_ints[0]);
    mut_ints[0] = 2;

    const a_const_copy = a;
    mut_ints[0] = 3;
    const const_ints = a_const_copy.items(.int);
    try testing.expectEqual([]const u8, @TypeOf(const_ints)); // items() slice constness is inherited
    try testing.expectEqual(2, const_ints[0]);
}

Todo

  • support tagged unions and sorting
  • add tests from std.MultiArrayList

About

Similar to std.MultiArrayList() but backed by fixed size arrays

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages