Parent

Files

Ref::AbstractReferenceKeyMap

Abstract base class for WeakKeyMap and SoftKeyMap.

The classes behave similar to Hashes, but the keys in the map are not strong references and can be reclaimed by the garbage collector at any time. When a key is reclaimed, the map entry will be removed.

Public Class Methods

new() click to toggle source

Create a new map. Values added to the hash will be cleaned up by the garbage collector if there are no other reference except in the map.

# File lib/ref/abstract_reference_key_map.rb, line 21
def initialize
  @values = {}
  @references_to_keys_map = {}
  @lock = SafeMonitor.new
  @reference_cleanup = lambda{|object_id| remove_reference_to(object_id)}
end

Public Instance Methods

[](key) click to toggle source

Get a value from the map by key. If the value has been reclaimed by the garbage collector, this will return nil.

# File lib/ref/abstract_reference_key_map.rb, line 30
def [](key)
  rkey = ref_key(key)
  @values[rkey] if rkey
end
[]=(key, value) click to toggle source

Add a key/value to the map.

# File lib/ref/abstract_reference_key_map.rb, line 36
def []=(key, value)
  ObjectSpace.define_finalizer(key, @reference_cleanup)
  @lock.synchronize do
    @references_to_keys_map[key.__id__] = self.class.reference_class.new(key)
    @values[key.__id__] = value
  end
end
clear() click to toggle source

Clear the map of all key/value pairs.

# File lib/ref/abstract_reference_key_map.rb, line 77
def clear
  @lock.synchronize do
    @values.clear
    @references_to_keys_map.clear
  end
end
delete(key) click to toggle source

Remove the value associated with the key from the map.

# File lib/ref/abstract_reference_key_map.rb, line 45
def delete(key)
  rkey = ref_key(key)
  if rkey
    @references_to_keys_map.delete(rkey)
    @values.delete(rkey)
  else
    nil
  end
end
each() click to toggle source

Iterate through all the key/value pairs in the map that have not been reclaimed by the garbage collector.

# File lib/ref/abstract_reference_key_map.rb, line 69
def each
  @references_to_keys_map.each do |rkey, ref|
    key = ref.object
    yield(key, @values[rkey]) if key
  end
end
inspect() click to toggle source
# File lib/ref/abstract_reference_key_map.rb, line 91
def inspect
  live_entries = {}
  each do |key, value|
    live_entries[key] = value
  end
  live_entries.inspect
end
keys() click to toggle source

Get an array of keys that have not yet been garbage collected.

# File lib/ref/abstract_reference_key_map.rb, line 56
def keys
  @values.keys.collect{|rkey| @references_to_keys_map[rkey].object}.compact
end
merge!(other_hash) click to toggle source

Merge the values from another hash into this map.

# File lib/ref/abstract_reference_key_map.rb, line 85
def merge!(other_hash)
  other_hash.each do |key, value|
    self[key] = value
  end
end
to_a() click to toggle source

Turn the map into an arry of [key, value] entries.

# File lib/ref/abstract_reference_key_map.rb, line 61
def to_a
  array = []
  each{|k,v| array << [k, v]}
  array
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.