Skip to content
This repository was archived by the owner on Mar 15, 2022. It is now read-only.

Commit 6d4d69c

Browse files
committed
Merge pull request #9 from ianunruh/master
Added ability to check the size of a reference map
2 parents 3766879 + 36d3ede commit 6d4d69c

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

lib/ref/abstract_reference_key_map.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def [](key)
3232
@values[rkey] if rkey
3333
end
3434

35+
alias_method :get, :[]
36+
3537
# Add a key/value to the map.
3638
def []=(key, value)
3739
ObjectSpace.define_finalizer(key, @reference_cleanup)
@@ -41,6 +43,8 @@ def []=(key, value)
4143
end
4244
end
4345

46+
alias_method :put, :[]=
47+
4448
# Remove the value associated with the key from the map.
4549
def delete(key)
4650
rkey = ref_key(key)
@@ -88,6 +92,24 @@ def merge!(other_hash)
8892
end
8993
end
9094

95+
# The number of entries in the map
96+
def size
97+
@references_to_keys_map.count do |_, ref|
98+
ref.object
99+
end
100+
end
101+
102+
alias_method :length, :size
103+
104+
# True if there are entries that exist in the map
105+
def empty?
106+
@references_to_keys_map.each do |_, ref|
107+
return false if ref.object
108+
end
109+
110+
true
111+
end
112+
91113
def inspect
92114
live_entries = {}
93115
each do |key, value|

lib/ref/abstract_reference_value_map.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def [](key)
3333
value
3434
end
3535

36+
alias_method :get, :[]
37+
3638
# Add a key/value to the map.
3739
def []=(key, value)
3840
ObjectSpace.define_finalizer(value, @reference_cleanup)
@@ -49,6 +51,8 @@ def []=(key, value)
4951
value
5052
end
5153

54+
alias_method :put, :[]=
55+
5256
# Remove the entry associated with the key from the map.
5357
def delete(key)
5458
ref = @references.delete(key)
@@ -102,6 +106,24 @@ def merge!(other_hash)
102106
end
103107
end
104108

109+
# The number of entries in the map
110+
def size
111+
@references.count do |_, ref|
112+
ref.object
113+
end
114+
end
115+
116+
alias_method :length, :size
117+
118+
# True if there are entries that exist in the map
119+
def empty?
120+
@references.each do |_, ref|
121+
return false if ref.object
122+
end
123+
124+
true
125+
end
126+
105127
def inspect
106128
live_entries = {}
107129
each do |key, value|

test/reference_key_map_behavior.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,23 @@ def test_can_iterate_over_all_entries
145145
end
146146
end
147147

148+
def test_size
149+
Ref::Mock.use do
150+
hash = map_class.new
151+
assert hash.empty?
152+
assert_equal 0, hash.size
153+
key_1 = Object.new
154+
key_2 = Object.new
155+
hash[key_1] = "value 1"
156+
hash[key_2] = "value 2"
157+
refute hash.empty?
158+
assert_equal 2, hash.size
159+
Ref::Mock.gc(key_2)
160+
refute hash.empty?
161+
assert_equal 1, hash.size
162+
end
163+
end
164+
148165
def test_inspect
149166
Ref::Mock.use do
150167
hash = map_class.new

test/reference_value_map_behavior.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,23 @@ def test_can_iterate_over_all_entries
125125
end
126126
end
127127

128+
def test_size
129+
Ref::Mock.use do
130+
hash = map_class.new
131+
assert hash.empty?
132+
assert_equal 0, hash.size
133+
value_1 = "value 1"
134+
value_2 = "value 2"
135+
hash["key 1"] = value_1
136+
hash["key 2"] = value_2
137+
refute hash.empty?
138+
assert_equal 2, hash.size
139+
Ref::Mock.gc(value_2)
140+
refute hash.empty?
141+
assert_equal 1, hash.size
142+
end
143+
end
144+
128145
def test_inspect
129146
Ref::Mock.use do
130147
hash = map_class.new

0 commit comments

Comments
 (0)