-
-
Notifications
You must be signed in to change notification settings - Fork 243
/
StackList.h
109 lines (85 loc) · 2.54 KB
/
StackList.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef INSIGHTS_STACK_LIST_H
#define INSIGHTS_STACK_LIST_H
/// \brief Base class for \ref StackList.
///
template<typename T>
struct StackListEntry
{
StackListEntry* next{nullptr};
StackListEntry* prev{nullptr};
};
/// \brief StackList is a container for a list which elements exist only on the stack.
///
/// The purpose is to keep allocation with new and delete away as long as stack seems to be
/// available. The class is range based for loop ready.
/// To use it a class needs to derive from \ref StackListEntry.
///
/// Example:
/// \code
// class Foo : public StackListEntry<Foo> {...};
/// \endcode
template<typename T>
class StackList
{
public:
using TStackListEntry = StackListEntry<T>;
StackList() = default;
void push(TStackListEntry& entry) noexcept
{
entry.next = nullptr;
if(!mFirst) {
mFirst = &entry;
mFirst->prev = nullptr;
} else {
mLast->next = &entry;
entry.prev = mLast;
}
mLast = &entry;
}
T* pop() noexcept
{
TStackListEntry* last = mLast;
if(mLast) {
mLast = mLast->prev;
if(mLast) {
mLast->next = nullptr;
last->next = nullptr;
last->prev = nullptr;
} else {
mFirst = nullptr;
}
}
return static_cast<T*>(last);
}
T& back() noexcept { return *static_cast<T*>(mLast); }
constexpr bool empty() const noexcept { return (nullptr == mLast); }
class StackListIterator
{
public:
StackListIterator(StackList& list)
: mCurrent{list.mFirst}
, mLast{list.mLast}
{
}
constexpr StackListIterator& begin() noexcept { return *this; }
constexpr StackListIterator& end() noexcept { return *this; }
constexpr T& operator*() noexcept { return *static_cast<T*>(mCurrent); }
constexpr StackListIterator& operator++() noexcept
{
if(mCurrent) {
mCurrent = mCurrent->next;
}
return *this;
}
constexpr bool operator!=(const TStackListEntry*) const noexcept { return (nullptr != mCurrent); }
private:
TStackListEntry* mCurrent;
TStackListEntry* mLast;
};
constexpr StackListIterator begin() noexcept { return StackListIterator{*this}; }
constexpr TStackListEntry* end() noexcept { return mLast; }
private:
TStackListEntry* mFirst{nullptr};
TStackListEntry* mLast{nullptr};
};
#endif