├── .gitignore ├── LICENSE ├── README.org ├── examples ├── PlatformIO │ ├── README.org │ ├── include │ │ └── README │ ├── lib │ │ └── README │ ├── platformio.ini │ ├── src │ │ └── main.cpp │ └── test │ │ ├── README │ │ └── test_vector.cpp └── VectorTester │ └── VectorTester.ino ├── library.json ├── library.properties └── src ├── Vector.h └── Vector ├── Vector.cpp ├── VectorDefinitions.h └── VectorIterator.h /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.idea 3 | /build 4 | /bin 5 | /lib 6 | /sftp-config.json 7 | .tags 8 | .tags_sorted_by_file 9 | .pio 10 | -------------------------------------------------------------------------------- /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: Vector 2 | #+AUTHOR: Peter Polidoro 3 | #+EMAIL: peter@polidoro.io 4 | 5 | * Library Information 6 | - Name :: Vector 7 | - Version :: 1.2.2 8 | - License :: BSD 9 | - URL :: https://github.com/janelia-arduino/Vector 10 | - Author :: Peter Polidoro 11 | - Email :: peter@polidoro.io 12 | 13 | A sequence container similar to the C++ 14 | [[http://www.cplusplus.com/reference/vector/vector/][std::vector]], but 15 | instead of allocating memory dynamically, this container points to an 16 | external, statically allocated c style array. The maximum size is 17 | fixed at compile time, but the size can change by pushing and popping 18 | elements from the vector. Static memory allocation is used to avoid 19 | dynamic allocation problems on very small embedded processors. Care 20 | must be taken not to dereference an empty vector, access elements 21 | beyond bounds, or use without setting the storage array. 22 | 23 | This library is very similar to 24 | [[https://github.com/janelia-arduino/Array][Array]], however Array 25 | stores data internally in the container and this library stores data 26 | externally. The pointer to the external memory causes this container 27 | to use more memory than the Array container, but storing the data 28 | externally avoids needing the maximum size as a class template 29 | parameter. 30 | 31 | * Vector vs Array 32 | ** Vector 33 | 34 | #+BEGIN_SRC C++ 35 | const int ELEMENT_COUNT_MAX = 5; 36 | int storage_array[ELEMENT_COUNT_MAX]; 37 | Vector vector(storage_array); 38 | vector.push_back(77); 39 | #+END_SRC 40 | 41 | ** Array 42 | 43 | #+BEGIN_SRC C++ 44 | const int ELEMENT_COUNT_MAX = 5; 45 | Array array; 46 | array.push_back(77); 47 | #+END_SRC 48 | -------------------------------------------------------------------------------- /examples/PlatformIO/README.org: -------------------------------------------------------------------------------- 1 | #+TITLE: PlatformIO example 2 | 3 | This example can be built using [[https://docs.platformio.org][PlatformIO]], 4 | instead of the Arduino IDE. 5 | 6 | ** Running the code 7 | 8 | *** Platform: native 9 | 10 | - To run the code in src/ natively: 11 | 12 | #+BEGIN_SRC sh 13 | pio run -e native && .pio/build/native/program 14 | #+END_SRC 15 | 16 | - To run the code in test/ natively: 17 | 18 | #+BEGIN_SRC sh 19 | pio test -e native 20 | #+END_SRC 21 | 22 | *** Platform: Arduino 23 | 24 | Before doing any of this, edit =platformio.ini= for your board. 25 | 26 | - To run the code in src/ on a device and then monitor its output: 27 | 28 | #+BEGIN_SRC sh 29 | pio run -e uno -t upload && pio device monitor 30 | #+END_SRC 31 | 32 | - To run the tests on a device: 33 | 34 | #+BEGIN_SRC sh 35 | pio test -e uno 36 | #+END_SRC 37 | 38 | *** All platforms at once 39 | 40 | - To run the tests natively on all platforms described in `platformio.ini`: 41 | 42 | #+BEGIN_SRC sh 43 | pio test 44 | #+END_SRC 45 | 46 | -------------------------------------------------------------------------------- /examples/PlatformIO/include/README: -------------------------------------------------------------------------------- 1 | This directory is intended for project header files. 2 | 3 | A header file is a file containing C declarations and macro definitions 4 | to be shared between several project source files. You request the use of a 5 | header file in your project source file (C, C++, etc) located in `src` folder 6 | by including it, with the C preprocessing directive `#include'. 7 | 8 | ```src/main.c 9 | 10 | #include "header.h" 11 | 12 | int main (void) 13 | { 14 | ... 15 | } 16 | ``` 17 | 18 | Including a header file produces the same results as copying the header file 19 | into each source file that needs it. Such copying would be time-consuming 20 | and error-prone. With a header file, the related declarations appear 21 | in only one place. If they need to be changed, they can be changed in one 22 | place, and programs that include the header file will automatically use the 23 | new version when next recompiled. The header file eliminates the labor of 24 | finding and changing all the copies as well as the risk that a failure to 25 | find one copy will result in inconsistencies within a program. 26 | 27 | In C, the usual convention is to give header files names that end with `.h'. 28 | It is most portable to use only letters, digits, dashes, and underscores in 29 | header file names, and at most one dot. 30 | 31 | Read more about using header files in official GCC documentation: 32 | 33 | * Include Syntax 34 | * Include Operation 35 | * Once-Only Headers 36 | * Computed Includes 37 | 38 | https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html 39 | -------------------------------------------------------------------------------- /examples/PlatformIO/lib/README: -------------------------------------------------------------------------------- 1 | This directory is intended for project specific (private) libraries. 2 | PlatformIO will compile them to static libraries and link into executable file. 3 | 4 | The source code of each library should be placed in a an own separate directory 5 | ("lib/your_library_name/[here are source files]"). 6 | 7 | For example, see a structure of the following two libraries `Foo` and `Bar`: 8 | 9 | |--lib 10 | | | 11 | | |--Bar 12 | | | |--docs 13 | | | |--examples 14 | | | |--src 15 | | | |- Bar.c 16 | | | |- Bar.h 17 | | | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html 18 | | | 19 | | |--Foo 20 | | | |- Foo.c 21 | | | |- Foo.h 22 | | | 23 | | |- README --> THIS FILE 24 | | 25 | |- platformio.ini 26 | |--src 27 | |- main.c 28 | 29 | and a contents of `src/main.c`: 30 | ``` 31 | #include 32 | #include 33 | 34 | int main (void) 35 | { 36 | ... 37 | } 38 | 39 | ``` 40 | 41 | PlatformIO Library Dependency Finder will find automatically dependent 42 | libraries scanning project source files. 43 | 44 | More information about PlatformIO Library Dependency Finder 45 | - https://docs.platformio.org/page/librarymanager/ldf.html 46 | -------------------------------------------------------------------------------- /examples/PlatformIO/platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | ; common options for all environments 12 | [env] 13 | monitor_speed = 115200 14 | test_speed = 115200 15 | lib_deps = 16 | # https://github.com/janelia-arduino/Vector.git#branchname 17 | Vector 18 | 19 | [env:uno] 20 | platform = atmelavr 21 | framework = arduino 22 | board = uno 23 | 24 | ; Documentation for native platform: 25 | ; https://docs.platformio.org/en/latest/platforms/native.html 26 | [env:native] 27 | platform = native 28 | #build_flags = -DNATIVE 29 | #lib_deps = 30 | # ${env.lib_deps} 31 | # Dep1 32 | # Dep2 33 | -------------------------------------------------------------------------------- /examples/PlatformIO/src/main.cpp: -------------------------------------------------------------------------------- 1 | #ifdef ARDUINO 2 | # include 3 | #else 4 | # include 5 | #endif 6 | 7 | #include 8 | 9 | char storage[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\r', '\n' }; 10 | Vector vector(storage, sizeof(storage)); 11 | 12 | #ifdef ARDUINO 13 | 14 | void setup() { 15 | Serial.begin(115200); 16 | Serial.println("Starting up."); 17 | } 18 | 19 | void loop() { 20 | Serial.print("Vector size is "); 21 | Serial.println(vector.size()); 22 | 23 | Serial.print("Vector content: "); 24 | for (unsigned int i = 0; i < vector.size(); i++) { 25 | Serial.print(vector.at(i)); 26 | } 27 | 28 | delay(2000); 29 | } 30 | 31 | #else /* !defined(ARDUINO) */ 32 | 33 | int main(int argc, char **argv) { 34 | puts("Starting up."); 35 | printf("Vector size is %d\n", vector.size()); 36 | 37 | printf("Vector content: "); 38 | for (unsigned int i = 0; i < vector.size(); i++) { 39 | putchar(vector.at(i)); 40 | } 41 | 42 | return 0; 43 | } 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /examples/PlatformIO/test/README: -------------------------------------------------------------------------------- 1 | This directory is intended for PlatformIO Unit Testing and project tests. 2 | 3 | Unit Testing is a software testing method by which individual units of 4 | source code, sets of one or more MCU program modules together with associated 5 | control data, usage procedures, and operating procedures, are tested to 6 | determine whether they are fit for use. Unit testing finds problems early 7 | in the development cycle. 8 | 9 | More information about PlatformIO Unit Testing: 10 | - https://docs.platformio.org/page/plus/unit-testing.html 11 | -------------------------------------------------------------------------------- /examples/PlatformIO/test/test_vector.cpp: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // test_vector.cpp 3 | // 4 | // 5 | // Authors: 6 | // Darsey Litzenberger dlitz@dlitz.net 7 | // ---------------------------------------------------------------------------- 8 | #include 9 | #include 10 | 11 | static void test_vector_size() { 12 | int storage[8] = { 21, 42, 84, 0, 0, 0, 0, 0 }; 13 | Vector v(storage, 3); 14 | TEST_ASSERT_EQUAL(8, v.max_size()); 15 | TEST_ASSERT_EQUAL(3, v.size()); 16 | v.push_back(10); 17 | v.push_back(20); 18 | v.push_back(30); 19 | TEST_ASSERT_EQUAL(6, v.size()); 20 | v.pop_back(); 21 | TEST_ASSERT_EQUAL(5, v.size()); 22 | } 23 | 24 | static void test_vector_iterator() { 25 | int storage[8] = { 21, 42, 84, 0, 0, 0, 0, 0 }; 26 | Vector v(storage, 3); 27 | 28 | const int expected[3] = { 21, 42, 84 }; 29 | int i=0; 30 | for (auto it = v.begin(); it != v.end(); ++it, ++i) { 31 | TEST_ASSERT_EQUAL(expected[i], *it); 32 | } 33 | TEST_ASSERT_EQUAL(3, i); 34 | } 35 | 36 | static void runTests() { 37 | UNITY_BEGIN(); 38 | RUN_TEST(test_vector_size); 39 | RUN_TEST(test_vector_iterator); 40 | UNITY_END(); 41 | } 42 | 43 | #ifdef ARDUINO 44 | 45 | void setup() { 46 | runTests(); 47 | } 48 | 49 | void loop() { 50 | } 51 | 52 | #else /* !defined(ARDUINO) */ 53 | 54 | int main(int argc, char **argv) 55 | { 56 | runTests(); 57 | return 0; 58 | } 59 | 60 | #endif /* !defined(ARDUINO) */ 61 | -------------------------------------------------------------------------------- /examples/VectorTester/VectorTester.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | const long BAUD = 115200; 7 | 8 | const int ELEMENT_COUNT_MAX = 5; 9 | typedef Vector Elements; 10 | const size_t DELAY = 500; 11 | 12 | void setup() 13 | { 14 | Serial.begin(BAUD); 15 | while (!Serial) 16 | { 17 | // wait for serial port to connect. 18 | } 19 | } 20 | 21 | void loop() 22 | { 23 | int storage_array[ELEMENT_COUNT_MAX]; 24 | Elements vector; 25 | vector.setStorage(storage_array); 26 | Serial << "vector.max_size(): " << vector.max_size() << endl; 27 | Serial << "vector.size(): " << vector.size() << endl; 28 | Serial << "vector:" << endl; 29 | Serial << vector << endl; 30 | delay(DELAY); 31 | 32 | vector.push_back(10); 33 | vector.push_back(8); 34 | vector.push_back(7); 35 | Serial << "vector.max_size(): " << vector.max_size() << endl; 36 | Serial << "vector.size(): " << vector.size() << endl; 37 | Serial << "vector:" << endl; 38 | Serial << vector << endl; 39 | vector.remove(0); 40 | Serial << "vector.remove(0):" << endl; 41 | Serial << vector << endl; 42 | vector.remove(1); 43 | Serial << "vector.remove(1):" << endl; 44 | Serial << vector << endl; 45 | delay(DELAY); 46 | 47 | int storage_array2[ELEMENT_COUNT_MAX]; 48 | Elements vector2(storage_array2); 49 | vector2.push_back(1); 50 | vector2.push_back(2); 51 | vector2.push_back(4); 52 | vector2.pop_back(); 53 | Serial << "vector2.max_size(): " << vector2.max_size() << endl; 54 | Serial << "vector2.size(): " << vector2.size() << endl; 55 | Serial << "vector2:" << endl; 56 | Serial << vector2 << endl; 57 | delay(DELAY); 58 | Serial << "Print vector2 elements using iterators: "; 59 | for (int element : vector2) 60 | { 61 | Serial << element << " "; 62 | } 63 | Serial << endl; 64 | delay(DELAY); 65 | 66 | int storage_array3[ELEMENT_COUNT_MAX]; 67 | storage_array3[0] = 3; 68 | storage_array3[1] = 5; 69 | Elements vector3(storage_array3); 70 | Serial << "vector3.max_size(): " << vector3.max_size() << endl; 71 | Serial << "vector3.size(): " << vector3.size() << endl; 72 | Serial << "vector3:" << endl; 73 | Serial << vector3 << endl; 74 | delay(DELAY); 75 | 76 | int storage_array4[ELEMENT_COUNT_MAX]; 77 | storage_array4[0] = 3; 78 | storage_array4[1] = 5; 79 | Elements vector4(storage_array4,2); 80 | Serial << "vector4.max_size(): " << vector4.max_size() << endl; 81 | Serial << "vector4.size(): " << vector4.size() << endl; 82 | Serial << "vector4:" << endl; 83 | Serial << vector4 << endl; 84 | delay(DELAY); 85 | 86 | int storage_array5[1]; 87 | Elements vector5(storage_array5); 88 | Serial << "vector5.max_size(): " << vector5.max_size() << endl; 89 | Serial << "vector5.size(): " << vector5.size() << endl; 90 | Serial << "vector5:" << endl; 91 | Serial << vector5 << endl; 92 | delay(DELAY); 93 | 94 | int storage_array6[ELEMENT_COUNT_MAX]; 95 | Elements vector6(storage_array6); 96 | vector6.assign(ELEMENT_COUNT_MAX-1,8); 97 | Serial << "vector6:" << endl; 98 | Serial << vector6 << endl; 99 | delay(DELAY); 100 | } 101 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Vector", 3 | "version": "1.2.2", 4 | "description": "An array container similar to the C++ std::vector", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/janelia-arduino/Vector.git" 8 | }, 9 | "authors": [ 10 | { 11 | "name": "Peter Polidoro", 12 | "email": "peter@polidoro.io", 13 | "url": "https://github.com/peterpolidoro", 14 | "maintainer": true 15 | } 16 | ], 17 | "license": "BSD-3-Clause", 18 | "homepage": "https://github.com/janelia-arduino/Vector", 19 | "frameworks": "*", 20 | "platforms": "*" 21 | } 22 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Vector 2 | version=1.2.2 3 | author=Peter Polidoro 4 | maintainer=Peter Polidoro 5 | sentence=An array container similar to the C++ std::vector 6 | paragraph=Like this project? Please star it on GitHub! 7 | category=Data Storage 8 | url=https://github.com/janelia-arduino/Vector.git 9 | architectures=* 10 | -------------------------------------------------------------------------------- /src/Vector.h: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // Vector.h 3 | // 4 | // 5 | // Authors: 6 | // Peter Polidoro peter@polidoro.io 7 | // ---------------------------------------------------------------------------- 8 | #ifndef VECTOR_H 9 | #define VECTOR_H 10 | #ifdef ARDUINO 11 | #include 12 | #else 13 | #include 14 | #endif 15 | #include "Vector/VectorIterator.h" 16 | 17 | 18 | template 19 | class Vector 20 | { 21 | public: 22 | Vector(); 23 | template 24 | Vector(T (&values)[MAX_SIZE], 25 | size_t size=0); 26 | template 27 | void setStorage(T (&values)[MAX_SIZE], 28 | size_t size=0); 29 | void setStorage(T * values, 30 | size_t max_size, 31 | size_t size); 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 45 | void fill(const Vector & values); 46 | template 47 | void assign(size_t n, 48 | const U & value); 49 | template 51 | void assign(size_t n, 52 | const U (&values)[N]); 53 | template 54 | void assign(size_t n, 55 | const Vector & values); 56 | void push_back(const T & value); 57 | void pop_back(); 58 | void remove(size_t index); 59 | size_t size() const; 60 | size_t max_size() const; 61 | bool empty() const; 62 | bool full() const; 63 | const T * data() const; 64 | T * data(); 65 | typedef VectorIterator iterator; 66 | iterator begin(); 67 | iterator end(); 68 | typedef VectorIterator const_iterator; 69 | const_iterator begin() const; 70 | const_iterator end() const; 71 | 72 | private: 73 | T * values_; 74 | size_t max_size_; 75 | size_t size_; 76 | }; 77 | 78 | #ifdef ARDUINO /* `Print` is declared in the Arduino library and gives a compilation error elsewhere. */ 79 | template 80 | inline Print & operator <<(Print & stream, 81 | const Vector & vector) 82 | { 83 | stream.print("["); 84 | for (size_t i=0; i 13 | #endif 14 | 15 | template 16 | Vector::Vector() 17 | { 18 | values_ = NULL; 19 | max_size_ = 0; 20 | size_ = 0; 21 | } 22 | 23 | template 24 | template 25 | Vector::Vector(T (&values)[MAX_SIZE], 26 | size_t size) 27 | { 28 | setStorage(values,size); 29 | } 30 | 31 | template 32 | template 33 | void Vector::setStorage(T (&values)[MAX_SIZE], 34 | size_t size) 35 | { 36 | values_ = values; 37 | max_size_ = MAX_SIZE; 38 | size_ = size; 39 | } 40 | 41 | template 42 | void Vector::setStorage(T * values, 43 | size_t max_size, 44 | size_t size) 45 | { 46 | values_ = values; 47 | max_size_ = max_size; 48 | size_ = size; 49 | } 50 | 51 | template 52 | const T & Vector::operator[](size_t index) const 53 | { 54 | return values_[index]; 55 | } 56 | 57 | template 58 | T & Vector::operator[](size_t index) 59 | { 60 | return values_[index]; 61 | } 62 | 63 | template 64 | T & Vector::at(size_t index) 65 | { 66 | return values_[index]; 67 | } 68 | 69 | template 70 | const T & Vector::at(size_t index) const 71 | { 72 | return values_[index]; 73 | } 74 | 75 | template 76 | T & Vector::front() 77 | { 78 | return values_[0]; 79 | } 80 | 81 | template 82 | T & Vector::back() 83 | { 84 | return values_[size_-1]; 85 | } 86 | 87 | template 88 | void Vector::clear() 89 | { 90 | size_ = 0; 91 | } 92 | 93 | template 94 | template 95 | void Vector::fill(const U & value) 96 | { 97 | assign(max_size_,value); 98 | } 99 | 100 | template 101 | template 103 | void Vector::fill(const U (&values)[N]) 104 | { 105 | assign(N,values); 106 | } 107 | 108 | template 109 | template 110 | void Vector::fill(const Vector & values) 111 | { 112 | assign(values.size(),values); 113 | } 114 | 115 | template 116 | template 117 | void Vector::assign(size_t n, 118 | const U & value) 119 | { 120 | size_t assign_size = ((n < max_size_) ? n : max_size_); 121 | size_ = assign_size; 122 | for (size_t i=0; i 129 | template 131 | void Vector::assign(size_t n, 132 | const U (&values)[N]) 133 | { 134 | size_t n_smallest = ((n < N) ? n : N); 135 | size_t assign_size = ((n_smallest < max_size_) ? n_smallest : max_size_); 136 | size_ = assign_size; 137 | for (size_t i=0; i 144 | template 145 | void Vector::assign(size_t n, 146 | const Vector & values) 147 | { 148 | size_t n_smallest = ((n < values.size()) ? n : values.size()); 149 | size_t assign_size = ((n_smallest < max_size_) ? n_smallest : max_size_); 150 | size_ = assign_size; 151 | for (size_t i=0; i 158 | void Vector::push_back(const T & value) 159 | { 160 | if ((values_ != NULL) && (size_ < max_size_)) 161 | { 162 | values_[size_++] = value; 163 | } 164 | } 165 | 166 | template 167 | void Vector::pop_back() 168 | { 169 | if (size_ > 0) 170 | { 171 | --size_; 172 | } 173 | } 174 | 175 | template 176 | void Vector::remove(size_t index) 177 | { 178 | if (size_ > index) 179 | { 180 | for (size_t i=index; i<(size_-1); ++i) 181 | { 182 | values_[i] = values_[i+1]; 183 | } 184 | --size_; 185 | } 186 | } 187 | 188 | template 189 | size_t Vector::size() const 190 | { 191 | return size_; 192 | } 193 | 194 | template 195 | size_t Vector::max_size() const 196 | { 197 | return max_size_; 198 | } 199 | 200 | template 201 | bool Vector::empty() const 202 | { 203 | return size_ == 0; 204 | } 205 | 206 | template 207 | bool Vector::full() const 208 | { 209 | return size_ == max_size_; 210 | } 211 | 212 | template 213 | T * Vector::data() 214 | { 215 | return values_; 216 | } 217 | 218 | template 219 | const T * Vector::data() const 220 | { 221 | return values_; 222 | } 223 | 224 | template 225 | typename Vector::iterator Vector::begin() 226 | { 227 | return iterator(values_); 228 | } 229 | 230 | template 231 | typename Vector::iterator Vector::end() 232 | { 233 | return iterator(values_,size_); 234 | } 235 | 236 | template 237 | typename Vector::const_iterator Vector::begin() const 238 | { 239 | return const_iterator(values_); 240 | } 241 | 242 | template 243 | typename Vector::const_iterator Vector::end() const 244 | { 245 | return const_iterator(values_,size_); 246 | } 247 | 248 | #endif 249 | -------------------------------------------------------------------------------- /src/Vector/VectorIterator.h: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // VectorIterator.h 3 | // 4 | // 5 | // Authors: 6 | // Peter Polidoro peter@polidoro.io 7 | // ---------------------------------------------------------------------------- 8 | #ifndef VECTOR_ITERATOR_H 9 | #define VECTOR_ITERATOR_H 10 | 11 | template 12 | class VectorIterator 13 | { 14 | public: 15 | VectorIterator(T * values_ptr) : values_ptr_{values_ptr}, position_{0} {} 16 | 17 | VectorIterator(T * values_ptr, size_t size) : values_ptr_{values_ptr}, position_{size} {} 18 | 19 | bool operator!=(const VectorIterator & other) const 20 | { 21 | return !(*this == other); 22 | } 23 | 24 | bool operator==(const VectorIterator & other) const 25 | { 26 | return position_ == other.position_; 27 | } 28 | 29 | VectorIterator & 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 | --------------------------------------------------------------------------------