List.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. 
  2. // zlib open source license
  3. //
  4. // Copyright (c) 2018 to 2019 David Forsgren Piuva
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would be
  17. // appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not be
  20. // misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. #ifndef DFPSR_COLLECTION_LIST
  25. #define DFPSR_COLLECTION_LIST
  26. #include "collections.h"
  27. #include <cstdint>
  28. #include <vector>
  29. #include <algorithm>
  30. namespace dsr {
  31. // TODO: Remove the std::vector dependency by reimplementing the basic features.
  32. // A wrapper over std::vector to improve safety, readability and performance.
  33. // Technically, there's nothing wrong with the internals of std::vector, but its interface is horrible.
  34. // * std::vector will create too many small allocations unless manually told how to reserve memory in advance.
  35. // * Forced use of iterators for cloning and element removal is both overly complex and bloating the code.
  36. // Most people joining your project won't be able to read the code if using std::iterator.
  37. // Safer to access elements by index, or an iterating high-level function performing a lambda for each element.
  38. // If performance is important, then use Buffer and SafePointer instead,
  39. // so that you get memory bound and alignment checks for SIMD vectors.
  40. // * Unsigned indices will either force dangerous casting from signed, or prevent
  41. // the ability to loop backwards without crashing when the x < 0u criteria cannot be met.
  42. // Unlike Buffer, List is a value type, so be careful not to pass it by value unless you intend to clone its content.
  43. template <typename T>
  44. class List {
  45. private:
  46. std::vector<T> backend;
  47. List(const std::vector<T>& backend) : backend(backend) {}
  48. public:
  49. // Constructor
  50. List() {}
  51. // Clonable by default!
  52. // Be very careful not to accidentally pass a List by value instead of reference,
  53. // otherwise your side-effects might write to a temporary copy
  54. // or time is wasted to clone a List every time you look something up.
  55. List(const List& source) : backend(std::vector<T>(source.backend.begin(), source.backend.end())) {}
  56. // Construct using one argument per element.
  57. template<typename... ELEMENTS>
  58. List(ELEMENTS... elements)
  59. : backend({elements...}) {}
  60. // Post-condition: Returns the number of elements in the array list.
  61. int64_t length() const {
  62. return (int64_t)this->backend.size();
  63. }
  64. // Post-condition: Returns the element at index from the range 0..length-1.
  65. T& operator[] (int64_t index) {
  66. impl_baseZeroBoundCheck(index, this->length(), "List index");
  67. return this->backend[index];
  68. }
  69. // Post-condition: Returns the write-protected element at index from the range 0..length-1.
  70. const T& operator[] (int64_t index) const {
  71. impl_baseZeroBoundCheck(index, this->length(), "List index");
  72. return this->backend[index];
  73. }
  74. // Post-condition: Returns an index to the first element, which is always zero.
  75. // Can be used for improving readability when used together with lastIndex.
  76. int64_t firstIndex() const { return 0; }
  77. // Post-condition: Returns an index to the last element.
  78. int64_t lastIndex() const { return this->length() - 1; }
  79. // Post-condition: Returns a reference to the first element.
  80. T& first() {
  81. impl_nonZeroLengthCheck(this->length(), "Length");
  82. return this->backend[0];
  83. }
  84. // Post-condition: Returns a reference to the first element from a write protected array list.
  85. const T& first() const {
  86. impl_nonZeroLengthCheck(this->length(), "Length");
  87. return this->backend[0];
  88. }
  89. // Post-condition: Returns a reference to the last element.
  90. T& last() {
  91. impl_nonZeroLengthCheck(this->length(), "Length");
  92. return this->backend[this->lastIndex()];
  93. }
  94. // Post-condition: Returns a reference to the last element from a write protected array list.
  95. const T& last() const {
  96. impl_nonZeroLengthCheck(this->length(), "Length");
  97. return this->backend[this->lastIndex()];
  98. }
  99. // Side-effect: Removes all elements by setting the count to zero.
  100. void clear() {
  101. this->backend.clear();
  102. }
  103. // Side-effect: Makes sure that the buffer have room for at least minimumLength elements.
  104. // Warning! Reallocation may invalidate old pointers and references to elements in the replaced buffer.
  105. void reserve(int64_t minimumLength) {
  106. this->backend.reserve(minimumLength);
  107. }
  108. // Side-effect: Swap the order of two elements.
  109. // Useful for moving and sorting elements.
  110. void swap(int64_t indexA, int64_t indexB) {
  111. impl_baseZeroBoundCheck(indexA, this->length(), "Swap index A");
  112. impl_baseZeroBoundCheck(indexB, this->length(), "Swap index B");
  113. std::swap(this->backend[indexA], this->backend[indexB]);
  114. }
  115. // Side-effect: Pushes a new element at the end.
  116. // Warning! Reallocation may invalidate old pointers and references to elements in the replaced buffer.
  117. // Post-condition: Returns a reference to the new element in the list.
  118. T& push(const T& newValue) {
  119. // Optimize for speed by assuming that we have enough memory.
  120. if (this->length() == 0) {
  121. this->backend.reserve(32);
  122. } else if (this->length() >= (int64_t)this->backend.capacity()) {
  123. this->backend.reserve((int64_t)this->backend.capacity() * 4);
  124. }
  125. this->backend.push_back(newValue);
  126. return this->last();
  127. }
  128. // Side-effect: Pushes a new element at the end.
  129. // Warning! Reallocation may invalidate old pointers and references to elements in the replaced buffer.
  130. // Post-condition: Returns an index to the new element in the list.
  131. int64_t pushGetIndex(const T& newValue) {
  132. this->push(newValue);
  133. return this->lastIndex();
  134. }
  135. // Side-effect: Pushes a new element constructed using the given arguments.
  136. // Warning! Reallocation may invalidate old pointers and references to elements in the replaced buffer.
  137. // Post-condition: Returns a reference to the new element in the list.
  138. template<typename... ARGS>
  139. T& pushConstruct(ARGS&&... args) {
  140. // Optimize for speed by assuming that we have enough memory.
  141. if (this->length() == 0) {
  142. this->backend.reserve(32);
  143. } else if (this->length() >= (int64_t)this->backend.capacity()) {
  144. this->backend.reserve((int64_t)this->backend.capacity() * 4);
  145. }
  146. this->backend.emplace_back(args...);
  147. return this->last();
  148. }
  149. // Side-effect: Pushes a new element constructed using the given arguments.
  150. // Warning! Reallocation may invalidate old pointers and references to elements in the replaced buffer.
  151. // Post-condition: Returns an index to the new element in the list.
  152. template<typename... ARGS>
  153. int64_t pushConstructGetIndex(ARGS&&... args) {
  154. pushConstruct(args...);
  155. return this->lastIndex();
  156. }
  157. // Side-effect: Deletes the element at removedIndex.
  158. // We can assume that the order is stable in the STD implementation, because ListTest.cpp would catch alternative interpretations.
  159. void remove(int64_t removedIndex) {
  160. this->backend.erase(this->backend.begin() + removedIndex);
  161. }
  162. // Side-effect: Deletes the last element.
  163. void pop() {
  164. this->backend.pop_back();
  165. }
  166. };
  167. }
  168. #endif