libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
list.h
Go to the documentation of this file.
1
8#pragma once
9#include <cstddef>
10#include <vector>
11#include <initializer_list>
12#include <functional>
13#include <algorithm>
16
18
39template <typename T>
40class List {
42 public:
45
47 using Data = typename std::vector<T>;
48
50 using Iterator = typename std::vector<T>::iterator;
51
53 using ConstIterator = typename std::vector<T>::const_iterator;
54
56 using RevIterator = typename std::vector<T>::reverse_iterator;
57
59 using ConstRevIterator = typename std::vector<T>::const_reverse_iterator;
60
62 using TestFunc = std::function<bool(const T &)>;
63
65 List() = default;
66
71 explicit List(size_t size) : d(size) {}
72
78 List(size_t size, const T &defaultValue) : d(size, defaultValue) {}
79
81 List(const List &other) : d(other.d) {}
82
84 List(List &&other) noexcept : d(std::move(other.d)) {}
85
90 List(std::initializer_list<T> initList) : d(initList) {}
91
93 ~List() = default;
94
97 d = other.d;
98 return *this;
99 }
100
102 List &operator=(List &&other) noexcept {
103 d = std::move(other.d);
104 return *this;
105 }
106
112 List &operator=(std::initializer_list<T> initList) {
113 d = initList;
114 return *this;
115 }
116
124 return *this;
125 }
126
133 pushToBack(std::move(item));
134 return *this;
135 }
136
144 return *this;
145 }
146
153 pushToBack(std::move(list));
154 return *this;
155 }
156
159 return d.begin();
160 }
161
164 return d.cbegin();
165 }
166
169 return d.cbegin();
170 }
171
174 return d.cbegin();
175 }
176
179 return d.rbegin();
180 }
181
184 return d.rbegin();
185 }
186
189 return d.crbegin();
190 }
191
194 return d.crbegin();
195 }
196
199 return d.end();
200 }
201
204 return d.cend();
205 }
206
209 return d.cend();
210 }
211
214 return d.cend();
215 }
216
219 return d.rend();
220 }
221
224 return d.rend();
225 }
226
229 return d.crend();
230 }
231
234 return d.crend();
235 }
236
242 T &at(size_t index) {
243 return d.at(index);
244 }
245
247 const T &at(size_t index) const {
248 return d.at(index);
249 }
250
256 T &operator[](size_t index) {
257 return d[index];
258 }
259
261 const T &operator[](size_t index) const {
262 return d[index];
263 }
264
266 T &front() {
267 return d.front();
268 }
269
271 const T &front() const {
272 return d.front();
273 }
274
276 T &back() {
277 return d.back();
278 }
279
281 const T &back() const {
282 return d.back();
283 }
284
287 return d.data();
288 }
289
291 const T *data() const noexcept {
292 return d.data();
293 }
294
297 return d.empty();
298 }
299
301 size_t size() const noexcept {
302 return d.size();
303 }
304
307 return d.max_size();
308 }
309
314 void reserve(size_t newCapacity) {
315 d.reserve(newCapacity);
316 }
317
320 return d.capacity();
321 }
322
324 void shrink() {
325 d.shrink_to_fit();
326 return;
327 }
328
331 d.clear();
332 return;
333 }
334
342 return d.insert(pos, value);
343 }
344
352 return d.insert(pos, std::move(value));
353 }
354
361 Iterator insert(size_t pos, const T &value) {
362 return d.insert(constBegin() + pos, value);
363 }
364
371 Iterator insert(size_t pos, T &&value) {
372 return d.insert(constBegin() + pos, std::move(value));
373 }
374
382 template <typename... Args> Iterator emplace(ConstIterator pos, Args &&...args) {
383 return d.emplace(pos, std::forward<Args>(args)...);
384 }
385
393 template <typename... Args> Iterator emplace(size_t pos, Args &&...args) {
394 return d.emplace(constBegin() + pos, std::forward<Args>(args)...);
395 }
396
403 return d.erase(pos);
404 }
405
413 return d.erase(first, last);
414 }
415
421 Iterator remove(size_t index) {
422 return d.erase(d.begin() + index);
423 }
424
429 void removeIf(TestFunc func) {
430 d.erase(std::remove_if(d.begin(), d.end(), func), d.end());
431 return;
432 }
433
439 bool removeFirst(const T &value) {
440 bool ret = false;
441 for(auto item = begin(); item != end(); ++item) {
442 if(*item == value) {
443 remove(item);
444 ret = true;
445 break;
446 }
447 }
448 return ret;
449 }
450
455 void pushToBack(const T &value) {
456 d.push_back(value);
457 return;
458 }
459
464 void pushToBack(const List<T> &list) {
465 d.insert(d.end(), list.d.begin(), list.d.end());
466 return;
467 }
468
474 d.push_back(std::move(value));
475 return;
476 }
477
483 d.insert(d.end(), std::make_move_iterator(list.d.begin()), std::make_move_iterator(list.d.end()));
484 return;
485 }
486
493 template <typename... Args> T &emplaceToBack(Args &&...args) {
494 return d.emplace_back(std::forward<Args>(args)...);
495 }
496
498 void popFromBack() {
499 d.pop_back();
500 return;
501 }
502
511 void resize(size_t newSize) {
512 d.resize(newSize);
513 return;
514 }
515
521 void resize(size_t newSize, const T &value) {
522 d.resize(newSize, value);
523 return;
524 }
525
530 void swap(List<T> &other) noexcept {
531 d.swap(other.d);
532 return;
533 }
534
541 bool set(size_t index, const T &val) {
542 if(index >= size()) return false;
543 d[index] = val;
544 return true;
545 }
546
551 List<T> sort() const {
552 auto ret = *this;
553 std::sort(ret.begin(), ret.end());
554 return ret;
555 }
556
561 List<T> reverse() const {
562 auto ret = *this;
563 std::reverse(ret.begin(), ret.end());
564 return ret;
565 }
566
572 auto ret = sort();
573 ret.d.erase(std::unique(ret.begin(), ret.end()), ret.end());
574 return ret;
575 }
576
582 template <typename Func>
583 void forEach(Func &&func) const {
584 for(const auto &item : d) func(item);
585 return;
586 }
587
593 bool contains(const T &val) const {
594 for(const auto &item : d) {
595 if(item == val) return true;
596 }
597 return false;
598 }
599
605 ssize_t indexOf(const T &value) const {
606 for(size_t i = 0; i < d.size(); ++i) {
607 if(d[i] == value) return static_cast<ssize_t>(i);
608 }
609 return -1;
610 }
611
617 ssize_t lastIndexOf(const T &value) const {
618 for(ssize_t i = static_cast<ssize_t>(d.size()) - 1; i >= 0; --i) {
619 if(d[static_cast<size_t>(i)] == value) return i;
620 }
621 return -1;
622 }
623
629 size_t count(const T &value) const {
630 size_t n = 0;
631 for(const auto &item : d) {
632 if(item == value) ++n;
633 }
634 return n;
635 }
636
643 List<T> mid(size_t pos, size_t length) const {
644 List<T> ret;
645 size_t end = pos + length;
646 if(end > d.size()) end = d.size();
647 if(pos >= d.size()) return ret;
648 ret.reserve(end - pos);
649 for(size_t i = pos; i < end; ++i) ret.pushToBack(d[i]);
650 return ret;
651 }
652
654 friend bool operator==(const List<T> &lhs, const List<T> &rhs) {
655 return lhs.d == rhs.d;
656 }
657
659 friend bool operator!=(const List<T> &lhs, const List<T> &rhs) {
660 return lhs.d != rhs.d;
661 }
662
664 friend bool operator<(const List<T> &lhs, const List<T> &rhs) {
665 return lhs.d < rhs.d;
666 }
667
669 friend bool operator>(const List<T> &lhs, const List<T> &rhs) {
670 return lhs.d > rhs.d;
671 }
672
674 friend bool operator<=(const List<T> &lhs, const List<T> &rhs) {
675 return lhs.d <= rhs.d;
676 }
677
679 friend bool operator>=(const List<T> &lhs, const List<T> &rhs) {
680 return lhs.d >= rhs.d;
681 }
682
683 private:
684 Data d;
685};
686
Dynamic array container wrapping std::vector.
Definition list.h:40
bool contains(const T &val) const
Returns true if the list contains the given value.
Definition list.h:593
List(size_t size, const T &defaultValue)
Constructs a list with a given number of copies of a value.
Definition list.h:78
void shrink()
Releases unused memory by shrinking capacity to fit the current size.
Definition list.h:324
RevIterator revBegin() noexcept
Returns a mutable reverse iterator to the last element.
Definition list.h:183
ConstRevIterator crbegin() const noexcept
Returns a const reverse iterator to the last element.
Definition list.h:188
ConstIterator end() const noexcept
Returns a const iterator to one past the last element.
Definition list.h:203
ConstIterator constEnd() const noexcept
Returns a const iterator to one past the last element.
Definition list.h:213
friend bool operator>=(const List< T > &lhs, const List< T > &rhs)
Lexicographic greater-than-or-equal comparison.
Definition list.h:679
ConstIterator cend() const noexcept
Returns a const iterator to one past the last element.
Definition list.h:208
size_t maxSize() const noexcept
Returns the maximum number of elements the list can theoretically hold.
Definition list.h:306
Iterator erase(ConstIterator first, ConstIterator last)
Removes elements in the range [first, last).
Definition list.h:412
ConstRevIterator constRevBegin() const noexcept
Returns a const reverse iterator to the last element.
Definition list.h:193
ConstIterator constBegin() const noexcept
Returns a const iterator to the first element.
Definition list.h:173
List & operator=(std::initializer_list< T > initList)
Assigns from an initializer list, replacing all contents.
Definition list.h:112
friend bool operator<(const List< T > &lhs, const List< T > &rhs)
Lexicographic less-than comparison.
Definition list.h:664
Iterator insert(ConstIterator pos, T &&value)
Inserts a value before the position given by an iterator (move overload).
Definition list.h:351
Iterator remove(size_t index)
Removes the element at the given index.
Definition list.h:421
List & operator=(List &&other) noexcept
Move assignment operator.
Definition list.h:102
Iterator remove(ConstIterator pos)
Removes the element at the given iterator position.
Definition list.h:402
const T * data() const noexcept
Returns a pointer to the underlying contiguous storage.
Definition list.h:291
void pushToBack(T &&value)
Moves an item onto the back of the list.
Definition list.h:473
void reserve(size_t newCapacity)
Pre-allocates storage for at least newCapacity elements.
Definition list.h:314
List(const List &other)
Copy constructor.
Definition list.h:81
List< T > reverse() const
Returns a reversed copy of this list.
Definition list.h:561
ConstRevIterator crend() const noexcept
Returns a const reverse iterator to one before the first element.
Definition list.h:228
RevIterator rbegin() noexcept
Returns a mutable reverse iterator to the last element.
Definition list.h:178
ssize_t indexOf(const T &value) const
Returns the index of the first occurrence of value.
Definition list.h:605
void popFromBack()
Removes the last element from the list.
Definition list.h:498
friend bool operator<=(const List< T > &lhs, const List< T > &rhs)
Lexicographic less-than-or-equal comparison.
Definition list.h:674
Iterator begin() noexcept
Returns a mutable iterator to the first element.
Definition list.h:158
Iterator emplace(ConstIterator pos, Args &&...args)
Emplaces an object right before the given position.
Definition list.h:382
typename std::vector< T >::iterator Iterator
Mutable forward iterator.
Definition list.h:50
ConstRevIterator constRevEnd() const noexcept
Returns a const reverse iterator to one before the first element.
Definition list.h:233
ssize_t lastIndexOf(const T &value) const
Returns the index of the last occurrence of value.
Definition list.h:617
ConstIterator cbegin() const noexcept
Returns a const iterator to the first element.
Definition list.h:168
List()=default
Default constructor. Creates an empty list.
List & operator+=(List &&list)
Appends all items from another list (move overload).
Definition list.h:152
friend bool operator!=(const List< T > &lhs, const List< T > &rhs)
Returns true if the lists differ.
Definition list.h:659
void swap(List< T > &other) noexcept
Swaps the list data with another list of the same type.
Definition list.h:530
size_t size() const noexcept
Returns the number of elements in the list.
Definition list.h:301
List(List &&other) noexcept
Move constructor.
Definition list.h:84
friend bool operator==(const List< T > &lhs, const List< T > &rhs)
Returns true if both lists have identical contents.
Definition list.h:654
bool removeFirst(const T &value)
Removes the first instance of value from the list.
Definition list.h:439
List & operator+=(T &&item)
Appends a single item to the back of the list (move overload).
Definition list.h:132
List(std::initializer_list< T > initList)
Constructs a list from an initializer list.
Definition list.h:90
Iterator emplace(size_t pos, Args &&...args)
Emplaces an object right before the given index.
Definition list.h:393
T * data() noexcept
Returns a pointer to the underlying contiguous storage.
Definition list.h:286
std::function< bool(const T &)> TestFunc
Predicate function type used by removeIf().
Definition list.h:62
ConstIterator begin() const noexcept
Returns a const iterator to the first element.
Definition list.h:163
typename std::vector< T >::reverse_iterator RevIterator
Mutable reverse iterator.
Definition list.h:56
void clear() noexcept
Removes all elements from the list.
Definition list.h:330
void pushToBack(List< T > &&list)
Moves all items from another list to the back of this list.
Definition list.h:482
friend bool operator>(const List< T > &lhs, const List< T > &rhs)
Lexicographic greater-than comparison.
Definition list.h:669
Iterator end() noexcept
Returns a mutable iterator to one past the last element.
Definition list.h:198
size_t count(const T &value) const
Returns the number of occurrences of value.
Definition list.h:629
typename std::vector< T >::const_iterator ConstIterator
Const forward iterator.
Definition list.h:53
Iterator insert(size_t pos, T &&value)
Inserts a value before the given index (move overload).
Definition list.h:371
void forEach(Func &&func) const
Calls func for every element.
Definition list.h:583
const T & front() const
Returns a reference to the first element.
Definition list.h:271
const T & at(size_t index) const
Returns a reference to the element at index with bounds checking.
Definition list.h:247
List< T > mid(size_t pos, size_t length) const
Returns a sublist starting at pos with length elements.
Definition list.h:643
List< T > unique()
Returns a list of all the unique items in this list.
Definition list.h:571
List & operator+=(const List &list)
Appends all items from another list to the back of this list.
Definition list.h:142
bool set(size_t index, const T &val)
Sets an item in the list by index.
Definition list.h:541
typename std::vector< T >::const_reverse_iterator ConstRevIterator
Const reverse iterator.
Definition list.h:59
void resize(size_t newSize)
Resizes the list.
Definition list.h:511
const T & operator[](size_t index) const
Returns a reference to the element at index without bounds checking.
Definition list.h:261
RevIterator rend() noexcept
Returns a mutable reverse iterator to one before the first element.
Definition list.h:218
Iterator insert(ConstIterator pos, const T &value)
Inserts a value before the position given by an iterator.
Definition list.h:341
~List()=default
Destructor.
T & front()
Returns a reference to the first element.
Definition list.h:266
T & operator[](size_t index)
Returns a reference to the element at index without bounds checking.
Definition list.h:256
List< T > sort() const
Returns a sorted copy of this list.
Definition list.h:551
RevIterator revEnd() noexcept
Returns a mutable reverse iterator to one before the first element.
Definition list.h:223
List(size_t size)
Constructs a list with a given number of default-constructed elements.
Definition list.h:71
List & operator+=(const T &item)
Appends a single item to the back of the list.
Definition list.h:122
T & back()
Returns a reference to the last element.
Definition list.h:276
void resize(size_t newSize, const T &value)
Resizes the list, constructs any new items with the given value.
Definition list.h:521
void pushToBack(const T &value)
Pushes an item onto the back of the list.
Definition list.h:455
const T & back() const
Returns a reference to the last element.
Definition list.h:281
Iterator insert(size_t pos, const T &value)
Inserts a value before the given index.
Definition list.h:361
typename std::vector< T > Data
Underlying std::vector storage type.
Definition list.h:47
List & operator=(const List &other)
Copy assignment operator.
Definition list.h:96
void removeIf(TestFunc func)
Runs a test function on all the items and removes them if it returns true.
Definition list.h:429
T & at(size_t index)
Returns a reference to the element at index with bounds checking.
Definition list.h:242
T & emplaceToBack(Args &&...args)
Emplaces an object on the back of the list.
Definition list.h:493
bool isEmpty() const noexcept
Returns true if the list has no elements.
Definition list.h:296
void pushToBack(const List< T > &list)
Pushes all items from another list to the back of this list.
Definition list.h:464
size_t capacity() const noexcept
Returns the number of elements the list can hold without reallocating.
Definition list.h:319
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
const T & value(const Result< T > &r)
Returns the value from a Result.
Definition result.h:56
#define PROMEKI_SHARED_FINAL(TYPE)
Macro for non-polymorphic native shared objects.
Definition sharedptr.h:88