Thread: Better way of doing this?

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Arrow Better way of doing this?

    This is fairly simple, and I have 1 way of doing it, but I don't like it and I feel that there should be an easier way but I can't find it This is my (pseudo)code:
    Code:
    void getDirList(bool wantDirectory, (...))
    {
         (...)
         do
         {
              if(wantDirectory)
              {
                   if(fileData.attributes & FILE_ATTRIBUTE_DIRECTORY)
                        add the filename;
              }
              else
              {
                   if(!(fileData.attributes & FILE_ATTRIBUTE_DIRECTORY))
                        add the filename;
              }
         }while(there's still more files)
    }
    It feels like the if/else should be eliminated, replaced by just one check of some sort, but I can't see how. Does anybody else have any ideas?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Re: Better way of doing this?

    Code:
    void getDirList(bool wantDirectory, (...))
    {
         (...)
         do
         {
              if(!wantDirectory ^ (fileData.attributes & FILE_ATTRIBUTE_DIRECTORY))
                        add the filename;
         }while(there's still more files)
    }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Do you mean like this?
    Code:
    void getDirList(bool wantDirectory, (...))
    {
         (...)
         do
         {
              if(wantDirectory == ((fileData.attributes & FILE_ATTRIBUTE_DIRECTORY) != 0))
                   add the filename;
    
         }while(there's still more files)
    }

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    No.. Use the excusive-or '^' operator. Basically, it returns true if exactly one of its arguments is true. So, for simplicity say your two conditions are 'p' and 'q'. Then both of these formulae will return true if either both are false or both are true, and return false otherwise.

    !(p ^ q)
    !p ^ q equivalently p ^ !q

    Actually, for clarity of code, I'd structure it like this:
    Code:
    if(p && q) ...
    else if(!p && !q) ...
    ... or ...
    if( (p && q) || (!p && !q) ) ...
    Last edited by Zach L.; 08-06-2003 at 06:47 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Heh, both on the dot. Truthfully, I was thinking that it would either be XOR or another comparison involving wantDirectory, but I couldn't figure out how to put it Thanks guys!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    jlou's answer should work too Zach, but ours is nicer.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Both screw with my mind
    Now, the question is... which is more readable, and which runs faster?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by FillYourBrain
    jlou's answer should work too Zach, but ours is nicer.
    True true... I was being a bit hasty, and didn't see the '!= 0' bit.

    Ah well, all equally obfuscated solutions.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by Hunter2
    Both screw with my mind
    Now, the question is... which is more readable, and which runs faster?
    Don't worry about speed that much. It really won't make too much of a difference which one you do (nothing noticeable). I edited my post above to include the "readable" version (bah! who wants that!).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ooooooooo, the elusive logical OR! I shoulda thought of that earlier.. but then, I would have missed out on all the fun with the various obfuscated ways of doing it. Heck, I think I'll go with the XOR just 'cuz I've never used it before. Maybe having it as a code example will come in handy sometime in the future
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Uhh, I kinda like the XOR better, too, even though its been a while since I've seen it used in code. Maybe I'll start thinking of it as a good solution.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oh, just one thing I found out, it should be:
    Code:
    if(!wantDirectory ^ ((fileData.attributes & FILE_ATTRIBUTE_DIRECTORY) != 0))
    Because ^ is a bitwise operator and (fileData.attributes & FILE_ATTRIBUTE_DIRECTORY) is almost certainly never going to be exactly 1, and wantDirectory is only ever going to be 1 or 0 (the 0 works with this arrangement, but not the 1).

    I found that out when directories started showing up under the files list
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    if(wantDirectory ^ !(fileData.attributes & FILE_ATTRIBUTE_DIRECTORY) )

    would do it too. Glad you caught that one.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Right! I'll go with that one, I didn't like the "!= 0" anyways
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed