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:
Or alternatively:Code:Combination combination(args...); while (true) { doStuff(combination); if (combination.isAtEnd()) { break; } else { ++combination; } }
Of course, neither is ideal. The only way I can see around this is to start each instance of the class in an invalid, and set it to the first valid start when increment is called for the first time, so the code would become:Code:Combination combination(args...); doStuff(combination); while (!combination.isAtEnd()) { ++combination; doStuff(combination); }
Is that the sort of solution I should be looking at? The downside is that the object starts in an invalid state. Of course, I might be missing something obvious here (hence the post...), so any thoughts are appreciated.Code:Combination combination(args...); while (!combination.isAtEnd()) { ++combination; doStuff(combination); }
Thanks
Mike


LinkBack URL
About LinkBacks
Reply With Quote



