Is there an easy way to fake an unknown size multi-dimension array using a flat one? I have an application where 90开发者_C百科% of the uses of the array would be easier/quicker without using recursion, and a single requirement that needs to add some depth to the array. The only way I can think of it would be keeping a running list of start indexes and end indexes, where the 1D array would look like this:
[0] = 1
[1] = 2
[2] = 3
[3] = 4
[4] = 5
...and the start/end lists would look like this:
Start End
----- ---
[0] = 1 [0] = 2
[1] = 3 [1] = 4
Which would represent a multi-dimension array that looks like this:
[0] = 1
[1] = [0] = 2
[1] = 3
[2] = [0] = 4
[1] = 5
This would work with more than 2 dimensions, but at that point I'm having trouble figuring out how to determine which depth I was at given an index in the original 1D array and the start/end lists. I'm also having a hard time figuring out what search terms to use to look for this type of thing. Any general guidance/ideas would be appreciated. Thanks.
Edit - To give some context, this is for supporting nested transactions in a command pattern implementation. The 1D array contains commands, and the artificial depths are only there to give names to each transaction. Since the transactions will be used sparingly, it seems apparent that quickly going through a small list of ints would be faster than recursively going through a multi-dimensional array of commands, and checking at each index whether or not there was a single command or an array of commands within.
In general, storing end positions seems redundant. An array ends where the next array starts, doesn't it?
Secondly, the problem is that for each extra dimension you need additional lookup lists, unless they have fixed size. I do not know if this comes down to being more efficient than your original solution.
精彩评论