Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #9253 > unrolled thread

Unique array of arrays - how can I do this?

Started bythomas.lehmann.private@googlemail.com
First post2013-05-04 04:51 -0700
Last post2013-05-06 00:06 +0200
Articles 4 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  Unique array of arrays - how can I do this? thomas.lehmann.private@googlemail.com - 2013-05-04 04:51 -0700
    Re: Unique array of arrays - how can I do this? The Natural Philosopher <tnp@invalid.invalid> - 2013-05-04 13:20 +0100
    Re: Unique array of arrays - how can I do this? Denis McMahon <denismfmcmahon@gmail.com> - 2013-05-04 12:25 +0000
    Re: Unique array of arrays - how can I do this? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-06 00:06 +0200

#9253 — Unique array of arrays - how can I do this?

Fromthomas.lehmann.private@googlemail.com
Date2013-05-04 04:51 -0700
SubjectUnique array of arrays - how can I do this?
Message-ID<e6f6a70e-e5a3-435b-b354-273c2386153e@googlegroups.com>
Hi,

While calculation I get a solution as an array of integers.
There're many solutions and I would like to keep them in an array.

Because of the algorithm there are different paths to get to
the same solution but I want to have it once only.

I know that I can use array_unique but that's too slow
when called too often. I can't wait until end of calculation
because then I run into memory problems.

So I need an efficient way to add elements once only to
an array while calculation with an array as element.

How can I do this?

[toc] | [next] | [standalone]


#9256

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-05-04 13:20 +0100
Message-ID<km2udr$8cg$1@news.albasani.net>
In reply to#9253
On 04/05/13 12:51, thomas.lehmann.private@googlemail.com wrote:
> Hi,
>
> While calculation I get a solution as an array of integers.
> There're many solutions and I would like to keep them in an array.
>
> Because of the algorithm there are different paths to get to
> the same solution but I want to have it once only.
>
> I know that I can use array_unique but that's too slow
> when called too often. I can't wait until end of calculation
> because then I run into memory problems.
>
> So I need an efficient way to add elements once only to
> an array while calculation with an array as element.
>
> How can I do this?
1/ write in in a proper language like C
2/. use a database to store stuff in which has fast algortihms for 
dealing with matrix type stuff.

because it sounds like that's what you have - a 2D matrix of integers
you could produce e.g a row hash and index that which would make it fast 
to check if you have a POSSIBLE match, and then sort through any 
possibles to see if they are exactmatches.

depending on the size though,I'd be tempted to make it a C coded RAM 
based thing.
preferably using statically allocated memory if you know a maximum size, 
to avoid pointer indirection stuff.

If its huge, I'd use a database and a hash table.


-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.

[toc] | [prev] | [next] | [standalone]


#9258

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-05-04 12:25 +0000
Message-ID<km2un0$ole$2@dont-email.me>
In reply to#9253
On Sat, 04 May 2013 04:51:28 -0700, thomas.lehmann.private wrote:

> Hi,
> 
> While calculation I get a solution as an array of integers. There're
> many solutions and I would like to keep them in an array.
> 
> Because of the algorithm there are different paths to get to the same
> solution but I want to have it once only.
> 
> I know that I can use array_unique but that's too slow when called too
> often. I can't wait until end of calculation because then I run into
> memory problems.
> 
> So I need an efficient way to add elements once only to an array while
> calculation with an array as element.
> 
> How can I do this?

Given that your answers are integers, at the point that you put your 
answers into the array, assuming that the answer is in $ans:

if ( !isset( $arr ) )
  $arr = array();
$arr[$ans] = $ans;

When you finish calculations:

sort( $arr );
foreach ( $arr as $k=>$v )
  echo "Answer {$k} is {$v}.\n";

or:

sort( $arr );
echo "The answers are: " . implode( $arr, ", " ) . ".\n";

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#9260

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-05-06 00:06 +0200
Message-ID<1654165.He4Uvg28Du@PointedEars.de>
In reply to#9253
thomas.lehmann.private@googlemail.com wrote:

> While calculation I get a solution as an array of integers.
> There're many solutions and I would like to keep them in an array.
> 
> Because of the algorithm there are different paths to get to
> the same solution but I want to have it once only.
> 
> I know that I can use array_unique but that's too slow
> when called too often. I can't wait until end of calculation
> because then I run into memory problems.
> 
> So I need an efficient way to add elements once only to
> an array while calculation with an array as element.
> 
> How can I do this?

You need to be more specific about your problem.  What is the calculation?  
What kind of integers are in one array?  How do you add them?  How many 
integers?  How many arrays of integers in your array?

And please get a real name.  That said, avoid Google Groups for posting; use 
a newsreader instead.

<http://www.catb.org/~esr/faqs/smart-questions.html>


PointedEars
-- 
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
  -- Richard Cornford, comp.lang.javascript, 2011-11-14

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web