-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.h
More file actions
80 lines (66 loc) · 1.43 KB
/
Vector.h
File metadata and controls
80 lines (66 loc) · 1.43 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
#pragma once
namespace MonitoringComponents {
template <class Ty>
class Vector {
public:
typedef unsigned Size;
#define ScaleFactor 1.75;
Vector(Size initialCpacity = 5) {
firstElement = afterLastElement = new Ty[initialCpacity];
capacity = initialCpacity;
}
void push(Ty& item) {
if (size() == capacity) {
resize();
}
*afterLastElement = item;
afterLastElement++;
}
void push(Ty&& item) {
if (size() == capacity) {
resize();
}
*afterLastElement = item;
afterLastElement++;
}
Ty pop() {
//if(empty()) throw "Vector is empty, cant pop()";
auto retVal = *(afterLastElement - 1);
afterLastElement -= 1;
return retVal;
}
Ty& get(Size index) {
//if(index < size()) throw "Vector index is bad, probably not less than size()";
return *(firstElement + index);
}
Size size() {
return afterLastElement - firstElement;
}
bool empty() {
return size() == 0;
}
~Vector() {
dispose();
}
private:
Ty* firstElement;
Ty* afterLastElement;
Size capacity;
void resize() {
Size newCapacity = capacity * ScaleFactor;
Ty* newFirst, * newAfterLast;
newFirst = newAfterLast = new Ty[newCapacity];
for (Size i = 0; i < capacity; i++) {
*newAfterLast = get(i);
++newAfterLast;
}
dispose();
capacity = newCapacity;
firstElement = newFirst;
afterLastElement = newAfterLast;
}
void dispose() {
delete[] firstElement;
}
};
};