Class: Factbase::IndexedAnd
- Inherits:
-
Object
- Object
- Factbase::IndexedAnd
- Defined in:
- lib/factbase/indexed/indexed_and.rb
Overview
Indexed term ‘and’.
Instance Method Summary collapse
-
#initialize(term, idx) ⇒ IndexedAnd
constructor
A new instance of IndexedAnd.
- #predict(maps, fb, params) ⇒ Object
Constructor Details
#initialize(term, idx) ⇒ IndexedAnd
Returns a new instance of IndexedAnd.
8 9 10 11 |
# File 'lib/factbase/indexed/indexed_and.rb', line 8 def initialize(term, idx) @term = term @idx = idx end |
Instance Method Details
#predict(maps, fb, params) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/factbase/indexed/indexed_and.rb', line 13 def predict(maps, fb, params) return if @idx.nil? key = [maps.object_id, @term.operands.first, @term.op] r = nil if @term.operands.all? { |o| o.op == :eq } && @term.operands.size > 1 \ && @term.operands.all? { |o| o.operands.first.is_a?(Symbol) && _scalar?(o.operands[1]) } props = @term.operands.map { |o| o.operands.first }.sort! key = [maps.object_id, props, :multi_and_eq] entry = @idx[key] maps_array = maps.to_a if entry.nil? entry = { index: {}, indexed_count: 0 } @idx[key] = entry end if entry[:indexed_count] < maps_array.size maps_array[entry[:indexed_count]..].each do |m| _all_tuples(m, props).each do |t| entry[:index][t] ||= [] entry[:index][t] << m end end entry[:indexed_count] = maps_array.size end tuples = Enumerator.product( *@term.operands.sort_by { |o| o.operands.first }.map do |o| if o.operands[1].is_a?(Symbol) params[o.operands[1].to_s] || [] else [o.operands[1]] end end ) j = tuples.flat_map { |t| entry[:index][t] || [] }.uniq(&:object_id) r = maps.respond_to?(:repack) ? maps.repack(j) : j else fail = false @term.operands.each do |o| n = o.predict(maps, fb, params) if n.nil? fail = true break end if r.nil? r = n elsif n.size < r.size * 8 small, large = n.size < r.size ? [n.to_a, r.to_a] : [r.to_a, n.to_a] ids = Set.new(small.map(&:object_id)) r = large.select { |f| ids.include?(f.object_id) } end break if r.size < maps.size / 32 break if r.size < 128 end return if fail end r end |