-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.cpp
More file actions
155 lines (134 loc) · 3.64 KB
/
methods.cpp
File metadata and controls
155 lines (134 loc) · 3.64 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
#include "vector2.h"
Vector *createVector(){
Vector *v = new Vector;
v->capacity = 0;
v->size = 0;
v->data = nullptr;
return v;
}
void deleteVector(Vector *v){
if(v->data){
v->capacity = 0;
v->size = 0;
int *arr = (v->data);
v->data = 0;
delete[](arr);
}
// if (v){
// delete[](v);
// v = nullptr;
// }
}
Vector *copyVector(Vector *v){
Vector *new_v = createVector();
new_v->data = new int[v->capacity];
new_v->size = v->size;
new_v->capacity = v->capacity;
memcpy(new_v->data, v->data, sizeof(int) * (v->size));
return new_v;
}
void swapVector(Vector *lh, Vector *rh){
Vector *tmp = copyVector(lh);
reserve(lh, rh->capacity);
memcpy((lh -> data), (rh -> data), (sizeof(int) * (rh -> capacity)));
lh -> size = rh -> size;
lh -> capacity = rh -> capacity;
reserve(rh, tmp->capacity);
memcpy((rh -> data), (tmp -> data), (sizeof(int) * (tmp -> capacity)));
rh -> size = tmp -> size;
rh -> capacity = tmp -> capacity;
deleteVector(tmp);
}
int getValue(Vector *v, size_t index){
if((v -> data) && (getSize(v) > index))
return *((v -> data) + index);
}
size_t getSize(Vector *v){
return (v -> size);
}
size_t getCapacity(Vector *v){
return (v -> capacity);
}
void reserve(Vector *v, size_t newCap){
if (newCap > getCapacity(v)){
int *arr = new int[newCap];
memcpy(arr, v->data, (v->size) * sizeof(int));
delete [](v->data);
v->data = arr;
v->capacity = newCap;
}
}
void append(Vector *v, int value){
if(getSize(v) + 1 < getCapacity(v)){
(v -> data)[getSize(v)] = value;
}
else{
int tmp = (v -> data) ? 0 : 1;
reserve(v, getCapacity(v) * 2 + tmp);
(v -> data)[getSize(v)] = value;
}
(v -> size) += 1;
}
void append(Vector *v, int *array, size_t count){
if(count + getSize(v) > getCapacity(v)){
int tmp = (count + getSize(v)) ? 0 : 1;
reserve(v, count + getSize(v) + tmp);
}
int tmp_count = getSize(v);
for(int i = 0; i < count; i++){
(v -> data)[i + tmp_count] = array[i];
}
(v -> size) += count;
}
void insert(Vector *v, size_t index, int value){
if(getSize(v) + 1 > getCapacity(v)){
int tmp = (v -> data) ? 0 : 1;
reserve(v, getCapacity(v) * 2 + tmp);
}
for(int i = getSize(v) - 1; i >= index; i--){
(v -> data)[i + 1] = (v -> data)[i];
}
if(index < getSize(v)){
(v -> data)[index] = value;
v -> size += 1;
}
}
void erase(Vector *v, size_t index){
if(index < getSize(v)){
for(int i = index; i < getSize(v) - 1; i++){
(v -> data)[i] = (v -> data)[i + 1];
}
v -> size -= 1;
}
}
size_t indexOf(Vector *v, int value){
for(int i = 0; i < getSize(v); i++){
if((v -> data)[i] == value)
return i;
}
return -1;
}
void squeeze(Vector *v){
if(getSize(v) != 0){
int *arr = new int[getSize(v)];
memcpy(arr, (v -> data), sizeof(int) * getSize(v));
delete [](v -> data);
(v -> data) = arr;
v -> capacity = v -> size;
}
else
deleteVector(v);
}
void clear(Vector *v){
for(int i = 0; i < getCapacity(v); i++){
*((v -> data) + i) = 0;
}
v -> size = 0;
}
void print(Vector *v){
for(int i = 0; i < getSize(v); i++){
cout << (v -> data)[i];
i == getSize(v) - 1 ? cout << "" : cout << ", ";
}
cout << "\n";
}