Class: ExactCover::MatrixPreprocessor
- Inherits:
-
Object
- Object
- ExactCover::MatrixPreprocessor
- Defined in:
- lib/exact_cover/matrix_preprocessor.rb
Overview
Transforms an array into a linked list of data and column objects
Instance Attribute Summary collapse
-
#col_count ⇒ Object
readonly
Returns the value of attribute col_count.
-
#matrix ⇒ Object
readonly
Returns the value of attribute matrix.
-
#row_count ⇒ Object
readonly
Returns the value of attribute row_count.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(matrix) ⇒ MatrixPreprocessor
constructor
A new instance of MatrixPreprocessor.
Constructor Details
#initialize(matrix) ⇒ MatrixPreprocessor
Returns a new instance of MatrixPreprocessor.
8 9 10 11 12 |
# File 'lib/exact_cover/matrix_preprocessor.rb', line 8 def initialize(matrix) @matrix = matrix @row_count = matrix.size @col_count = matrix[0].size end |
Instance Attribute Details
#col_count ⇒ Object (readonly)
Returns the value of attribute col_count.
6 7 8 |
# File 'lib/exact_cover/matrix_preprocessor.rb', line 6 def col_count @col_count end |
#matrix ⇒ Object (readonly)
Returns the value of attribute matrix.
4 5 6 |
# File 'lib/exact_cover/matrix_preprocessor.rb', line 4 def matrix @matrix end |
#row_count ⇒ Object (readonly)
Returns the value of attribute row_count.
5 6 7 |
# File 'lib/exact_cover/matrix_preprocessor.rb', line 5 def row_count @row_count end |
Instance Method Details
#call ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/exact_cover/matrix_preprocessor.rb', line 14 def call # Generates the column objects headers = [] (0..col_count - 1).each do |col| col_size = size(col) headers[col] = ColumnObject.new(col_size, col.to_s) end # Fill the left and right for each header object prev = headers[-1] headers.each do |current| current.left = prev prev.right = current prev = current end # Generates the data objects (one for each 1 in the matrix) data_objects = [] row_objects_array = [] col_objects_array = [] (0..row_count - 1).each do |row| data_objects[row] = [] row_objects_array[row] = [] (0..col_count - 1).each do |col| column = headers[col] col_objects_array[col] = [column] if col_objects_array[col].nil? next unless matrix[row][col] == 1 data_object = DataObject.new(column) data_objects[row][col] = data_object row_objects_array[row] << data_object col_objects_array[col] << data_object end end # Fill the left and right fields row_objects_array.each do |row_objects| prev = row_objects[-1] loop do break if row_objects.empty? current = row_objects.shift current.left = prev prev.right = current prev = current end end # Fill the up and down fields col_objects_array.each do |col_objects| prev = col_objects[-1] loop do break if col_objects.empty? current = col_objects.shift current.up = prev prev.down = current prev = current end end # insert the root between headers[-1] and headers[0] root = ColumnObject.new(0, "root") headers[-1].right = root headers[0].left = root root.right = headers[0] root.left = headers[-1] root end |