Table<T>, a convenient 2d array with neat features
Monkey Forums/Monkey Code/Table<T>, a convenient 2d array with neat features
| ||
| Heya. I dunno if I posted this before, but I wrote this class for use in some of the puzzle games I've been working on for the past year. Features include the ability to get/set/iterate through individual rows or columns, native 1d data access, 2d data accessors (Get/Set), and support for exceptions to prevent undefined behavior on the desktop target. It may not be 100% complete, although I haven't needed to add new features to it for a while for my purposes. If you guys followed me on gist/github, you may have seen this code already! But for everyone else, here it is: https://gist.github.com/nobuyukinyuu/7763863 A quick example of how this is easy to use:
Import table
Function Main:Int()
'Makes a 5x5 table
Local t:=New Table<Int>(5,5)
'Initialize each cell in a row to the same value?
Local rowNum:Int
For local row:= EachIn t.Rows()
For local i:Int = 0 until t.Width 'We could've also said eachIn row, or until row.Length()
row[i] = rowNum
Next
rowNum+=1
Next
'Randomize the entire table.
For local i:Int = 0 until t.data.Length
t.data[i] = Rnd($FFFFFFF)
Next
'Gimme a copy of column 3.
Local col:Int[] = t.Column(3)
'Let's change all of column 3 to 0.
For local i:Int = 0 until col.Length
col[i] = 0
Next
t.SetColumn(col, 3)
'Catching an out of range index.
Try
Local invalid:Int = t.Get(-1,-1)
Catch ex:TableIndexOutOfRangeError
Print ("Invalid cell!")
End Try
End Function
|
| ||
| Very nice! you could also include Find:<T>[](value). i also wonder if getting data from columns and such should be returned as a TableData class, which is essentially an extended stack. up to you. |
| ||
| what is a TableData class? Mainly, I returned the individual series as arrays because I don't see an advantage to using a stack in those cases. the dimensions of the array are "fixed" after initializing, so there wouldn't be advantageous to return a Stack, unless (maybe?) to use a built-in sorter... Find:T[](val:T) might be a good idea although I'm not sure what the optimal find strategy would be since there's no set order to the data. A good use case example of where this would be useful to be built in vs. performing the search yourself would be "nice to have". |
| ||
| Nice! Width and Height should probably be read only. I mean it's pretty clear one shouldn't change them, but some day at 3 pm it might happen ... |