![]() | ![]() |
|
Welcome to the HEXUS.community discussion forums forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and other features. By joining our free community you will have access to post topics, respond to polls and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! |
|
|||||||
| Software and web development Databases, graphics, programming, scripting and web development. |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
Ah, Mrs. Peel!
Join Date: Oct 2003
Location: Hertfordshire, England
Posts: 3,317
Thanks: 2
Thanked 4 Times in 4 Posts
|
Loops in C++
One of the classes I use in a project of mine is a combinatorial class - it's essentially a list of digits, and each time it is incremented it represents another unique set of digits. For instance, say you're choosing three digits from five, the increments would be:
[0,1,2] [0,1,3] [0,1,4] [0,2,3] and so on. The problem I have is with looping. It has a method .isAtEnd() which simply returns a Boolean true if the combinatorial is the final one generated by the class. Unfortunately, this doesn't lend itself especially well to looping. To iterate throw each value, a loop could look like this: Code:
Combination combination(args...);
while (true) {
doStuff(combination);
if (combination.isAtEnd()) {
break;
} else {
++combination;
}
}
Code:
Combination combination(args...);
doStuff(combination);
while (!combination.isAtEnd()) {
++combination;
doStuff(combination);
}
Code:
Combination combination(args...);
while (!combination.isAtEnd()) {
++combination;
doStuff(combination);
}
Thanks Mike |
|
"Well, there was your Uncle Tiberius who died wrapped in cabbage leaves but we assumed that was a freak accident."
|
|
|
|
|
#2 (permalink) |
|
Seething Cauldron of Hatred
Join Date: Aug 2005
Posts: 5,502
Thanks: 39
Thanked 124 Times in 102 Posts
|
Re: Loops in C++
ok, first off i'm drunk, second off i'm not sure i've understood what your asking, but do you want something like
Code:
do
{
++combination;
doStuff(combination);
} while (!combination.isAtEnd());
|
|
throw new ArgumentException (String, String, Exception)
|
|
|
|
|
#3 (permalink) |
|
Ah, Mrs. Peel!
Join Date: Oct 2003
Location: Hertfordshire, England
Posts: 3,317
Thanks: 2
Thanked 4 Times in 4 Posts
|
Re: Loops in C++
That would only work if the class started off in an invalid state, and the first call of ++combination put it into the first valid state, as described in my final example. Otherwise, it doesn't call doStuff on the first state.
|
|
"Well, there was your Uncle Tiberius who died wrapped in cabbage leaves but we assumed that was a freak accident."
|
|
|
|
|
#4 (permalink) |
|
Hadron Collider
Join Date: Aug 2007
Location: Bristol
Posts: 1,252
Thanks: 75
Thanked 96 Times in 65 Posts
|
Re: Loops in C++
I think you need to explain what this "class started off in an invalid state" business is all about.
I don't see a real problem with any of your alternatives. Particularly your last one, or what TheAnimus wrote. |
|
|
|
|
|
#5 (permalink) |
|
Gentoo Ricer
Join Date: Jan 2005
Location: /var/portage
Posts: 4,115
Thanks: 14
Thanked 101 Times in 96 Posts
|
Re: Loops in C++
Behold, the awesome power of forloops!
Code:
for(doStuff(); !combination.isAtEnd(); combination++) {
doStuff();
}
|
|
Main Rig -> J&W RS780UVD-AM2+ | AMD X4 9750 | 4x2GB GieL PC2-6400 | 2x500GB (md-raid0) | Sapphire HD4870 | Gentoo (AMD64) Server Box -> Asus P5B-E Plus | C2D E6320 | 2x1GB OCZ PC2-8500 | 4x500GB (md-raid5) | BFG 8800GTS 320Mb | Gentoo (Hardened/AMD64) Test Box -> P4E 3.2Ghz Rev. E0 | Asus P4C800-E Deluxe | 2x1GB PC3200 | 1x500Gb | NVidia TNT 2 | Gentoo (X86) Currently breaking: eINIT
|
|
|
|
|
#6 (permalink) |
|
Large Member
Join Date: Apr 2004
Posts: 3,107
Thanks: 8
Thanked 13 Times in 12 Posts
|
Re: Loops in C++
Are you thinking in terms of optimization? If so does isAtEnd just return a boolean property for said class, or does it perform a calculation and return?
|
|
To err is human. To really foul things up ... you need a computer.
|
|
|
|
|
#7 (permalink) |
|
Ah, Mrs. Peel!
Join Date: Oct 2003
Location: Hertfordshire, England
Posts: 3,317
Thanks: 2
Thanked 4 Times in 4 Posts
|
Re: Loops in C++
Originally Posted by Fraz
Well, in the first couple of solutions, doStuff() is repeated - hardly ideal, especially when doStuff() may not really be that simple i.e. parameters!
Secondly, if you use an iterator-like approach, which is more or less what I suggested at the end / TheAnimus suggested, this means you have an object which can be passed around but potentially might have unusable values e.g. If the the first (valid) value that this class might take is [0, 1, 2] (after the first call of ++), what value does it take beforehand? One solution is to rewrite it as an iterator, and have next() and hasNext() methods, but this breaks existing code (or, at least, moreso than other solutions). The question isn't so much "What is a solution to this problem," rather "What is the best solution to the problem?" - so probably what makes for the clearest code? isAtEnd just performs a simple integer comparison. |
|
"Well, there was your Uncle Tiberius who died wrapped in cabbage leaves but we assumed that was a freak accident."
|
|
|
![]() |
| Breadcrumb | ||||||
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Passive northbridge cooler | fray_bentos | HEXUS.hardware | 4 | 26-10-2007 08:56 AM |
| Best music recording/creating software, like fruity loops? | johnnr892 | Audio/Visual - Standard and HD | 11 | 12-04-2007 07:11 PM |