Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Common/AP_ExpandingArray.h
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/1415/*16* ExpandingArray class description17*18* ExpandingArrayGeneric implements most of the required functionality and is type agnostic allowing smaller overall code size19* ExpandingArray<T> is the template and implements the small number of type specific methods20*21* Elements are organised into "chunks" with each chunk holding "chunk_size" elements22* The "chunk_ptrs" array holds pointers to all allocated chunks23*24* The "expand" function allows expanding the array by a specified number of chunks25* The "expand_to_hold" function expands the array (if necessary) to hold at least the specified number of elements26*27* When the array is expanded up to two memory allocations are required:28* 1. if the chunk_ptrs array (which holds points to all allocated chunks) is full, this array will be re-allocated.29* During this operation a new copy of the chunk_ptr array will be created with "chunk_ptr_increment" more rows,30* the old array's data will be copied to the new array and finally the old array will be freed.31* 2. a new chunk will be allocated and a pointer to this new chunk will be added to the chunk_ptrs array32*33* Warnings:34* 1. memset, memcpy, memcmp cannot be used because the individual elements are not guaranteed to be next to each other in memory35* 2. operator[] functions do not perform any range checking so max_items() should be used when necessary to avoid out-of-bound memory access36* 3. elements_per_chunk (provided in constructor) should be a factor of 2 (i.e. 16, 32, 64) for best performance37*/3839#pragma once4041#include <AP_Common/AP_Common.h>4243class AP_ExpandingArrayGeneric44{45public:4647AP_ExpandingArrayGeneric(uint16_t element_size, uint16_t elements_per_chunk) :48elem_size(element_size),49chunk_size(elements_per_chunk)50{}5152~AP_ExpandingArrayGeneric(void);5354/* Do not allow copies */55CLASS_NO_COPY(AP_ExpandingArrayGeneric);5657// current maximum number of items (using expand may increase this)58uint16_t max_items() const { return chunk_size * chunk_count; }5960// expand the array by specified number of chunks, returns true on success61bool expand(uint16_t num_chunks = 1);6263// expand to hold at least num_items64bool expand_to_hold(uint16_t num_items);6566protected:6768const uint16_t elem_size; // number of bytes for each element69const uint16_t chunk_size; // the number of T elements in each chunk70const uint16_t chunk_ptr_increment = 32; // chunk_ptrs array is grown by this many elements each time it fills7172typedef uint8_t* chunk_ptr_t; // pointer to a chunk7374chunk_ptr_t *chunk_ptrs; // array of pointers to allocated chunks75uint16_t chunk_count_max; // number of elements in chunk_ptrs array76uint16_t chunk_count; // number of allocated chunks77};7879template <typename T>80class AP_ExpandingArray : public AP_ExpandingArrayGeneric81{82public:8384AP_ExpandingArray(uint16_t elements_per_chunk) :85AP_ExpandingArrayGeneric(sizeof(T), elements_per_chunk)86{}8788/* Do not allow copies */89CLASS_NO_COPY(AP_ExpandingArray);9091// allow use as an array for assigning to elements. no bounds checking is performed92T &operator[](uint16_t i)93{94const uint16_t chunk_num = i / chunk_size;95const uint16_t chunk_index = (i % chunk_size);96#pragma GCC diagnostic push97#pragma GCC diagnostic ignored "-Wcast-align"98T *el_array = (T *)chunk_ptrs[chunk_num];99#pragma GCC diagnostic pop100return el_array[chunk_index];101}102103// allow use as an array for accessing elements. no bounds checking is performed104const T &operator[](uint16_t i) const105{106const uint16_t chunk_num = i / chunk_size;107const uint16_t chunk_index = (i % chunk_size);108#pragma GCC diagnostic push109#pragma GCC diagnostic ignored "-Wcast-align"110const T *el_array = (const T *)chunk_ptrs[chunk_num];111#pragma GCC diagnostic pop112return el_array[chunk_index];113}114};115116117