Permalink
Browse files

Speed things up a little

  • Loading branch information...
1 parent 379ff57 commit a0815f9c09b3b0842e21aeacbeb910d245304d51 @pjotrp committed Jun 27, 2012
Showing with 17 additions and 6 deletions.
  1. +7 −1 lib/bio-table/diff.rb
  2. +10 −5 lib/bio-table/table.rb
@@ -13,8 +13,14 @@ def self.diff_tables t1, t2, options
l2 = t2.map { |row| columns.map { |i| row.all_fields[i] } }
logger.warn "Not all selected keys are unique!" if l1.uniq.size != l1.size or l2.uniq.size != l2.size
diff = l2 - l1
+ # create index for table 2
+ idx2 = {}
+ t2.each do | row |
+ key = columns.map { |i| row.all_fields[i] }
+ idx2[key] = row
+ end
diff.each do |values|
- t.push(t2.row_by_columns(columns.zip(values)))
+ t.push(t2.row_by_columns(columns.zip(values),idx2))
end
t
end
@@ -93,13 +93,18 @@ def row_by_name name
self[rownames.index(name)]
end
- def row_by_columns zip
+ def row_by_columns zip,idx=nil
index = zip.first[0]
value = zip.first[1]
- each do | row |
- fields = row.all_fields
- if fields[index] == value
- return row if row.match_all_fields?(zip)
+ if idx
+ row = idx[zip.transpose[1]]
+ return row if row.match_all_fields?(zip)
+ else
+ each do | row |
+ fields = row.all_fields
+ if fields[index] == value
+ return row if row.match_all_fields?(zip)
+ end
end
end
nil

0 comments on commit a0815f9

Please sign in to comment.