-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixed_forward_list.hpp
201 lines (160 loc) · 5.93 KB
/
fixed_forward_list.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#pragma once
#include <forward_list>
#include <algorithm>
#include "stack_allocator.hpp"
// XXX
template <typename T>
using __std_forward_list_node_t = std::_Fwd_list_node<T>;
template <typename T, size_t Capacity>
class FixedForwardList
: public std::forward_list<
T, StackAllocator<__std_forward_list_node_t<T>, Capacity>> {
public:
using Allocator = StackAllocator<__std_forward_list_node_t<T>, Capacity>;
using BaseType = std::forward_list<T, Allocator>;
using ReservsedMemoryType = typename Allocator::ReservedMemory;
using reference = typename BaseType::reference;
using const_reference = typename BaseType::const_reference;
using iterator = typename BaseType::iterator;
using const_iterator = typename BaseType::const_iterator;
using size_type = typename BaseType::size_type;
using allocator_type = typename BaseType::allocator_type;
using pointer = typename BaseType::pointer;
using const_pointer = typename BaseType::const_pointer;
using BaseType::assign;
using BaseType::begin;
using BaseType::end;
FixedForwardList() : BaseType(Allocator(&stack_data_)) {}
explicit FixedForwardList(size_type count, const T& value = T())
: FixedForwardList() {
assign(count, value);
}
template <typename InputIterator>
FixedForwardList(InputIterator first, InputIterator last)
: FixedForwardList() {
assign(first, last);
}
template <size_t Cap>
FixedForwardList(const FixedForwardList<T, Cap>& other)
: FixedForwardList() {
assign(other.first, other.end());
}
template <size_t Cap>
FixedForwardList(FixedForwardList<T, Cap>&& other)
: FixedForwardList() {
assign(other.first, other.end());
other.clear();
}
FixedForwardList(const FixedForwardList& other) : FixedForwardList() {
assign(other.begin(), other.end());
}
FixedForwardList(FixedForwardList&& other) : FixedForwardList() {
assign(other.begin(), other.end());
other.clear();
}
FixedForwardList(std::initializer_list<T> init) : FixedForwardList() {
assign(init);
}
FixedForwardList& operator=(const FixedForwardList& other) {
assign(other.begin(), other.end());
return *this;
}
FixedForwardList& operator=(FixedForwardList&& other) {
assign(other.begin(), other.end());
other.clear();
}
FixedForwardList& operator=(std::initializer_list<T> ilist) { assign(ilist); }
template <size_t Cap>
FixedForwardList& operator=(const FixedForwardList<T, Cap>& other) {
assign(other.begin(), other.end());
}
template <size_t Cap>
FixedForwardList& operator=(FixedForwardList<T, Cap>&& other) {
assign(other.begin(), other.end());
other.clear();
}
FixedForwardList(const std::forward_list<T>& flist) : FixedForwardList() {
assign(flist.begin(), flist.end());
}
FixedForwardList(std::forward_list<T>&& flist) : FixedForwardList() {
assign(flist.begin(), flist.end());
flist.clear();
}
FixedForwardList& operator=(const std::forward_list<T>& flist) {
assign(flist.begin(), flist.end());
}
FixedForwardList& operator=(std::forward_list<T>&& flist) {
assign(flist.begin(), flist.end());
flist.clear();
}
void swap(FixedForwardList& other) {
if (this != &other)
FixedSwap(*this, other);
}
// TODO add this series of operations
/*
void merge(FixedForwardList& other);
void merge(FixedForwardList&& other);
template <typename Compare>
void merge(FixedForwardList& other, Compare comp);
template <typename Compare>
void merge(FixedForwardList&& other, Compare comp);
void splice_after(const_iterator pos,
FixedForwardList& other);
void splice_after(const_iterator pos,
FixedForwardList&& other);
void splice_after(const_iterator pos, FixedForwardList& other,
const_iterator it);
void splice_after(const_iterator pos, FixedForwardList&& other,
const_iterator it);
void splice_after(const_iterator pos, FixedForwardList& other,
const_iterator first, const_iterator last);
void splice_after(const_iterator pos, FixedForwardList&& other,
const_iterator first, const_iterator last);
*/
private:
ReservsedMemoryType stack_data_;
};
template <typename T, size_t Capacity>
void swap(FixedForwardList<T, Capacity>& lhs,
FixedForwardList<T, Capacity>& rhs) {
FixedSwap(lhs, rhs);
}
template <typename T, size_t Capacity1, size_t Capacity2>
inline bool operator==(const FixedForwardList<T, Capacity1>& lhs,
const FixedForwardList<T, Capacity2>& rhs) {
auto litr = lhs.cbegin();
auto ritr = rhs.cbegin();
while (litr != lhs.cend() && ritr != rhs.cend()) {
if (*litr != *ritr) return false;
++litr;
++ritr;
}
return litr == lhs.cend() && ritr == rhs.cend();
}
template <typename T, size_t Capacity1, size_t Capacity2>
inline bool operator!=(const FixedForwardList<T, Capacity1>& lhs,
const FixedForwardList<T, Capacity2>& rhs) {
return !(lhs == rhs);
}
template <typename T, size_t Capacity1, size_t Capacity2>
inline bool operator<(const FixedForwardList<T, Capacity1>& lhs,
const FixedForwardList<T, Capacity2>& rhs) {
return std::lexicographical_compare(std::begin(lhs), std::end(lhs),
std::begin(rhs), std::end(rhs));
}
template <typename T, size_t Capacity1, size_t Capacity2>
inline bool operator<=(const FixedForwardList<T, Capacity1>& lhs,
const FixedForwardList<T, Capacity2>& rhs) {
return !(rhs < lhs);
}
template <typename T, size_t Capacity1, size_t Capacity2>
inline bool operator>(const FixedForwardList<T, Capacity1>& lhs,
const FixedForwardList<T, Capacity2>& rhs) {
return rhs < lhs;
}
template <typename T, size_t Capacity1, size_t Capacity2>
inline bool operator>=(const FixedForwardList<T, Capacity1>& lhs,
const FixedForwardList<T, Capacity2>& rhs) {
return !(lhs < rhs);
}