• HEXUS
  • HEXUS.tv
  • channel
  • gaming
  • lifestyle
  • trust
  • community
  • ESReality
  • HEXUS.community discussion forumsVisit Corsair.com

    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!

    Go Back   HEXUS.community discussion forums > HEXUS.help - buying advice & technical queries > Operating systems & applications > Software and web development

    Software and web development Databases, graphics, programming, scripting and web development.

    Reply
     
    LinkBack Thread Tools
    Old 08-07-2008, 10:23 PM   #1 (permalink)
    Ah, Mrs. Peel!
     
    mike_w's Avatar
     
    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;
        }
    }
    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."
    mike_w is offline   Reply With Quote
    Old 08-07-2008, 11:30 PM   #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)
    TheAnimus is offline   Reply With Quote
    Old 08-07-2008, 11:34 PM   #3 (permalink)
    Ah, Mrs. Peel!
     
    mike_w's Avatar
     
    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."
    mike_w is offline   Reply With Quote
    Old 08-07-2008, 11:49 PM   #4 (permalink)
    Hadron Collider
     
    Fraz's Avatar
     
    Join Date: Aug 2007
    Location: Bristol
    Posts: 1,252
    Thanks: 75
    Thanked 96 Times in 65 Posts
    Fraz's system
    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.

    Fraz is offline   Reply With Quote
    Old 09-07-2008, 09:17 PM   #5 (permalink)
    Gentoo Ricer
     
    Join Date: Jan 2005
    Location: /var/portage
    Posts: 4,115
    Thanks: 14
    Thanked 101 Times in 96 Posts
    aidanjt's system
    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

    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
    aidanjt is offline   Reply With Quote
    Old 13-07-2008, 12:48 PM   #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.
    yamangman is offline   Reply With Quote
    Old 14-07-2008, 11:03 PM   #7 (permalink)
    Ah, Mrs. Peel!
     
    mike_w's Avatar
     
    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 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."
    mike_w is offline   Reply With Quote
    Reply

    Breadcrumb
    Go Back   HEXUS.community discussion forums > HEXUS.help - buying advice & technical queries > Operating systems & applications > Software and web development


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Trackbacks are On
    Pingbacks are On
    Refbacks are On
    Forum Jump

    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



    All times are GMT +1. The time now is 09:13 PM.

    Any representations/statements made on the HEXUS.community discussion forums are the representations/statements of the author i.e. the person/organisation making them. If any such representations/statements are disputed they are a matter between the parties concerned. HEXUS Limited accepts no responsibility for any misrepresentations, inaccurate or false statements made by any person/organisation other than HEXUS Limited employees.
    Powered by vBulletin® Version 3.7.2
    Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
    Content Relevant URLs by vBSEO 3.2.0
    © Copyright 2007 HEXUS Limited. All rights reserved. Unauthorised reproduction strictly prohibited.