mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-14 10:20:40 +08:00
Update array_hash_map
This commit is contained in:
@@ -21,7 +21,7 @@ const Entry = struct {
|
||||
// 基于数组简易实现的哈希表
|
||||
pub fn ArrayHashMap(comptime T: type) type {
|
||||
return struct {
|
||||
bucket: ?std.ArrayList(?T) = null,
|
||||
buckets: ?std.ArrayList(?T) = null,
|
||||
mem_allocator: std.mem.Allocator = undefined,
|
||||
|
||||
const Self = @This();
|
||||
@@ -30,16 +30,16 @@ pub fn ArrayHashMap(comptime T: type) type {
|
||||
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
||||
self.mem_allocator = allocator;
|
||||
// 初始化一个长度为 100 的桶(数组)
|
||||
self.bucket = std.ArrayList(?T).init(self.mem_allocator);
|
||||
self.buckets = std.ArrayList(?T).init(self.mem_allocator);
|
||||
var i: i32 = 0;
|
||||
while (i < 100) : (i += 1) {
|
||||
try self.bucket.?.append(null);
|
||||
try self.buckets.?.append(null);
|
||||
}
|
||||
}
|
||||
|
||||
// 析构方法
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.bucket != null) self.bucket.?.deinit();
|
||||
if (self.buckets != null) self.buckets.?.deinit();
|
||||
}
|
||||
|
||||
// 哈希函数
|
||||
@@ -51,7 +51,7 @@ pub fn ArrayHashMap(comptime T: type) type {
|
||||
// 查询操作
|
||||
pub fn get(self: *Self, key: usize) []const u8 {
|
||||
var index = hashFunc(key);
|
||||
var pair = self.bucket.?.items[index];
|
||||
var pair = self.buckets.?.items[index];
|
||||
return pair.?.val;
|
||||
}
|
||||
|
||||
@@ -59,20 +59,20 @@ pub fn ArrayHashMap(comptime T: type) type {
|
||||
pub fn put(self: *Self, key: usize, val: []const u8) !void {
|
||||
var pair = Entry.init(key, val);
|
||||
var index = hashFunc(key);
|
||||
self.bucket.?.items[index] = pair;
|
||||
self.buckets.?.items[index] = pair;
|
||||
}
|
||||
|
||||
// 删除操作
|
||||
pub fn remove(self: *Self, key: usize) !void {
|
||||
var index = hashFunc(key);
|
||||
// 置为 null ,代表删除
|
||||
self.bucket.?.items[index] = null;
|
||||
self.buckets.?.items[index] = null;
|
||||
}
|
||||
|
||||
// 获取所有键值对
|
||||
pub fn entrySet(self: *Self) !*std.ArrayList(T) {
|
||||
var entry_set = std.ArrayList(T).init(self.mem_allocator);
|
||||
for (self.bucket.?.items) |item| {
|
||||
for (self.buckets.?.items) |item| {
|
||||
if (item == null) continue;
|
||||
try entry_set.append(item.?);
|
||||
}
|
||||
@@ -82,7 +82,7 @@ pub fn ArrayHashMap(comptime T: type) type {
|
||||
// 获取所有键
|
||||
pub fn keySet(self: *Self) !*std.ArrayList(usize) {
|
||||
var key_set = std.ArrayList(usize).init(self.mem_allocator);
|
||||
for (self.bucket.?.items) |item| {
|
||||
for (self.buckets.?.items) |item| {
|
||||
if (item == null) continue;
|
||||
try key_set.append(item.?.key);
|
||||
}
|
||||
@@ -92,7 +92,7 @@ pub fn ArrayHashMap(comptime T: type) type {
|
||||
// 获取所有值
|
||||
pub fn valueSet(self: *Self) !*std.ArrayList([]const u8) {
|
||||
var value_set = std.ArrayList([]const u8).init(self.mem_allocator);
|
||||
for (self.bucket.?.items) |item| {
|
||||
for (self.buckets.?.items) |item| {
|
||||
if (item == null) continue;
|
||||
try value_set.append(item.?.val);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user