std.buf_map.Allocator
别名
别名目标 std.mem.Allocator
std.buf_map.BufMap
容器
BufMap 在键和值进入映射前复制它们, 在它们被移除时释放。
字段
hash_map: BufMapHashMap
std.buf_map.BufMap.BufMapHashMap
类型
std.buf_map.BufMap.copy
函数
fn copy(self: BufMap, value: []const u8) ![]u8
参数
self: BufMap
value: []const u8
std.buf_map.BufMap.count
函数
返回映射中存储的键值对数量。
pub fn count(self: BufMap) BufMapHashMap.Size
参数
self: BufMap
std.buf_map.BufMap.deinit
函数
释放映射的后备存储,以及所有已存储的键和值。
pub fn deinit(self: *BufMap) void
参数
self: *BufMap
std.buf_map.BufMap.free
函数
fn free(self: BufMap, value: []const u8) void
参数
self: BufMap
value: []const u8
std.buf_map.BufMap.get
函数
返回映射持有的与某个键关联的 值的副本。若从映射移除此 键,返回的字符串将失效。
pub fn get(self: BufMap, key: []const u8) ?[]const u8
参数
self: BufMap
key: []const u8
std.buf_map.BufMap.getPtr
函数
查找与某个键关联的值的地址。 映射调整大小后,返回的指针失效。
pub fn getPtr(self: BufMap, key: []const u8) ?*[]const u8
参数
self: BufMap
key: []const u8
std.buf_map.BufMap.init
函数
创建由指定分配器支持的 BufMap。 该分配器用于底层分配 及字符串去重。
pub fn init(allocator: Allocator) BufMap
参数
allocator: Allocator
std.buf_map.BufMap.iterator
函数
返回遍历映射条目的迭代器。
pub fn iterator(self: *const BufMap) BufMapHashMap.Iterator
参数
self: *const BufMap
std.buf_map.BufMap.put
函数
将 key 和 value 复制到 BufMap。
pub fn put(self: *BufMap, key: []const u8, value: []const u8) !void
参数
std.buf_map.BufMap.putMove
函数
与 put 相同,但键和值的所有权转移给 BufMap,
而不是复制。
若 putMove 失败,键和值的所有权不转移。
pub fn putMove(self: *BufMap, key: []u8, value: []u8) !void
参数
std.buf_map.BufMap.remove
函数
从映射中移除项并释放其值。 此操作使 get() 为该键返回的值失效。
pub fn remove(self: *BufMap, key: []const u8) void
参数
self: *BufMap
key: []const u8
std.buf_map.mem
别名
别名目标 std.mem
std.buf_map.std
别名
别名目标 std
std.buf_map.StringHashMap
别名
别名目标 std.StringHashMap
std.buf_map.testing
别名
别名目标 std.testing
源码
std/buf_map.zig
const std = @import("std.zig");
const StringHashMap = std.StringHashMap;
const mem = std.mem;
const Allocator = mem.Allocator;
const testing = std.testing;
/// BufMap 在键和值进入映射前复制它们,
/// 在它们被移除时释放。
pub const BufMap = struct {
hash_map: BufMapHashMap,
const BufMapHashMap = StringHashMap([]const u8);
/// 创建由指定分配器支持的 BufMap。
/// 该分配器用于底层分配
/// 及字符串去重。
pub fn init(allocator: Allocator) BufMap {
return .{ .hash_map = BufMapHashMap.init(allocator) };
}
/// 释放映射的后备存储,以及所有已存储的键和值。
///
pub fn deinit(self: *BufMap) void {
var it = self.hash_map.iterator();
while (it.next()) |entry| {
self.free(entry.key_ptr.*);
self.free(entry.value_ptr.*);
}
self.hash_map.deinit();
}
/// 与 `put` 相同,但键和值的所有权转移给 BufMap,
/// 而不是复制。
/// 若 `putMove` 失败,键和值的所有权不转移。
pub fn putMove(self: *BufMap, key: []u8, value: []u8) !void {
const get_or_put = try self.hash_map.getOrPut(key);
if (get_or_put.found_existing) {
self.free(get_or_put.key_ptr.*);
self.free(get_or_put.value_ptr.*);
get_or_put.key_ptr.* = key;
}
get_or_put.value_ptr.* = value;
}
/// 将 `key` 和 `value` 复制到 BufMap。
pub fn put(self: *BufMap, key: []const u8, value: []const u8) !void {
const value_copy = try self.copy(value);
errdefer self.free(value_copy);
const get_or_put = try self.hash_map.getOrPut(key);
if (get_or_put.found_existing) {
self.free(get_or_put.value_ptr.*);
} else {
get_or_put.key_ptr.* = self.copy(key) catch |err| {
_ = self.hash_map.remove(key);
return err;
};
}
get_or_put.value_ptr.* = value_copy;
}
/// 查找与某个键关联的值的地址。
/// 映射调整大小后,返回的指针失效。
pub fn getPtr(self: BufMap, key: []const u8) ?*[]const u8 {
return self.hash_map.getPtr(key);
}
/// 返回映射持有的与某个键关联的
/// 值的副本。若从映射移除此
/// 键,返回的字符串将失效。
pub fn get(self: BufMap, key: []const u8) ?[]const u8 {
return self.hash_map.get(key);
}
/// 从映射中移除项并释放其值。
/// 此操作使 get() 为该键返回的值失效。
pub fn remove(self: *BufMap, key: []const u8) void {
const kv = self.hash_map.fetchRemove(key) orelse return;
self.free(kv.key);
self.free(kv.value);
}
/// 返回映射中存储的键值对数量。
pub fn count(self: BufMap) BufMapHashMap.Size {
return self.hash_map.count();
}
/// 返回遍历映射条目的迭代器。
pub fn iterator(self: *const BufMap) BufMapHashMap.Iterator {
return self.hash_map.iterator();
}
fn free(self: BufMap, value: []const u8) void {
self.hash_map.allocator.free(value);
}
fn copy(self: BufMap, value: []const u8) ![]u8 {
return self.hash_map.allocator.dupe(u8, value);
}
};
test "BufMap" {
const allocator = std.testing.allocator;
var bufmap = BufMap.init(allocator);
defer bufmap.deinit();
try bufmap.put("x", "1");
try testing.expect(mem.eql(u8, bufmap.get("x").?, "1"));
try testing.expect(1 == bufmap.count());
try bufmap.put("x", "2");
try testing.expect(mem.eql(u8, bufmap.get("x").?, "2"));
try testing.expect(1 == bufmap.count());
try bufmap.put("x", "3");
try testing.expect(mem.eql(u8, bufmap.get("x").?, "3"));
try testing.expect(1 == bufmap.count());
bufmap.remove("x");
try testing.expect(0 == bufmap.count());
try bufmap.putMove(try allocator.dupe(u8, "k"), try allocator.dupe(u8, "v1"));
try bufmap.putMove(try allocator.dupe(u8, "k"), try allocator.dupe(u8, "v2"));
}