類別 Rinda::TupleBag

TupleBag 是元組的無序集合。它是元組空間的基礎。

公開實例方法

delete(tuple) 按一下以切換來源

TupleBag 中移除 tuple

# File lib/rinda/tuplespace.rb, line 341
def delete(tuple)
  key = bin_key(tuple)
  bin = @hash[key]
  return nil unless bin
  bin.delete(tuple)
  @hash.delete(key) if bin.empty?
  tuple
end
delete_unless_alive() 按一下以切換來源

TupleBag 中刪除死亡元組,並傳回已刪除的元組。

# File lib/rinda/tuplespace.rb, line 381
def delete_unless_alive
  deleted = []
  @hash.each do |key, bin|
    bin.delete_if do |tuple|
      if tuple.alive?
        false
      else
        deleted.push(tuple)
        true
      end
    end
  end
  deleted
end
find(template) 按一下以切換來源

找出與 template 相符的存活元組。

# File lib/rinda/tuplespace.rb, line 361
def find(template)
  bin_for_find(template).find do |tuple|
    tuple.alive? && template.match(tuple)
  end
end
find_all(template) 按一下以切換來源

找出所有與 template 相符的存活元組。

# File lib/rinda/tuplespace.rb, line 352
def find_all(template)
  bin_for_find(template).find_all do |tuple|
    tuple.alive? && template.match(tuple)
  end
end
find_all_template(tuple) 按一下以切換來源

TupleBag 中找出所有元組,當這些元組視為範本時,會與 tuple 相符且為存活狀態。

# File lib/rinda/tuplespace.rb, line 371
def find_all_template(tuple)
  @enum.find_all do |template|
    template.alive? && template.match(tuple)
  end
end
has_expires?() 按一下以切換來源

如果 TupleBag 中有任何過期項目,則傳回 true

# File lib/rinda/tuplespace.rb, line 323
def has_expires?
  @enum.find do |tuple|
    tuple.expires
  end
end
push(tuple) 按一下以切換來源

tuple 加入 TupleBag

# File lib/rinda/tuplespace.rb, line 332
def push(tuple)
  key = bin_key(tuple)
  @hash[key] ||= TupleBin.new
  @hash[key].add(tuple)
end

非公開實例方法

bin_for_find(template) 按一下以切換來源
# File lib/rinda/tuplespace.rb, line 412
def bin_for_find(template)
  key = bin_key(template)
  key ? @hash.fetch(key, []) : @enum
end
bin_key(tuple) 按一下以切換來源
# File lib/rinda/tuplespace.rb, line 403
def bin_key(tuple)
  head = tuple[0]
  if head.class == Symbol
    return head
  else
    false
  end
end
each_entry(&blk) 按一下以切換來源
# File lib/rinda/tuplespace.rb, line 397
def each_entry(&blk)
  @hash.each do |k, v|
    v.each(&blk)
  end
end