Array.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_ARRAY
  25. #define DFPSR_COLLECTION_ARRAY
  26. #include "collections.h"
  27. namespace dsr {
  28. // A fixed size collection of elements initialized to the same default value.
  29. // Unlike Buffer, Array is a value type, so be careful not to pass it by value unless you intend to clone its content.
  30. template <typename T>
  31. class Array {
  32. private:
  33. int64_t elementCount = 0;
  34. T *elements = nullptr;
  35. public:
  36. // Constructor
  37. Array(const int64_t newLength, const T& defaultValue)
  38. : elementCount(newLength) {
  39. impl_nonZeroLengthCheck(newLength, "New array length");
  40. this->elements = new T[newLength];
  41. for (int64_t index = 0; index < newLength; index++) {
  42. this->elements[index] = defaultValue;
  43. }
  44. }
  45. // Clonable by default!
  46. // Be very careful not to accidentally pass an Array by value instead of reference,
  47. // otherwise your side-effects might write to a temporary copy
  48. // or time is wasted to clone an Array every time you look something up.
  49. Array(const Array<T>& source) {
  50. // Allocate to the same size as source.
  51. this->elements = new T[source.elementCount];
  52. this->elementCount = source.elementCount;
  53. // Copy elements from source.
  54. for (int64_t e = 0; e < this->elementCount; e++) {
  55. // Assign one element at a time, so that objects can be copy constructed.
  56. // If the element type T is trivial and does not require calling constructors, using safeMemoryCopy with SafePointer will be much faster than using Array<T>.
  57. this->elements[e] = source.elements[e];
  58. }
  59. };
  60. // When assigning to the array, memory can be reused when the size is the same.
  61. Array& operator=(const Array<T>& source) {
  62. // Reallocate to the same size as source if needed.
  63. if (this->elementCount != source.elementCount) {
  64. if (this->elements) delete[] this->elements;
  65. this->elements = new T[source.elementCount];
  66. }
  67. this->elementCount = source.elementCount;
  68. // Copy elements from source.
  69. for (int64_t e = 0; e < this->elementCount; e++) {
  70. // Assign one element at a time, so that objects can be copy constructed.
  71. // If the element type T is trivial and does not require calling constructors, using safeMemoryCopy with SafePointer will be much faster than using Array<T>.
  72. this->elements[e] = source.elements[e];
  73. }
  74. return *this;
  75. };
  76. // Destructor
  77. ~Array() { if (this->elements) delete[] this->elements; }
  78. // Element access
  79. T& operator[] (const int64_t index) {
  80. impl_baseZeroBoundCheck(index, this->length(), "Array index");
  81. return this->elements[index];
  82. }
  83. const T& operator[] (const int64_t index) const {
  84. impl_baseZeroBoundCheck(index, this->length(), "Array index");
  85. return this->elements[index];
  86. }
  87. int64_t length() const {
  88. return this->elementCount;
  89. }
  90. };
  91. }
  92. #endif