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: b706e2d85061322d3acfdca760293f4f6a56029a
4
- data.tar.gz: 1dbc8f2d1cf9652d05b2b78e7dc3658dece7ea5e
3
+ metadata.gz: 33887e0ad362811a5165c4bcf889cd5610a17101
4
+ data.tar.gz: b9d2a978fcefe7f67883fef2235b26011cf801c0
5
5
  SHA512:
6
- metadata.gz: 553538201c8c359742f0432c14cea0be9e4e03e12b114921867f26be8765978ca1a1a336d61693704a583674f7d27b3114a566e5f4650f84b3826266b0f0c167
7
- data.tar.gz: 4839ef8d7196f56935220e891fc3f977c49bd28ce47ee52977e4c8ee3fecc3c62e87eedda4c6cefa4a4c1e5e4669855885d5a2113214cc00c0764d474689fb18
6
+ metadata.gz: d4b356091a218b458ecf8286bec17a4005f2827856d0dbda09bd70587db7154ef87fa491f1084411cfd2f3d8e768dd897748d09f0e52285eff5663977cde6885
7
+ data.tar.gz: 9becb8117223f6220887774f6872a980d825ff4042aa9bd02fe238e2e86b228d4098c0c7b710dfe8b9701748db427815924add4a482c03f6af758212ba0b4c21
@@ -2,7 +2,7 @@ module DataStructures101
2
2
 
3
3
  class ChainedHashTable < Hash::BaseHashTable
4
4
 
5
- def initialize(capacity: 31, prime: 109345121, hash_lambda: nil)
5
+ def initialize(capacity: 31, prime: 109345121, compression_lambda: nil)
6
6
  super
7
7
  end
8
8
 
@@ -3,20 +3,20 @@ module DataStructures101
3
3
  class BaseHashTable
4
4
  include Enumerable
5
5
 
6
- attr_reader :size, :hash_lambda, :capacity
6
+ attr_reader :size, :compression_lambda, :capacity
7
7
 
8
- def initialize(capacity:, prime:, hash_lambda:)
8
+ def initialize(capacity:, prime:, compression_lambda:)
9
9
  @capacity = capacity
10
10
  @size = 0
11
11
  @table = Array.new(@capacity)
12
12
 
13
- @hash_lambda = hash_lambda
13
+ @compression_lambda = compression_lambda
14
14
 
15
- if @hash_lambda.nil?
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
- @hash_lambda = ->(key) { return (((key.hash * scale + shift) % prime) % @capacity).abs }
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(hash_lambda.call(key), key, value)
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(hash_lambda.call(key), key)
37
+ bucket_find(compression_lambda.call(key, @capacity), key)
38
38
  end
39
39
 
40
40
  def delete(key)
41
- bucket_delete(hash_lambda.call(key), key)
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, hash_lambda: nil, probe_lambda: nil)
9
- super(capacity: capacity, prime: prime, hash_lambda: hash_lambda)
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 }
@@ -1,3 +1,3 @@
1
1
  module DataStructures101
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_structures_101
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - aegis