Class: TT::Dimension
- Inherits:
-
Object
- Object
- TT::Dimension
- Includes:
- Enumerable
- Defined in:
- TT_Lib2/dimension.rb
Overview
A 2d matrix Array that can be iterated in rows, columns or like a regular
Array
.
Note: it does not behave exactly like a regular
Array
. Use the .to_a
method to get a regular
array.
Instance Attribute Summary collapse
- #height ⇒ Object readonly
- #width ⇒ Object readonly
Instance Method Summary collapse
-
#[](*args) ⇒ Object
Value at the given index.
- #[]=(*args) ⇒ Object
-
#column(index) ⇒ Array
Column at index.
-
#columns ⇒ Array<Array>
An
Array
containing all the columns. - #each(column = false) {|array, index| ... } ⇒ Object
- #each_column {|column, index| ... } ⇒ Object
- #each_row {|row, index| ... } ⇒ Object
-
#initialize(*args) ⇒ Dimension
constructor
A new instance of Dimension.
- #inspect ⇒ String
-
#length ⇒ Integer
(also: #size)
The size of the
Dimension
. - #map ⇒ TT::Dimension
- #map! ⇒ TT::Dimension
-
#row(index) ⇒ Array
Row at index.
-
#rows ⇒ Array<Array>
An array containing all the rows.
-
#set_column(index, new_column) ⇒ Array
Sets the column at the given index.
-
#set_row(index, new_row) ⇒ Array
Sets the row at the given index.
-
#to_a(orient_by_columns = false) ⇒ Array
Converts the Dimension to an array.
Constructor Details
#new(array, width, height, obj = nil) ⇒ Dimension #new(width, height, obj = nil) ⇒ Dimension
Returns a new instance of Dimension
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'TT_Lib2/dimension.rb', line 36 def initialize(*args) # Check if the first aruments is an +Array+ - use that to populate the dataset. arr = args.first.is_a?(Array) ? args.shift : nil # Validate arguments if args.length < 2 raise ArgumentError, 'Missing arguments. Requires width and height.' end # Extract remaining arguments @width, @height, obj = args # Create 2-dimensional array @d = Array.new(@width * @height, obj) # Populate with the given data, if any. unless arr.nil? unless arr.length == @width * @height raise ArgumentError, 'Array length does not match width and height.' end arr.each_index { |i| @d[i] = arr[i] } end end |
Instance Attribute Details
#height ⇒ Object (readonly)
22 23 24 |
# File 'TT_Lib2/dimension.rb', line 22 def height @height end |
#width ⇒ Object (readonly)
20 21 22 |
# File 'TT_Lib2/dimension.rb', line 20 def width @width end |
Instance Method Details
#[](index) ⇒ Object #[](row, column) ⇒ Object
Returns value at the given index
72 73 74 75 76 77 78 79 80 |
# File 'TT_Lib2/dimension.rb', line 72 def [](*args) case args.length when 1 return @d[ args.first ] when 2 column, row = args return @d[ (row * @width) + column ] end end |
#[]=(index) ⇒ Object #[]=(row, column) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'TT_Lib2/dimension.rb', line 89 def []=(*args) value = args.pop case args.length when 1 @d[args.first] = value when 2 column, row = args @d[ (row * @width) + column ] = value end end |
#column(index) ⇒ Array
Returns column at index
137 138 139 140 141 142 143 |
# File 'TT_Lib2/dimension.rb', line 137 def column(index) arr = [] 0.upto(@height - 1) { |j| arr << @d[index + (j * @width)] } return arr end |
#columns ⇒ Array<Array>
Returns an Array
containing all the columns
162 163 164 165 166 167 168 169 170 171 172 |
# File 'TT_Lib2/dimension.rb', line 162 def columns arr = [] 0.upto(@width - 1) { |i| column = [] 0.upto(@height - 1) { |j| column << @d[i + (j * @width)] } arr << column } return arr end |
#each(column = false) {|array, index| ... } ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'TT_Lib2/dimension.rb', line 183 def each(column = false, &block) if column 0.upto(@width - 1) { |i| 0.upto(@height - 1) { |j| index = i + (j * @width) if block.arity > 1 yield( @d[index], index ) else yield( @d[index] ) end } } else @d.each { |i| yield(i) } end end |
#each_column {|column, index| ... } ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'TT_Lib2/dimension.rb', line 220 def each_column(&block) 0.upto(@width - 1) { |i| column = [] 0.upto(@height - 1) { |j| column << @d[i + (j * @width)] } if block.arity > 1 yield(column, i) else yield(column) end } end |
#each_row {|row, index| ... } ⇒ Object
205 206 207 208 209 210 211 212 213 |
# File 'TT_Lib2/dimension.rb', line 205 def each_row(&block) 0.step(@d.length - 1, @width) { |i| if block.arity > 1 yield( @d[i, @width], (i / @width) ) else yield( @d[i, @width] ) end } end |
#inspect ⇒ String
255 256 257 |
# File 'TT_Lib2/dimension.rb', line 255 def inspect return "#<#{self.class}(#{@width}x#{@height})>" end |
#length ⇒ Integer Also known as: size
Returns the size of the Dimension
237 238 239 |
# File 'TT_Lib2/dimension.rb', line 237 def length return @width * @height end |
#map ⇒ TT::Dimension
261 262 263 264 265 266 267 |
# File 'TT_Lib2/dimension.rb', line 261 def map new_dim = TT::Dimension.new( self.width, self.height ) self.each_with_index { |value, index| new_dim[index] = yield( value ) } new_dim end |
#map! ⇒ TT::Dimension
271 272 273 274 |
# File 'TT_Lib2/dimension.rb', line 271 def map! @d.map! { |value| yield(value) } self end |
#row(index) ⇒ Array
Returns row at index
105 106 107 |
# File 'TT_Lib2/dimension.rb', line 105 def row(index) return @d[index * @width, @width] end |
#rows ⇒ Array<Array>
Returns an array containing all the rows
124 125 126 127 128 129 130 |
# File 'TT_Lib2/dimension.rb', line 124 def rows arr = [] 0.step(@d.length - 1, @width) { |i| arr << @d[i, @width] } return arr end |
#set_column(index, new_column) ⇒ Array
Sets the column at the given index.
153 154 155 156 157 |
# File 'TT_Lib2/dimension.rb', line 153 def set_column(index, new_column) 0.upto(@height - 1) { |j| @d[index + (j * @width)] = new_column[j] } end |
#set_row(index, new_row) ⇒ Array
Sets the row at the given index.
117 118 119 |
# File 'TT_Lib2/dimension.rb', line 117 def set_row(index, new_row) return @d[index * @width, @width] = new_row end |
#to_a(orient_by_columns = false) ⇒ Array
Returns converts the Dimension to an array.
245 246 247 248 249 250 251 |
# File 'TT_Lib2/dimension.rb', line 245 def to_a(orient_by_columns = false) arr = [] self.each(orient_by_columns) { |value| arr << value } return arr end |