Method: Coopy::CompareTable#align_columns

Defined in:
lib/coopy/compare_table.rb

#align_columns(align, a, b) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/coopy/compare_table.rb', line 156

def align_columns(align, a, b) 
  align.range(a.width,b.width)
  align.tables(a,b)
  align.set_rowlike(false)
    
  slop = 5
  
  va = a.get_cell_view
  vb = b.get_cell_view
  ra_best = 0
  rb_best = 0
  ct_best = -1
  ma_best = nil
  mb_best = nil
  ra_header = 0
  rb_header = 0
  ra_uniques = 0
  rb_uniques = 0
  (0..slop-1).each do |ra| 
    break if (ra>=a.height)
    (0..slop-1).each do |rb| 
      break if (rb>=b.height)
      ma = {}
      mb = {}
      ct = 0
      uniques = 0
      (0..a.width-1).each do |ca| 
        key = va.to_s(a.get_cell(ca,ra))
        if (ma.has_key?(key)) 
          ma[key] = -1
          uniques-=1
        else 
          ma[key] = ca
          uniques+=1
        end
      end
      if (uniques>ra_uniques) 
        ra_header = ra
        ra_uniques = uniques
      end
      uniques = 0
      (0..b.width-1).each do |cb|
        key = vb.to_s(b.get_cell(cb,rb))
        if (mb.has_key?(key)) 
          mb[key] = -1
          uniques-=1
        else 
          mb[key] = cb
          uniques+=1
        end
      end
      if (uniques>rb_uniques) 
        rb_header = rb
        rb_uniques = uniques
      end

      ma.keys.each do |key| 
        i0 = ma[key]
        i1 = mb[key]
        if (i1 && i1>=0 && i0>=0) 
          ct+=1
        end
      end

      if (ct>ct_best) 
        ct_best = ct
        ma_best = ma
        mb_best = mb
        ra_best = ra
        rb_best = rb
      end
    end
  end

  return if (ma_best.nil?)
  ma_best.keys.each do |key|
    i0 = ma_best[key]
    i1 = mb_best[key]
    if (i1>=0 && i0>=0) 
      align.link(i0,i1)
    end
  end
  align.headers(ra_header,rb_header)
end