跳转到内容
标准库Zig 0.17.0官方原文
源码std/buf_set.zig

第 1 行

std.buf_set.Allocator

别名

第 4 行

别名目标 std.mem.Allocator

std.buf_set.BufSet

容器

第 10 行

BufSet 是字符串集合。BufSet 在内部复制字符串, 绝不接管传入字符串的所有权。

字段

hash_map: BufSetHashMap

std.buf_set.BufSet.allocator

函数

第 69 行

获取此集合使用的分配器

pub fn allocator(self: *const BufSet) Allocator

参数

self: *const BufSet

std.buf_set.BufSet.BufSetHashMap

类型

第 13 行

std.buf_set.BufSet.clone

函数

第 89 行

使用相同分配器创建此 BufSet 的副本。

pub fn clone(self: *const BufSet) Allocator.Error!BufSet

参数

self: *const BufSet

错误

OutOfMemory

用法示例

test clone {
    var original = BufSet.init(testing.allocator);
    defer original.deinit();
    try original.insert("x");

    var cloned = try original.clone();
    defer cloned.deinit();
    cloned.remove("x");
    try testing.expect(original.count() == 1);
    try testing.expect(cloned.count() == 0);

    try testing.expectError(
        error.OutOfMemory,
        original.cloneWithAllocator(testing.failing_allocator),
    );
}

std.buf_set.BufSet.cloneWithAllocator

函数

第 74 行

使用指定分配器创建此 BufSet 的副本。

pub fn cloneWithAllocator( self: *const BufSet, new_allocator: Allocator, ) Allocator.Error!BufSet

参数

self: *const BufSet
new_allocator: Allocator

错误

OutOfMemory

std.buf_set.BufSet.contains

函数

第 47 行

检查集合是否包含与传入字符串匹配的项。

pub fn contains(self: BufSet, value: []const u8) bool

参数

self: BufSet
value: []const u8

std.buf_set.BufSet.copy

函数

第 114 行

fn copy(self: *const BufSet, value: []const u8) ![]const u8

参数

self: *const BufSet
value: []const u8

std.buf_set.BufSet.count

函数

第 58 行

返回集合中存储的元素数

pub fn count(self: *const BufSet) usize

参数

self: *const BufSet

std.buf_set.BufSet.deinit

函数

第 24 行

释放 BufSet 及其所有已存储键。

pub fn deinit(self: *BufSet) void

参数

self: *BufSet

std.buf_set.BufSet.free

函数

第 110 行

fn free(self: *const BufSet, value: []const u8) void

参数

self: *const BufSet
value: []const u8

std.buf_set.BufSet.init

函数

第 19 行

使用分配器创建 BufSet。该分配器在内部 用于底层内存分配和 字符串复制。

pub fn init(a: Allocator) BufSet

参数

std.buf_set.BufSet.insert

函数

第 36 行

向 BufSet 插入元素。该元素会被复制, 因此调用者可立即删除或复用 所传字符串。

pub fn insert(self: *BufSet, value: []const u8) !void

参数

self: *BufSet
value: []const u8

std.buf_set.BufSet.iterator

函数

第 64 行

返回遍历集合中元素的迭代器。 迭代顺序任意。

pub fn iterator(self: *const BufSet) Iterator

参数

self: *const BufSet

std.buf_set.BufSet.Iterator

常量

第 14 行

std.buf_set.BufSet.remove

函数

第 52 行

从集合中移除条目。

pub fn remove(self: *BufSet, value: []const u8) void

参数

self: *BufSet
value: []const u8

std.buf_set.mem

别名

第 3 行

别名目标 std.mem

std.buf_set.std

别名

第 1 行

别名目标 std

std.buf_set.StringHashMap

别名

第 2 行

别名目标 std.StringHashMap

std.buf_set.testing

别名

第 5 行

别名目标 std.testing

源码

std/buf_set.zig

const std = @import("std.zig");
const StringHashMap = std.StringHashMap;
const mem = @import("mem.zig");
const Allocator = mem.Allocator;
const testing = std.testing;

