Conditional looping to create partial combinations, how possible?
- From: zebulon <zebulonorguk@xxxxxxxxxxxxxx>
- Date: Tue, 17 Feb 2009 09:00:56 -0800 (PST)
Hi,
I am trying to avoid redundancy in my code and would like to know if
there is an elegant solution for this.
Let's say I have 2 vectors (va and vb) that may contain Int and String
elements respectively, or be empty. I'd like to generate all *partial*
combinations of Int/String, i.e. if one of the vectors is empty, I
still loop over the second vector elements. If both vectors are empty
I still execute once the code.
For instance: if va contains nothing and vb contains 3 strings, I
should end up with 3 objects, each containing 1 string from vb. If va
contains 2 ints and vb is empty, I should get 2 objects, each
containing 1 int. If va contains 2 ints and vb 3 strings, then I
should get 6 objects, each containing a combination (va_element,
vb_element). If both va and vb are empty, I create 1 object with no
attributes.
These examples will clarify what I want to do:
Let's say I could use a nested loop :
for (vector<int>::iterator vai = va.begin(); vai != va.end(); ++vai)
{
for (vector<string>::iterator vbi = vb.begin(); vbi != vb.end(); +
+vbi)
{
// create the object
Object* oneobject = new Object();
// store vai and vbi combination
oneobject->addInt(*vai);
oneobject->addString(*vbi);
// store the object pointer somewhere;
vector_objects.push_back(oneobject);
}
}
The problem here is that if one of my vectors is empty, no object at
all will be created.
So I solved it using some tests:
if (!va.empty() && vb.empty()) //only ints
{
for (vector<int>::iterator vai = va.begin(); vai != va.end(); +
+vai)
{
// create object
oneobject->addInt(*vai);
// store
}
}
else if (va.empty() && !vb.empty()) //only strings
for (vector<int>::iterator vbi = vb.begin(); vbi != vb.end(); ++vbi)
{
// etc...
}
else if (!va.empty() && !vb.empty()) //int and strings
{
for (vector<int>::iterator vbi = vb.begin(); vbi != vb.end(); +
+vbi)
{
for (vector<int>::iterator vbi = vb.begin(); vbi != vb.end(); +
+vbi)
{
// etc...
}
}
}
else if (!va.empty() && !vb.empty()) //no int no strings
{
// etc...
}
The problem is that there is a lot of code redundency (common parts in
the loops), and if there are more than two vectors, the number of if
statements would be impossible to manage.
Actually, instead of Int and String, if I have pointers to objects, I
know I could use inheritance/polymorphism in order to store them in
the same container. However, the objects can be heterogenous and
inheritence is not suitable.
Is there an elegant way to avoid all the if statements ?
.
- Follow-Ups:
- Re: Conditional looping to create partial combinations, how possible?
- From: Pascal J. Bourguignon
- Re: Conditional looping to create partial combinations, how possible?
- From: Mensanator
- Re: Conditional looping to create partial combinations, how possible?
- From: gw7rib
- Re: Conditional looping to create partial combinations, how possible?
- Prev by Date: Re: Regular Expression String search/match
- Next by Date: Re: Conditional looping to create partial combinations, how possible?
- Previous by thread: Nexus Programming Language
- Next by thread: Re: Conditional looping to create partial combinations, how possible?
- Index(es):
Relevant Pages
|