AP_ExpandingArray.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. /*
  14. * ExpandingArray class description
  15. *
  16. * ExpandingArrayGeneric implements most of the required functionality and is type agnostic allowing smaller overall code size
  17. * ExpandingArray<T> is the template and implements the small number of type specific methods
  18. *
  19. * Elements are organised into "chunks" with each chunk holding "chunk_size" elements
  20. * The "chunk_ptrs" array holds pointers to all allocated chunks
  21. *
  22. * The "expand" function allows expanding the array by a specified number of chunks
  23. * The "expand_to_hold" function expands the array (if necessary) to hold at least the specified number of elements
  24. *
  25. * When the array is expanded up to two memory allocations are required:
  26. * 1. if the chunk_ptrs array (which holds points to all allocated chunks) is full, this array will be re-allocated.
  27. * During this operation a new copy of the chunk_ptr array will be created with "chunk_ptr_increment" more rows,
  28. * the old array's data will be copied to the new array and finally the old array will be freed.
  29. * 2. a new chunk will be allocated and a pointer to this new chunk will be added to the chunk_ptrs array
  30. *
  31. * Warnings:
  32. * 1. memset, memcpy, memcmp cannot be used because the individual elements are not guaranteed to be next to each other in memory
  33. * 2. operator[] functions do not perform any range checking so max_items() should be used when necessary to avoid out-of-bound memory access
  34. * 3. elements_per_chunk (provided in constructor) should be a factor of 2 (i.e. 16, 32, 64) for best performance
  35. */
  36. #pragma once
  37. #include <AP_Common/AP_Common.h>
  38. class AP_ExpandingArrayGeneric
  39. {
  40. public:
  41. AP_ExpandingArrayGeneric(uint16_t element_size, uint16_t elements_per_chunk) :
  42. elem_size(element_size),
  43. chunk_size(elements_per_chunk)
  44. {}
  45. ~AP_ExpandingArrayGeneric(void);
  46. /* Do not allow copies */
  47. AP_ExpandingArrayGeneric(const AP_ExpandingArrayGeneric &other) = delete;
  48. AP_ExpandingArrayGeneric &operator=(const AP_ExpandingArrayGeneric&) = delete;
  49. // current maximum number of items (using expand may increase this)
  50. uint16_t max_items() const { return chunk_size * chunk_count; }
  51. // expand the array by specified number of chunks, returns true on success
  52. bool expand(uint16_t num_chunks = 1);
  53. // expand to hold at least num_items
  54. bool expand_to_hold(uint16_t num_items);
  55. protected:
  56. const uint16_t elem_size; // number of bytes for each element
  57. const uint16_t chunk_size; // the number of T elements in each chunk
  58. const uint16_t chunk_ptr_increment = 32; // chunk_ptrs array is grown by this many elements each time it fills
  59. typedef uint8_t* chunk_ptr_t; // pointer to a chunk
  60. chunk_ptr_t *chunk_ptrs; // array of pointers to allocated chunks
  61. uint16_t chunk_count_max; // number of elements in chunk_ptrs array
  62. uint16_t chunk_count; // number of allocated chunks
  63. };
  64. template <typename T>
  65. class AP_ExpandingArray : public AP_ExpandingArrayGeneric
  66. {
  67. public:
  68. AP_ExpandingArray<T>(uint16_t elements_per_chunk) :
  69. AP_ExpandingArrayGeneric(sizeof(T), elements_per_chunk)
  70. {}
  71. /* Do not allow copies */
  72. AP_ExpandingArray<T>(const AP_ExpandingArray<T> &other) = delete;
  73. AP_ExpandingArray<T> &operator=(const AP_ExpandingArray<T>&) = delete;
  74. // allow use as an array for assigning to elements. no bounds checking is performed
  75. T &operator[](uint16_t i)
  76. {
  77. const uint16_t chunk_num = i / chunk_size;
  78. const uint16_t chunk_index = (i % chunk_size) * elem_size;
  79. return (T &)(chunk_ptrs[chunk_num][chunk_index]);
  80. }
  81. // allow use as an array for accessing elements. no bounds checking is performed
  82. const T &operator[](uint16_t i) const
  83. {
  84. const uint16_t chunk_num = i / chunk_size;
  85. const uint16_t chunk_index = (i % chunk_size) * elem_size;
  86. return (const T &)(chunk_ptrs[chunk_num][chunk_index]);
  87. }
  88. };