This commit is contained in:
krahets
2026-03-29 05:06:58 +08:00
parent 37523d4ceb
commit 967293c421
51 changed files with 288 additions and 5073 deletions
+19 -35
View File
@@ -1422,17 +1422,9 @@ comments: true
=== "Ruby"
```ruby title="hash_map_chaining.rb"
=begin
File: hash_map_chaining.rb
Created Time: 2024-04-13
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative './array_hash_map'
# ## Хеш-таблица с цепочками ###
### Хеш-таблица с цепочками ###
class HashMapChaining
# ## Конструктор ###
### Конструктор ###
def initialize
@size = 0 # Число пар ключ-значение
@capacity = 4 # Вместимость хеш-таблицы
@@ -1441,17 +1433,17 @@ comments: true
@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]
@@ -1463,7 +1455,7 @@ comments: true
nil
end
# ## Операция добавления ###
### Операция добавления ###
def put(key, val)
# Когда коэффициент загрузки превышает порог, выполнить расширение
extend if load_factor > @load_thres
@@ -1482,7 +1474,7 @@ comments: true
@size += 1
end
# ## Операция удаления ###
### Операция удаления ###
def remove(key)
index = hash_func(key)
bucket = @buckets[index]
@@ -1496,7 +1488,7 @@ comments: true
end
end
# ## Расширение хеш-таблицы ###
### Расширение хеш-таблицы ###
def extend
# Временно сохранить исходную хеш-таблицу
buckets = @buckets
@@ -1512,7 +1504,7 @@ comments: true
end
end
# ## Вывести хеш-таблицу ###
### Вывести хеш-таблицу ###
def print
for bucket in @buckets
res = []
@@ -3176,19 +3168,11 @@ comments: true
=== "Ruby"
```ruby title="hash_map_open_addressing.rb"
=begin
File: hash_map_open_addressing.rb
Created Time: 2024-04-13
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative './array_hash_map'
# ## Хеш-таблица с открытой адресацией ###
### Хеш-таблица с открытой адресацией ###
class HashMapOpenAddressing
TOMBSTONE = Pair.new(-1, '-1') # Удалить метку
# ## Конструктор ###
### Конструктор ###
def initialize
@size = 0 # Число пар ключ-значение
@capacity = 4 # Вместимость хеш-таблицы
@@ -3197,17 +3181,17 @@ comments: true
@buckets = Array.new(@capacity) # Массив корзин
end
# ## Хеш-функция ###
### Хеш-функция ###
def hash_func(key)
key % @capacity
end
# ## Коэффициент загрузки ###
### Коэффициент загрузки ###
def load_factor
@size / @capacity
end
# ## Найти индекс корзины, соответствующий key ###
### Найти индекс корзины, соответствующий key ###
def find_bucket(key)
index = hash_func(key)
first_tombstone = -1
@@ -3232,7 +3216,7 @@ comments: true
first_tombstone == -1 ? index : first_tombstone
end
# ## Операция поиска ###
### Операция поиска ###
def get(key)
# Найти индекс корзины, соответствующий key
index = find_bucket(key)
@@ -3242,7 +3226,7 @@ comments: true
nil
end
# ## Операция добавления ###
### Операция добавления ###
def put(key, val)
# Когда коэффициент загрузки превышает порог, выполнить расширение
extend if load_factor > @load_thres
@@ -3258,7 +3242,7 @@ comments: true
@size += 1
end
# ## Операция удаления ###
### Операция удаления ###
def remove(key)
# Найти индекс корзины, соответствующий key
index = find_bucket(key)
@@ -3269,7 +3253,7 @@ comments: true
end
end
# ## Расширение хеш-таблицы ###
### Расширение хеш-таблицы ###
def extend
# Временно сохранить исходную хеш-таблицу
buckets_tmp = @buckets
@@ -3283,7 +3267,7 @@ comments: true
end
end
# ## Вывести хеш-таблицу ###
### Вывести хеш-таблицу ###
def print
for pair in @buckets
if pair.nil?