Skip to content

Commit c5a3d67

Browse files
committed
Refactored Vector.
1 parent fb719ba commit c5a3d67

File tree

2 files changed

+282
-87
lines changed

2 files changed

+282
-87
lines changed

src/vector.tcc

Lines changed: 150 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,127 +2,224 @@
22

33
#include <Arduino.h>
44

5+
6+
template <class T>
7+
void swap_(T& a, T& b) noexcept {
8+
T tmp {a};
9+
a = b;
10+
b = tmp;
11+
}
12+
13+
514
/*!
615
* Generic Vector.
716
*/
817
template <class T>
918
class Vector {
1019
public:
11-
Vector() {}
20+
Vector() = default;
21+
Vector(Vector const& v);
1222

1323
/*! Create a Vector with `size` elements.
1424
*
15-
* \param size Size of the Vector.
25+
* \param size Vector size.
1626
*/
1727
Vector(size_t const);
1828

1929
/*!
2030
* Create a Vector with `size` elements from a C array.
2131
*
22-
* \param size Size of the Vector.
23-
* \param data Pointer to data.
32+
* \param data C array.
2433
*/
25-
Vector(size_t const, T* const);
34+
template <size_t n>
35+
Vector(T const (&)[n]);
2636

27-
Vector(Vector const& v);
37+
/*!
38+
* Create a Vector with `size` elements from a block of raw memory.
39+
*
40+
* \param data Pointer to data, Vector takes ownership.
41+
* \param size Vector size.
42+
*/
43+
Vector(T* const, size_t const);
2844

2945
~Vector();
3046

3147
Vector& operator=(Vector);
48+
T& operator[](size_t const);
49+
T const& operator [](size_t const) const;
3250

51+
T* begin();
52+
T* end();
53+
T const* begin() const;
54+
T const* end() const;
3355

3456
/*!
35-
* Get a reference to an element.
57+
* Get the underlying data.
3658
*
37-
* This can be used for both retrieval as well as assignment.
59+
* \return data.
60+
*/
61+
T* data() const;
62+
63+
/*!
64+
* Get the number of elements.
3865
*
39-
* \param index Index.
66+
* \return Vector size.
67+
*/
68+
size_t size() const;
69+
70+
/*!
71+
* Set the number of elements.
4072
*
41-
* \return Reference to element at index `index`.
73+
* \param size Vector size.
4274
*/
43-
T& operator[](size_t) const;
75+
void resize(size_t const);
76+
77+
/*! Clear the contents. */
78+
void clear();
4479

45-
size_t size() const {
46-
return size_;
47-
}
4880
/*!
49-
* Resize the Vector.
81+
* Add an element to the back.
5082
*
51-
* \param size New size of the Vector.
83+
* \param el Element.
5284
*/
53-
void resize(size_t);
85+
void push_back(T const&);
86+
87+
/*!
88+
* Remove an element from the back.
89+
*
90+
* \return Element.
91+
*/
92+
T pop_back();
5493

5594
template <class U>
5695
friend void swap(Vector<U>&, Vector<U>&) noexcept;
5796

58-
//T* begin();
59-
//T* end();
60-
//T const* begin() const;
61-
//T const* end() const;
62-
6397
private:
98+
void copy_(T const* const);
99+
64100
size_t size_ {0};
65101
T* data_ {nullptr};
66102
};
67103

