v2.9
This commit is contained in:
9
lib/Vector/.gitignore
vendored
Normal file
9
lib/Vector/.gitignore
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
.DS_Store
|
||||
/.idea
|
||||
/build
|
||||
/bin
|
||||
/lib
|
||||
/sftp-config.json
|
||||
.tags
|
||||
.tags_sorted_by_file
|
||||
.pio
|
1
lib/Vector/.piopm
Normal file
1
lib/Vector/.piopm
Normal file
@ -0,0 +1 @@
|
||||
{"type": "library", "name": "Vector", "version": "1.2.2", "spec": {"owner": "janelia-arduino", "id": 5825, "name": "Vector", "requirements": null, "uri": null}}
|
32
lib/Vector/LICENSE
Normal file
32
lib/Vector/LICENSE
Normal file
@ -0,0 +1,32 @@
|
||||
License Agreement
|
||||
(3-clause BSD License)
|
||||
Janelia Research Campus Software Copyright 1.1
|
||||
|
||||
Copyright (c) 2021, Howard Hughes Medical Institute
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of the Howard Hughes Medical Institute nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written
|
||||
permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
47
lib/Vector/README.org
Normal file
47
lib/Vector/README.org
Normal file
@ -0,0 +1,47 @@
|
||||
#+TITLE: Vector
|
||||
#+AUTHOR: Peter Polidoro
|
||||
#+EMAIL: peter@polidoro.io
|
||||
|
||||
* Library Information
|
||||
- Name :: Vector
|
||||
- Version :: 1.2.2
|
||||
- License :: BSD
|
||||
- URL :: https://github.com/janelia-arduino/Vector
|
||||
- Author :: Peter Polidoro
|
||||
- Email :: peter@polidoro.io
|
||||
|
||||
A sequence container similar to the C++
|
||||
[[http://www.cplusplus.com/reference/vector/vector/][std::vector]], but
|
||||
instead of allocating memory dynamically, this container points to an
|
||||
external, statically allocated c style array. The maximum size is
|
||||
fixed at compile time, but the size can change by pushing and popping
|
||||
elements from the vector. Static memory allocation is used to avoid
|
||||
dynamic allocation problems on very small embedded processors. Care
|
||||
must be taken not to dereference an empty vector, access elements
|
||||
beyond bounds, or use without setting the storage array.
|
||||
|
||||
This library is very similar to
|
||||
[[https://github.com/janelia-arduino/Array][Array]], however Array
|
||||
stores data internally in the container and this library stores data
|
||||
externally. The pointer to the external memory causes this container
|
||||
to use more memory than the Array container, but storing the data
|
||||
externally avoids needing the maximum size as a class template
|
||||
parameter.
|
||||
|
||||
* Vector vs Array
|
||||
** Vector
|
||||
|
||||
#+BEGIN_SRC C++
|
||||
const int ELEMENT_COUNT_MAX = 5;
|
||||
int storage_array[ELEMENT_COUNT_MAX];
|
||||
Vector<int> vector(storage_array);
|
||||
vector.push_back(77);
|
||||
#+END_SRC
|
||||
|
||||
** Array
|
||||
|
||||
#+BEGIN_SRC C++
|
||||
const int ELEMENT_COUNT_MAX = 5;
|
||||
Array<int,ELEMENT_COUNT_MAX> array;
|
||||
array.push_back(77);
|
||||
#+END_SRC
|
45
lib/Vector/examples/PlatformIO/README.org
Normal file
45
lib/Vector/examples/PlatformIO/README.org
Normal file
@ -0,0 +1,45 @@
|
||||
#+TITLE: PlatformIO example
|
||||
|
||||
This example can be built using [[https://docs.platformio.org][PlatformIO]],
|
||||
instead of the Arduino IDE.
|
||||
|
||||
** Running the code
|
||||
|
||||
*** Platform: native
|
||||
|
||||
- To run the code in src/ natively:
|
||||
|
||||
#+BEGIN_SRC sh
|
||||
pio run -e native && .pio/build/native/program
|
||||
#+END_SRC
|
||||
|
||||
- To run the code in test/ natively:
|
||||
|
||||
#+BEGIN_SRC sh
|
||||
pio test -e native
|
||||
#+END_SRC
|
||||
|
||||
*** Platform: Arduino
|
||||
|
||||
Before doing any of this, edit =platformio.ini= for your board.
|
||||
|
||||
- To run the code in src/ on a device and then monitor its output:
|
||||
|
||||
#+BEGIN_SRC sh
|
||||
pio run -e uno -t upload && pio device monitor
|
||||
#+END_SRC
|
||||
|
||||
- To run the tests on a device:
|
||||
|
||||
#+BEGIN_SRC sh
|
||||
pio test -e uno
|
||||
#+END_SRC
|
||||
|
||||
*** All platforms at once
|
||||
|
||||
- To run the tests natively on all platforms described in `platformio.ini`:
|
||||
|
||||
#+BEGIN_SRC sh
|
||||
pio test
|
||||
#+END_SRC
|
||||
|
38
lib/Vector/examples/PlatformIO/include/README
Normal file
38
lib/Vector/examples/PlatformIO/include/README
Normal file
@ -0,0 +1,38 @@
|
||||
This directory is intended for project header files.
|
||||
|
||||
A header file is a file containing C declarations and macro definitions
|
||||
to be shared between several project source files. You request the use of a
|
||||
header file in your project source file (C, C++, etc) located in `src` folder
|
||||
by including it, with the C preprocessing directive `#include'.
|
||||
|
||||
```src/main.c
|
||||
|
||||
#include "header.h"
|
||||
|
||||
int main (void)
|
||||
{
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Including a header file produces the same results as copying the header file
|
||||
into each source file that needs it. Such copying would be time-consuming
|
||||
and error-prone. With a header file, the related declarations appear
|
||||
in only one place. If they need to be changed, they can be changed in one
|
||||
place, and programs that include the header file will automatically use the
|
||||
new version when next recompiled. The header file eliminates the labor of
|
||||
finding and changing all the copies as well as the risk that a failure to
|
||||
find one copy will result in inconsistencies within a program.
|
||||
|
||||
In C, the usual convention is to give header files names that end with `.h'.
|
||||
It is most portable to use only letters, digits, dashes, and underscores in
|
||||
header file names, and at most one dot.
|
||||
|
||||
Read more about using header files in official GCC documentation:
|
||||
|
||||
* Include Syntax
|
||||
* Include Operation
|
||||
* Once-Only Headers
|
||||
* Computed Includes
|
||||
|
||||
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
|
45
lib/Vector/examples/PlatformIO/lib/README
Normal file
45
lib/Vector/examples/PlatformIO/lib/README
Normal file
@ -0,0 +1,45 @@
|
||||
This directory is intended for project specific (private) libraries.
|
||||
PlatformIO will compile them to static libraries and link into executable file.
|
||||
|
||||
The source code of each library should be placed in a an own separate directory
|
||||
("lib/your_library_name/[here are source files]").
|
||||
|
||||
For example, see a structure of the following two libraries `Foo` and `Bar`:
|
||||
|
||||
|--lib
|
||||
| |
|
||||
| |--Bar
|
||||
| | |--docs
|
||||
| | |--examples
|
||||
| | |--src
|
||||
| | |- Bar.c
|
||||
| | |- Bar.h
|
||||
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
|
||||
| |
|
||||
| |--Foo
|
||||
| | |- Foo.c
|
||||
| | |- Foo.h
|
||||
| |
|
||||
| |- README --> THIS FILE
|
||||
|
|
||||
|- platformio.ini
|
||||
|--src
|
||||
|- main.c
|
||||
|
||||
and a contents of `src/main.c`:
|
||||
```
|
||||
#include <Foo.h>
|
||||
#include <Bar.h>
|
||||
|
||||
int main (void)
|
||||
{
|
||||
...
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
PlatformIO Library Dependency Finder will find automatically dependent
|
||||
libraries scanning project source files.
|
||||
|
||||
More information about PlatformIO Library Dependency Finder
|
||||
- https://docs.platformio.org/page/librarymanager/ldf.html
|
32
lib/Vector/examples/PlatformIO/platformio.ini
Normal file
32
lib/Vector/examples/PlatformIO/platformio.ini
Normal file
@ -0,0 +1,32 @@
|
||||
; PlatformIO Project Configuration File
|
||||
;
|
||||
; Build options: build flags, source filter
|
||||
; Upload options: custom upload port, speed and extra flags
|
||||
; Library options: dependencies, extra library storages
|
||||
; Advanced options: extra scripting
|
||||
;
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
; common options for all environments
|
||||
[env]
|
||||
monitor_speed = 115200
|
||||
test_speed = 115200
|
||||
lib_deps =
|
||||
# https://github.com/janelia-arduino/Vector.git#branchname
|
||||
Vector
|
||||
|
||||
[env:uno]
|
||||
platform = atmelavr
|
||||
framework = arduino
|
||||
board = uno
|
||||
|
||||
; Documentation for native platform:
|
||||
; https://docs.platformio.org/en/latest/platforms/native.html
|
||||
[env:native]
|
||||
platform = native
|
||||
#build_flags = -DNATIVE
|
||||
#lib_deps =
|
||||
# ${env.lib_deps}
|
||||
# Dep1
|
||||
# Dep2
|
45
lib/Vector/examples/PlatformIO/src/main.cpp
Normal file
45
lib/Vector/examples/PlatformIO/src/main.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#ifdef ARDUINO
|
||||
# include <Arduino.h>
|
||||
#else
|
||||
# include <cstdio>
|
||||
#endif
|
||||
|
||||
#include <Vector.h>
|
||||
|
||||
char storage[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\r', '\n' };
|
||||
Vector<char> vector(storage, sizeof(storage));
|
||||
|
||||
#ifdef ARDUINO
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Starting up.");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.print("Vector size is ");
|
||||
Serial.println(vector.size());
|
||||
|
||||
Serial.print("Vector content: ");
|
||||
for (unsigned int i = 0; i < vector.size(); i++) {
|
||||
Serial.print(vector.at(i));
|
||||
}
|
||||
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
#else /* !defined(ARDUINO) */
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
puts("Starting up.");
|
||||
printf("Vector size is %d\n", vector.size());
|
||||
|
||||
printf("Vector content: ");
|
||||
for (unsigned int i = 0; i < vector.size(); i++) {
|
||||
putchar(vector.at(i));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
10
lib/Vector/examples/PlatformIO/test/README
Normal file
10
lib/Vector/examples/PlatformIO/test/README
Normal file
@ -0,0 +1,10 @@
|
||||
This directory is intended for PlatformIO Unit Testing and project tests.
|
||||
|
||||
Unit Testing is a software testing method by which individual units of
|
||||
source code, sets of one or more MCU program modules together with associated
|
||||
control data, usage procedures, and operating procedures, are tested to
|
||||
determine whether they are fit for use. Unit testing finds problems early
|
||||
in the development cycle.
|
||||
|
||||
More information about PlatformIO Unit Testing:
|
||||
- https://docs.platformio.org/page/plus/unit-testing.html
|
60
lib/Vector/examples/PlatformIO/test/test_vector.cpp
Normal file
60
lib/Vector/examples/PlatformIO/test/test_vector.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
// ----------------------------------------------------------------------------
|
||||
// test_vector.cpp
|
||||
//
|
||||
//
|
||||
// Authors:
|
||||
// Darsey Litzenberger dlitz@dlitz.net
|
||||
// ----------------------------------------------------------------------------
|
||||
#include <unity.h>
|
||||
#include <Vector.h>
|
||||
|
||||
static void test_vector_size() {
|
||||
int storage[8] = { 21, 42, 84, 0, 0, 0, 0, 0 };
|
||||
Vector<int> v(storage, 3);
|
||||
TEST_ASSERT_EQUAL(8, v.max_size());
|
||||
TEST_ASSERT_EQUAL(3, v.size());
|
||||
v.push_back(10);
|
||||
v.push_back(20);
|
||||
v.push_back(30);
|
||||
TEST_ASSERT_EQUAL(6, v.size());
|
||||
v.pop_back();
|
||||
TEST_ASSERT_EQUAL(5, v.size());
|
||||
}
|
||||
|
||||
static void test_vector_iterator() {
|
||||
int storage[8] = { 21, 42, 84, 0, 0, 0, 0, 0 };
|
||||
Vector<int> v(storage, 3);
|
||||
|
||||
const int expected[3] = { 21, 42, 84 };
|
||||
int i=0;
|
||||
for (auto it = v.begin(); it != v.end(); ++it, ++i) {
|
||||
TEST_ASSERT_EQUAL(expected[i], *it);
|
||||
}
|
||||
TEST_ASSERT_EQUAL(3, i);
|
||||
}
|
||||
|
||||
static void runTests() {
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(test_vector_size);
|
||||
RUN_TEST(test_vector_iterator);
|
||||
UNITY_END();
|
||||
}
|
||||
|
||||
#ifdef ARDUINO
|
||||
|
||||
void setup() {
|
||||
runTests();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
}
|
||||
|
||||
#else /* !defined(ARDUINO) */
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
runTests();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* !defined(ARDUINO) */
|
100
lib/Vector/examples/VectorTester/VectorTester.ino
Normal file
100
lib/Vector/examples/VectorTester/VectorTester.ino
Normal file
@ -0,0 +1,100 @@
|
||||
#include <Arduino.h>
|
||||
#include <Streaming.h>
|
||||
#include <Vector.h>
|
||||
|
||||
|
||||
const long BAUD = 115200;
|
||||
|
||||
const int ELEMENT_COUNT_MAX = 5;
|
||||
typedef Vector<int> Elements;
|
||||
const size_t DELAY = 500;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(BAUD);
|
||||
while (!Serial)
|
||||
{
|
||||
// wait for serial port to connect.
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
int storage_array[ELEMENT_COUNT_MAX];
|
||||
Elements vector;
|
||||
vector.setStorage(storage_array);
|
||||
Serial << "vector.max_size(): " << vector.max_size() << endl;
|
||||
Serial << "vector.size(): " << vector.size() << endl;
|
||||
Serial << "vector:" << endl;
|
||||
Serial << vector << endl;
|
||||
delay(DELAY);
|
||||
|
||||
vector.push_back(10);
|
||||
vector.push_back(8);
|
||||
vector.push_back(7);
|
||||
Serial << "vector.max_size(): " << vector.max_size() << endl;
|
||||
Serial << "vector.size(): " << vector.size() << endl;
|
||||
Serial << "vector:" << endl;
|
||||
Serial << vector << endl;
|
||||
vector.remove(0);
|
||||
Serial << "vector.remove(0):" << endl;
|
||||
Serial << vector << endl;
|
||||
vector.remove(1);
|
||||
Serial << "vector.remove(1):" << endl;
|
||||
Serial << vector << endl;
|
||||
delay(DELAY);
|
||||
|
||||
int storage_array2[ELEMENT_COUNT_MAX];
|
||||
Elements vector2(storage_array2);
|
||||
vector2.push_back(1);
|
||||
vector2.push_back(2);
|
||||
vector2.push_back(4);
|
||||
vector2.pop_back();
|
||||
Serial << "vector2.max_size(): " << vector2.max_size() << endl;
|
||||
Serial << "vector2.size(): " << vector2.size() << endl;
|
||||
Serial << "vector2:" << endl;
|
||||
Serial << vector2 << endl;
|
||||
delay(DELAY);
|
||||
Serial << "Print vector2 elements using iterators: ";
|
||||
for (int element : vector2)
|
||||
{
|
||||
Serial << element << " ";
|
||||
}
|
||||
Serial << endl;
|
||||
delay(DELAY);
|
||||
|
||||
int storage_array3[ELEMENT_COUNT_MAX];
|
||||
storage_array3[0] = 3;
|
||||
storage_array3[1] = 5;
|
||||
Elements vector3(storage_array3);
|
||||
Serial << "vector3.max_size(): " << vector3.max_size() << endl;
|
||||
Serial << "vector3.size(): " << vector3.size() << endl;
|
||||
Serial << "vector3:" << endl;
|
||||
Serial << vector3 << endl;
|
||||
delay(DELAY);
|
||||
|
||||
int storage_array4[ELEMENT_COUNT_MAX];
|
||||
storage_array4[0] = 3;
|
||||
storage_array4[1] = 5;
|
||||
Elements vector4(storage_array4,2);
|
||||
Serial << "vector4.max_size(): " << vector4.max_size() << endl;
|
||||
Serial << "vector4.size(): " << vector4.size() << endl;
|
||||
Serial << "vector4:" << endl;
|
||||
Serial << vector4 << endl;
|
||||
delay(DELAY);
|
||||
|
||||
int storage_array5[1];
|
||||
Elements vector5(storage_array5);
|
||||
Serial << "vector5.max_size(): " << vector5.max_size() << endl;
|
||||
Serial << "vector5.size(): " << vector5.size() << endl;
|
||||
Serial << "vector5:" << endl;
|
||||
Serial << vector5 << endl;
|
||||
delay(DELAY);
|
||||
|
||||
int storage_array6[ELEMENT_COUNT_MAX];
|
||||
Elements vector6(storage_array6);
|
||||
vector6.assign(ELEMENT_COUNT_MAX-1,8);
|
||||
Serial << "vector6:" << endl;
|
||||
Serial << vector6 << endl;
|
||||
delay(DELAY);
|
||||
}
|
21
lib/Vector/library.json
Normal file
21
lib/Vector/library.json
Normal file
@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "Vector",
|
||||
"version": "1.2.2",
|
||||
"description": "An array container similar to the C++ std::vector",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/janelia-arduino/Vector.git"
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "Peter Polidoro",
|
||||
"email": "peter@polidoro.io",
|
||||
"url": "https://github.com/peterpolidoro",
|
||||
"maintainer": true
|
||||
}
|
||||
],
|
||||
"license": "BSD-3-Clause",
|
||||
"homepage": "https://github.com/janelia-arduino/Vector",
|
||||
"frameworks": "*",
|
||||
"platforms": "*"
|
||||
}
|
9
lib/Vector/library.properties
Normal file
9
lib/Vector/library.properties
Normal file
@ -0,0 +1,9 @@
|
||||
name=Vector
|
||||
version=1.2.2
|
||||
author=Peter Polidoro <peter@polidoro.io>
|
||||
maintainer=Peter Polidoro <peter@polidoro.io>
|
||||
sentence=An array container similar to the C++ std::vector
|
||||
paragraph=Like this project? Please star it on GitHub!
|
||||
category=Data Storage
|
||||
url=https://github.com/janelia-arduino/Vector.git
|
||||
architectures=*
|
99
lib/Vector/src/Vector.h
Normal file
99
lib/Vector/src/Vector.h
Normal file
@ -0,0 +1,99 @@
|
||||
// ----------------------------------------------------------------------------
|
||||
// Vector.h
|
||||
//
|
||||
//
|
||||
// Authors:
|
||||
// Peter Polidoro peter@polidoro.io
|
||||
// ----------------------------------------------------------------------------
|
||||
#ifndef VECTOR_H
|
||||
#define VECTOR_H
|
||||
#ifdef ARDUINO
|
||||
#include <Arduino.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
#endif
|
||||
#include "Vector/VectorIterator.h"
|
||||
|
||||
|
||||
template <typename T>
|
||||
class Vector
|
||||
{
|
||||
public:
|
||||
Vector();
|
||||
template <size_t MAX_SIZE>
|
||||
Vector(T (&values)[MAX_SIZE],
|
||||
size_t size=0);
|
||||
template <size_t MAX_SIZE>
|
||||
void setStorage(T (&values)[MAX_SIZE],
|
||||
size_t size=0);
|
||||
void setStorage(T * values,
|
||||
size_t max_size,
|
||||
size_t size);
|
||||
const T & operator[](size_t index) const;
|
||||
T & operator[](size_t index);
|
||||
const T & at(size_t index) const;
|
||||
T & at(size_t index);
|
||||
T & front();
|
||||
T & back();
|
||||
void clear();
|
||||
template <typename U>
|
||||
void fill(const U & value);
|
||||
template <typename U,
|
||||
size_t N>
|
||||
void fill(const U (&values)[N]);
|
||||
template <typename U>
|
||||
void fill(const Vector<U> & values);
|
||||
template <typename U>
|
||||
void assign(size_t n,
|
||||
const U & value);
|
||||
template <typename U,
|
||||
size_t N>
|
||||
void assign(size_t n,
|
||||
const U (&values)[N]);
|
||||
template <typename U>
|
||||
void assign(size_t n,
|
||||
const Vector<U> & values);
|
||||
void push_back(const T & value);
|
||||
void pop_back();
|
||||
void remove(size_t index);
|
||||
size_t size() const;
|
||||
size_t max_size() const;
|
||||
bool empty() const;
|
||||
bool full() const;
|
||||
const T * data() const;
|
||||
T * data();
|
||||
typedef VectorIterator<T> iterator;
|
||||
iterator begin();
|
||||
iterator end();
|
||||
typedef VectorIterator<const T> const_iterator;
|
||||
const_iterator begin() const;
|
||||
const_iterator end() const;
|
||||
|
||||
private:
|
||||
T * values_;
|
||||
size_t max_size_;
|
||||
size_t size_;
|
||||
};
|
||||
|
||||
#ifdef ARDUINO /* `Print` is declared in the Arduino library and gives a compilation error elsewhere. */
|
||||
template <typename T>
|
||||
inline Print & operator <<(Print & stream,
|
||||
const Vector<T> & vector)
|
||||
{
|
||||
stream.print("[");
|
||||
for (size_t i=0; i<vector.size(); ++i)
|
||||
{
|
||||
if (i != 0)
|
||||
{
|
||||
stream.print(",");
|
||||
}
|
||||
stream.print(vector[i]);
|
||||
}
|
||||
stream.print("]");
|
||||
return stream;
|
||||
}
|
||||
#endif
|
||||
|
||||
#include "Vector/VectorDefinitions.h"
|
||||
|
||||
#endif
|
8
lib/Vector/src/Vector/Vector.cpp
Normal file
8
lib/Vector/src/Vector/Vector.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
// ----------------------------------------------------------------------------
|
||||
// Vector.cpp
|
||||
//
|
||||
//
|
||||
// Authors:
|
||||
// Peter Polidoro peter@polidoro.io
|
||||
// ----------------------------------------------------------------------------
|
||||
#include "../Vector.h"
|
248
lib/Vector/src/Vector/VectorDefinitions.h
Normal file
248
lib/Vector/src/Vector/VectorDefinitions.h
Normal file
@ -0,0 +1,248 @@
|
||||
// ----------------------------------------------------------------------------
|
||||
// VectorDefinitions.h
|
||||
//
|
||||
//
|
||||
// Authors:
|
||||
// Peter Polidoro peter@polidoro.io
|
||||
// ----------------------------------------------------------------------------
|
||||
#ifndef VECTOR_DEFINITIONS_H
|
||||
#define VECTOR_DEFINITIONS_H
|
||||
|
||||
#ifndef ARDUINO
|
||||
#include <cstring>
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
Vector<T>::Vector()
|
||||
{
|
||||
values_ = NULL;
|
||||
max_size_ = 0;
|
||||
size_ = 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <size_t MAX_SIZE>
|
||||
Vector<T>::Vector(T (&values)[MAX_SIZE],
|
||||
size_t size)
|
||||
{
|
||||
setStorage(values,size);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <size_t MAX_SIZE>
|
||||
void Vector<T>::setStorage(T (&values)[MAX_SIZE],
|
||||
size_t size)
|
||||
{
|
||||
values_ = values;
|
||||
max_size_ = MAX_SIZE;
|
||||
size_ = size;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Vector<T>::setStorage(T * values,
|
||||
size_t max_size,
|
||||
size_t size)
|
||||
{
|
||||
values_ = values;
|
||||
max_size_ = max_size;
|
||||
size_ = size;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T & Vector<T>::operator[](size_t index) const
|
||||
{
|
||||
return values_[index];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T & Vector<T>::operator[](size_t index)
|
||||
{
|
||||
return values_[index];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T & Vector<T>::at(size_t index)
|
||||
{
|
||||
return values_[index];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T & Vector<T>::at(size_t index) const
|
||||
{
|
||||
return values_[index];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T & Vector<T>::front()
|
||||
{
|
||||
return values_[0];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T & Vector<T>::back()
|
||||
{
|
||||
return values_[size_-1];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Vector<T>::clear()
|
||||
{
|
||||
size_ = 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename U>
|
||||
void Vector<T>::fill(const U & value)
|
||||
{
|
||||
assign(max_size_,value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename U,
|
||||
size_t N>
|
||||
void Vector<T>::fill(const U (&values)[N])
|
||||
{
|
||||
assign(N,values);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename U>
|
||||
void Vector<T>::fill(const Vector<U> & values)
|
||||
{
|
||||
assign(values.size(),values);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename U>
|
||||
void Vector<T>::assign(size_t n,
|
||||
const U & value)
|
||||
{
|
||||
size_t assign_size = ((n < max_size_) ? n : max_size_);
|
||||
size_ = assign_size;
|
||||
for (size_t i=0; i<assign_size; ++i)
|
||||
{
|
||||
values_[i] = value;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename U,
|
||||
size_t N>
|
||||
void Vector<T>::assign(size_t n,
|
||||
const U (&values)[N])
|
||||
{
|
||||
size_t n_smallest = ((n < N) ? n : N);
|
||||
size_t assign_size = ((n_smallest < max_size_) ? n_smallest : max_size_);
|
||||
size_ = assign_size;
|
||||
for (size_t i=0; i<assign_size; ++i)
|
||||
{
|
||||
values_[i] = values[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename U>
|
||||
void Vector<T>::assign(size_t n,
|
||||
const Vector<U> & values)
|
||||
{
|
||||
size_t n_smallest = ((n < values.size()) ? n : values.size());
|
||||
size_t assign_size = ((n_smallest < max_size_) ? n_smallest : max_size_);
|
||||
size_ = assign_size;
|
||||
for (size_t i=0; i<assign_size; ++i)
|
||||
{
|
||||
values_[i] = values[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Vector<T>::push_back(const T & value)
|
||||
{
|
||||
if ((values_ != NULL) && (size_ < max_size_))
|
||||
{
|
||||
values_[size_++] = value;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Vector<T>::pop_back()
|
||||
{
|
||||
if (size_ > 0)
|
||||
{
|
||||
--size_;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Vector<T>::remove(size_t index)
|
||||
{
|
||||
if (size_ > index)
|
||||
{
|
||||
for (size_t i=index; i<(size_-1); ++i)
|
||||
{
|
||||
values_[i] = values_[i+1];
|
||||
}
|
||||
--size_;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
size_t Vector<T>::size() const
|
||||
{
|
||||
return size_;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
size_t Vector<T>::max_size() const
|
||||
{
|
||||
return max_size_;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Vector<T>::empty() const
|
||||
{
|
||||
return size_ == 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Vector<T>::full() const
|
||||
{
|
||||
return size_ == max_size_;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T * Vector<T>::data()
|
||||
{
|
||||
return values_;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T * Vector<T>::data() const
|
||||
{
|
||||
return values_;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vector<T>::iterator Vector<T>::begin()
|
||||
{
|
||||
return iterator(values_);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vector<T>::iterator Vector<T>::end()
|
||||
{
|
||||
return iterator(values_,size_);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vector<T>::const_iterator Vector<T>::begin() const
|
||||
{
|
||||
return const_iterator(values_);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename Vector<T>::const_iterator Vector<T>::end() const
|
||||
{
|
||||
return const_iterator(values_,size_);
|
||||
}
|
||||
|
||||
#endif
|
45
lib/Vector/src/Vector/VectorIterator.h
Normal file
45
lib/Vector/src/Vector/VectorIterator.h
Normal file
@ -0,0 +1,45 @@
|
||||
// ----------------------------------------------------------------------------
|
||||
// VectorIterator.h
|
||||
//
|
||||
//
|
||||
// Authors:
|
||||
// Peter Polidoro peter@polidoro.io
|
||||
// ----------------------------------------------------------------------------
|
||||
#ifndef VECTOR_ITERATOR_H
|
||||
#define VECTOR_ITERATOR_H
|
||||
|
||||
template<typename T>
|
||||
class VectorIterator
|
||||
{
|
||||
public:
|
||||
VectorIterator(T * values_ptr) : values_ptr_{values_ptr}, position_{0} {}
|
||||
|
||||
VectorIterator(T * values_ptr, size_t size) : values_ptr_{values_ptr}, position_{size} {}
|
||||
|
||||
bool operator!=(const VectorIterator<T> & other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
bool operator==(const VectorIterator<T> & other) const
|
||||
{
|
||||
return position_ == other.position_;
|
||||
}
|
||||
|
||||
VectorIterator & operator++()
|
||||
{
|
||||
++position_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
T & operator*() const
|
||||
{
|
||||
return *(values_ptr_ + position_);
|
||||
}
|
||||
|
||||
private:
|
||||
T * values_ptr_;
|
||||
size_t position_;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user