├── .gitignore ├── src ├── Array │ ├── Array.cpp │ ├── ArrayIterator.h │ └── ArrayDefinitions.h └── Array.h ├── library.properties ├── LICENSE ├── README.org └── examples ├── ConstTester └── ConstTester.ino └── ArrayTester └── ArrayTester.ino /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.idea 3 | /build 4 | /bin 5 | /lib 6 | /sftp-config.json 7 | .tags 8 | .tags_sorted_by_file 9 | -------------------------------------------------------------------------------- /src/Array/Array.cpp: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // Array.cpp 3 | // 4 | // 5 | // Authors: 6 | // Peter Polidoro peter@polidoro.io 7 | // ---------------------------------------------------------------------------- 8 | #include "../Array.h" 9 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Array 2 | version=1.2.1 3 | author=Peter Polidoro 4 | maintainer=Peter Polidoro 5 | sentence=An array container similar to the C++ std::array 6 | paragraph=Like this project? Please star it on GitHub! 7 | category=Data Storage 8 | url=https://github.com/janelia-arduino/Array.git 9 | architectures=* 10 | -------------------------------------------------------------------------------- /src/Array/ArrayIterator.h: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // ArrayIterator.h 3 | // 4 | // 5 | // Authors: 6 | // Peter Polidoro peter@polidoro.io 7 | // ---------------------------------------------------------------------------- 8 | #ifndef ARRAY_ITERATOR_H 9 | #define ARRAY_ITERATOR_H 10 | 11 | template 12 | class ArrayIterator 13 | { 14 | public: 15 | ArrayIterator(T * values_ptr) : values_ptr_{values_ptr}, position_{0} {} 16 | 17 | ArrayIterator(T * values_ptr, size_t size) : values_ptr_{values_ptr}, position_{size} {} 18 | 19 | bool operator!=(const ArrayIterator & other) const 20 | { 21 | return !(*this == other); 22 | } 23 | 24 | bool operator==(const ArrayIterator & other) const 25 | { 26 | return position_ == other.position_; 27 | } 28 | 29 | ArrayIterator & operator++() 30 | { 31 | ++position_; 32 | return *this; 33 | } 34 | 35 | T & operator*() const 36 | { 37 | return *(values_ptr_ + position_); 38 | } 39 | 40 | private: 41 | T * values_ptr_; 42 | size_t position_; 43 | }; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | License Agreement 2 | (3-clause BSD License) 3 | Janelia Research Campus Software Copyright 1.1 4 | 5 | Copyright (c) 2021, Howard Hughes Medical Institute 6 | All rights reserved. 7 | 8 | Redistribution and use in source and binary forms, with or without 9 | modification, are permitted provided that the following conditions are met: 10 | 11 | * Redistributions of source code must retain the above copyright notice, this 12 | list of conditions and the following disclaimer. 13 | 14 | * Redistributions in binary form must reproduce the above copyright notice, 15 | this list of conditions and the following disclaimer in the documentation 16 | and/or other materials provided with the distribution. 17 | 18 | * Neither the name of the Howard Hughes Medical Institute nor the 19 | names of its contributors may be used to endorse or promote products 20 | derived from this software without specific prior written 21 | permission. 22 | 23 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 26 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 27 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 29 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | #+TITLE: Array 2 | #+AUTHOR: Peter Polidoro 3 | #+EMAIL: peter@polidoro.io 4 | 5 | * Library Information 6 | - Name :: Array 7 | - Version :: 1.2.1 8 | - License :: BSD 9 | - URL :: https://github.com/janelia-arduino/Array 10 | - Author :: Peter Polidoro 11 | - Email :: peter@polidoro.io 12 | 13 | An array container similar to the C++ 14 | [[http://www.cplusplus.com/reference/array/array/][std::array]], with 15 | some [[http://www.cplusplus.com/reference/vector/vector/][std::vector]] 16 | methods added. The maximum size is fixed as a template parameter, but 17 | the size is variable, like a vector. Values can be pushed and popped 18 | and the size adjusts accordingly. The data are stored internally as a 19 | statically allocated c style array. Care must be taken not to 20 | dereference an empty array or access elements beyond bounds. 21 | 22 | This library is very similar to 23 | [[https://github.com/janelia-arduino/Vector][Vector]], however Vector 24 | stores data externally, outside the container, and this library stores 25 | data internally. The pointer to the external memory causes the Vector 26 | container to use more memory than this container, but storing the data 27 | internally makes it necessary to use the maximum size as a class 28 | template parameter. 29 | 30 | * Array vs Vector 31 | 32 | ** Array 33 | 34 | #+BEGIN_SRC C++ 35 | const int ELEMENT_COUNT_MAX = 5; 36 | Array array; 37 | array.push_back(77); 38 | #+END_SRC 39 | 40 | ** Vector 41 | 42 | #+BEGIN_SRC C++ 43 | const int ELEMENT_COUNT_MAX = 5; 44 | int storage_array[ELEMENT_COUNT_MAX]; 45 | Vector vector(storage_array); 46 | vector.push_back(77); 47 | #+END_SRC 48 | -------------------------------------------------------------------------------- /examples/ConstTester/ConstTester.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | const long BAUD = 115200; 7 | 8 | const size_t ELEMENT_COUNT_MAX = 5; 9 | typedef Array Elements; 10 | 11 | const size_t INDEX = 2; 12 | const size_t DELAY = 500; 13 | 14 | void setup() 15 | { 16 | Serial.begin(BAUD); 17 | while (!Serial) 18 | { 19 | // wait for serial port to connect. 20 | } 21 | } 22 | 23 | 24 | void loop() 25 | { 26 | const size_t c_style_array[ELEMENT_COUNT_MAX] = {15,14,13,12,11}; 27 | 28 | Elements array(c_style_array); 29 | array[2] = 28; 30 | Serial << "array:" << array << endl; 31 | delay(DELAY); 32 | Serial << "array[" << INDEX << "] = " << array[INDEX] << endl; 33 | delay(DELAY); 34 | size_t * array_data_ptr = array.data(); 35 | Serial << "array_data_ptr[" << INDEX << "] = " << array_data_ptr[INDEX] << endl; 36 | delay(DELAY); 37 | Serial << endl; 38 | delay(DELAY); 39 | 40 | const Elements const_array(c_style_array); 41 | Serial << "const_array:" << const_array << endl; 42 | delay(DELAY); 43 | Serial << "const_array[" << INDEX << "] = " << const_array[INDEX] << endl; 44 | delay(DELAY); 45 | const size_t * const_array_data_ptr = const_array.data(); 46 | Serial << "const_array_data_ptr[" << INDEX << "] = " << const_array_data_ptr[INDEX] << endl; 47 | delay(DELAY); 48 | Serial << endl; 49 | delay(DELAY); 50 | Serial << "Print const_array elements using iterators: "; 51 | for (const int element : const_array) 52 | { 53 | Serial << element << " "; 54 | } 55 | Serial << endl; 56 | delay(DELAY); 57 | 58 | const Elements const_array_copy(const_array); 59 | Serial << "const_array_copy:" << const_array_copy << endl; 60 | delay(DELAY); 61 | Serial << "const_array_copy[" << INDEX << "] = " << const_array_copy[INDEX] << endl; 62 | delay(DELAY); 63 | const size_t * const_array_copy_data_ptr = const_array_copy.data(); 64 | Serial << "const_array_copy_data_ptr[" << INDEX << "] = " << const_array_copy_data_ptr[INDEX] << endl; 65 | delay(DELAY); 66 | Serial << endl; 67 | delay(DELAY); 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/Array.h: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // Array.h 3 | // 4 | // 5 | // Authors: 6 | // Peter Polidoro peter@polidoro.io 7 | // ---------------------------------------------------------------------------- 8 | #ifndef ARRAY_H 9 | #define ARRAY_H 10 | 11 | #ifdef ARDUINO 12 | #include 13 | #else 14 | #include 15 | #endif 16 | #include "Array/ArrayIterator.h" 17 | 18 | 19 | template 21 | class Array 22 | { 23 | public: 24 | Array(); 25 | Array(const T & value); 26 | template 28 | Array(const U (&values)[N]); 29 | template 31 | Array(const Array & values); 32 | const T & operator[](size_t index) const; 33 | T & operator[](size_t index); 34 | const T & at(size_t index) const; 35 | T & at(size_t index); 36 | T & front(); 37 | T & back(); 38 | void clear(); 39 | template 40 | void fill(const U & value); 41 | template 43 | void fill(const U (&values)[N]); 44 | template 46 | void fill(const Array & values); 47 | template 48 | void assign(size_t n, 49 | const U & value); 50 | template 52 | void assign(size_t n, 53 | const U (&values)[N]); 54 | template 56 | void assign(size_t n, 57 | const Array & values); 58 | void push_back(const T & value); 59 | void pop_back(); 60 | void remove(size_t index); 61 | size_t size() const; 62 | size_t max_size() const; 63 | bool empty() const; 64 | bool full() const; 65 | const T * data() const; 66 | T * data(); 67 | typedef ArrayIterator iterator; 68 | iterator begin(); 69 | iterator end(); 70 | typedef ArrayIterator const_iterator; 71 | const_iterator begin() const; 72 | const_iterator end() const; 73 | private: 74 | T values_[MAX_SIZE]; 75 | size_t size_; 76 | }; 77 | 78 | template 80 | inline Print & operator <<(Print & stream, 81 | const Array & array) 82 | { 83 | stream.print("["); 84 | for (size_t i=0; i 2 | #include 3 | #include 4 | 5 | 6 | const long BAUD = 115200; 7 | 8 | const int ELEMENT_COUNT_MAX = 5; 9 | typedef Array Elements; 10 | const size_t DELAY = 500; 11 | 12 | void stuffFullArray(Elements & array) 13 | { 14 | array.clear(); 15 | for (int i=0; i array_copy_2(array_simple); 143 | Serial << "array_copy_2:" << endl; 144 | Serial << array_copy_2 << endl; 145 | Serial << "array_copy_2.max_size():" << endl; 146 | Serial << array_copy_2.max_size() << endl; 147 | delay(DELAY); 148 | 149 | // initialize with another array instance 150 | Array array_copy_3(array_copy_2); 151 | Serial << "array_copy_3:" << endl; 152 | Serial << array_copy_3 << endl; 153 | Serial << "array_copy_3.max_size():" << endl; 154 | Serial << array_copy_3.max_size() << endl; 155 | delay(DELAY); 156 | 157 | // get pointer to raw data 158 | size_t * array_copy_3_ptr = array_copy_3.data(); 159 | size_t index = 2; 160 | if (index < array_copy_3.size()) 161 | { 162 | Serial << "array_copy_3_ptr[index]:" << endl; 163 | Serial << array_copy_3_ptr[index] << endl; 164 | } 165 | delay(DELAY); 166 | 167 | Elements array_copy_4(array_simple); 168 | Serial << "array_copy_4:" << endl; 169 | Serial << array_copy_4 << endl; 170 | Serial << "array_copy_4.size():" << endl; 171 | Serial << array_copy_4.size() << endl; 172 | array_copy_4.remove(2); 173 | Serial << "array_copy_4.remove(2):" << endl; 174 | Serial << array_copy_4 << endl; 175 | Serial << "array_copy_4.size():" << endl; 176 | Serial << array_copy_4.size() << endl; 177 | delay(DELAY); 178 | 179 | } 180 | -------------------------------------------------------------------------------- /src/Array/ArrayDefinitions.h: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // ArrayDefinitions.h 3 | // 4 | // 5 | // Authors: 6 | // Peter Polidoro peter@polidoro.io 7 | // ---------------------------------------------------------------------------- 8 | #ifndef ARRAY_DEFINITIONS_H 9 | #define ARRAY_DEFINITIONS_H 10 | 11 | #ifndef ARDUINO 12 | #include 13 | #endif 14 | 15 | 16 | template 18 | Array::Array() 19 | { 20 | size_ = 0; 21 | } 22 | 23 | template 25 | Array::Array(const T & value) 26 | { 27 | size_ = 0; 28 | fill(value); 29 | } 30 | 31 | template 33 | template 35 | Array::Array(const U (&values)[N]) 36 | { 37 | size_ = 0; 38 | fill(values); 39 | } 40 | 41 | template 43 | template 45 | Array::Array(const Array & values) 46 | { 47 | size_ = 0; 48 | fill(values); 49 | } 50 | 51 | template 53 | const T & Array::operator[](size_t index) const 54 | { 55 | return values_[index]; 56 | } 57 | 58 | template 60 | T & Array::operator[](size_t index) 61 | { 62 | return values_[index]; 63 | } 64 | 65 | template 67 | const T & Array::at(size_t index) const 68 | { 69 | return values_[index]; 70 | } 71 | 72 | template 74 | T & Array::at(size_t index) 75 | { 76 | return values_[index]; 77 | } 78 | 79 | template 81 | T & Array::front() 82 | { 83 | return values_[0]; 84 | } 85 | 86 | template 88 | T & Array::back() 89 | { 90 | return values_[size_-1]; 91 | } 92 | 93 | template 95 | void Array::clear() 96 | { 97 | size_ = 0; 98 | } 99 | 100 | template 102 | template 103 | void Array::fill(const U & value) 104 | { 105 | assign(MAX_SIZE,value); 106 | } 107 | 108 | template 110 | template 112 | void Array::fill(const U (&values)[N]) 113 | { 114 | assign(N,values); 115 | } 116 | 117 | template 119 | template 121 | void Array::fill(const Array & values) 122 | { 123 | assign(values.size(),values); 124 | } 125 | 126 | template 128 | template 129 | void Array::assign(size_t n, 130 | const U & value) 131 | { 132 | size_t assign_size = ((n < MAX_SIZE) ? n : MAX_SIZE); 133 | size_ = assign_size; 134 | for (size_t i=0; i 142 | template 144 | void Array::assign(size_t n, 145 | const U (&values)[N]) 146 | { 147 | size_t n_smallest = ((n < N) ? n : N); 148 | size_t assign_size = ((n_smallest < MAX_SIZE) ? n_smallest : MAX_SIZE); 149 | size_ = assign_size; 150 | for (size_t i=0; i 158 | template 160 | void Array::assign(size_t n, 161 | const Array & values) 162 | { 163 | size_t n_smallest = ((n < values.size()) ? n : values.size()); 164 | size_t assign_size = ((n_smallest < MAX_SIZE) ? n_smallest : MAX_SIZE); 165 | size_ = assign_size; 166 | for (size_t i=0; i 174 | void Array::push_back(const T & value) 175 | { 176 | if (size_ < MAX_SIZE) 177 | { 178 | values_[size_++] = value; 179 | } 180 | } 181 | 182 | template 184 | void Array::pop_back() 185 | { 186 | if (size_ > 0) 187 | { 188 | --size_; 189 | } 190 | } 191 | 192 | template 194 | void Array::remove(size_t index) 195 | { 196 | if (size_ > index) 197 | { 198 | for (size_t i=index; i<(size_-1); ++i) 199 | { 200 | values_[i] = values_[i+1]; 201 | } 202 | --size_; 203 | } 204 | } 205 | 206 | template 208 | size_t Array::size() const 209 | { 210 | return size_; 211 | } 212 | 213 | template 215 | size_t Array::max_size() const 216 | { 217 | return MAX_SIZE; 218 | } 219 | 220 | template 222 | bool Array::empty() const 223 | { 224 | return size_ == 0; 225 | } 226 | 227 | template 229 | bool Array::full() const 230 | { 231 | return size_ == MAX_SIZE; 232 | } 233 | 234 | template 236 | const T * Array::data() const 237 | { 238 | return values_; 239 | } 240 | 241 | template 243 | T * Array::data() 244 | { 245 | return values_; 246 | } 247 | 248 | template 250 | typename Array::iterator Array::begin() 251 | { 252 | return iterator(values_); 253 | } 254 | 255 | template 257 | typename Array::iterator Array::end() 258 | { 259 | return iterator(values_,size_); 260 | } 261 | 262 | template 264 | typename Array::const_iterator Array::begin() const 265 | { 266 | return const_iterator(values_); 267 | } 268 | 269 | template 271 | typename Array::const_iterator Array::end() const 272 | { 273 | return const_iterator(values_,size_); 274 | } 275 | 276 | #endif 277 | --------------------------------------------------------------------------------