0

Given:

class Buf {
  // has fixed buffer size, forming a cyclic buffer tor t->v pairs
  void add(time_type t, value_type v); // adds value v at time t, overwriting the oldest buffered value
  value_type get(time_type t); // returns the value at time t1 for t1 <= t < t2 (for t1 and t2 datapoints in the buffer)
  ...
};

What would you call this class?

I admit it is somehow subjective, but it shouldn't lead to or require extended discussion of the answers, so I hope it's OK. :-)


So far I'm thinking of RecentValueBuffer since the class maps (recent) timestamps to values corresponding to these timestamps. I'm a bit unsure about "recent" because that seems to imply a short time-range/number of samples.

Martin Ba
  • 37,187
  • 33
  • 183
  • 337

4 Answers4

3

You should ask yourself if the user of the class needs to know, or care, that the internal implementation is a circular buffer. If not, name it something that makes it clear what the purpose of the class is: maybe something like TimeMap since it seems to be mapping values to discrete points in time. Then you can always change the internal implementation to something else (say, a Hashtable) without changing the name of your class.

If it's important to the semantics that it's always a circular buffer, then consider making it a generic container CircularBuffer or the like and use templates to define the types of the keys and values.

mtreit
  • 789
  • 4
  • 6
1

Some combination of a few meaningful aspects of its purpose and design:

  • Fixed_Sized
  • FIFO
  • Circular
  • Buffer
  • Recent
  • Last_N
  • Time_Window

Choose one or combine however many you like until you're happy with the result.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
0

CircularBuffer or RingBuffer

I wrote one recently for keeping a running average (called RunningAverage), and referred to it my comments as a 'ring buffer', so I apparently that's my preference. :) Probably because it's shorter.

Mud
  • 28,277
  • 11
  • 59
  • 92
0

You could use the STL-compliant boost::circular_buffer here rather than roll your own version of that structure.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • Thanks for pointing that out! I'll go with an underlying `vector` though, as that seems less complex and I need to wrap the circular buffer anyway to implement the `get` function. (Less complex: Think iterator invalidation etc.) – Martin Ba Nov 10 '10 at 06:53