List.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <stdint.h>
  28. #include <vector>
  29. #include <algorithm>
  30. namespace dsr {
  31. // TODO: Remove the std::vector dependency by reimplementing the basic features.
  32. // An array list for constant time random access to elements in a LIFO stack.
  33. // Technically, there's nothing wrong with the internals of std::vector, but its interface is horrible.
  34. // * Forced use of iterators for cloning and element removal is both overly complex and bloating the code.
  35. // Most people joining your project won't be able to read the code if using iterators, so just don't.
  36. // * Unsigned indices will either force dangerous casting from signed, or prevent
  37. // the ability to loop backwards without crashing when the x < 0u criteria cannot be met.
  38. template <typename T>
  39. class List {
  40. private:
  41. std::vector<T> backend;
  42. public:
  43. // Constructor
  44. List() {}
  45. // Clonable by default!
  46. // Pass by reference if you don't want to lose your changes and waste time duplicating memory
  47. List(const List& source) : backend(std::vector<T>(source.backend.begin(), source.backend.end())) {}
  48. // Post-condition: Returns the number of elements in the array list
  49. int64_t length() const {
  50. return (int64_t)this->backend.size();
  51. }
  52. // Post-condition: Returns the element at index from the range 0..length-1
  53. T& operator[] (int64_t index) {
  54. impl_baseZeroBoundCheck(index, this->length(), "List index");
  55. return this->backend[index];
  56. }
  57. // Post-condition: Returns the write-protected element at index from the range 0..length-1
  58. const T& operator[] (int64_t index) const {
  59. impl_baseZeroBoundCheck(index, this->length(), "List index");
  60. return this->backend[index];
  61. }
  62. // Post-condition: Returns a reference to the first element
  63. T& first() {
  64. impl_nonZeroLengthCheck(this->length(), "Length");
  65. return this->backend[0];
  66. }
  67. // Post-condition: Returns a reference to the first element from a write protected array list
  68. const T& first() const {
  69. impl_nonZeroLengthCheck(this->length(), "Length");
  70. return this->backend[0];
  71. }
  72. // Post-condition: Returns a reference to the last element
  73. T& last() {
  74. impl_nonZeroLengthCheck(this->length(), "Length");
  75. return this->backend[this->length() - 1];
  76. }
  77. // Post-condition: Returns a reference to the last element from a write protected array list
  78. const T& last() const {
  79. impl_nonZeroLengthCheck(this->length(), "Length");
  80. return this->backend[this->length() - 1];
  81. }
  82. // Side-effect: Removes all elements by setting the count to zero
  83. void clear() {
  84. this->backend.clear();
  85. }
  86. // Side-effect: Makes sure that the buffer have room for at least minimumLength elements
  87. // Warning! Reallocation may invalidate old pointers and references to elements in the replaced buffer
  88. void reserve(int64_t minimumLength) {
  89. this->backend.reserve(minimumLength);
  90. }
  91. // Side-effect: Swap the order of two elements
  92. // Useful for moving and sorting elements
  93. void swap(int64_t indexA, int64_t indexB) {
  94. impl_baseZeroBoundCheck(indexA, this->length(), "Swap index A");
  95. impl_baseZeroBoundCheck(indexB, this->length(), "Swap index B");
  96. std::swap(this->backend[indexA], this->backend[indexB]);
  97. }
  98. // Side-effect: Adds a new element at the end
  99. // Warning! Reallocation may invalidate old pointers and references to elements in the replaced buffer
  100. // Post-condition: Returns a reference to the new element in the list
  101. T& push(const T& newValue) {
  102. // Optimize for speed by assuming that we have enough memory
  103. if (this->length() == 0) {
  104. this->backend.reserve(32);
  105. } else if (this->length() >= (int64_t)this->backend.capacity()) {
  106. this->backend.reserve((int64_t)this->backend.capacity() * 4);
  107. }
  108. this->backend.push_back(newValue);
  109. return this->last();
  110. }
  111. // Side-effect: Pushes a new element constructed using the given arguments
  112. template<typename... ARGS>
  113. T& pushConstruct(ARGS... args) {
  114. this->backend.emplace_back(args...);
  115. return this->last();
  116. }
  117. // Side-effect: Deletes the element at removedIndex
  118. // We can assume that the order is stable in the STD implementation, because ListTest.cpp would catch alternative interpretations
  119. void remove(int64_t removedIndex) {
  120. this->backend.erase(this->backend.begin() + removedIndex);
  121. }
  122. // Side-effect: Deletes the last element
  123. void pop() {
  124. this->backend.pop_back();
  125. }
  126. };
  127. }
  128. #endif