In order to not escalate the CVS war, I'll present my solution for how to deal with cyclic arrays in Array.flatten here:
//! Flatten a multi-dimensional array to a one-dimensional array. //! @trows //! @[flatten] throws an error if @[a] is cyclic. array flatten(array a) { array ret=({}); multiset seen = (<>);
void low_flatten(array aa) { foreach(aa, mixed b) if(arrayp(b)) { if(!sizeof([array]b)) continue; if(seen[b]) error("Trying to flatten a cyclic array.\n"); seen[b]=1; low_flatten([array]b); } else ret += ({ b }); }; low_flatten(a); return ret; }
(aside from the fact the programs like the one below hangs pike)
void foo() {
void bar(array i) { error("gazonk\n"); };
array a = ({ 0 }); a[0]=a; bar(a); }
Your code will throw an error for the perfectly legitimate case:
array(array(int)) a = ({({0, 1})}); flatten(a+a);
/ Henrik Grubbström (Lysator)
Previous text:
2003-04-28 16:12: Subject: Array.flatten
In order to not escalate the CVS war, I'll present my solution for how to deal with cyclic arrays in Array.flatten here:
//! Flatten a multi-dimensional array to a one-dimensional array. //! @trows //! @[flatten] throws an error if @[a] is cyclic. array flatten(array a) { array ret=({}); multiset seen = (<>);
void low_flatten(array aa) { foreach(aa, mixed b) if(arrayp(b)) { if(!sizeof([array]b)) continue; if(seen[b]) error("Trying to flatten a cyclic array.\n"); seen[b]=1; low_flatten([array]b); } else ret += ({ b }); }; low_flatten(a); return ret; }
(aside from the fact the programs like the one below hangs pike)
void foo() {
void bar(array i) { error("gazonk\n"); };
array a = ({ 0 }); a[0]=a; bar(a); }
/ Martin Nilsson (har bott i google)
While the fix grubba made does not necessarily produce useful results for cyclic structures, it seemed like a decent solution IMHO.
/ Johan Sundström (folkskådare)
Previous text:
2003-04-28 16:12: Subject: Array.flatten
In order to not escalate the CVS war, I'll present my solution for how to deal with cyclic arrays in Array.flatten here:
//! Flatten a multi-dimensional array to a one-dimensional array. //! @trows //! @[flatten] throws an error if @[a] is cyclic. array flatten(array a) { array ret=({}); multiset seen = (<>);
void low_flatten(array aa) { foreach(aa, mixed b) if(arrayp(b)) { if(!sizeof([array]b)) continue; if(seen[b]) error("Trying to flatten a cyclic array.\n"); seen[b]=1; low_flatten([array]b); } else ret += ({ b }); }; low_flatten(a); return ret; }
(aside from the fact the programs like the one below hangs pike)
void foo() {
void bar(array i) { error("gazonk\n"); };
array a = ({ 0 }); a[0]=a; bar(a); }
/ Martin Nilsson (har bott i google)
On Mon, Apr 28, 2003 at 04:15:03PM +0200, Martin Nilsson (har bott i google) @ Pike (-) developers forum wrote:
if(!sizeof([array]b)) continue;
^^^^^^^
Sorry for intrusion... But what this construct means in this context? Is it from v7.5 or v7.4?
I couldn't find anything in the docs (except for "a list of lvalues", but this is not the case, IMHO).
Regards, /Al
It doesn't mean more then that we tell the compiler we *know* it's an array there, so don't warn me about it. It has no effect outside the type warning system.
/ Mirar
Previous text:
2003-04-28 17:08: Subject: Re: Array.flatten
On Mon, Apr 28, 2003 at 04:15:03PM +0200, Martin Nilsson (har bott i google) @ Pike (-) developers forum wrote:
if(!sizeof([array]b)) continue;
^^^^^^^
Sorry for intrusion... But what this construct means in this context? Is it from v7.5 or v7.4?
I couldn't find anything in the docs (except for "a list of lvalues", but this is not the case, IMHO).
Regards, /Al
/ Brevbäraren
pike-devel@lists.lysator.liu.se