-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularlist.h
47 lines (41 loc) · 1.25 KB
/
circularlist.h
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
#ifndef CIRCULARLIST_H
#define CIRCULARLIST_H
#include <QList>
//#include "boost/circular_buffer.hpp"
template <class T>
class CircularList : public QList<T>
{
public:
inline CircularList(){}
inline CircularList(int capacity) {m_Capacity = capacity;}
inline ~CircularList() {}
void setCapacity(int capacity){m_Capacity = capacity;}
int capacity() const {return m_Capacity;}
const T& before(const T& t) const
{
Q_ASSERT(!QList<T>::isEmpty());
int idx = QList<T>::indexOf(t);
return idx == 0 ? QList<T>::last() : QList<T>::at(idx - 1);
}
T& before(const T& t)
{
Q_ASSERT(!QList<T>::isEmpty());
int idx = QList<T>::indexOf(t);
return idx == 0 ? QList<T>::last() : QList<T>::operator[](idx - 1);
}
const T& after(const T& t) const
{
Q_ASSERT(!QList<T>::isEmpty());
int idx = QList<T>::indexOf(t);
return idx == QList<T>::count() - 1 ? QList<T>::front() : QList<T>::at(idx - 1);
}
T& after(const T& t)
{
Q_ASSERT(!QList<T>::isEmpty());
int idx = QList<T>::indexOf(t);
return idx == QList<T>::count() - 1 ? QList<T>::front() : QList<T>::operator[](idx - 1);
}
private:
int m_Capacity;
};
#endif // CIRCULARLIST_H