473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

many for cycles

Hi,

I have an array and I want to make many "for" cycles as follows:

int[] a=new int[n];
....//init a

for (i1=0;i1<a.Length;i1++)
for (i2=0;i2<a.Length-1;i2++)
for (i3=0;i3<a.Length-2;i3++)
...
for (ik=0;ik<a.Length-k-1;ik++)
{
f(a[i1],a[i2]....) //some operation over
//selected elements
}

I want the count of "for" cycles to be set at run time as parameter.

How can I do this? I want this to be very fast.
Thanks.

Nov 17 '05 #1
3 1714
Hi,

Why don't you create a function with a single "for" and make recursive
calls? That code doesn't look right at all.

Hope this helps
Salva

"xx****@abv.bg" wrote:
Hi,

I have an array and I want to make many "for" cycles as follows:

int[] a=new int[n];
....//init a

for (i1=0;i1<a.Length;i1++)
for (i2=0;i2<a.Length-1;i2++)
for (i3=0;i3<a.Length-2;i3++)
...
for (ik=0;ik<a.Length-k-1;ik++)
{
f(a[i1],a[i2]....) //some operation over
//selected elements
}

I want the count of "for" cycles to be set at run time as parameter.

How can I do this? I want this to be very fast.
Thanks.

Nov 17 '05 #2
Hi,

What is what you want to do?

Using your current code it will be SLOW

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<xx****@abv.bg> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,

I have an array and I want to make many "for" cycles as follows:

int[] a=new int[n];
...//init a

for (i1=0;i1<a.Length;i1++)
for (i2=0;i2<a.Length-1;i2++)
for (i3=0;i3<a.Length-2;i3++)
...
for (ik=0;ik<a.Length-k-1;ik++)
{
f(a[i1],a[i2]....) //some operation over
//selected elements
}

I want the count of "for" cycles to be set at run time as parameter.

How can I do this? I want this to be very fast.
Thanks.

Nov 17 '05 #3
Salvador's suggestion is the most interesting, but I'm not sure it will
work... Recursive calls require you to pass a variable number of parameters
which can be a bit inefficient.

On the other hand, you can create a special, multi-index iterator object.
The object would be a chain-of-responsibility pattern, where only one member
of the chain operates, but with the twist that the chain will "double-dip".
At runtime, you create the structure based on the number of "loops" you want
to iterate over.

The challenge is going to be how to get the actual values indexed back up
the chain.

So, I can see three "chain methods" where a chain method is a method that
operates on the object or another object below it in the chain.
..Increment
..Reset
..Collect
where collect returns an array containing the values you will ultimately
pass to your function.

(I guess the Collect method is technically a decorator method, in that it
operates on each object in the chain, not just one object in the chain.)

public interface IRespIterator
{
public bool Increment();
public void Reset();
public void Collect(int arg, int[] list);
}

public class ResponsibleTerminator : IRespIterator
{
public bool Increment()
{ return false; }
public void Reset() { }
public void Collect(int[] myarray, int arg, int[] list)
{
f(list); // here is where you call your function with the variable
list of parameters
}
}

public class ResponsibleIterator : IRespIterator
{
private ResponsibleIterator childIterator ;
private int myLimit = 0;
private int myCurrent = 0;

public ResponsibleIterator(int limit, IRespIterator mychild)
{
childIterator = mychild;
myLimit = limit;
}
public bool Increment()
{
if (childIterator.Increment() == True)
return True;

if (myCurrent < myLimit)
{
myCurrent++;
childIterator.Reset();
return True;
}
else
return False;
}
public void Reset()
{
myCurrent = 0;
childIterator.Reset();
}
void Collect(int[] myarray, int arg, int[] list)
{
list[arg] = myarray[myCurrent];
arg++;
childIterator.Collect(myarray, arg, list);
}
}

Now, when you want to iterate over three loops, creating a list of three
elements,
you could do this:

//---------- in the calling class

IRespIterator newlist = new ResponsibleTerminator();

For (int i = 0; i < loopcount; i++)
{
newlist = new ResponsibleIterator(a.Length - i, newlist);
}

int[] paramlist = new int[loopcount];

Do while (newlist.Increment())
{
newlist.Collect(a, 0, paramlist);
}
All done.

Warning... air code. I did not compile this or debug it. I am known for
making goofy syntax errors due to the fact that I program in many languages,
so make sure you understand what I am trying to do.

Note: the code above is pretty much all of the code you'd need to do this.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
<xx****@abv.bg> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,

I have an array and I want to make many "for" cycles as follows:

int[] a=new int[n];
...//init a

for (i1=0;i1<a.Length;i1++)
for (i2=0;i2<a.Length-1;i2++)
for (i3=0;i3<a.Length-2;i3++)
...
for (ik=0;ik<a.Length-k-1;ik++)
{
f(a[i1],a[i2]....) //some operation over
//selected elements
}

I want the count of "for" cycles to be set at run time as parameter.

How can I do this? I want this to be very fast.
Thanks.

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul Rowe | last post by:
Hello; I am wondering if anyone knows of a way to issue the CONNECT BY clause on data that contains cycles? I have a business requirement that specifies that cycles could be present in the data...
11
by: thechaosengine | last post by:
Hi all, I have a very general but quite significant question about objects. My question is, when should I create them? I know thats a crap question so let me explain a bit further. Lets...
4
by: Kevin Klein | last post by:
I am talking of C/C++ on unix platform using gcc. So feel free to take off any crossposted NG's if others are not offended, but I seek generic C/C++ and also unix and gcc specific answers. ...
11
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple...
10
by: Nitin | last post by:
Ppl , Want to have ur opinions on having function calls like the one stated below: function funcA ( struct A *st_A , struct B *st_B ) { st_A->a = st_B->a
7
by: Lalasa | last post by:
Hi, Can anybody tell me how many cpu cycles File.copy would take and how many cpu cycles File.Move would take? CFile::Rename in C++ takes just one cpu cycle. As there is no File.Rename in C#,...
23
by: CMM | last post by:
Just discovered yet another out and out *retarded* bug in VS2005... this time in the way Application_Startup /w the Application Framework enabled works (yes, it's a BUG... it's already doc'd in the...
4
by: Martin Blais | last post by:
Hi I'm a tad confused over a problem involving cycles between packages. Assume the following sets of files:: driver.py a/__init__.py a/alice.py
8
by: crossmar | last post by:
Could someone help me with this delimma? I have three tables: -tblProspects (contains prospect info, name, address, etc.) -tblActiveCycle (contains Cycle info that prospect is currently in) ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.