module CommandSearch::Optimizer
Public Instance Methods
denest!(ast, parent_type = :and)
click to toggle source
# File lib/command_search/optimizer.rb, line 5 def denest!(ast, parent_type = :and) ast.map! do |node| type = node[:type] next node unless type == :and || type == :or || type == :not denest!(node[:value], type) next [] if node[:value] == [] if type == :not only_child = node[:value].count == 1 child = node[:value].first next child[:value] if only_child && child[:type] == :not next node end next node[:value] if node[:value].count == 1 next node[:value] if type == parent_type next node[:value] if type == :and && parent_type == :not next node if type == :and denest!(node[:value], type) # type == :or, parent_type == :and node[:value].uniq! next node[:value] if node[:value].count == 1 node end ast.flatten! end
optimize!(ast)
click to toggle source
# File lib/command_search/optimizer.rb, line 29 def optimize!(ast) denest!(ast) ast.uniq! ast end