module HungarianAlgorithmC

Constants

LIMITS

Limits for 'signed long long int' C data type

VERSION

Public Class Methods

find_pairings(matrix) click to toggle source
# File lib/hungarian_algorithm_c.rb, line 12
def find_pairings(matrix)
  validate!(matrix)
  array = value_capped_flattened_array(matrix)
  pairs(array, matrix.size)
end

Private Class Methods

pairs(p1, p2) click to toggle source
columns
VALUE pairs(VALUE self, VALUE flattened_array_ruby, VALUE row_size_val) {
  VALUE output;
  hungarian_problem_t p;
  int** matrix;
  int row_size = NUM2INT(row_size_val);
  int array_size = row_size * row_size;
  int array_c[array_size];

  int index;
  for (index = 0; index < array_size; index++) {
    signed long long int rounded_element = NUM2LL(rb_ary_entry(flattened_array_ruby, index));
    array_c[index] = rounded_element;
  }

  matrix = array_to_matrix(array_c, row_size, row_size);

  hungarian_init(&p, matrix, row_size, row_size, 0);
  hungarian_solve(&p);
  output = indices_array(&p, row_size);
  hungarian_free(&p);

  return output;
}
rectangular?(matrix) click to toggle source
# File lib/hungarian_algorithm_c.rb, line 41
def rectangular?(matrix)
  row_size = matrix.size
  matrix.all? { |column| row_size == column.size }
end
validate!(matrix) click to toggle source
# File lib/hungarian_algorithm_c.rb, line 36
def validate!(matrix)
  return if rectangular?(matrix)
  raise ArgumentError.new('matrix must be rectangular')
end
value_capped_flattened_array(matrix) click to toggle source
# File lib/hungarian_algorithm_c.rb, line 18
def value_capped_flattened_array(matrix)
  [].tap do |array|
    matrix.each do |row|
      row.each do |element|
        array << begin
          if element > LIMITS[:positive]
            LIMITS[:positive]
          elsif element < LIMITS[:negative]
            LIMITS[:negative]
          else
            element
          end
        end
      end
    end
  end
end