hashtable 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hashtable/hash_table.rb +3 -1
- data/lib/hashtable/sparse_bit_array.rb +2 -2
- data/lib/hashtable/traits.rb +84 -0
- data/lib/hashtable/version.rb +1 -1
- data/lib/hashtable.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5852fb24336d190f81a89d83eb6b23184f37972507641ac2e0b78c323a3ef65
|
4
|
+
data.tar.gz: a809dbacc1536fd55d18e30d677aba9fdbe331cdda5f2612942a394fa7bad5f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 044d20bcefe08d1c84d7a7fec897fc4d2f892b5a29c28e8f373ddb804039fc0679f9bb1b52992415e3c9cf8f0d62adf39ba42bd4deee7181422062b97916e01f
|
7
|
+
data.tar.gz: 47d1688da243dd2b5377f880bee64e7b91709b7b1efd0ff252d8a85849f5b5bfe155f68ffe780e8a87689e96e379849001c4757727a977d4b809faecb6cdd700
|
data/lib/hashtable/hash_table.rb
CHANGED
@@ -88,7 +88,7 @@ module HashTable
|
|
88
88
|
# @param [object] key
|
89
89
|
# @param [object] traits
|
90
90
|
def adds(keys, traits)
|
91
|
-
|
91
|
+
keys.map { |key| set_as_interal(key, traits) }
|
92
92
|
end
|
93
93
|
|
94
94
|
protected
|
@@ -135,6 +135,8 @@ module HashTable
|
|
135
135
|
|
136
136
|
bucket[1] = value unless value.nil?
|
137
137
|
end
|
138
|
+
return index if traits.respond_to?(:lookup_key_and_value)
|
139
|
+
|
138
140
|
bucket[0]
|
139
141
|
end
|
140
142
|
|
@@ -54,7 +54,7 @@ module HashTable
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def reset(index)
|
57
|
-
bits[index / BITWORD_SIZE] &= ~(1 << (index % BITWORD_SIZE))
|
57
|
+
@bits[index / BITWORD_SIZE] &= ~(1 << (index % BITWORD_SIZE))
|
58
58
|
end
|
59
59
|
|
60
60
|
# v = @bits[i]
|
@@ -274,7 +274,7 @@ module HashTable
|
|
274
274
|
@current_index = if element.index > index
|
275
275
|
@elements[0..element_i].rindex { |e| e.index <= index } || 0
|
276
276
|
else
|
277
|
-
@elements[element_i
|
277
|
+
@elements[element_i..].select { |e| e.index < index }.length + element_i
|
278
278
|
end
|
279
279
|
end
|
280
280
|
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# module HashTable
|
4
|
+
module HashTable
|
5
|
+
class Traits
|
6
|
+
def hash_lookup_key(key)
|
7
|
+
key
|
8
|
+
end
|
9
|
+
|
10
|
+
def lookup_key_to_storage_key(key)
|
11
|
+
key
|
12
|
+
end
|
13
|
+
|
14
|
+
def storage_key_to_lookup_key(key)
|
15
|
+
key
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class StringTraits
|
20
|
+
attr_reader :string_table, :string_index
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
@string_table = "\0"
|
24
|
+
@string_index = 1
|
25
|
+
end
|
26
|
+
|
27
|
+
def hash_lookup_key(key)
|
28
|
+
result = 0
|
29
|
+
key.each_byte { |byte| result += byte * 13 }
|
30
|
+
result
|
31
|
+
end
|
32
|
+
|
33
|
+
def lookup_key_to_storage_key(key)
|
34
|
+
@string_table += "#{key}\0"
|
35
|
+
old_si = @string_index
|
36
|
+
@string_index += key.length + 1
|
37
|
+
old_si
|
38
|
+
end
|
39
|
+
|
40
|
+
def storage_key_to_lookup_key(offset)
|
41
|
+
@string_table[offset..][/[^\0]+/]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class StringTraits2
|
46
|
+
attr_reader :string_table, :buckets, :hash_h
|
47
|
+
|
48
|
+
def initialize
|
49
|
+
@string_table = "\0"
|
50
|
+
@string_index = 1
|
51
|
+
@buckets = {}
|
52
|
+
@hash_h = {}
|
53
|
+
end
|
54
|
+
|
55
|
+
def hash_lookup_key(key)
|
56
|
+
return @hash_h[key] unless @hash_h[key].nil?
|
57
|
+
|
58
|
+
result = 0
|
59
|
+
key.each_byte { |byte| result += byte * 13 }
|
60
|
+
@hash_h[key] = result
|
61
|
+
result
|
62
|
+
end
|
63
|
+
|
64
|
+
def lookup_key_to_storage_key(key)
|
65
|
+
return @buckets[key] unless @buckets[key].nil?
|
66
|
+
|
67
|
+
@string_table += "#{key}\0"
|
68
|
+
old_si = @string_index
|
69
|
+
@string_index += key.length + 1
|
70
|
+
@buckets[key] = old_si
|
71
|
+
old_si
|
72
|
+
end
|
73
|
+
|
74
|
+
def lookup_key_and_value(key, value)
|
75
|
+
value.inject([@buckets[key]]) do |sum, v|
|
76
|
+
sum << lookup_key_to_storage_key(v)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def storage_key_to_lookup_key(offset)
|
81
|
+
@string_table[offset..][/[^\0]+/]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/lib/hashtable/version.rb
CHANGED
data/lib/hashtable.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashtable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cat1237
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-05-
|
11
|
+
date: 2022-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -65,6 +65,7 @@ files:
|
|
65
65
|
- lib/hashtable.rb
|
66
66
|
- lib/hashtable/hash_table.rb
|
67
67
|
- lib/hashtable/sparse_bit_array.rb
|
68
|
+
- lib/hashtable/traits.rb
|
68
69
|
- lib/hashtable/version.rb
|
69
70
|
homepage: https://github.com/Cat1237/hashtable.git
|
70
71
|
licenses:
|