Class: Bmg::Operator::Image

Inherits:
Object
  • Object
show all
Includes:
Binary
Defined in:
lib/bmg/operator/image.rb

Overview

Image operator.

Extends each tuple with its image in right.

Constant Summary collapse

DEFAULT_OPTIONS =
{

  # Whether we need to convert each image as an Array,
  # instead of keeping a Relation instance
  array: false

}

Instance Attribute Summary

Attributes included from Bmg::Operator

#type

Instance Method Summary collapse

Methods included from Bmg::Operator

#inspect, #to_s

Methods included from Relation

#debug, #delete, empty, #empty?, #insert, new, #one, #one_or_nil, #to_json, #update, #visit, #with_typecheck, #without_typecheck, #ys_by_x

Methods included from Algebra

#allbut, #autosummarize, #autowrap, #constants, #extend, #group, #image, #join, #matching, #materialize, #not_matching, #page, #project, #rename, #restrict, #spied, #summarize, #union, #unspied

Methods included from Algebra::Shortcuts

#image, #join, #matching, #not_matching, #prefix, #rxmatch, #suffix

Constructor Details

#initialize(type, left, right, as, on, options = {}) ⇒ Image

Returns a new instance of Image.



19
20
21
22
23
24
25
26
# File 'lib/bmg/operator/image.rb', line 19

def initialize(type, left, right, as, on, options = {})
  @type = type
  @left = left
  @right = right
  @as = as
  @on = on
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Method Details

#eachObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bmg/operator/image.rb', line 34

def each
  index = Hash.new{|h,k| h[k] = empty_image }
  right.each_with_object(index) do |t, index|
    key = tuple_project(t, on)
    index[key].operand << tuple_image(t, on)
  end
  if options[:array]
    index = index.each_with_object({}) do |(k,v),ix|
      ix[k] = v.to_a
    end
  end
  left.each do |tuple|
    key = tuple_project(tuple, on)
    image = index[key] || (options[:array] ? [] : empty_image)
    yield tuple.merge(as => image)
  end
end

#to_astObject



52
53
54
# File 'lib/bmg/operator/image.rb', line 52

def to_ast
  [ :image, left.to_ast, right.to_ast, as, on, options.dup ]
end