fn BoundedArray(comptime T: type, comptime buffer_capacity: usize) type
A structure with an array and a length, that can be used as a slice.
Useful to pass around small arrays whose exact size is only known at runtime, but whose maximum size is known at comptime, without requiring an Allocator
.
var actual_size = 32;
var a = try BoundedArray(u8, 64).init(actual_size);
var slice = a.slice(); // a slice of the 64-byte array
var a_clone = a; // creates a copy - the structure doesn't use any internal pointers
Functions
fn addManyAsArray(self: *Self, comptime n: usize) error{Overflow}!*align(alignment) [n]T
Resize the slice, adding
n
new elements, which haveundefined
values. The r…Resize the slice, adding
n
new elements, which haveundefined
values. The return value is a slice pointing to the uninitialized elements.fn addOneAssumeCapacity(self: *Self) *T
Increase length by 1, returning pointer to the new item. Asserts that there is …
Increase length by 1, returning pointer to the new item. Asserts that there is space for the new item.
fn appendAssumeCapacity(self: *Self, item: T) void
Extend the slice by 1 element, asserting the capacity is already enough to stor…
Extend the slice by 1 element, asserting the capacity is already enough to store the new item.
fn appendNTimes(self: *Self, value: T, n: usize) error{Overflow}!void
Append a value to the slice
n
times. Allocates more memory as necessary.fn appendNTimesAssumeCapacity(self: *Self, value: T, n: usize) void
Append a value to the slice
n
times. Asserts the capacity is enough.fn appendSlice(self: *Self, items: []const T) error{Overflow}!void
Append the slice of items to the slice.
fn appendSliceAssumeCapacity(self: *Self, items: []const T) void
Append the slice of items to the slice, asserting the capacity is already enoug…
Append the slice of items to the slice, asserting the capacity is already enough to store the new items.
fn constSlice(self: *const Self) []const align(alignment) T
View the internal array as a constant slice whose size was previously set.
fn ensureUnusedCapacity(self: Self, additional_count: usize) error{Overflow}!void
Check that the slice can hold at least
additional_count
items.fn init(len: usize) error{Overflow}!Self
Set the actual length of the slice. Returns error.Overflow if it exceeds the le…
Set the actual length of the slice. Returns error.Overflow if it exceeds the length of the backing array.
fn insert(self: *Self, i: usize, item: T) error{Overflow}!void
Insert
item
at indexi
by movingslice[n .. slice.len]
to make room. This…Insert
item
at indexi
by movingslice[n .. slice.len]
to make room. This operation is O(N).fn insertSlice(self: *Self, i: usize, items: []const T) error{Overflow}!void
Insert slice
items
at indexi
by movingslice[i .. slice.len]
to make room…Insert slice
items
at indexi
by movingslice[i .. slice.len]
to make room. This operation is O(N).fn orderedRemove(self: *Self, i: usize) T
Remove the element at index
i
, shift elements after indexi
forward, and re…Remove the element at index
i
, shift elements after indexi
forward, and return the removed element. Asserts the slice has at least one item. This operation is O(N).fn pop(self: *Self) T
Remove and return the last element from the slice. Asserts the slice has at lea…
Remove and return the last element from the slice. Asserts the slice has at least one item.
fn popOrNull(self: *Self) ?T
Remove and return the last element from the slice, or return
null
if the slic…Remove and return the last element from the slice, or return
null
if the slice is empty.fn replaceRange(self: *Self, start: usize, len: usize, new_items: []const T) error{Overflow}!void
Replace range of elements
slice[start..][0..len]
withnew_items
. Grows slic…Replace range of elements
slice[start..][0..len]
withnew_items
. Grows slice iflen < new_items.len
. Shrinks slice iflen > new_items.len
.fn resize(self: *Self, len: usize) error{Overflow}!void
Adjust the slice’s length to
len
. Does not initialize added items if any.fn slice(self: anytype) switch (@TypeOf(&self.buffer)) { *align(alignment) [buffer_capacity]T => []align(alignment) T, *align(alignment) const [buffer_capacity]T => []align(alignment) const T, else => unreachable, }
View the internal array as a slice whose size was previously set.
fn swapRemove(self: *Self, i: usize) T
Remove the element at the specified index and return it. The empty slot is fill…
Remove the element at the specified index and return it. The empty slot is filled from the end of the slice. This operation is O(1).
fn unusedCapacitySlice(self: *Self) []align(alignment) T
Return a slice of only the extra capacity after items. This can be useful for w…
Return a slice of only the extra capacity after items. This can be useful for writing directly into it. Note that such an operation must be followed up with a call to
resize()