实质上是用 ArrayList(u8) 实现的 u1 值栈。
字段
bytes: std.array_list.Managed(u8)
bit_len: usize = 0
std.BitStack.Allocator
别名
别名目标 std.mem.Allocator
std.BitStack.ArrayList
别名
std.BitStack.BitStack
别名
别名目标 std.BitStack
std.BitStack.deinit
函数
pub fn deinit(self: *@This()) void
参数
self: *@This()
std.BitStack.ensureTotalCapacity
函数
pub fn ensureTotalCapacity(self: *@This(), bit_capacity: usize) Allocator.Error!void
参数
self: *@This()
bit_capacity: usize
错误
- OutOfMemory
std.BitStack.init
函数
pub fn init(allocator: Allocator) @This()
参数
allocator: Allocator
std.BitStack.peek
函数
pub fn peek(self: *const @This()) u1
参数
self: *const @This()
std.BitStack.peekWithState
函数
用于操作固定大小缓冲区的独立函数。
pub fn peekWithState(buf: []const u8, bit_len: usize) u1
参数
buf: []const u8
bit_len: usize
std.BitStack.pop
函数
pub fn pop(self: *@This()) u1
参数
self: *@This()
std.BitStack.popWithState
函数
用于操作固定大小缓冲区的独立函数。
pub fn popWithState(buf: []const u8, bit_len: *usize) u1
参数
buf: []const u8
bit_len: *usize
std.BitStack.push
函数
pub fn push(self: *@This(), b: u1) Allocator.Error!void
参数
self: *@This()
b: u1
错误
- OutOfMemory
std.BitStack.pushWithStateAssumeCapacity
函数
用于操作固定大小缓冲区的独立函数。
pub fn pushWithStateAssumeCapacity(buf: []u8, bit_len: *usize, b: u1) void
参数
buf: []u8
bit_len: *usize
b: u1
std.BitStack.std
别名
别名目标 std
std.BitStack.testing
别名
别名目标 std.testing
源码
std/BitStack.zig
//! 实质上是用 ArrayList(u8) 实现的 u1 值栈。
const BitStack = @This();
const std = @import("std");
const Allocator = std.mem.Allocator;
const ArrayList = std.array_list.Managed;
bytes: std.array_list.Managed(u8),
bit_len: usize = 0,
pub fn init(allocator: Allocator) @This() {
return .{
.bytes = std.array_list.Managed(u8).init(allocator),
};
}
pub fn deinit(self: *@This()) void {
self.bytes.deinit();
self.* = undefined;
}
pub fn ensureTotalCapacity(self: *@This(), bit_capacity: usize) Allocator.Error!void {
const byte_capacity = (bit_capacity + 7) >> 3;
try self.bytes.ensureTotalCapacity(byte_capacity);
}
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);
}
pub fn peek(self: *const @This()) u1 {
return peekWithState(self.bytes.items, self.bit_len);
}
pub fn pop(self: *@This()) u1 {
return popWithState(self.bytes.items, &self.bit_len);
}
/// 用于操作固定大小缓冲区的独立函数。
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;
}
/// 用于操作固定大小缓冲区的独立函数。
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));
}
/// 用于操作固定大小缓冲区的独立函数。
pub fn popWithState(buf: []const u8, bit_len: *usize) u1 {
const b = peekWithState(buf, bit_len.*);
bit_len.* -= 1;
return b;
}
const testing = std.testing;
test BitStack {
var stack = BitStack.init(testing.allocator);
defer stack.deinit();
try stack.push(1);
try stack.push(0);
try stack.push(0);
try stack.push(1);
try testing.expectEqual(@as(u1, 1), stack.peek());
try testing.expectEqual(@as(u1, 1), stack.pop());
try testing.expectEqual(@as(u1, 0), stack.peek());
try testing.expectEqual(@as(u1, 0), stack.pop());
try testing.expectEqual(@as(u1, 0), stack.pop());
try testing.expectEqual(@as(u1, 1), stack.pop());
}