| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- #pragma once
- #include <string.h>
- #include <string>
- template <typename T, int SIZE =10>
- class WrappedBuffer
- {
- public:
- WrappedBuffer();
- virtual ~WrappedBuffer();
- void put(T item);
- void put(T item[], unsigned int count);
- void pop_back();
- unsigned int size() const;
- std::basic_string<T> toString() const;
- T* toArray(T arr[SIZE]) const;
- protected:
- T buffer[SIZE];
- int curR;
- int curW;
- };
- template<typename T, int SIZE>
- WrappedBuffer<T, SIZE>::WrappedBuffer(): curR(0), curW(-1)
- { }
- template<typename T, int SIZE>
- WrappedBuffer<T, SIZE>::~WrappedBuffer()
- { }
- template<typename T, int SIZE>
- void WrappedBuffer<T, SIZE>::put(T item)
- {
- if (curW +1 == SIZE)
- {
- curR = 1;
- curW = 0;
- buffer[0] = item;
- }
- else if (curW == -1)
- {
- curR = SIZE;
- buffer[curW = 0] = item;
- }
- else
- {
- buffer[++curW] = item;
- if (curR == curW)
- {
- if (++curR > SIZE)
- curR = 0;
- }
- }
- }
- template<typename T, int SIZE>
- void WrappedBuffer<T, SIZE>::put(T items[], unsigned int count)
- {
- unsigned int newSize = size() + count;
- if (!count)
- return;
- while (count > SIZE)
- {
- count -= SIZE;
- items += SIZE;
- }
- if (curW + count >= SIZE)
- {
- if (curW +1 != SIZE)
- {
- memcpy(&buffer[curW +1], items, sizeof(T) * (SIZE - curW -1));
- items += (SIZE - curW -1);
- count -= (SIZE - curW -1);
- }
- curW = -1;
- }
- memcpy(&buffer[curW +1], items, sizeof(T) * count);
- curW += count;
- if (curW == SIZE)
- {
- curW = 0;
- curR = 1;
- }
- else if (newSize >= SIZE)
- curR = (curW +1) % SIZE;
- }
- template<typename T, int SIZE>
- void WrappedBuffer<T, SIZE>::pop_back()
- {
- unsigned int oldSize = size();
- if (oldSize == 0)
- return;
- else if (oldSize == 1)
- {
- curW = -1;
- curR = 0;
- }
- else if (--curW < 0)
- {
- curW += SIZE;
- }
- }
- template<typename T, int SIZE>
- unsigned int WrappedBuffer<T, SIZE>::size() const
- {
- if (curW == -1)
- return 0;
- return (curR > curW) ? (SIZE - curR + curW +1) : (curW - curR +1);
- }
- template<typename T, int SIZE>
- std::basic_string<T> WrappedBuffer<T, SIZE>::toString() const
- {
- std::basic_string<T> result(size(), (T) 0);
- const unsigned int size = this->size();
- int from = (curR == SIZE) ? 0 : curR;
- unsigned int j = 0;
- for (int i = from; (curW > curR && i <= curW) || (curW <= curR && i < SIZE); ++i)
- result[j++] = buffer[i];
- for (int i = 0; j < size; ++i)
- result[j++] = buffer[i];
- return result;
- }
- template<typename T, int SIZE>
- T* WrappedBuffer<T, SIZE>::toArray(T arr[SIZE]) const
- {
- if (!arr)
- arr = new T[SIZE]();
- //TODO
- return arr;
- }
|