Last active
December 30, 2015 02:39
-
-
Save nobuyukinyuu/7763863 to your computer and use it in GitHub Desktop.
Preliminary code for a 2d container.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class Table<T> | |
Field Width:Int, Height:Int | |
Field data:T[] | |
'Summary: Creates a new data table of the specified size. | |
Method New(width:Int, height:Int) | |
If width <= 0 or height <= 0 | |
Print("Warning: Table of invalid dimensions defined. (" + width + "," + height + ")") | |
Throw New TableInitError() | |
Else | |
Width = width; Height = height | |
data = data.Resize(width * height) | |
End If | |
End Method | |
'Summary: Returns the table to uninitialized values. | |
Method Clear:Void() | |
'I wonder if this is a good idea | |
Local newdata:T[] | |
newdata = newdata.Resize(Width * Height) | |
data = newdata | |
End Method | |
'Summary: Returns the value at (x,y). | |
Method Get:T(x:Int, y:Int) | |
#If CONFIG="debug" | |
If (0 > x) Or (x >= Width) Then Print("Warning: X is out of range. " + x) | |
If (0 > y) Or (y >= Height) Then Print("Warning: Y is out of range. " + y) | |
If (0 > x) Or (x >= Width) Or (0 > y) Or (y >= Height) | |
Throw New TableIndexOutOfRangeError() | |
End If | |
#EndIf | |
Return data[y * Width + x] | |
End Method | |
'Summary: Sets the value at (x,y). | |
Method Set:Void(value:T, x:Int, y:Int) | |
#If CONFIG="debug" | |
If (0 > x) Or (x >= Width) Then Print("Warning: X is out of range. " + x) | |
If (0 > y) Or (y >= Height) Then Print("Warning: Y is out of range. " + y) | |
If (0 > x) Or (x >= Width) Or (0 > y) Or (y >= Height) | |
Throw New TableIndexOutOfRangeError() | |
End If | |
#EndIf | |
data[y * Width + x] = value | |
End Method | |
'Summary: Returns an array representing a row in the table structure. | |
Method Row:T[] (index:Int) | |
If (0 > index) Or (index >= Height) Then Throw New TableIndexOutOfRangeError() | |
Local out:T[] | |
out = out.Resize(Width) | |
For Local i:Int = 0 Until Width | |
out[i] = data[ (index * Width) + i] | |
Next | |
Return out | |
End Method | |
'Summary: Returns an array representing a column in the table structure. | |
Method Column:T[] (index:Int) | |
If (0 > index) Or (index >= Width) Then Throw New TableIndexOutOfRangeError() | |
Local out:T[] | |
out = out.Resize(Height) | |
For Local i:Int = 0 Until Height | |
out[i] = data[ (i * Width) + index] | |
Next | |
Return out | |
End Method | |
'Summary: Sets a row of values to the ones in [value]. | |
Method SetRow:Void(value:T[], index:Int) | |
For Local i:Int = 0 Until Width | |
data[ (index * Width) + i] = value[i] | |
Next | |
End Method | |
'Summary: Sets a column of values to the ones in [value]. | |
Method SetColumn:Void(value:T[], index:Int) | |
For Local i:Int = 0 Until Height | |
data[ (i * Width) + index] = value[i] | |
Next | |
End Method | |
'Summary: Provides an object enumerating all rows in the table. | |
Method Rows:TableRows<T>() | |
Return New TableRows<T>(Self) | |
End Method | |
'Summary: Provides an object enumerating all columns in the table. | |
Method Columns:TableColumns<T>() | |
Return New TableColumns<T>(Self) | |
End Method | |
End Class | |
'Summary: Thrown when table doesn't init correctly. | |
Class TableInitError Extends Throwable | |
End Class | |
'Summary: Thrown when a row or column value is out of range. | |
Class TableIndexOutOfRangeError Extends Throwable | |
End Class | |
'The below classes are for allowing EachIn enumeration of rows and columns. | |
'#Region Row/Column Enumerators | |
Class TableRows<T> | |
Private | |
Field table:Table<T> | |
Public | |
Method New(table:Table<T>) | |
Self.table = table | |
End | |
Method ObjectEnumerator:RowEnumerator<T>() | |
Return New RowEnumerator<T>(table) | |
End | |
End Class | |
Class TableColumns<T> | |
Private | |
Field table:Table<T> | |
Public | |
Method New(table:Table<T>) | |
Self.table = table | |
End | |
Method ObjectEnumerator:ColumnEnumerator<T>() | |
Return New ColumnEnumerator<T>(table) | |
End | |
End Class | |
Class RowEnumerator<T> | |
Private | |
Field table:Table<T> | |
Field index:Int | |
Public | |
Method New(table:Table<T>) | |
Self.table = table | |
End Method | |
Method HasNext:Bool() | |
Return index < table.Height | |
End Method | |
Method NextObject:T[] () | |
index+=1 | |
Return table.Row(index - 1) | |
End Method | |
End Class | |
Class ColumnEnumerator<T> | |
Private | |
Field table:Table<T> | |
Field index:Int | |
Public | |
Method New(table:Table<T>) | |
Self.table = table | |
End Method | |
Method HasNext:Bool() | |
Return index < table.Width | |
End Method | |
Method NextObject:T[] () | |
index+=1 | |
Return table.Column(index - 1) | |
End Method | |
End Class | |
'#End Region |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment