class ReplaceCommonElementsInFormulae

Constants

VALUES

Attributes

common_elements[RW]
common_elements_used[RW]

Public Class Methods

replace(*args) click to toggle source
# File src/simplify/replace_common_elements_in_formulae.rb, line 3
def self.replace(*args)
  self.new.replace(*args)
end

Public Instance Methods

replace(input, common_elements) click to toggle source
# File src/simplify/replace_common_elements_in_formulae.rb, line 10
def replace(input, common_elements)
  @common_elements_used ||= Hash.new { |h, k| h[k] = 0 }
  @common_elements = common_elements
  input.each do |ref, ast|
    replace_repeated_formulae(ast)
  end
  input
end
replace_repeated_formulae(ast) click to toggle source
# File src/simplify/replace_common_elements_in_formulae.rb, line 21
def replace_repeated_formulae(ast)
  return ast unless ast.is_a?(Array)
  return ast if VALUES.has_key?(ast.first)    
  replacement = @common_elements[ast]
  if replacement
    @common_elements_used[replacement] += 1
    ast.replace(replacement)
  else
    ast.each { |a| replace_repeated_formulae(a) }
  end
end