-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVector.h
More file actions
162 lines (138 loc) · 4.05 KB
/
Vector.h
File metadata and controls
162 lines (138 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#ifndef VECTOR_H
#define VECTOR_H
#include <Arduino.h>
template<typename T>
class Vector {
private:
T* data;
size_t capacity;
size_t length;
public:
// Constructor
Vector()
: data(nullptr), capacity(0), length(0) {}
// Destructor
~Vector() {
clear();
}
// Get size
size_t size() const {
return length;
}
// Get capacity
size_t max_size() const {
return capacity;
}
// Clear the vector
void clear() {
if (data != nullptr) {
delete[] data;
data = nullptr;
capacity = 0;
length = 0;
}
}
// Push an element to the back of the vector
void push_back(const T& value) {
if (length >= capacity) {
if (capacity == 0) {
capacity = 1;
} else {
capacity *= 2;
}
T* newData = new T[capacity];
if (newData == nullptr) {
// Handle memory allocation failure
Serial.println("Memory allocation failed!");
return;
}
// Copy existing elements to the new array
for (size_t i = 0; i < length; ++i) {
newData[i] = data[i];
}
// Delete the old data
delete[] data;
// Update data pointer
data = newData;
}
data[length++] = value;
}
// Pop an element from the back of the vector
void pop_back() {
if (length > 0) {
--length;
}
}
// Pop elements from the front of the vector
void popFront(size_t count = 1) {
if (count >= length) {
clear(); // If count exceeds or equals the length, clear the vector
return;
}
// Shift elements to the left by count positions
for (size_t i = count; i < length; ++i) {
data[i - count] = data[i];
}
// Update length
length -= count;
}
// Access an element by index
T& operator[](size_t index) {
if (index >= length) {
// Handle out-of-bounds access
Serial.println("Index out of bounds!");
// Return a reference to the first element for simplicity
return data[0];
}
return data[index];
}
// Get iterator to the beginning of the vector
T* begin() const {
return data;
}
// Get iterator to the end of the vector
T* end() const {
return data + length;
}
// Copy constructor
Vector(const Vector& other)
: data(nullptr), capacity(0), length(0) {
*this = other; // Use assignment operator for deep copy
}
// Assignment operator
Vector& operator=(const Vector& other) {
if (this != &other) { // Avoid self-assignment
clear(); // Clear existing data
// Allocate memory for new data
capacity = other.capacity;
length = other.length;
data = new T[capacity];
// Copy elements from the other vector
for (size_t i = 0; i < length; ++i) {
data[i] = other.data[i];
}
}
return *this;
}
// Move constructor
Vector(Vector&& other) noexcept
: data(nullptr), capacity(0), length(0) {
*this = std::move(other); // Use move assignment operator
}
// Move assignment operator
Vector& operator=(Vector&& other) noexcept {
if (this != &other) { // Avoid self-assignment
clear(); // Clear existing data
// Move data from the other vector
data = other.data;
capacity = other.capacity;
length = other.length;
// Reset the other vector
other.data = nullptr;
other.capacity = 0;
other.length = 0;
}
return *this;
}
};
#endif