Overview
std.BitStack
Comprehensive reference for Zig's std.BitStack module covering collections and data-structure utilities.
This page syncs automatically from Zig's source:
std/BitStack.md.Zig Standard Library Documentation
| Key | Value |
|---|---|
| Module | std.BitStack |
| Declarations | 9 |
| Breakdown | 9 functions |
| Generated (unix epoch) | 1760148098 |
Overview
Effectively a stack of u1 values implemented using ArrayList(u8).
Table of Contents
Functions (9)
init
Function – Expand to view signature, parameters, and examples.
pub fn init(allocator: Allocator) @This() {
return .{
.bytes = std.array_list.Managed(u8).init(allocator),
};
}
Parameters & Return:
| Name | Type | Description | Default |
|---|---|---|---|
allocator | Allocator | – | – |
| Return | @This() | – | – |
deinit
Function – Expand to view signature, parameters, and examples.
pub fn deinit(self: *@This()) void {
self.bytes.deinit();
self.* = undefined;
}
Parameters & Return:
| Name | Type | Description | Default |
|---|---|---|---|
self | *@This() | – | – |
| Return | void | – | – |
ensureTotalCapacity
Function – Expand to view signature, parameters, and examples.
pub fn ensureTotalCapacity(self: *@This(), bit_capacity: usize) Allocator.Error!void {
const byte_capacity = (bit_capacity + 7) >> 3;
try self.bytes.ensureTotalCapacity(byte_capacity);
}
Parameters & Return:
| Name | Type | Description | Default |
|---|---|---|---|
self | *@This() | – | – |
bit\_capacity | usize | – | – |
| Return | Allocator.Error!void | – | – |
push
Function – Expand to view signature, parameters, and examples.
pub fn push(self: *@This(), b: u1) Allocator.Error!void {
const byte_index = self.bit_len >> 3;
if (self.bytes.items.len <= byte_index) {
try self.bytes.append(0);
}
pushWithStateAssumeCapacity(self.bytes.items, &self.bit_len, b);
}
Parameters & Return:
| Name | Type | Description | Default |
|---|---|---|---|
self | *@This() | – | – |
b | u1 | – | – |
| Return | Allocator.Error!void | – | – |
peek
Function – Expand to view signature, parameters, and examples.
pub fn peek(self: *const @This()) u1 {
return peekWithState(self.bytes.items, self.bit_len);
}
Parameters & Return:
| Name | Type | Description | Default |
|---|---|---|---|
self | *const @This() | – | – |
| Return | u1 | – | – |
pop
Function – Expand to view signature, parameters, and examples.
pub fn pop(self: *@This()) u1 {
return popWithState(self.bytes.items, &self.bit_len);
}
Parameters & Return:
| Name | Type | Description | Default |
|---|---|---|---|
self | *@This() | – | – |
| Return | u1 | – | – |
pushWithStateAssumeCapacity
Function – Standalone function for working with a fixed-size buffer
Standalone function for working with a fixed-size buffer.
pub fn pushWithStateAssumeCapacity(buf: []u8, bit_len: *usize, b: u1) void {
const byte_index = bit_len.* >> 3;
const bit_index = @as(u3, @intCast(bit_len.* & 7));
buf[byte_index] &= ~(@as(u8, 1) << bit_index);
buf[byte_index] |= @as(u8, b) << bit_index;
bit_len.* += 1;
}
Parameters & Return:
| Name | Type | Description | Default |
|---|---|---|---|
buf | []u8 | – | – |
bit\_len | *usize | – | – |
b | u1 | – | – |
| Return | void | – | – |
peekWithState
Function – Standalone function for working with a fixed-size buffer
Standalone function for working with a fixed-size buffer.
pub fn peekWithState(buf: []const u8, bit_len: usize) u1 {
const byte_index = (bit_len - 1) >> 3;
const bit_index = @as(u3, @intCast((bit_len - 1) & 7));
return @as(u1, @intCast((buf[byte_index] >> bit_index) & 1));
}
Parameters & Return:
| Name | Type | Description | Default |
|---|---|---|---|
buf | []const u8 | – | – |
bit\_len | usize | – | – |
| Return | u1 | – | – |
popWithState
Function – Standalone function for working with a fixed-size buffer
Standalone function for working with a fixed-size buffer.
pub fn popWithState(buf: []const u8, bit_len: *usize) u1 {
const b = peekWithState(buf, bit_len.*);
bit_len.* -= 1;
return b;
}
Parameters & Return:
| Name | Type | Description | Default |
|---|---|---|---|
buf | []const u8 | – | – |
bit\_len | *usize | – | – |
| Return | u1 | – | – |