Module: Bmg::TupleAlgebra

Defined in:
lib/bmg/support/tuple_algebra.rb

Overview

Tuple-level counterparts of the relational operators.

Constant Summary collapse

SET_FROM =

allbut and project below test every attribute of every tuple for membership in an attribute list, so the cost of that lookup is paid tuples x attributes times. Converting the list to a Set is worth it, but only past a few elements: below that an Array scan wins, on every ruby (measured in bench/set_vs_array.rb). Join keys, in particular, are usually one or two attributes and are better left alone.

Callers that iterate are expected to hoist this out of their loop.

3

Class Method Summary collapse

Class Method Details

.allbut(tuple, butlist) ⇒ Object



22
23
24
# File 'lib/bmg/support/tuple_algebra.rb', line 22

def allbut(tuple, butlist)
  tuple.reject{|k,v| butlist.include?(k) }
end

.membership(attrlist) ⇒ Object



17
18
19
# File 'lib/bmg/support/tuple_algebra.rb', line 17

def membership(attrlist)
  attrlist.size < SET_FROM ? attrlist : attrlist.to_set
end

.project(tuple, attrlist) ⇒ Object



27
28
29
# File 'lib/bmg/support/tuple_algebra.rb', line 27

def project(tuple, attrlist)
  tuple.reject{|k,v| !attrlist.include?(k) }
end

.rename(tuple, renaming) ⇒ Object



32
33
34
35
36
37
# File 'lib/bmg/support/tuple_algebra.rb', line 32

def rename(tuple, renaming)
  tuple.each_with_object({}){|(k,v),m|
    m[renaming[k] || k] = v
    m
  }
end

.symbolize_keys(h) ⇒ Object



40
41
42
43
# File 'lib/bmg/support/tuple_algebra.rb', line 40

def symbolize_keys(h)
  return h if h.empty?
  h.each_with_object({}){|(k,v),h| h[k.to_sym] = v }
end