mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-14 02:10:37 +08:00
build
This commit is contained in:
@@ -1212,7 +1212,7 @@ index = hash(key) % capacity
|
||||
key: usize = undefined,
|
||||
val: []const u8 = undefined,
|
||||
|
||||
pub fn init(key: usize, val: []const u8) Pair {
|
||||
pub fn init(key: usize, val: []const u8) Pair {
|
||||
return Pair {
|
||||
.key = key,
|
||||
.val = val,
|
||||
@@ -1223,25 +1223,25 @@ index = hash(key) % capacity
|
||||
// 基于数组简易实现的哈希表
|
||||
fn ArrayHashMap(comptime T: type) type {
|
||||
return struct {
|
||||
buckets: ?std.ArrayList(?T) = null,
|
||||
bucket: ?std.ArrayList(?T) = null,
|
||||
mem_allocator: std.mem.Allocator = undefined,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
// 构造方法
|
||||
// 构造函数
|
||||
pub fn init(self: *Self, allocator: std.mem.Allocator) !void {
|
||||
self.mem_allocator = allocator;
|
||||
// 初始化数组,包含 100 个桶
|
||||
self.buckets = std.ArrayList(?T).init(self.mem_allocator);
|
||||
// 初始化一个长度为 100 的桶(数组)
|
||||
self.bucket = std.ArrayList(?T).init(self.mem_allocator);
|
||||
var i: i32 = 0;
|
||||
while (i < 100) : (i += 1) {
|
||||
try self.buckets.?.append(null);
|
||||
try self.bucket.?.append(null);
|
||||
}
|
||||
}
|
||||
|
||||
// 析构方法
|
||||
// 析构函数
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.buckets != null) self.buckets.?.deinit();
|
||||
if (self.bucket != null) self.bucket.?.deinit();
|
||||
}
|
||||
|
||||
// 哈希函数
|
||||
@@ -1253,7 +1253,7 @@ index = hash(key) % capacity
|
||||
// 查询操作
|
||||
pub fn get(self: *Self, key: usize) []const u8 {
|
||||
var index = hashFunc(key);
|
||||
var pair = self.buckets.?.items[index];
|
||||
var pair = self.bucket.?.items[index];
|
||||
return pair.?.val;
|
||||
}
|
||||
|
||||
@@ -1261,44 +1261,44 @@ index = hash(key) % capacity
|
||||
pub fn put(self: *Self, key: usize, val: []const u8) !void {
|
||||
var pair = Pair.init(key, val);
|
||||
var index = hashFunc(key);
|
||||
self.buckets.?.items[index] = pair;
|
||||
self.bucket.?.items[index] = pair;
|
||||
}
|
||||
|
||||
// 删除操作
|
||||
pub fn remove(self: *Self, key: usize) !void {
|
||||
var index = hashFunc(key);
|
||||
// 置为 null ,代表删除
|
||||
self.buckets.?.items[index] = null;
|
||||
self.bucket.?.items[index] = null;
|
||||
}
|
||||
|
||||
// 获取所有键值对
|
||||
pub fn pairSet(self: *Self) !*std.ArrayList(T) {
|
||||
pub fn pairSet(self: *Self) !std.ArrayList(T) {
|
||||
var entry_set = std.ArrayList(T).init(self.mem_allocator);
|
||||
for (self.buckets.?.items) |item| {
|
||||
for (self.bucket.?.items) |item| {
|
||||
if (item == null) continue;
|
||||
try entry_set.append(item.?);
|
||||
}
|
||||
return &entry_set;
|
||||
return entry_set;
|
||||
}
|
||||
|
||||
// 获取所有键
|
||||
pub fn keySet(self: *Self) !*std.ArrayList(usize) {
|
||||
pub fn keySet(self: *Self) !std.ArrayList(usize) {
|
||||
var key_set = std.ArrayList(usize).init(self.mem_allocator);
|
||||
for (self.buckets.?.items) |item| {
|
||||
for (self.bucket.?.items) |item| {
|
||||
if (item == null) continue;
|
||||
try key_set.append(item.?.key);
|
||||
}
|
||||
return &key_set;
|
||||
return key_set;
|
||||
}
|
||||
|
||||
// 获取所有值
|
||||
pub fn valueSet(self: *Self) !*std.ArrayList([]const u8) {
|
||||
pub fn valueSet(self: *Self) !std.ArrayList([]const u8) {
|
||||
var value_set = std.ArrayList([]const u8).init(self.mem_allocator);
|
||||
for (self.buckets.?.items) |item| {
|
||||
for (self.bucket.?.items) |item| {
|
||||
if (item == null) continue;
|
||||
try value_set.append(item.?.val);
|
||||
}
|
||||
return &value_set;
|
||||
return value_set;
|
||||
}
|
||||
|
||||
// 打印哈希表
|
||||
@@ -1405,7 +1405,7 @@ index = hash(key) % capacity
|
||||
|
||||
## 6.1.3. 哈希冲突与扩容
|
||||
|
||||
本质上看,哈希函数的作用是黄输入空间(`key` 范围)映射到输出空间(数组索引范围),而输入空间往往远大于输出空间。因此,**理论上一定存在“多个输入对应相同输出”的情况**。
|
||||
本质上看,哈希函数的作用是将输入空间(`key` 范围)映射到输出空间(数组索引范围),而输入空间往往远大于输出空间。因此,**理论上一定存在“多个输入对应相同输出”的情况**。
|
||||
|
||||
对于上述示例中的哈希函数,当输入的 `key` 后两位相同时,哈希函数的输出结果也相同。例如,查询学号为 12836 和 20336 的两个学生时,我们得到:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user