Sha256: 1e2c7aca80389d36a33d7b6a716a6e81a7fb8ff78a0c2004088e000b6c6010da

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

# encoding: utf-8

module Axiom
  class Optimizer
    module Function

      # Mixin for optimizations to Unary functions
      module Unary

        # The optimized operand
        #
        # @return [Relation]
        #
        # @api private
        attr_reader :operand

        # Initialize a Unary optimizer
        #
        # @return [undefined]
        #
        # @api private
        def initialize(*)
          super
          @operand = optimize_operand
        end

      private

        # Optimize the operand
        #
        # @return [Relation]
        #
        # @api private
        def optimize_operand
          Function.optimize_operand(operation.operand)
        end

        # Optimize when the operand is constant
        module ConstantOperand

          # Test if the operand is constant
          #
          # @return [Boolean]
          #
          # @api private
          def optimizable?
            Util.constant?(operand)
          end

          # Evaluate the operand and return the constant
          #
          # @return [Object]
          #
          # @api private
          def optimize
            operation.class.call(operand)
          end

        end # module ConstantOperand

        # Optimize when the operand is unoptimized
        module UnoptimizedOperand

          # Test if the operand is unoptimized
          #
          # @return [Boolean]
          #
          # @api private
          def optimizable?
            ! operand.equal?(operation.operand)
          end

          # Return a Aggregate with an optimized operand
          #
          # @return [Aggregate]
          #
          # @api private
          def optimize
            operation.class.new(operand)
          end

        end # module UnoptimizedOperand
      end # class Unary
    end # module Function
  end # class Optimizer
end # module Axiom

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
axiom-optimizer-0.1.1 lib/axiom/optimizer/function/unary.rb
axiom-optimizer-0.1.0 lib/axiom/optimizer/function/unary.rb