class Wongi::Engine::AlphaIndex

Attributes

index[R]
pattern[R]

Public Class Methods

new(pattern) click to toggle source
# File lib/wongi-engine/alpha_index.rb, line 7
def initialize(pattern)
  @pattern = pattern
  @index = Hash.new { |h, k| h[k] = Set.new }
end

Public Instance Methods

add(wme) click to toggle source
# File lib/wongi-engine/alpha_index.rb, line 12
def add(wme)
  collection_for_wme(wme).add(wme)
end
collection_for_wme(wme) click to toggle source
# File lib/wongi-engine/alpha_index.rb, line 24
def collection_for_wme(wme)
  index[hashed_key(wme)]
end
remove(wme) click to toggle source
# File lib/wongi-engine/alpha_index.rb, line 16
def remove(wme)
  collection = collection_for_wme(wme)
  collection.delete(wme)

  # release some memory
  index.delete(hashed_key(wme)) if collection.empty?
end

Private Instance Methods

hashed_key(wme) click to toggle source
# File lib/wongi-engine/alpha_index.rb, line 32
        def hashed_key(wme)
  key(wme).map(&:hash)
end
key(wme) click to toggle source
# File lib/wongi-engine/alpha_index.rb, line 28
        def key(wme)
  pattern.map { wme.public_send(_1) }
end