hamster 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +4 -0
- data/lib/hamster/hash.rb +3 -1
- data/lib/hamster/set.rb +3 -1
- data/lib/hamster/trie.rb +15 -11
- data/lib/hamster/version.rb +1 -1
- data/spec/hamster/hash/eql_spec.rb +10 -8
- metadata +2 -2
data/History.rdoc
CHANGED
data/lib/hamster/hash.rb
CHANGED
@@ -54,7 +54,9 @@ module Hamster
|
|
54
54
|
|
55
55
|
# Returns <tt>true</tt> if . <tt>eql?</tt> is synonymous with <tt>==</tt>
|
56
56
|
def eql?(other)
|
57
|
-
|
57
|
+
return true if equal?(other)
|
58
|
+
return false unless self.class.equal?(other.class)
|
59
|
+
return @trie.eql?(other.instance_eval{@trie})
|
58
60
|
end
|
59
61
|
alias :== :eql?
|
60
62
|
|
data/lib/hamster/set.rb
CHANGED
@@ -50,7 +50,9 @@ module Hamster
|
|
50
50
|
|
51
51
|
# Returns <tt>true</tt> if . <tt>eql?</tt> is synonymous with <tt>==</tt>
|
52
52
|
def eql?(other)
|
53
|
-
|
53
|
+
return true if equal?(other)
|
54
|
+
return false unless self.class.equal?(other.class)
|
55
|
+
return @trie.eql?(other.instance_eval{@trie})
|
54
56
|
end
|
55
57
|
alias :== :eql?
|
56
58
|
|
data/lib/hamster/trie.rb
CHANGED
@@ -37,7 +37,7 @@ module Hamster
|
|
37
37
|
def put(key, value)
|
38
38
|
index = index_for(key)
|
39
39
|
entry = @entries[index]
|
40
|
-
if !entry || entry.
|
40
|
+
if !entry || entry.key.eql?(key)
|
41
41
|
entries = @entries.dup
|
42
42
|
entries[index] = Entry.new(key, value)
|
43
43
|
self.class.new(@significant_bits, entries, @children)
|
@@ -57,7 +57,7 @@ module Hamster
|
|
57
57
|
def get(key)
|
58
58
|
index = index_for(key)
|
59
59
|
entry = @entries[index]
|
60
|
-
if entry && entry.
|
60
|
+
if entry && entry.key.eql?(key)
|
61
61
|
entry
|
62
62
|
else
|
63
63
|
child = @children[index]
|
@@ -69,12 +69,20 @@ module Hamster
|
|
69
69
|
|
70
70
|
# Returns a copy of <tt>self</tt> with the given key (and associated value) removed. If not found, returns <tt>self</tt>.
|
71
71
|
def remove(key)
|
72
|
-
|
72
|
+
find_and_remove(key) || Trie.new(@significant_bits)
|
73
|
+
end
|
74
|
+
|
75
|
+
def include?(key, value)
|
76
|
+
entry = get(key)
|
77
|
+
entry && value.eql?(entry.value)
|
73
78
|
end
|
74
79
|
|
75
80
|
# Returns <tt>true</tt> if . <tt>eql?</tt> is synonymous with <tt>==</tt>
|
76
81
|
def eql?(other)
|
77
|
-
|
82
|
+
return true if equal?(other)
|
83
|
+
return false unless self.class.equal?(other.class) && self.size == other.size
|
84
|
+
each { |entry| return false unless other.include?(entry.key, entry.value) }
|
85
|
+
true
|
78
86
|
end
|
79
87
|
alias :== :eql?
|
80
88
|
|
@@ -89,15 +97,15 @@ module Hamster
|
|
89
97
|
# Returns a replacement instance after removing the specified key.
|
90
98
|
# If not found, returns <tt>self</tt>.
|
91
99
|
# If empty, returns <tt>nil</tt>.
|
92
|
-
def
|
100
|
+
def find_and_remove(key)
|
93
101
|
index = index_for(key)
|
94
102
|
entry = @entries[index]
|
95
|
-
if entry && entry.
|
103
|
+
if entry && entry.key.eql?(key)
|
96
104
|
return remove_at(index)
|
97
105
|
else
|
98
106
|
child = @children[index]
|
99
107
|
if child
|
100
|
-
copy = child.
|
108
|
+
copy = child.find_and_remove(key)
|
101
109
|
if !copy.equal?(child)
|
102
110
|
children = @children.dup
|
103
111
|
children[index] = copy
|
@@ -141,10 +149,6 @@ module Hamster
|
|
141
149
|
@value = value
|
142
150
|
end
|
143
151
|
|
144
|
-
def key_eql?(key)
|
145
|
-
@key.eql?(key)
|
146
|
-
end
|
147
|
-
|
148
152
|
end
|
149
153
|
|
150
154
|
end
|
data/lib/hamster/version.rb
CHANGED
@@ -10,20 +10,22 @@ describe Hamster::Hash do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
it "is true for two empty instances" do
|
13
|
-
|
14
|
-
Hamster::Hash.new.should eql(Hamster::Hash.new)
|
15
|
-
end
|
13
|
+
Hamster::Hash.new.should eql(Hamster::Hash.new)
|
16
14
|
end
|
17
15
|
|
18
16
|
it "is true for two instances with the same key/value pairs" do
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
a.should eql(b)
|
23
|
-
end
|
17
|
+
a = Hamster::Hash.new.put("a", "Aye").put("b", "Bee").put("c", "See")
|
18
|
+
b = Hamster::Hash.new.put("a", "Aye").put("b", "Bee").put("c", "See")
|
19
|
+
a.should eql(b)
|
24
20
|
end
|
25
21
|
|
26
22
|
it "is false for two instances with different key/value pairs" do
|
23
|
+
a = Hamster::Hash.new.put("a", "Aye").put("b", "Bee").put("c", "See")
|
24
|
+
b = Hamster::Hash.new.put("a", "Aye").put("b", "Bee").put("d", "Dee")
|
25
|
+
a.should_not eql(b)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "is false for two instances with different numbers of overlapping key/value pairs" do
|
27
29
|
a = Hamster::Hash.new.put("a", "Aye").put("b", "Bee").put("c", "See")
|
28
30
|
b = Hamster::Hash.new.put("a", "Aye").put("b", "Bee")
|
29
31
|
a.should_not eql(b)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hamster
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simon Harris
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-28 00:00:00 +11:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|