|
|
Josh @ Dreamland
|
 |
Reply #16 Posted on: February 11, 2011, 05:03:36 PM |
|
|
Prince of all Goldfish
 Location: Ohio, United States Joined: Feb 2008
Posts: 2276
|
It will work provided that the [] is explicit. If you pointerize it, it will fail. ENIGMA will need to offer its own array class. It'll be a small, templated job; the kind you don't have to be afraid to instantiate.
|
|
|
|
|
Logged
|
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble "I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
|
|
|
RetroX
|
 |
Reply #17 Posted on: February 11, 2011, 06:02:46 PM |
|
|
Master of all things Linux
 Location: US Joined: Apr 2008
Posts: 1121
|
I would be surprised if that actually works, RetroX. y2 would have the type int*. GCC can't know the size of the int*(sizeof would be useless in this case). It's the same reason you can't do "for (int& x : (int*) malloc(sizeof(int)*10))".
Basically what Josh said. It you use malloc, it won't work. If you use a fixed-size array, it will. Otherwise, you need to use a vector or some other class that has begin() and end() iterator functions. Fede-lasse: Just looked at C#'s documentation, and that is thoroughly retarded and 100% of the time, it won't be useful at all to take a multidimensional array and flatten it out into one dimension. Why would I make the matrix: 0 1 2 3 When I really just want to treat it like a list: 0 1 2 3 If you really want, someone can code a function to flatten a 2D array into a list. But to convince them, you'd have to give an example of how that would actually be useful.
|
|
|
|
« Last Edit: February 11, 2011, 06:07:15 PM by RetroX »
|
Logged
|
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)Why do all the pro-Microsoft people have troll avatars? 
|
|
|
|
|
|
|
RetroX
|
 |
Reply #20 Posted on: February 14, 2011, 05:20:42 PM |
|
|
Master of all things Linux
 Location: US Joined: Apr 2008
Posts: 1121
|
@Retro If you and Josh fail to give a concise "no," then I could just code it, I suppose...
It pretty much is a concise "no." I don't see how this would be useful at all, but it's so easy to code that I'll do it anyways (when pointed to the proper array class/struct/whatever). I certainly don't want it interfering with the normal foreach, though. I'd still like to see an example.
|
|
|
|
« Last Edit: February 14, 2011, 05:24:23 PM by RetroX »
|
Logged
|
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)Why do all the pro-Microsoft people have troll avatars? 
|
|
|
Rusky
|
 |
Reply #21 Posted on: February 14, 2011, 07:03:08 PM |
|
|
 Location: Airship Abubalay Joined: Feb 2008
Posts: 806
|
For multi-dimensional arrays: - a level editor where you need to access tiles by coordinates but also iterate over all of them in no particular order to shift them or select them all or select those that meet a certain criteria
- cellular automata like the game of life where, again, you don't really care about the order of iteration but you need to access them in a grid for drawing
- foreach could be used to recursively iterate over all the files in a directory tree
Explicitly flattening a multi-dimensional array and then iterating over that is also much less efficient than just foreaching over it. In general: - C# allows objects to be "enumerable"; one example of this would be using foreach on a normally unordered container, or through a query to access some subset of data in a certain way
- It's nice to have a standard way to iterate over all the elements of a container no matter how it's laid out- multidimensional array, unordered set, relational database, list, tree, queue, stack, etc.
Of course you can always just write out the loop yourself but that's tedious, doesn't have a standard interface, which would allow for general algorithms on collections of things, etc. Multi-dimensional foreach doesn't have to interfere with regular foreach anyway: for (int a : new int[][]) vs for (int[] a : new int[][])
|
|
|
|
« Last Edit: February 14, 2011, 07:05:25 PM by Rusky »
|
Logged
|
|
|
|
RetroX
|
 |
Reply #22 Posted on: February 14, 2011, 07:19:15 PM |
|
|
Master of all things Linux
 Location: US Joined: Apr 2008
Posts: 1121
|
I kind of see what you're going for. Explicitly flattening isn't a good idea, I guess.
Although, if you think of how a regular fixed-size 2D array is stored, you can still iterate through it easily:
In terms of memory:
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}is equivalent to:
{1, 2, 3, 4, 5, 6, 7, 8, 9} And as so, simply pointing to the beginnings and endings of the array will allow it to be iterable. The same applies to any n-dimensional array. overall_index = Σ(dimension * index)
|
|
|
|
« Last Edit: February 14, 2011, 07:50:28 PM by RetroX »
|
Logged
|
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)Why do all the pro-Microsoft people have troll avatars? 
|
|
|
|
|
RetroX
|
 |
Reply #24 Posted on: February 15, 2011, 07:47:14 PM |
|
|
Master of all things Linux
 Location: US Joined: Apr 2008
Posts: 1121
|
That's true for regular C arrays, but it doesn't work for anything else- arrays that store their size, containers with an unknown layout, interfaces that look like containers but access data some other way, etc.
It doesn't matter where the data array is stored, as long as it's all in one piece for it to work this way. Even if it doesn't, you can create an iterator class that will go through the array's elements and make begin() and end() functions return that class instead of a basic pointer. It just needs ++ and * operator overloads. EDIT: And I suppose that that won't work for an n-dimensional array. We can just add support for 2D arrays and leave it at that, though. EDIT 2: Or, maybe, with a little template recursion, I can get it to work with n-dimensional. I'll look into it.
|
|
|
|
« Last Edit: February 15, 2011, 08:18:21 PM by RetroX »
|
Logged
|
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)Why do all the pro-Microsoft people have troll avatars? 
|
|
|
|
|
|
|
|
|
|
|
|
|
|