Circular Buffer template for any data type
Dependents: serqet serqet2 EMGvoorjan kopija_NUCLEO_CELL_LOCKER_copy ... more
Revision 0:b241b75b052b, committed 2012-10-22
- Comitter:
- hamparawa
- Date:
- Mon Oct 22 13:17:26 2012 +0000
- Commit message:
- first version
Changed in this revision
circular_buffer.cpp | Show annotated file Show diff for this revision Revisions of this file |
circular_buffer.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r b241b75b052b circular_buffer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/circular_buffer.cpp Mon Oct 22 13:17:26 2012 +0000 @@ -0,0 +1,180 @@ + +#ifndef CIRCULAR_BUFFER_CPP +#define CIRCULAR_BUFFER_CPP + +#include <stdio.h> +#include "circular_buffer.h" + +/* +template<class T> +circular_buffer<T>::circular_buffer() +{ + capacity = 0; + size = 0; + + buffer = 0; + + reset(); +} +*/ + +template<class T> +circular_buffer<T>::circular_buffer(int tcapacity) +{ + capacity = tcapacity; + buffer = new T[capacity]; + + reset(); +} + +template<class T> +circular_buffer<T>::~circular_buffer() +{ + if (buffer != 0) + delete[] buffer; +} + +template<class T> +void circular_buffer<T>::reset() +{ + start_pos = 0; + end_pos = 0; + size = 0; +} + +template<class T> +int circular_buffer<T>::get_capacity() +{ + return capacity; +} + +template<class T> +int circular_buffer<T>::get_size() +{ + return size; +} + +template<class T> +void circular_buffer<T>::push_back(T item) +{ + if (size == capacity) + pop_front(); + + buffer[end_pos] = item; + increment(end_pos); + size++; +} + +/* +template<class T> +void circular_buffer<T>::push_front(T item) +{ + if (size == capacity) + pop_back(); + + buffer[start_pos] = item; + decrement(start_pos); + size++; +} + +*/ + +template<class T> +void circular_buffer<T>::pop_back() +{ + if (size != 0) + { + size--; + decrement(end_pos); + } +} + +template<class T> +void circular_buffer<T>::pop_front() +{ + if (size != 0) + { + size--; + increment(start_pos); + } +} + +template<class T> +void circular_buffer<T>::increment(int& index) +{ + index++; + if (index >= capacity) + index = 0; +} + +template<class T> +void circular_buffer<T>::decrement(int& index) +{ + index--; + if (index < 0) + index = capacity - 1; +} + + +template<class T> +int circular_buffer<T>::if_increment(int index) +{ + index++; + if (index >= capacity) + index = 0; + + return index; +} + +template<class T> +int circular_buffer<T>::if_decrement(int index) +{ + index--; + if (index < capacity) + index = capacity - 1; + + return index; +} + +template<class T> +T& circular_buffer<T>::front() +{ + return buffer[start_pos]; +} + +template<class T> +T& circular_buffer<T>::back() +{ + return buffer[if_decrement(end_pos)]; +} + + +template<class T> +T& circular_buffer<T>::operator[](int index) +{ + int real_index = 0; + +// if (size == 0) // no item +// return NULL; + + real_index = index + start_pos; + if (real_index >= capacity) + real_index -= capacity; + + return buffer[real_index]; +} + + +template<class T> +T& circular_buffer<T>::at(int index) +{ + int real_index = 0; + + real_index = index + start_pos; + if (real_index >= capacity) + real_index -= capacity; + + return buffer[real_index]; +} + +#endif // CIRCULAR_BUFFER_CPP
diff -r 000000000000 -r b241b75b052b circular_buffer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/circular_buffer.h Mon Oct 22 13:17:26 2012 +0000 @@ -0,0 +1,53 @@ +#ifndef CIRCULAR_BUFFER +#define CIRCULAR_BUFFER + + +template<class T> +class circular_buffer +{ +public: +// circular_buffer(); + circular_buffer(int capacity); + ~circular_buffer(); + + int get_capacity(); // get the maximum capacity of the buf + int get_size(); // get the current item count + + void push_back(T item); +// void push_front(T item); + + void pop_back(); + void pop_front(); + + T& front(); + T& back(); + + T& at(int index); + T& operator[](int index); + + +protected: + + int capacity; + int size; + int start_pos; + int end_pos; + + T *buffer; + + void increment(int& index); + void decrement(int& index); + + int if_increment(int index); + int if_decrement(int index); + + void reset(); + + +}; + +#include "circular_buffer.cpp" + +#endif // CIRCULAR_BUFFER + +