/// BufSet 是字符串集合。BufSet 在内部复制字符串,
/// 绝不接管传入字符串的所有权。
///
pub const BufSet = struct {
    hash_map: BufSetHashMap,

    const BufSetHashMap = StringHashMap(void);
    pub const Iterator = BufSetHashMap.KeyIterator;

    /// 使用分配器创建 BufSet。该分配器在内部
    /// 用于底层内存分配和
    /// 字符串复制。
    pub fn init(a: Allocator) BufSet {
        return .{ .hash_map = BufSetHashMap.init(a) };
    }

    /// 释放 BufSet 及其所有已存储键。
    pub fn deinit(self: *BufSet) void {
        var it = self.hash_map.keyIterator();
        while (it.next()) |key_ptr| {
            self.free(key_ptr.*);
        }
        self.hash_map.deinit();
        self.* = undefined;
    }

    /// 向 BufSet 插入元素。该元素会被复制,
    /// 因此调用者可立即删除或复用
    /// 所传字符串。
    pub fn insert(self: *BufSet, value: []const u8) !void {
        const gop = try self.hash_map.getOrPut(value);
        if (!gop.found_existing) {
            gop.key_ptr.* = self.copy(value) catch |err| {
                _ = self.hash_map.remove(value);
                return err;
            };
        }
    }

    /// 检查集合是否包含与传入字符串匹配的项。
    pub fn contains(self: BufSet, value: []const u8) bool {
        return self.hash_map.contains(value);
    }

    /// 从集合中移除条目。
    pub fn remove(self: *BufSet, value: []const u8) void {
        const kv = self.hash_map.fetchRemove(value) orelse return;
        self.free(kv.key);
    }

    /// 返回集合中存储的元素数
    pub fn count(self: *const BufSet) usize {
        return self.hash_map.count();
    }

    /// 返回遍历集合中元素的迭代器。
    /// 迭代顺序任意。
    pub fn iterator(self: *const BufSet) Iterator {
        return self.hash_map.keyIterator();
    }

    /// 获取此集合使用的分配器
    pub fn allocator(self: *const BufSet) Allocator {
        return self.hash_map.allocator;
    }

    /// 使用指定分配器创建此 BufSet 的副本。
    pub fn cloneWithAllocator(
        self: *const BufSet,
        new_allocator: Allocator,
    ) Allocator.Error!BufSet {
        const cloned_hashmap = try self.hash_map.cloneWithAllocator(new_allocator);
        const cloned = BufSet{ .hash_map = cloned_hashmap };
        var it = cloned.hash_map.keyIterator();
        while (it.next()) |key_ptr| {
            key_ptr.* = try cloned.copy(key_ptr.*);
        }

        return cloned;
    }

    /// 使用相同分配器创建此 BufSet 的副本。
    pub fn clone(self: *const BufSet) Allocator.Error!BufSet {
        return self.cloneWithAllocator(self.allocator());
    }

    test clone {
        var original = BufSet.init(testing.allocator);
        defer original.deinit();
        try original.insert("x");

        var cloned = try original.clone();
        defer cloned.deinit();
        cloned.remove("x");
        try testing.expect(original.count() == 1);
        try testing.expect(cloned.count() == 0);

        try testing.expectError(
            error.OutOfMemory,
            original.cloneWithAllocator(testing.failing_allocator),
        );
    }

    fn free(self: *const BufSet, value: []const u8) void {
        self.hash_map.allocator.free(value);
    }

    fn copy(self: *const BufSet, value: []const u8) ![]const u8 {
        const result = try self.hash_map.allocator.alloc(u8, value.len);
        @memcpy(result, value);
        return result;
    }
};

test BufSet {
    var bufset = BufSet.init(std.testing.allocator);
    defer bufset.deinit();

    try bufset.insert("x");
    try testing.expect(bufset.count() == 1);
    bufset.remove("x");
    try testing.expect(bufset.count() == 0);

    try bufset.insert("x");
    try bufset.insert("y");
    try bufset.insert("z");
}

test "clone with arena" {
    const allocator = std.testing.allocator;
    var arena = std.heap.ArenaAllocator.init(allocator);
    defer arena.deinit();

    var buf = BufSet.init(allocator);
    defer buf.deinit();
    try buf.insert("member1");
    try buf.insert("member2");

    _ = try buf.cloneWithAllocator(arena.allocator());
}