This commit is contained in:
krahets
2024-04-16 04:01:59 +08:00
parent 1bc9502c19
commit cdd8923e98
23 changed files with 1113 additions and 107 deletions

View File

@ -11,7 +11,7 @@ icon: material/graphql
In the journey of life, we are like individual nodes, connected by countless invisible edges.
Every encountering and parting leaves a unique mark on this vast network graph.
Each encounter and parting leaves a distinctive imprint on this vast network graph.
## Chapter contents

View File

@ -601,13 +601,45 @@ The design of hash algorithms is a complex issue that requires consideration of
=== "Ruby"
```ruby title="simple_hash.rb"
[class]{}-[func]{add_hash}
### 加法哈希 ###
def add_hash(key)
hash = 0
modulus = 1_000_000_007
[class]{}-[func]{mul_hash}
key.each_char { |c| hash += c.ord }
[class]{}-[func]{xor_hash}
hash % modulus
end
[class]{}-[func]{rot_hash}
### 乘法哈希 ###
def mul_hash(key)
hash = 0
modulus = 1_000_000_007
key.each_char { |c| hash = 31 * hash + c.ord }
hash % modulus
end
### 异或哈希 ###
def xor_hash(key)
hash = 0
modulus = 1_000_000_007
key.each_char { |c| hash ^= c.ord }
hash % modulus
end
### 旋转哈希 ###
def rot_hash(key)
hash = 0
modulus = 1_000_000_007
key.each_char { |c| hash = (hash << 4) ^ (hash >> 28) ^ c.ord }
hash % modulus
end
```
=== "Zig"

View File

@ -1426,7 +1426,99 @@ The code below provides a simple implementation of a separate chaining hash tabl
=== "Ruby"
```ruby title="hash_map_chaining.rb"
[class]{HashMapChaining}-[func]{}
### 键式地址哈希表 ###
class HashMapChaining
### 构造方法 ###
def initialize
@size = 0 # 键值对数量
@capacity = 4 # 哈希表容量
@load_thres = 2.0 / 3.0 # 触发扩容的负载因子阈值
@extend_ratio = 2 # 扩容倍数
@buckets = Array.new(@capacity) { [] } # 桶数组
end
### 哈希函数 ###
def hash_func(key)
key % @capacity
end
### 负载因子 ###
def load_factor
@size / @capacity
end
### 查询操作 ###
def get(key)
index = hash_func(key)
bucket = @buckets[index]
# 遍历桶,若找到 key ,则返回对应 val
for pair in bucket
return pair.val if pair.key == key
end
# 若未找到 key , 则返回 nil
nil
end
### 添加操作 ###
def put(key, val)
# 当负载因子超过阈值时,执行扩容
extend if load_factor > @load_thres
index = hash_func(key)
bucket = @buckets[index]
# 遍历桶,若遇到指定 key ,则更新对应 val 并返回
for pair in bucket
if pair.key == key
pair.val = val
return
end
end
# 若无该 key ,则将键值对添加至尾部
pair = Pair.new(key, val)
bucket << pair
@size += 1
end
### 删除操作 ###
def remove(key)
index = hash_func(key)
bucket = @buckets[index]
# 遍历桶,从中删除键值对
for pair in bucket
if pair.key == key
bucket.delete(pair)
@size -= 1
break
end
end
end
### 扩容哈希表 ###
def extend
# 暫存原哈希表
buckets = @buckets
# 初始化扩容后的新哈希表
@capacity *= @extend_ratio
@buckets = Array.new(@capacity) { [] }
@size = 0
# 将键值对从原哈希表搬运至新哈希表
for bucket in buckets
for pair in bucket
put(pair.key, pair.val)
end
end
end
### 打印哈希表 ###
def print
for bucket in @buckets
res = []
for pair in bucket
res << "#{pair.key} -> #{pair.val}"
end
pp res
end
end
end
```
=== "Zig"
@ -3086,7 +3178,118 @@ The code below implements an open addressing (linear probing) hash table with la
=== "Ruby"
```ruby title="hash_map_open_addressing.rb"
[class]{HashMapOpenAddressing}-[func]{}
### 开放寻址哈希表 ###
class HashMapOpenAddressing
TOMBSTONE = Pair.new(-1, '-1') # 删除标记
### 构造方法 ###
def initialize
@size = 0 # 键值对数量
@capacity = 4 # 哈希表容量
@load_thres = 2.0 / 3.0 # 触发扩容的负载因子阈值
@extend_ratio = 2 # 扩容倍数
@buckets = Array.new(@capacity) # 桶数组
end
### 哈希函数 ###
def hash_func(key)
key % @capacity
end
### 负载因子 ###
def load_factor
@size / @capacity
end
### 搜索 key 对应的桶索引 ###
def find_bucket(key)
index = hash_func(key)
first_tombstone = -1
# 线性探测,当遇到空桶时跳出
while !@buckets[index].nil?
# 若遇到 key ,返回对应的桶索引
if @buckets[index].key == key
# 若之前遇到了删除标记,则将键值对移动至该索引处
if first_tombstone != -1
@buckets[first_tombstone] = @buckets[index]
@buckets[index] = TOMBSTONE
return first_tombstone # 返回移动后的桶索引
end
return index # 返回桶索引
end
# 记录遇到的首个删除标记
first_tombstone = index if first_tombstone == -1 && @buckets[index] == TOMBSTONE
# 计算桶索引,越过尾部则返回头部
index = (index + 1) % @capacity
end
# 若 key 不存在,则返回添加点的索引
first_tombstone == -1 ? index : first_tombstone
end
### 查询操作 ###
def get(key)
# 搜索 key 对应的桶索引
index = find_bucket(key)
# 若找到键值对,则返回对应 val
return @buckets[index].val unless [nil, TOMBSTONE].include?(@buckets[index])
# 若键值对不存在,则返回 nil
nil
end
### 添加操作 ###
def put(key, val)
# 当负载因子超过阈值时,执行扩容
extend if load_factor > @load_thres
# 搜索 key 对应的桶索引
index = find_bucket(key)
# 若找到键值对,则覆盖 val 开返回
unless [nil, TOMBSTONE].include?(@buckets[index])
@buckets[index].val = val
return
end
# 若键值对不存在,则添加该键值对
@buckets[index] = Pair.new(key, val)
@size += 1
end
### 删除操作 ###
def remove(key)
# 搜索 key 对应的桶索引
index = find_bucket(key)
# 若找到键值对,则用删除标记覆盖它
unless [nil, TOMBSTONE].include?(@buckets[index])
@buckets[index] = TOMBSTONE
@size -= 1
end
end
### 扩容哈希表 ###
def extend
# 暂存原哈希表
buckets_tmp = @buckets
# 初始化扩容后的新哈希表
@capacity *= @extend_ratio
@buckets = Array.new(@capacity)
@size = 0
# 将键值对从原哈希表搬运至新哈希表
for pair in buckets_tmp
put(pair.key, pair.val) unless [nil, TOMBSTONE].include?(pair)
end
end
### 打印哈希表 ###
def print
for pair in @buckets
if pair.nil?
puts "Nil"
elsif pair == TOMBSTONE
puts "TOMBSTONE"
else
puts "#{pair.key} -> #{pair.val}"
end
end
end
end
```
=== "Zig"

