Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > variation with repetitions in C++

Reply
Thread Tools

variation with repetitions in C++

 
 
Branka
Guest
Posts: n/a
 
      04-04-2006
Hi I have 20 'for' loops to create more than 40 million variations with
repetitions. More precisely: I have total of nine factors with three
possible levels (3^9) and 11 factors with 2 possible levels (2^11),
which gives total of 40 310 784 possible variations.

for (a=0; a<3; a++)
{for (b=0; b<2; b++)
{for (c=1; c<3; c++)
{for (d=0; d<3; d++)
{for (e=0; e<3; e++)
{for (f=1; f<3; f++)
{for (g=0; g<3; g++)
{for (h=1; h<3; h++)
{for (i=0; i<3; i+=2)
{for (j=0; j<3; j++)
{for (k=1; k<3; k++)
{for (l=0; l<3; l++)
{for (m=0; m<3; m+=2)
{for (n=0; n<3; n++)
{for (p=0; p<3; p+=2)
{for (q=0; q<3; q++)
{for (t=1; t<3; t++)
{for (u=0; u<3; u++)
{for (x=1; x<3; x++)
{for (y=1; y<3; y++)


Using this approach I have to create a text file which lists all
possible variation and access it for some additional operations.

I was wondering if anyone has an idea how to make this more 'object
oriented' by creating a code which handles the pattern of these
variation and thus give me the ability to skip creation of text file
(requires a lots of memory) and simply create one variation after the
other so that I can do all necessary operation on only one variation at
the time.

I would really appreciate any help or hint where I can find this code.

B

 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      04-04-2006
> I was wondering if anyone has an idea how to make this more 'object
> oriented' by creating a code which handles the pattern of these
> variation and thus give me the ability to skip creation of text file
> (requires a lots of memory) and simply create one variation after the
> other so that I can do all necessary operation on only one variation at
> the time.
>
> I would really appreciate any help or hint where I can find this code.
>
> B
>


You can use std::set and std::next_permutation, although they are not
object oriented.

Regards,
Ben
 
Reply With Quote
 
 
 
 
Branka
Guest
Posts: n/a
 
      04-04-2006
Can you be a bit more specific please.

B

 
Reply With Quote
 
Heinz Ozwirk
Guest
Posts: n/a
 
      04-04-2006
"Branka" <(E-Mail Removed)> schrieb im Newsbeitrag news:(E-Mail Removed) oups.com...
> Hi I have 20 'for' loops to create more than 40 million variations with
> repetitions. More precisely: I have total of nine factors with three
> possible levels (3^9) and 11 factors with 2 possible levels (2^11),
> which gives total of 40 310 784 possible variations.
>

....
>
> I was wondering if anyone has an idea how to make this more 'object
> oriented' by creating a code which handles the pattern of these
> variation and thus give me the ability to skip creation of text file
> (requires a lots of memory) and simply create one variation after the
> other so that I can do all necessary operation on only one variation at
> the time.


If you need objects very badly you could implement one class which simply counts from 0 to n-1 for a given n:

class Counter
{
public:
Counter(size_t n): limit(n), current(0) {}
bool Next() { return (current % limit) != 0;
private:
size_t limit;
size_t current;
};

Then create another class to represent a "string" of counters:

class Counters
{
Counters(size_t n, size_t* limits): elements(n)
{
for (size_t i = 0; i < n; ++i)
{
elements[i] = Counter(limits[i]);
}
}
bool Next()
{
for (size_t i = 0; i < elements.size(); ++i)
{
if (elements[i].Next()) return true;
}
return false;
}
private:
std::vector<Counter> elements;
};

Ok, add some method to access the values of all those counters, create an instance of Counters with 20 elements and the limits you need and the start a loop which ends when Counters::Next returns false.

Of cause you can also forget about objects and simply count from 0 to 2^11 + 3^9 and use / and % to extract your "digits" from that number.

HTH
Heinz
 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      04-04-2006
Branka wrote:
> Can you be a bit more specific please.
>
> B
>


Sorry, I meant std::multiset since repetition is allowed. What you want
to do, I think, is to create the permutations of a set of 1, 2 and 3.
Consult your book on std::next_permutation it should be very
straightforward.

Regards,
Ben
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Matching repetitions with /g Wolfram Humann Perl Misc 6 08-23-2011 01:30 PM
How to count some repetitions nick048 C++ 2 11-01-2006 12:43 PM
How to count some repetitions nick048 C++ 0 11-01-2006 10:22 AM
How to generate all possible permutations with repetitions? darin dimitrov C Programming 4 10-27-2004 08:18 AM
count word repetitions for each pair of lines tony Perl Misc 1 07-23-2003 02:22 PM



Advertisments