POV-Ray : Newsgroups : povray.general : Flatten n-dimension array? Server Time
1 Nov 2024 13:19:08 EDT (-0400)
  Flatten n-dimension array? (Message 1 to 3 of 3)  
From: [GDS|Entropy]
Subject: Flatten n-dimension array?
Date: 18 Oct 2011 09:00:01
Message: <web.4e9d76d82fef771f77e50e540@news.povray.org>
Is there a way in SDL to flatten an n-dimensional array?

Everything I've tried so far seems to require knowing the array size in advance
and accessing specifically: flat[foo] = array[x][y]

It looks like the only way to make a method which can take an array as input and
flatten it will need to have a switch statement in it to fork to appropriately
deep loops. I don't like that though.

Any other options?

Thanks,
Ian


Post a reply to this message

From: Warp
Subject: Re: Flatten n-dimension array?
Date: 18 Oct 2011 10:22:31
Message: <4e9d8ba5@news.povray.org>
[GDS|Entropy] <nomail@nomail> wrote:
> Is there a way in SDL to flatten an n-dimensional array?

> Everything I've tried so far seems to require knowing the array size in advance
> and accessing specifically: flat[foo] = array[x][y]

> It looks like the only way to make a method which can take an array as input and
> flatten it will need to have a switch statement in it to fork to appropriately
> deep loops. I don't like that though.

> Any other options?

  I'm too lazy to check now, but wouldn't dimension_size() return 0 for
dimensions higher than what the array is? (It could also cause an error,
in which case this couldn't be used.)

  A recursive macro can be created to do what you want (once you know the
dimensions of the array). It's not completely trivial to write, but shouldn't
be too difficult either.

-- 
                                                          - Warp


Post a reply to this message

From: [GDS|Entropy]
Subject: Re: Flatten n-dimension array?
Date: 18 Oct 2011 19:45:01
Message: <web.4e9e0ea59b1e54a277e50e540@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
>   I'm too lazy to check now, but wouldn't dimension_size() return 0 for
> dimensions higher than what the array is?

The way I currently handle this is by using dimensions() to set the counter in a
for loop, which then measures each dimension individually [I assume oddly shaped
arrays] and utilizes this to produce a count of indices required in the 1D
array. This works based on my testing so far [not extensive].

>   A recursive macro can be created to do what you want (once you know the
> dimensions of the array). It's not completely trivial to write, but shouldn't
> be too difficult either.

That seems to be the generic solution I've seen implemented in languages similar
to SDL. I wanted something cleaner, but that may not be an option.

If the urge strikes you, feel free to take a stab at it along with me. :p

I'll put some more effort into this, but since it isn't really for anything I'm
doing specifically, other than making a .mcr file of array utilities typically
found in many C based languages, I may abandon it for things more interesting
and less time consuming. time == money ;)

I need to make some more context-selected pseudo overloads for higher
dimensions/etc.., but so far I have:
IndexOf() [Vector, non-vector]
Contains() [Vector, non-vector]
Shuffle() [Fisher-Yates, Bruteforce]
Resize()
CountDefined()

Just those few have made a *lot* of things I usually do much less tedious.

Ian


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.