Boost logo

Boost :

From: Neal D. Becker (nbecker_at_[hidden])
Date: 2003-04-10 10:43:25


I am looking at the cyclic_buffer.hpp. It is interesting. I implemented
something similar a while ago, which I called RingBuffer.

The logic can probably be simplified.

The buffer has a current position (logical beginning), call it "p". Suppose
the size of the buffer is "s". To access an element at offset "o", where o
is a signed value, we just look at b[i], where "b" is the underlying buffer
(e.g., vector) and "i" is an index. Index is computed as:

  int index (int o) {
    int x = (o + p) % int(s);
    if (x >= 0)
      return x;
    else
      return x + s;
  }

This function handles all mapping of logical position to physical position.

I believe if you factor out this function, as I did, you can eliminate
redundant code.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk