Class Kata "Ringbuffer"

Develop a class that uses a ring buffer [1, 2implemented.

Values can be appended to the "back" of a ring buffer in the same way as to a queue (Add()). And they can be removed "from the front" as if from a cue (Take()). However, the ring buffer has a limited capacity (Size()). If this is exhausted, values "at the beginning" are "overwritten", i.e. new values then take precedence over old ones.

The interface of the class should look like this:

class Ringbuffer {
	Ringbuffer(int size) {...}

	void Add(T value) {...}
	T Take() {...}
	int Count() {...} // Number of unread elements (<= Size())
	int Size() {...} // Size of the ring buffer
}

Example:

Class Kata Ringbuffer - Clean Code Developer Akademie

Variations

Initially, the case where old values are overwritten in favor of new ones can remain without special treatment. It can be overwritten silently.

As a further development, however, the ring buffer can be designed in such a way that its user can determine whether it should be overwritten silently or an exception should be thrown. Consider how the choice of an option can best be mapped in the class interface.

Resources

[1] Ring buffer, http://de.wikipedia.org/wiki/Warteschlange_(Datenstruktur)#Ringpuffer

[2] Circular buffer, http://en.wikipedia.org/wiki/Circular_buffer

en_USEnglish