View File

@ -1625,9 +1625,78 @@ The following code implements a simple hash table. Here, we encapsulate `key` an
=== "Ruby"
```ruby title="array_hash_map.rb"
[class]{Pair}-[func]{}
### 键值对 ###
class Pair
attr_accessor :key, :val
[class]{ArrayHashMap}-[func]{}
def initialize(key, val)
@key = key
@val = val
end
end
### 基于数组实现的哈希表 ###
class ArrayHashMap
### 构造方法 ###
def initialize
# 初始化数组,包含 100 个桶
@buckets = Array.new(100)
end
### 哈希函数 ###
def hash_func(key)
index = key % 100
end
### 查询操作 ###
def get(key)
index = hash_func(key)
pair = @buckets[index]
return if pair.nil?
pair.val
end
### 添加操作 ###
def put(key, val)
pair = Pair.new(key, val)
index = hash_func(key)
@buckets[index] = pair
end
### 删除操作 ###
def remove(key)
index = hash_func(key)
# 置为 nil ,代表删除
@buckets[index] = nil
end
### 获取所有键值对 ###
def entry_set
result = []
@buckets.each { |pair| result << pair unless pair.nil? }
result
end
### 获取所有键 ###
def key_set
result = []
@buckets.each { |pair| result << pair.key unless pair.nil? }
result
end
### 获取所有值 ###
def value_set
result = []
@buckets.each { |pair| result << pair.val unless pair.nil? }
result
end
### 打印哈希表 ###
def print
@buckets.each { |pair| puts "#{pair.key} -> #{pair.val}" unless pair.nil? }
end
end
```
=== "Zig"

View File

@ -408,7 +408,7 @@ The implementation code is as follows:
def is_empty(self) -> bool:
"""判断双向队列是否为空"""
return self.size() == 0
return self._size == 0
def push(self, num: int, is_front: bool):
"""入队操作"""

View File

@ -368,7 +368,7 @@ Below is the code for implementing a queue using a linked list:
def is_empty(self) -> bool:
"""判断队列是否为空"""
return not self._front
return self._size == 0
def push(self, num: int):
"""入队"""

View File

@ -365,7 +365,7 @@ Below is an example code for implementing a stack based on a linked list:
def is_empty(self) -> bool:
"""判断栈是否为空"""
return not self._peek
return self._size == 0
def push(self, val: int):
"""入栈"""
@ -1237,7 +1237,7 @@ Since the elements to be pushed onto the stack may continuously increase, we can
def is_empty(self) -> bool:
"""判断栈是否为空"""
return self._stack == []
return self._size == 0
def push(self, item: int):
"""入栈"""