Class: TT::Dimension

Inherits:
Object
  • Object
show all
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.

Since:

  • 2.5.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#new(array, width, height, obj = nil) ⇒ Dimension #new(width, height, obj = nil) ⇒ Dimension

Returns a new instance of Dimension

Overloads:

  • #new(array, width, height, obj = nil) ⇒ Dimension

    Parameters:

    • array (Array)

      creates an Dimension from an array

    • width (Integer)
    • height (Integer)
    • obj (Object) (defaults to: nil)

      default value

  • #new(width, height, obj = nil) ⇒ Dimension

    Parameters:

    • width (Integer)
    • height (Integer)
    • obj (Object) (defaults to: nil)

      default value

Since:

  • 2.5.0



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

#heightObject (readonly)

Since:

  • 2.5.0



22
23
24
# File 'TT_Lib2/dimension.rb', line 22

def height
  @height
end

#widthObject (readonly)

Since:

  • 2.5.0



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

Examples:

dim = Dimension.new([6,7,8,9], 2, 2)
puts dim[2]
-> 8
puts dim[0,1]
-> 8

Overloads:

  • #[](index) ⇒ Object

    Parameters:

    • index (Integer)

      an Integer between 0 and self.length-1

  • #[](row, column) ⇒ Object

    Parameters:

    • row (Integer)

      an Integer between 0 and self.width-1

    • column (Integer)

      an Integer between 0 and self.height-1

Returns:

  • (Object)

    value at the given index

Since:

  • 2.5.0



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

Overloads:

  • #[]=(index) ⇒ Object

    Parameters:

    • index (Integer)

      an Integer between 0 and self.length-1

  • #[]=(row, column) ⇒ Object

    Parameters:

    • row (Integer)

      an Integer between 0 and self.width-1

    • column (Integer)

      an Integer between 0 and self.height-1

Since:

  • 2.5.0



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

Parameters:

  • index (Integer)

    an Integer between 0 and self.width-1

Returns:

  • (Array)

    column at index

Since:

  • 2.5.0



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

#columnsArray<Array>

Returns an Array containing all the columns

Returns:

  • (Array<Array>)

    an Array containing all the columns

Since:

  • 2.5.0



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

Parameters:

  • column (Boolean) (defaults to: false)

    iterates columns when true or rows when false

Yields:

  • (array, index)

    an Array representing either a row or a column depening on column

Yield Parameters:

  • array (Array)
  • index (optional, Integer)

Since:

  • 2.5.0



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

Yields:

Yield Parameters:

  • column (Array)
  • index (optional, Integer)

Since:

  • 2.5.0



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

Yields:

Yield Parameters:

  • row (Array)
  • index (optional, Integer)

Since:

  • 2.5.0



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

#inspectString

Returns:

  • (String)

Since:

  • 2.5.0



255
256
257
# File 'TT_Lib2/dimension.rb', line 255

def inspect
  return "#<#{self.class}(#{@width}x#{@height})>"
end

#lengthInteger Also known as: size

Returns the size of the Dimension

Returns:

  • (Integer)

    the size of the Dimension

Since:

  • 2.5.0



237
238
239
# File 'TT_Lib2/dimension.rb', line 237

def length
  return @width * @height
end

#mapTT::Dimension

Returns:

Since:

  • 2.5.0



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

Returns:

Since:

  • 2.5.0



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

Parameters:

  • index (Integer)

    an Integer between 0 and self.height-1

Returns:

  • (Array)

    row at index

Since:

  • 2.5.0



105
106
107
# File 'TT_Lib2/dimension.rb', line 105

def row(index)
  return @d[index * @width, @width]
end

#rowsArray<Array>

Returns an array containing all the rows

Returns:

  • (Array<Array>)

    an array containing all the rows

Since:

  • 2.5.0



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.

Parameters:

  • index (Integer)

    an Integer between 0 and self.width-1

  • new_column (Array)

    an Array of the size self.height

Returns:

  • (Array)

    the new column

Since:

  • 2.5.0



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.

Parameters:

  • index (Integer)

    an Integer between 0 and self.height-1

  • new_row (Array)

    an Array of the size self.width

Returns:

  • (Array)

    the new row

Since:

  • 2.5.0



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.

Returns:

  • (Array)

    converts the Dimension to an array.

Since:

  • 2.5.0



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