Class: Swift::IdentityMap

Inherits:
Object
  • Object
show all
Defined in:
lib/swift/identity_map.rb

Overview

Weak hash set. – TODO: Is ‘hash set’ the real name for a hash where both the keys and values must be unique?

Instance Method Summary collapse

Constructor Details

#initializeIdentityMap

Returns a new instance of IdentityMap.



6
7
8
# File 'lib/swift/identity_map.rb', line 6

def initialize
  @cache, @reverse_cache, @finalize = {}, {}, method(:finalize)
end

Instance Method Details

#get(key) ⇒ Object



10
11
12
13
14
# File 'lib/swift/identity_map.rb', line 10

def get key
  value_id = @cache[key]
  return ObjectSpace._id2ref(value_id) unless value_id.nil?
  nil
end

#set(key, value) ⇒ Object

– TODO: Barf if the value.object_id already exists in the cache.



18
19
20
21
22
# File 'lib/swift/identity_map.rb', line 18

def set key, value
  @reverse_cache[value.object_id] = key
  @cache[key]                     = value.object_id
  ObjectSpace.define_finalizer(value, @finalize)
end