Module: WeakSet::WeakKeys
- Defined in:
- lib/weak_set/weak_keys.rb
Overview
This WeakSet strategy targets Ruby < 3.3.0.
Its ObjectSpace::WeakMap uses weak keys and weak values so that either the key or the value can be independently garbage collected. If either of them vanishes, the entry is removed.
The WeakMap does not allow to explicitly delete entries. We emulate this by setting the garbage-collectible value of a deleted entry to a simple new object. This value will be garbage collected on the next GC run which will then remove the entry. When accessing elements, we delete and filter out these recently deleted entries.
Class Method Summary collapse
-
.usable? ⇒ Bool
Checks if this strategy is usable for the current Ruby version.
Instance Method Summary collapse
-
#add(obj) ⇒ self
Adds the given object to the weak set and return
self
. -
#clear ⇒ self
Removes all elements and returns
self
. -
#delete?(obj) ⇒ self?
Deletes the given object from
self
and returnsself
if it was present in the set. -
#each {|element| ... } ⇒ self, Enumerator
Calls the given block once for each live element in
self
, passing that element as a parameter. -
#include?(obj) ⇒ Bool
true
if the given object is included inself
,false
otherwise. -
#initialize ⇒ void
Initialize the weak map.
-
#size ⇒ Integer
The number of live elements in
self
. -
#to_a ⇒ Array
The live elements contained in
self
as anArray
.
Class Method Details
.usable? ⇒ Bool
Checks if this strategy is usable for the current Ruby version.
28 29 30 |
# File 'lib/weak_set/weak_keys.rb', line 28 def self.usable? RUBY_ENGINE == "ruby" end |
Instance Method Details
#add(obj) ⇒ self
Adds the given object to the weak set and return self
. Use WeakSet#merge to
add many elements at once.
In contrast to other "regular" objects, we will not retain a strong reference to the added object. Unless some other live objects still references the object, it will eventually be garbage-collected.
39 40 41 42 |
# File 'lib/weak_set/weak_keys.rb', line 39 def add(obj) @map[obj] = obj self end |
#clear ⇒ self
Removes all elements and returns self
45 46 47 48 |
# File 'lib/weak_set/weak_keys.rb', line 45 def clear @map = ObjectSpace::WeakMap.new self end |
#delete?(obj) ⇒ self?
WeakSet does not test member equality with ==
or eql?
. Instead,
it always checks strict object equality, so that, e.g., different
strings are not considered equal, even if they may contain the same
string content.
Deletes the given object from self
and returns self
if it was present
in the set. If the object was not in the set, returns nil
.
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/weak_set/weak_keys.rb', line 51 def delete?(obj) return unless include?(obj) # If there is a valid entry in the WeakMap, we replace the value for the # obj with a temporary DeletedEntry object. As we do not keep any strong # reference to this object, this will cause the key/value entry to vanish # from the WeakMap when the DeletedEntry object is eventually garbage # collected. # This ensures that we don't retain unnecessary entries in the map which # we would have to skip over. @map[obj] = DeletedEntry.new self end |
#each {|element| ... } ⇒ self, Enumerator
Calls the given block once for each live element in self
, passing that
element as a parameter. Returns the weak set itself.
If no block is given, an Enumerator
is returned instead.
66 67 68 69 70 71 72 73 |
# File 'lib/weak_set/weak_keys.rb', line 66 def each return enum_for(__method__) { size } unless block_given? @map.values.each do |obj| yield(obj) unless DeletedEntry === obj end self end |
#include?(obj) ⇒ Bool
WeakSet does not test member equality with ==
or eql?
. Instead,
it always checks strict object equality, so that, e.g., different
strings are not considered equal, even if they may contain the same
string content.
Returns true
if the given object is included in self
, false
otherwise.
76 77 78 |
# File 'lib/weak_set/weak_keys.rb', line 76 def include?(obj) !!(@map.key?(obj) && @map[obj].equal?(obj)) end |
#initialize ⇒ void
Initialize the weak map
34 35 36 |
# File 'lib/weak_set/weak_keys.rb', line 34 def initialize @map = ObjectSpace::WeakMap.new end |
#size ⇒ Integer
Returns the number of live elements in self
.
81 82 83 84 85 86 87 |
# File 'lib/weak_set/weak_keys.rb', line 81 def size count = 0 @map.each_value do |obj| count = count.succ unless DeletedEntry === obj end count end |
#to_a ⇒ Array
The order of elements on the returned Array
is non-deterministic.
We do not preserve preserve insertion order.
Returns the live elements contained in self
as an Array
.
90 91 92 |
# File 'lib/weak_set/weak_keys.rb', line 90 def to_a @map.values.delete_if { |obj| DeletedEntry === obj } end |