mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-11 05:26:42 +08:00
build
This commit is contained in:
@@ -601,13 +601,7 @@ index = hash(key) % capacity
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="simple_hash.rb"
|
||||
=begin
|
||||
File: simple_hash.rb
|
||||
Created Time: 2024-04-14
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
# ## Аддитивное хеширование ###
|
||||
### Аддитивное хеширование ###
|
||||
def add_hash(key)
|
||||
hash = 0
|
||||
modulus = 1_000_000_007
|
||||
@@ -617,23 +611,7 @@ index = hash(key) % capacity
|
||||
hash % modulus
|
||||
end
|
||||
|
||||
=begin
|
||||
File: simple_hash.rb
|
||||
Created Time: 2024-04-14
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
# ## Аддитивное хеширование ###
|
||||
def add_hash(key)
|
||||
hash = 0
|
||||
modulus = 1_000_000_007
|
||||
|
||||
key.each_char { |c| hash += c.ord }
|
||||
|
||||
hash % modulus
|
||||
end
|
||||
|
||||
# ## Мультипликативное хеширование ###
|
||||
### Мультипликативное хеширование ###
|
||||
def mul_hash(key)
|
||||
hash = 0
|
||||
modulus = 1_000_000_007
|
||||
@@ -643,33 +621,7 @@ index = hash(key) % capacity
|
||||
hash % modulus
|
||||
end
|
||||
|
||||
=begin
|
||||
File: simple_hash.rb
|
||||
Created Time: 2024-04-14
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
# ## Аддитивное хеширование ###
|
||||
def add_hash(key)
|
||||
hash = 0
|
||||
modulus = 1_000_000_007
|
||||
|
||||
key.each_char { |c| hash += c.ord }
|
||||
|
||||
hash % modulus
|
||||
end
|
||||
|
||||
# ## Мультипликативное хеширование ###
|
||||
def mul_hash(key)
|
||||
hash = 0
|
||||
modulus = 1_000_000_007
|
||||
|
||||
key.each_char { |c| hash = 31 * hash + c.ord }
|
||||
|
||||
hash % modulus
|
||||
end
|
||||
|
||||
# ## XOR-хеширование ###
|
||||
### XOR-хеширование ###
|
||||
def xor_hash(key)
|
||||
hash = 0
|
||||
modulus = 1_000_000_007
|
||||
@@ -679,43 +631,7 @@ index = hash(key) % capacity
|
||||
hash % modulus
|
||||
end
|
||||
|
||||
=begin
|
||||
File: simple_hash.rb
|
||||
Created Time: 2024-04-14
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
# ## Аддитивное хеширование ###
|
||||
def add_hash(key)
|
||||
hash = 0
|
||||
modulus = 1_000_000_007
|
||||
|
||||
key.each_char { |c| hash += c.ord }
|
||||
|
||||
hash % modulus
|
||||
end
|
||||
|
||||
# ## Мультипликативное хеширование ###
|
||||
def mul_hash(key)
|
||||
hash = 0
|
||||
modulus = 1_000_000_007
|
||||
|
||||
key.each_char { |c| hash = 31 * hash + c.ord }
|
||||
|
||||
hash % modulus
|
||||
end
|
||||
|
||||
# ## XOR-хеширование ###
|
||||
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
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -1677,13 +1677,7 @@ index = hash(key) % capacity
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="array_hash_map.rb"
|
||||
=begin
|
||||
File: array_hash_map.rb
|
||||
Created Time: 2024-04-13
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
# ## Пара ключ-значение ###
|
||||
### Пара ключ-значение ###
|
||||
class Pair
|
||||
attr_accessor :key, :val
|
||||
|
||||
@@ -1693,36 +1687,20 @@ index = hash(key) % capacity
|
||||
end
|
||||
end
|
||||
|
||||
=begin
|
||||
File: array_hash_map.rb
|
||||
Created Time: 2024-04-13
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
# ## Пара ключ-значение ###
|
||||
class Pair
|
||||
attr_accessor :key, :val
|
||||
|
||||
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]
|
||||
@@ -1731,42 +1709,42 @@ index = hash(key) % capacity
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user