Array.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. // The simplest possible automatically deallocating array with bound checks.
  29. // Indices use signed indices, which can be used directly from high-level algorithms.
  30. // Because std::vector is a list of members, not a fixed size array of values.
  31. // Using a list instead of an array makes the code both dangerous and unreadable.
  32. // Using unsigned indices will either force dangerous casting from signed, or prevent
  33. // the ability to loop backwards without crashing when the x < 0u criteria cannot be met.
  34. template <typename T>
  35. class Array {
  36. private:
  37. const int32_t elementCount;
  38. T *elements = nullptr;
  39. public:
  40. // Constructor
  41. Array(const int32_t newLength, const T& defaultValue)
  42. : elementCount(newLength) {
  43. impl_nonZeroLengthCheck(newLength, "New array length");
  44. this->elements = new T[newLength];
  45. for (int32_t index = 0; index < newLength; index++) {
  46. this->elements[index] = defaultValue;
  47. }
  48. }
  49. // No implicit copies, only pass by reference
  50. Array(const Array&) = delete;
  51. Array& operator=(const Array&) = delete;
  52. // Destructor
  53. ~Array() { delete[] this->elements; }
  54. // Element access
  55. T& operator[] (const int32_t index) {
  56. impl_baseZeroBoundCheck(index, this->length(), "Array index");
  57. return this->elements[index];
  58. }
  59. const T& operator[] (const int32_t index) const {
  60. impl_baseZeroBoundCheck(index, this->length(), "Array index");
  61. return this->elements[index];
  62. }
  63. int32_t length() const {
  64. return this->elementCount;
  65. }
  66. };
  67. }
  68. #endif