Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
rklemme revised this gist . 1 changed file with 8 additions and 4 deletions. View gist @ 5717942
matrix.rb
12 
@@ -12,7 +12,9 @@ def [](row, col)
end
def []=(row, col, val)
- data[index(row, col)] = val
+ # data[index(row, col)] = val
+ idx = index(row, col)
+ val.nil? ? data.delete(idx) : data[idx] = val
end
def row(row)
@@ -32,7 +34,7 @@ def each_row
def each_column
@col_headers.each do |idx|
- yield column(idx)
+ yield col(idx)
end
self
end
@@ -47,11 +49,13 @@ def col_headers
private
def index(row, col)
- @row_headers.index(row) * @col_headers.size + @col_headers.index(col)
+ # @row_headers.index(row) * @col_headers.size + @col_headers.index(col)
+ "#{row}:#{col}"
end
def data
- @data ||= []
+ # @data ||= []
+ @data ||= {}
end
end
rklemme revised this gist . 1 changed file with 1 addition and 1 deletion. View gist @ 5aaac34
matrix.rb
2 
@@ -32,7 +32,7 @@ def each_row
def each_column
@col_headers.each do |idx|
- yield col(idx)
+ yield column(idx)
end
self
end
rklemme created this gist . View gist @ 12d6958
matrix.rb
130 
@@ -0,0 +1,130 @@
+#!/usr/bin/ruby
+
+class Matrix
+
+ def initialize(row_headers, col_headers)
+ @row_headers = row_headers
+ @col_headers = col_headers
+ end
+
+ def [](row, col)
+ data[index(row, col)]
+ end
+
+ def []=(row, col, val)
+ data[index(row, col)] = val
+ end
+
+ def row(row)
+ Row.new(self, row)
+ end
+
+ def column(col)
+ Column.new(self, col)
+ end
+
+ def each_row
+ @row_headers.each do |idx|
+ yield row(idx)
+ end
+ self
+ end
+
+ def each_column
+ @col_headers.each do |idx|
+ yield col(idx)
+ end
+ self
+ end
+
+ def row_headers
+ @row_headers.dup
+ end
+
+ def col_headers
+ @col_headers.dup
+ end
+
+private
+ def index(row, col)
+ @row_headers.index(row) * @col_headers.size + @col_headers.index(col)
+ end
+
+ def data
+ @data ||= []
+ end
+end
+
+class MatrixPart
+ attr_reader :matrix
+
+ def initialize(matrix)
+ @matrix = matrix
+ end
+
+ def [](item)
+ matrix[*indexes(item)]
+ end
+
+ def []=(item, val)
+ matrix[*indexes(item)] = val
+ end
+
+ include Enumerable
+
+ def each
+ each_address do |row, col|
+ yield matrix[row, col]
+ end
+ self
+ end
+end
+
+class Row < MatrixPart
+ def initialize(matrix, row_id)
+ super(matrix)
+ @row_id = row_id
+ end
+
+private
+ def indexes(col_id)
+ return @row_id, col_id
+ end
+
+ def each_address
+ matrix.col_headers.each {|col_id| yield @row_id, col_id}
+ self
+ end
+end
+
+class Column < MatrixPart
+ def initialize(matrix, col)
+ super(matrix)
+ @col_id = col
+ end
+
+private
+ def indexes(row_id)
+ return row_id, @col_id
+ end
+
+ def each_address
+ matrix.row_headers.each {|row_id| yield row_id, @col_id}
+ self
+ end
+end
+
+if $0 == __FILE__
+ require 'pp'
+
+ puts "test"
+ m = Matrix.new(%w{a b c}, %w{1 2 3 4})
+ m["a", "1"] = 123
+ m["b", "4"] = 123
+ row = m.row("a")
+ row["1"] = 22
+
+ row.each {|x| p x}
+
+ pp m
+end
Something went wrong with that request. Please try again.