-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathqskarsnikringlist.hpp
72 lines (60 loc) · 1.55 KB
/
qskarsnikringlist.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef QSKARSNIKRINGLIST_H
#define QSKARSNIKRINGLIST_H
#include <QVector>
template<class T> class QSkarsnikRingList
{
public:
QSkarsnikRingList(unsigned int initSize);
void append(const T &value);
const T& at(int i) const;
size_t size() const;
private:
QVector<T>* _data;
int _size;
int _firstIndex;
int _lastIndex;
bool _cycled;
int vectorIndex(int index) const;
};
#include "qskarsnikringlist.hpp"
template<class T>
QSkarsnikRingList<T>::QSkarsnikRingList(unsigned int initSize)
{
_data = new QVector<T>();
_data->reserve(initSize);
_size = initSize;
_cycled = false;
_firstIndex = 0;
_lastIndex = -1;
}
#include <QDebug>
template<class T>
void QSkarsnikRingList<T>::append(const T &value)
{
auto end = _lastIndex;
//qInfo() << "Last index :" << _lastIndex;
//qInfo() << "first index :" << _firstIndex;
_lastIndex = (_lastIndex + 1) % _size;
if (_data->size() < _size)
_data->append(value);
else
(*_data)[_lastIndex] = value;
if (_lastIndex < end && _cycled == false)
_cycled = true;
if (_cycled)
_firstIndex = vectorIndex(1);
}
template<class T> int QSkarsnikRingList<T>::vectorIndex(int index) const {
return (_firstIndex + index) % _size;
}
template<class T>
const T &QSkarsnikRingList<T>::at(int i) const
{
return _data->at(vectorIndex(i));
}
template<class T>
size_t QSkarsnikRingList<T>::size() const
{
return _data->size();
}
#endif // QSKARSNIKRINGLIST_H