Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Circular Buffer

Name: Anonymous 2011-01-06 10:01

Hi /prog/

Some time ago I started to write a well designed library of useful algorithms in C. As long as I needed more stuff, I simply added pieces.

Today I'm pretty bored, so I'd like to add an implementation for circular buffers. Since however I don't really need it (it's just for fun), I'm wondering which is the most useful semantics for a circular buffer.

For instance:
[list]
[*] Is it worthy to write a primitive which applies a function on every availabe element from the oldest to the most recent?
[*] What if the buffer is full and I want to insert a new element? Should I overwrite the oldest one or should I make the request fail instead?
[/list]

Which are, in general, the kind of functionalities you'll prefer?

Name: Anonymous 2011-01-06 10:30

[*] Is it worthy to write a primitive which applies a function on every availabe element from the oldest to the most recent?

You are speaking of the Visitor pattern. Visitors are useful, but in C, you'd have to do this using function pointers, and the overhead of calling a function through indirection is slow. C doesn't lend itself to this type of thing very well. So I probably wouldn't bother. Instead, I'd implement some C99 inline functions to quickly allow someone to iterate over the values and pop values out of the circular buffer.

[*] What if the buffer is full and I want to insert a new element? Should I overwrite the oldest one or should I make the request fail instead?

You can implement both.


// pushes the value pointed to by value forcefully overwriting old values.
void cbuffer_push(cbuffer_t *cb, void* value);

// pushes a value into the buffer if space is available. returns 1 if successful, 0 if no space is available
int cbuffer_try_push(cbuffer_t *cb, void* value);

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List