Class: Axiom::Relation::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/axiom/relation/index.rb

Overview

Tuples keyed by a tuple

Instance Method Summary collapse

Constructor Details

#initialize(key, header) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize an index

Parameters:



17
18
19
20
21
# File 'lib/axiom/relation/index.rb', line 17

def initialize(key, header)
  @key    = key
  @header = header
  @index  = Hash.new { |hash, tuple| hash[tuple] = Set.new }
end

Instance Method Details

#<<(tuple) ⇒ Index

Add a tuple to the index

Examples:

index << tuple

Parameters:

Returns:



48
49
50
51
# File 'lib/axiom/relation/index.rb', line 48

def <<(tuple)
  self[tuple] << tuple.project(@header)
  self
end

#[](tuple) ⇒ Set<Tuple>

Return the tuples in the index based on the tuple key

Examples:

index[tuple]  # => tuples

Parameters:

Returns:



85
86
87
# File 'lib/axiom/relation/index.rb', line 85

def [](tuple)
  @index[tuple.project(@key)]
end

#each {|key, tuples| ... } ⇒ Index

Iterate over each entry in the index

Examples:

index = Index.new(key_header, tuple_header)
index.each { |key, tuples| ... }

Yields:

  • (key, tuples)

Yield Parameters:

  • key (Tuple)

    the key for the tuples

  • tuples (Set<Tuple>)

    the indexed tuples

Returns:



69
70
71
72
73
# File 'lib/axiom/relation/index.rb', line 69

def each(&block)
  return to_enum unless block_given?
  @index.each(&block)
  self
end

#merge(tuples) ⇒ Index

Add a set of tuples to the index

Examples:

index.merge(tuples)

Parameters:

  • tuples (Enumerable<Tuple>)

Returns:



33
34
35
36
# File 'lib/axiom/relation/index.rb', line 33

def merge(tuples)
  tuples.each(&method(:<<))
  self
end