Thread: Queue with a circular array... nitpick !

  1. #16
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Thanks.

    In this case the max_size would be Size-1 ; because I'm using the state where the tail is one step behind head to denote the empty convention. The other conventions I read about are much much convoluted.

    I remember reading somewhere that x.empty() should by preferred to x.size()==0 . I don't remember the reason, but if it is legitimate; is that point valid here? .. as implementing empty using size seems to negate that ..

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    In this case the max_size would be Size-1 ; because I'm using the state where the tail is one step behind head to denote the empty convention. The other conventions I read about are much much convoluted.
    Ah yes, that is actually a logic error on my part.

    Quote Originally Posted by manasij7479
    I remember reading somewhere that x.empty() should by preferred to x.size()==0 . I don't remember the reason, but if it is legitimate; is that point valid here? .. as implementing empty using size seems to negate that ..
    In cases where there is some special optimisation possible that accesses the internals to determine empty but not size, with an improvement in efficiency that is not negligible, then yes. Of course, in that case a member empty can be implemented, then the non-member empty calls that instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by laserlight View Post
    In cases where there is some special optimisation possible that accesses the internals to determine empty but not size, with an improvement in efficiency that is not negligible, then yes. Of course, in that case a member empty can be implemented, then the non-member empty calls that instead.
    In this case, as it is an array, both size and empty ..as given in the original post relies on a call to diff().
    So, I'd guess there is nothing to be gained.

    Btw. .. When it is not negligible, is it by .. say... factors like that crawling to a position in a linked list to indexed locations in arrays ?

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    Btw. .. When it is not negligible, is it by .. say... factors like that crawling to a position in a linked list to indexed locations in arrays ?
    Yeah, having a linear time size() would be an example, though it is not applicable to the standard containers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please ..err..nitpick my code (implementing a stack)
    By manasij7479 in forum C++ Programming
    Replies: 6
    Last Post: 10-04-2011, 05:20 PM
  2. Circular Queue
    By Giridhar Sanjay in forum C++ Programming
    Replies: 6
    Last Post: 07-02-2011, 02:22 PM
  3. Stack and Queue "quack" circular array
    By mrsirpoopsalot in forum C++ Programming
    Replies: 15
    Last Post: 10-20-2009, 03:16 PM
  4. Circular array
    By Opel_Corsa in forum C++ Programming
    Replies: 5
    Last Post: 02-16-2006, 04:15 PM
  5. circular queue
    By strotee76 in forum C++ Programming
    Replies: 2
    Last Post: 05-24-2004, 09:55 AM