News for hackers. Stuff that matters.
Re:Show HN: Revring, a circular buffer with zero memory waste by akkartik on Thursday June 18, 2015 @04:57PM (#9741571)
If I understand correctly he's storing X items in a buffer of size X, but using an invalid value in the head pointer to indicate that the buffer is full.

It's easier to understand without the decrementing. Initialize head and tail to 0, keep incrementing and cycling through the end as usual. When removing an element check if head and tail are equal before removing. When adding an element check if head and tail are equal after adding, and set the head to a sentinel value, say MAX_INT. Now you can check for the sentinel before attempting to add an element.

Neat trick!

There are no comments on this comment yet.