data_structures_101 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33887e0ad362811a5165c4bcf889cd5610a17101
|
4
|
+
data.tar.gz: b9d2a978fcefe7f67883fef2235b26011cf801c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d4b356091a218b458ecf8286bec17a4005f2827856d0dbda09bd70587db7154ef87fa491f1084411cfd2f3d8e768dd897748d09f0e52285eff5663977cde6885
|
7
|
+
data.tar.gz: 9becb8117223f6220887774f6872a980d825ff4042aa9bd02fe238e2e86b228d4098c0c7b710dfe8b9701748db427815924add4a482c03f6af758212ba0b4c21
|
@@ -3,20 +3,20 @@ module DataStructures101
|
|
3
3
|
class BaseHashTable
|
4
4
|
include Enumerable
|
5
5
|
|
6
|
-
attr_reader :size, :
|
6
|
+
attr_reader :size, :compression_lambda, :capacity
|
7
7
|
|
8
|
-
def initialize(capacity:, prime:,
|
8
|
+
def initialize(capacity:, prime:, compression_lambda:)
|
9
9
|
@capacity = capacity
|
10
10
|
@size = 0
|
11
11
|
@table = Array.new(@capacity)
|
12
12
|
|
13
|
-
@
|
13
|
+
@compression_lambda = compression_lambda
|
14
14
|
|
15
|
-
if @
|
15
|
+
if @compression_lambda.nil?
|
16
16
|
random = Random.new
|
17
17
|
scale = random.rand(prime - 1) + 1
|
18
18
|
shift = random.rand(prime)
|
19
|
-
@
|
19
|
+
@compression_lambda = ->(key, cap) { return (((key.hash * scale + shift) % prime) % cap).abs }
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -25,7 +25,7 @@ module DataStructures101
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def insert(key, value)
|
28
|
-
old_value = bucket_insert(
|
28
|
+
old_value = bucket_insert(compression_lambda.call(key, @capacity), key, value)
|
29
29
|
|
30
30
|
# keep load factor <= 0.5
|
31
31
|
resize(new_capacity) if @size > @capacity / 2
|
@@ -34,11 +34,11 @@ module DataStructures101
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def [](key)
|
37
|
-
bucket_find(
|
37
|
+
bucket_find(compression_lambda.call(key, @capacity), key)
|
38
38
|
end
|
39
39
|
|
40
40
|
def delete(key)
|
41
|
-
bucket_delete(
|
41
|
+
bucket_delete(compression_lambda.call(key, @capacity), key)
|
42
42
|
end
|
43
43
|
|
44
44
|
def each
|
@@ -5,8 +5,8 @@ module DataStructures101
|
|
5
5
|
|
6
6
|
attr_reader :probe_lambda
|
7
7
|
|
8
|
-
def initialize(capacity: 31, prime: 109345121,
|
9
|
-
super(capacity: capacity, prime: prime,
|
8
|
+
def initialize(capacity: 31, prime: 109345121, compression_lambda: nil, probe_lambda: nil)
|
9
|
+
super(capacity: capacity, prime: prime, compression_lambda: compression_lambda)
|
10
10
|
|
11
11
|
@probe_lambda = if probe_lambda.nil?
|
12
12
|
->(h, i) { return (h + i) % @capacity }
|