By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

dynamicly defined number of loops?

P: 5
I want to program general class for function nodes of factor graph….

Functions (I define them separately) can have different number of variables, for example f_A(x1,x2), f_B(x1, x2, x3)… Each variable may take different number of values, let say N1 for x1, N2 for x2….

In the program I need to build a table of all possible values of a function.
If I knew the number of variables taken by function I would write something like this:

//for the function A, x1[], x2[] arrays of values of the variables

values * int;

values = new * int[N1*N2];

int n=0;

for(int i=0; i<N1; i++)

for(int j=0; j<N2; j++)

values[n]=f_A(x1[i], x2[j]);

In case of f_B there would be 2 inner loops(3 in total). But what can I do if I do not know the number of variables? I think I cannot program variable number of inner loops, can I? Actually for small number of variables I can write switch…case….but is it only solution?

Thank you very much if you answer.
Jun 20 '07 #1
Share this Question
Share on Google+
2 Replies

P: 1,806
Don't quite follow what you are trying to do, but.....
I think you could achieve (sort of) what you want with a recursive function.....

Expand|Select|Wrap|Line Numbers
  2. my_function(int start, int fin, list RemainingItems)
  3. {
  4.     for(int i=start; i<fin)
  5.     {
  6.       if(!RemainingItems.IsEmpty())
  7.       {
  8.         RemainingItems.NextElement();
  9.         my_function(StartVal; FinVal; RemainingItems);
  10.       }
  11.       doProcess();
  12.     }
  13. }
Obviously your example may be a little more complex, as you need to be able to find some of the values specified in the excerpt above. I am also assuming that each nested loop has basically the same behaviour?......

Jun 20 '07 #2

Expert Mod 5K+
P: 9,197
This is an excellent place for the Visitor design pattern.

Visitor allows you to access a derived object using a base class pointer and from there you are able to access derived methods using a derived pointer.

Check out: Design Patterns by Erich Fromm, et al. Addison-Wesley 1994.

Here I would see a base class of FunctionNode with an Accept() method for the Visitor.

TwoFuncttionNode derives from FunctionNode.
ThreeFunctionNode derives from FunctionNode.

So, you create a ThreeFunctionNode object and assign it to a FunctionNode*.

Next, you create the FunctionNodeVisitor hierarchy:

TwoFuncttionNodeVisitor derives from FunctionNodeVisitor.
ThreeFunctionNodeVisitor derives from FunctionNodeVisitor.

Now, you create a ThreeFunctionNodeVisitor object and assign it to a FunctionNodeVisitor*.

Next, you call the FunctionNode::Accept() using the FunctionNodeVisitor*.

The Accept() method calls FunctionNode::DoAccept(). This is a private virtual function that is overridden by ThreeFunctionNode. You are now in ThreeFunctionNode::DoAccept with a FunctionNodeVisitor pointer that is really a ThreeFunctonNodeVisitor pointer. That makes it safe to call FunctionNodeVisitor::VisitThreeFunctionNode using the this pointer of the FunctionNode object (really the address of a ThreeFunctionNode object).

You end up in ThreeFunctionNodeVisitor::VisitThreeFunctionNode with a ThreeFunctionNode pointer. You can now access any methods of this class. Or even add methods not in the original ThreeFunctionNode class.

Visitor removes the restriction that all derived classes must support only the base class methods.

Seriously, check out that book. Page 331. It's your answer.
Jun 21 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.