104+
68105
template <class T>
69-
void swap_(T& a, T& b) noexcept {
70-
T tmp {a};
71-
a = b;
72-
b = tmp;
106+
Vector<T>::Vector(Vector const& other)
107+
: size_ {other.size_}, data_ {new T[other.size_]} {
108+
copy_(other.data_);
73109
}
74110

75-
template <class U>
76-
void swap(Vector<U>& a, Vector<U>& b) noexcept {
77-
swap_(a.size_, b.size_);
78-
swap_(a.data_, b.data_);
111+
template <class T>
112+
Vector<T>::Vector(size_t const size)
113+
: size_ {size}, data_ {new T[size]} {}
114+
115+
template <class T>
116+
Vector<T>::Vector(T* const data, size_t const size)
117+
: size_ {size}, data_ {data} {}
118+
119+
template <class T>
120+
template <size_t n>
121+
Vector<T>::Vector(T const (&data)[n])
122+
: size_ {n}, data_ {new T[n]} {
123+
copy_(data);
124+
}
125+
126+
template <class T>
127+
Vector<T>::~Vector() {
128+
delete[] data_;
79129
}
80130

81131

82132
template <class T>
83-
Vector<T>::Vector(size_t const size) {
84-
resize(size);
133+
Vector<T>& Vector<T>::operator=(Vector<T> other) {
134+
swap(*this, other);
135+
return *this;
85136
}
86137

87138
template <class T>
88-
Vector<T>::Vector(size_t const size, T* const data) {
89-
this->size_ = size;
90-
data_ = data;
139+
T& Vector<T>::operator[](size_t const idx) {
140+
return data_[idx];
91141
}
92142

93143
template <class T>
94-
Vector<T>::Vector(Vector const& v) : size_ {v.size_} {
95-
data_ = new T[size_];
96-
for (size_t i {0}; i < size_; i++) {
97-
data_[i] = v.data_[i];
98-
}
144+
T const& Vector<T>::operator[](size_t const idx) const {
145+
return data_[idx];
99146
}
100147

148+
101149
template <class T>
102-
Vector<T>::~Vector() {
103-
delete[] data_;
150+
T* Vector<T>::begin() {
151+
return data_;
104152
}
105153

106154
template <class T>
107-
Vector<T>& Vector<T>::operator=(Vector v) {
108-
swap(*this, v);
109-
return *this;
155+
T* Vector<T>::end() {
156+
return data_ + size_;
157+
}
158+
159+
template <class T>
160+
T const* Vector<T>::begin() const {
161+
return data_;
162+
}
163+
164+
template <class T>
165+
T const* Vector<T>::end() const {
166+
return data_ + size_;
110167
}
111168

112169

113170
template <class T>
114-
T& Vector<T>::operator[](size_t const index) const {
115-
return data_[index];
171+
T* Vector<T>::data() const {
172+
return data_;
173+
}
174+
175+
template <class T>
176+
size_t Vector<T>::size() const {
177+
return size_;
116178
}
117179

118180
template <class T>
119181
void Vector<T>::resize(size_t const size) {
120-
T* newData {new T[size]};
121-
for (size_t i {0}; i < min(this->size_, size); i++) {
122-
newData[i] = data_[i];
123-
}
182+
size_ = min(size_, size);
183+
184+
T* data {new T[size]};
185+
swap_(data_, data);
186+
copy_(data);
187+
delete[] data;
188+
189+
size_ = size;
190+
}
191+
192+
template <class T>
193+
void Vector<T>::clear() {
194+
size_ = 0;
124195
delete[] data_;
125-
data_ = newData;
196+
data_ = nullptr;
197+
}
198+
199+
template <class T>
200+
void Vector<T>::push_back(T const& el) {
201+
resize(++size_);
202+
data_[size_ - 1] = el;
203+
}
204+
205+
template <class T>
206+
T Vector<T>::pop_back() {
207+
T el {data_[size_ - 1]};
208+
resize(--size_);
209+
return el;
210+
}
126211

127-
this->size_ = size;
212+
213+
template <class T>
214+
void Vector<T>::copy_(T const* const data) {
215+
for (size_t i {0}; i < size_; ++i) {
216+
data_[i] = data[i];
217+
}
218+
}
219+
220+
221+
template <class U>
222+
void swap(Vector<U>& a, Vector<U>& b) noexcept {
223+
swap_(a.size_, b.size_);
224+
swap_(a.data_, b.data_);
128225
}

0 commit comments

Comments
 (0)