-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.h
More file actions
116 lines (88 loc) · 2.37 KB
/
vector.h
File metadata and controls
116 lines (88 loc) · 2.37 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
//#include <glob.h>
#include <cstring>
template <class Type>
class Vector {
private:
struct Variable {
char *name = nullptr;
Type id = 0;
friend bool operator== (const Variable a, const Variable b) {
return strcmp(a.name, b.name) == 0;
}
friend bool operator== (const Variable a, const char str[]) {
return strcmp(a.name, str) == 0;
}
friend bool operator!= (const Variable a, const Variable b) {
return strcmp(a.name, b.name) != 0;
}
friend bool operator!= (const Variable a, const char str[]) {
return strcmp(a.name, str) != 0;
}
};
Variable *array = nullptr;
size_t size = 0;
size_t maxSize = 1;
public:
Vector() = default;
Variable operator[] (size_t pos) {
return array[pos];
}
void pushBack(Variable val) {
if (size == 0) {
array = new Variable[1];
++size;
array[0] = val;
return;
}
if (size == maxSize) {
maxSize *= 2;
auto newArr = new Variable[maxSize];
for (int i = 0; i < size; ++i) {
newArr[i] = array[i];
}
delete[] array;
array = newArr;
}
array[size] = val;
++size;
}
size_t find(const char name[]) {
for (int i = 0; i < size; ++i) {
if (array[i] == name)
return i;
}
}
size_t findFrom(const char start[], const char name[]) {
size_t current = find(start);
++current;
while (array[current].id != 0) {
if (array[current] == name)
return current;
++current;
}
}
size_t findFrom(size_t current, const char name[]) {
++current;
while (array[current].id != 0) {
if (array[current] == name)
return array[current].id;
++current;
}
}
size_t countFromToZero(size_t start) {
size_t current = start;
++current;
while (array[current].id != 0) {
++current;
}
return current - start - 1;
}
void dump() {
for (int i = 0; i < size; ++i) {
printf("%s %d\n", array[i].name, array[i].id);
}
}
~Vector() {
delete[] array;
}
};