Pages: 1 [2] 3
  Print  
Author Topic: foreach and Serializable  (Read 1811 times)
Offline (Unknown gender) luiscubal
Reply #15 Posted on: February 11, 2011, 07:11:52 AM
Member
Joined: Jun 2009
Posts: 476

View Profile Email
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))".
Logged
Offline (Male) Josh @ Dreamland
Reply #16 Posted on: February 11, 2011, 05:03:36 PM

Prince of all Goldfish
Developer
Location: Ohio, United States
Joined: Feb 2008
Posts: 2276

View Profile Email
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
Offline (Male) RetroX
Reply #17 Posted on: February 11, 2011, 06:02:46 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1121
MSN Messenger - classixretrox@gmail.com
View Profile Email
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)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Rusky
Reply #18 Posted on: February 13, 2011, 09:33:03 PM
Resident Troll
Location: Airship Abubalay
Joined: Feb 2008
Posts: 806
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
Great lack of imagination, Retro.
Logged
[www.abubalay.com]

Time traveling to Feb. 2014, back later.
Offline (Male) Fede-lasse
Reply #19 Posted on: February 14, 2011, 06:44:54 AM

AI Programmer
Fede
Location: Denmark, Europe
Joined: Oct 2008
Posts: -2342534e-10
MSN Messenger - lasse1706@hotmail.com
View Profile WWW Email
He just hates M$. :eng101:

@Retro
If you and Josh fail to give a concise "no," then I could just code it, I suppose...
Logged
Call me Fede.
Offline (Male) RetroX
Reply #20 Posted on: February 14, 2011, 05:20:42 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1121
MSN Messenger - classixretrox@gmail.com
View Profile Email
@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)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Rusky
Reply #21 Posted on: February 14, 2011, 07:03:08 PM
Resident Troll
Location: Airship Abubalay
Joined: Feb 2008
Posts: 806
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
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:
Code: [Select]
for (int a : new int[][]) vs
Code: [Select]
for (int[] a : new int[][])
« Last Edit: February 14, 2011, 07:05:25 PM by Rusky » Logged
[www.abubalay.com]

Time traveling to Feb. 2014, back later.
Offline (Male) RetroX
Reply #22 Posted on: February 14, 2011, 07:19:15 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1121
MSN Messenger - classixretrox@gmail.com
View Profile Email
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:
Code: [Select]
{{1, 2, 3},
 {4, 5, 6},
 {7, 8, 9}}
is equivalent to:
Code: [Select]
{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)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Rusky
Reply #23 Posted on: February 14, 2011, 10:36:56 PM
Resident Troll
Location: Airship Abubalay
Joined: Feb 2008
Posts: 806
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
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.
Logged
[www.abubalay.com]

Time traveling to Feb. 2014, back later.
Offline (Male) RetroX
Reply #24 Posted on: February 15, 2011, 07:47:14 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1121
MSN Messenger - classixretrox@gmail.com
View Profile Email
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)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Rusky
Reply #25 Posted on: February 16, 2011, 02:11:28 PM
Resident Troll
Location: Airship Abubalay
Joined: Feb 2008
Posts: 806
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
All the examples I listed don't store all their data in one piece...
Logged
[www.abubalay.com]

Time traveling to Feb. 2014, back later.
Offline (Male) RetroX
Reply #26 Posted on: February 16, 2011, 04:32:02 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1121
MSN Messenger - classixretrox@gmail.com
View Profile Email
I know.  Which is why you'd need an iterator class.

Code: [Select]
struct array
{
  size_t length;
  int* data; // would work with any type
};

struct array2d
{
  size_t length;
  array* data;
};

struct iter2d
{
  array2d* arr;
  size_t row;
  size_t column;
  iter2d& operator ++()
  {
    if (column < arr->data[row].length)
    {
      ++column;
    }
    else
    {
      column = 0;
      ++row;
    }
    return *this;
  }
  int& operator *() { return arr->data[row].data[column]; }
};
« Last Edit: February 16, 2011, 04:34:26 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)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Rusky
Reply #27 Posted on: February 17, 2011, 12:29:22 PM
Resident Troll
Location: Airship Abubalay
Joined: Feb 2008
Posts: 806
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
Which is what foreach would use...
Logged
[www.abubalay.com]

Time traveling to Feb. 2014, back later.
Offline (Male) Fede-lasse
Reply #28 Posted on: February 17, 2011, 03:38:51 PM

AI Programmer
Fede
Location: Denmark, Europe
Joined: Oct 2008
Posts: -2342534e-10
MSN Messenger - lasse1706@hotmail.com
View Profile WWW Email
... :P
Logged
Call me Fede.
Offline (Male) RetroX
Reply #29 Posted on: February 17, 2011, 09:29:16 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1121
MSN Messenger - classixretrox@gmail.com
View Profile Email
I don't know if I mis-explained or something but oh well.

It can be done and might be done eventually.
« Last Edit: February 17, 2011, 09:37:17 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)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Pages: 1 [2] 3
  Print