-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.h
65 lines (52 loc) · 1.18 KB
/
Vector.h
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
// Vector.h -- interface description for Vector ADT
#ifndef _Vector_h
#define _Vector_h 1
#include <stdint.h>
#include <assert.h>
template <typename T>
struct Vector {
private:
T* data;
uint32_t capacity;
uint32_t length;
public:
uint32_t size (void) const { return length; } // return the number of elements in the vector
Vector(void) {
data = new T[1];
length = 0;
capacity = 1;
}
Vector(uint32_t len) {
this->length = len;
this->capacity = len;
if (this->capacity == 0) {
this->capacity = 1;
}
this->data = new T[capacity];
}
Vector(const Vector& that) : Vector(that.size()) {
for (int i = 0; i < length; i++) {
this->data[i] = that.data[i];
}
}
~Vector(void) { delete[] data; }
T& operator[](uint32_t k) {
assert(k < this->length);
return this->data[k];
}
// append x to the end
void push_back(T x) {
if (this->capacity == this->length) {
this->capacity = this->capacity * 2;
T* new_data = new T[capacity];
for (uint32_t k = 0; k < this->length; k += 1) {
new_data[k] = this->data[k];
}
delete[] this->data;
this->data = new_data;
}
this->data[this->length] = x;
this->length += 1;
}
};
#endif /* _Vector_h */