Results 1 to 7 of 7

Thread: Loops in C++

  1. #1
    Ah, Mrs. Peel! mike_w's Avatar
    Join Date
    Oct 2003
    Location
    Hertfordshire, England
    Posts
    3,326
    Thanks
    3
    Thanked
    9 times in 7 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;
        }
    }
    Or alternatively:

    Code:
    Combination combination(args...);
    
    doStuff(combination);
    while (!combination.isAtEnd()) {
        ++combination;
        doStuff(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...);
    
    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.

    Thanks

    Mike
    "Well, there was your Uncle Tiberius who died wrapped in cabbage leaves but we assumed that was a freak accident."

  2. #2
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 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. #3
    Ah, Mrs. Peel! mike_w's Avatar
    Join Date
    Oct 2003
    Location
    Hertfordshire, England
    Posts
    3,326
    Thanks
    3
    Thanked
    9 times in 7 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. #4
    Get in the van. Fraz's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    2,919
    Thanks
    284
    Thanked
    397 times in 231 posts
    • Fraz's system
      • Motherboard:
      • Gigabyte X58A-UD5
      • CPU:
      • Watercooled i7-980X @ 4.2 GHz
      • Memory:
      • 24GB Crucial DDR3-1333
      • Storage:
      • 240 GB Vertex2E + 2 TB of Disk
      • Graphics card(s):
      • Water-cooled Sapphire 7970 @ 1175/1625
      • PSU:
      • Enermax Modu87+
      • Case:
      • Corsair 700D
      • Operating System:
      • Linux Mint 12 / Windows 7
      • Monitor(s):
      • Dell 30" 3008WFP and two Dell 24" 2412M
      • Internet:
      • Virgin Media 60 Mbps

    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. #5
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: Loops in C++

    Behold, the awesome power of forloops!

    Code:
    for(doStuff(); !combination.isAtEnd(); combination++) {
        doStuff();
    }
    *disclaimer: I've been awake for 14 hours and my head feels like it's going to melt, so if I've I completely misread what you're trying to do then by all means, ignore what I just said
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  6. #6
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 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. #7
    Ah, Mrs. Peel! mike_w's Avatar
    Join Date
    Oct 2003
    Location
    Hertfordshire, England
    Posts
    3,326
    Thanks
    3
    Thanked
    9 times in 7 posts

    Re: Loops in C++

    Quote Originally Posted by Fraz View Post
    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.
    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."

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Best music recording/creating software, like fruity loops?
    By johnnr892 in forum Consumer Electronics
    Replies: 14
    Last Post: 15-03-2012, 04:48 AM
  2. Passive northbridge cooler
    By fray_bentos in forum PC Hardware and Components
    Replies: 4
    Last Post: 26-10-2007, 08:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •