Sha256: b9f3f2c92853703e979c096a47f805a6832df5c524a11f373152f80e7175eeea

Contents?: true

Size: 1.3 KB

Versions: 20

Compression:

Stored size: 1.3 KB

Contents

require 'matrix'

module Gecode::Util
  # Methods that make a matrix an enumerable.
  module MatrixEnumMethods
    include Enumerable
  
    # Iterates over every element in the matrix.
    def each(&block)
      row_size.times do |i|
        column_size.times do |j|
          yield self[i,j]
        end
      end
    end
  end

  # Extends Matrix so that it's an enumerable.
  class EnumMatrix < Matrix
    include MatrixEnumMethods
    
    def row(i)
      wrap_if_wrapped make_vector_enumerable(super)
    end
    
    def column(i)
      wrap_if_wrapped make_vector_enumerable(super)
    end
    
    def minor(*args)
      matrix = super
      class <<matrix
        include MatrixEnumMethods
      end
      return wrap_if_wrapped(matrix)
    end
    
    private
    
    # Makes the specified vector enumerable.
    def make_vector_enumerable(vector)
      class <<vector
        include Enumerable
  
        # Iterates over every element in the matrix.
        def each(&block)
          size.times do |i|
            yield self[i]
          end
        end
      end
      return vector
    end
    
    # Wraps the specified enumerable if the matrix itself is already wrapped.
    def wrap_if_wrapped(enum)
      if respond_to? :model
        model.wrap_enum(enum)
      else
        enum
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
gecoder-with-gecode-0.8.3-mswin32 lib/gecoder/interface/enum_matrix.rb
gecoder-with-gecode-0.8.2-mswin32 lib/gecoder/interface/enum_matrix.rb
gecoder-with-gecode-0.8.1-mswin32 lib/gecoder/interface/enum_matrix.rb
gecoder-with-gecode-0.8.0-mswin32 lib/gecoder/interface/enum_matrix.rb
gecoder-with-gecode-0.7.1-mswin32 lib/gecoder/interface/enum_matrix.rb
gecoder-0.4.0 lib/gecoder/interface/enum_matrix.rb
gecoder-0.5.0 lib/gecoder/interface/enum_matrix.rb
gecoder-0.8.2 lib/gecoder/interface/enum_matrix.rb
gecoder-0.8.3 lib/gecoder/interface/enum_matrix.rb
gecoder-0.7.1 lib/gecoder/interface/enum_matrix.rb
gecoder-0.7.0 lib/gecoder/interface/enum_matrix.rb
gecoder-0.6.1 lib/gecoder/interface/enum_matrix.rb
gecoder-0.6.0 lib/gecoder/interface/enum_matrix.rb
gecoder-0.8.1 lib/gecoder/interface/enum_matrix.rb
gecoder-0.8.0 lib/gecoder/interface/enum_matrix.rb
gecoder-with-gecode-0.7.1 lib/gecoder/interface/enum_matrix.rb
gecoder-with-gecode-0.8.0 lib/gecoder/interface/enum_matrix.rb
gecoder-with-gecode-0.8.1 lib/gecoder/interface/enum_matrix.rb
gecoder-with-gecode-0.8.2 lib/gecoder/interface/enum_matrix.rb
gecoder-with-gecode-0.8.3 lib/gecoder/interface/enum_matrix.rb