Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
static_vector.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 15.06.2013 23:53:44 EDT
3 // File: static_vector.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __STATIC_VECTOR_HEADER
12 #define __STATIC_VECTOR_HEADER
13 
14 #include "ard_assert.hpp"
15 #include "array.hpp"
16 
17 namespace ustd {
18 
19  template<typename T, uint16_t capacity_>
20  class static_vector {
21  public:
22  typedef T value_type;
23  typedef uint16_t size_type;
24  //------------------- ctors -------------------
25  static_vector(): size_(0) {
26  }
27  explicit static_vector(size_type const & size, T const & val = T()): size_(size) {
28  for(size_type i = 0; i < size_; ++i) {
29  array_[i] = val;
30  }
31  }
32  static_vector(static_vector const & arg): size_(arg.size_), array_(arg.array_) {
33  }
35  size_ = rhs.size_;
36  array_ = rhs.array_;
37  return (*this);
38  }
39  //------------------- ops -------------------
40  void push_back(T const & in) {
41  ASSERT_MSG(size_ < capacity_, "static_vector too small")
42  array_[size_] = in;
43  ++size_;
44  }
45  void pop_back() {
46  --size_;
47  array_[size_] = 0;
48  }
49  size_type find(T const & val, size_type pos = 0) const {
50  for(; pos < size_; ++pos) {
51  if(array_[pos] == val)
52  return pos;
53  }
54  return size_;
55  }
56  void clear() {
57  size_ = 0;
58  }
59  void erase(size_type const & pos) {
60  if(pos < size_) {
61  for(size_type i = pos; i < size_; ++i) {
62  array_[i] = array_[i + 1];
63  }
64  pop_back();
65  }
66  }
67  //------------------- getter -------------------
68  T & operator[](size_type const & pos) {
69  return array_[pos];
70  }
71  T const & operator[](size_type const & pos) const {
72  return array_[pos];
73  }
74  T & back() {
75  ASSERT(size_ > 0)
76  return array_[size_ - 1];
77  }
78  T const & back() const {
79  ASSERT(size_ > 0)
80  return array_[size_ - 1];
81  }
82  T & front() {
83  ASSERT(size_ > 0)
84  return array_[0];
85  }
86  T const & front() const {
87  ASSERT(size_ > 0)
88  return array_[0];
89  }
90  T * data() {
91  return array_.data();
92  }
93  //------------------- special fct to support serializer -------------------
94  private:
95  template<typename U>
96  uint8_t read_impl(size_type const & adr, U const & val) {
97  ASSERT_MSG(0, "do not use unless T = byte")
98  return 0xFF;
99  }
100  uint8_t read_impl(size_type const & adr, uint8_t const & val) {
101  return array_[adr];
102  }
103  template<typename U>
104  void write_impl(size_type const & adr, U const & val) {
105  ASSERT_MSG(0, "do not use unless T = byte")
106  }
107  void write_impl(size_type const & adr, uint8_t const & val) {
108  array_[adr] = val;
109  }
110  public:
111  uint8_t read(size_type const & adr) {
112  return read_impl(adr, T());
113  }
114  void write(size_type const & adr, T const & val) {
115  write_impl(adr, val);
116  }
117  //------------------- size/capacity -------------------
118  size_type const & size() const {
119  return size_;
120  }
121  void set_size(size_type const & new_size) {
122  ASSERT(new_size <= capacity_);
123  size_ = new_size;
124  }
125  constexpr size_type capacity() {
126  return capacity_;
127  }
128  void resize(size_type const & new_size, T const & val = T()) {
129  if(new_size > size_) {
130  while(new_size > size_) {
131  push_back(val);
132  }
133  }
134  else {
135  size_ = new_size;
136  }
137  }
138  //------------------- info -------------------
139  bool empty() const {
140  return (size_ == size_type());
141  }
142  //------------------- iterator -------------------
143  //~ struct iterator {
144  //~ iterator(static_vector & svec, size_type const & pos): svec_(svec), pos_(pos) {
145  //~ }
146  //~ iterator & operator++() {
147  //~ ++pos_;
148  //~ return (*this);
149  //~ }
150  //~ bool operator!=(iterator const & rhs) const {
151  //~ return pos_ != rhs.pos_;
152  //~ }
153  //~ value_type & operator*() {
154  //~ return svec_[pos_];
155  //~ }
156  //~ static_vector & svec_;
157  //~ size_type pos_;
158  //~ };
159  //~ iterator begin() {
160  //~ return iterator((*this), 0);
161  //~ }
162  //~ iterator end() {
163  //~ return iterator((*this), size_);
164  //~ }
165  //------------------- print & serialize-------------------
166  template<typename S>
167  void print(S & os) const {
168  os << F("[");
169  for(size_type i = 0; i < size_; ++i) {
170  os << array_[i];
171  if(i != size_ - 1)
172  os << F(", ");
173  }
174  os << F("]");
175  }
176  template<typename Archive>
177  void serialize(Archive & ar) {
178  ar & size_;
179  ar & array_;
180  }
181  private:
182  size_type size_;
183  array<T, capacity_> array_;
184  };
185 }//end namespace ustd
186 
187 #endif //__STATIC_VECTOR_HEADER
T * data()
just return the internal cpp-array
Definition: array.hpp:79
static_vector(size_type const &size, T const &val=T())
Definition: static_vector.hpp:27
void write(size_type const &adr, T const &val)
Definition: static_vector.hpp:114
void pop_back()
Definition: static_vector.hpp:45
void push_back(T const &in)
Definition: static_vector.hpp:40
T const & back() const
Definition: static_vector.hpp:78
void print(S &os) const
Definition: static_vector.hpp:167
T & back()
Definition: static_vector.hpp:74
uint16_t size_type
Definition: static_vector.hpp:23
size_type find(T const &val, size_type pos=0) const
Definition: static_vector.hpp:49
#define ASSERT_MSG(exp, msg)
Definition: ard_assert.hpp:34
uint8_t read(size_type const &adr)
Definition: static_vector.hpp:111
static_vector(static_vector const &arg)
Definition: static_vector.hpp:32
constexpr size_type capacity()
Definition: static_vector.hpp:125
void set_size(size_type const &new_size)
Definition: static_vector.hpp:121
void resize(size_type const &new_size, T const &val=T())
Definition: static_vector.hpp:128
T const & front() const
Definition: static_vector.hpp:86
void erase(size_type const &pos)
Definition: static_vector.hpp:59
T value_type
Definition: static_vector.hpp:22
T const & operator[](size_type const &pos) const
Definition: static_vector.hpp:71
bool empty() const
Definition: static_vector.hpp:139
T & front()
Definition: static_vector.hpp:82
void serialize(Archive &ar)
Definition: static_vector.hpp:177
T * data()
Definition: static_vector.hpp:90
static_vector & operator=(static_vector const &rhs)
Definition: static_vector.hpp:34
#define F(x)
Definition: ustd_generic.hpp:15
T & operator[](size_type const &pos)
Definition: static_vector.hpp:68
size_type const & size() const
Definition: static_vector.hpp:118
Definition: static_vector.hpp:20
static_vector()
Definition: static_vector.hpp:25
#define ASSERT(exp)
Definition: ard_assert.hpp:33
void clear()
Definition: static_vector.hpp:56