-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyVector.cpp
More file actions
178 lines (159 loc) · 4.01 KB
/
MyVector.cpp
File metadata and controls
178 lines (159 loc) · 4.01 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "MyVector.hpp"
#include <iostream>
// Constructor
template<typename T>
MyVector<T>::MyVector(int capacity) : size(0) {
this->capacity = capacity;
elements = new T[capacity];
}
// Destructor
template<typename T>
MyVector<T>::~MyVector() {
delete [] elements;
}
// Copy constructor
template<typename T>
MyVector<T>::MyVector(const MyVector<T>& other) {
size = other.size;
capacity = other.capacity;
elements = new T[capacity];
//copy elements
for(int i = 0; i < size; i++) {
elements[i] = other.elements[i];
}
}
// Adds element to last position
template<typename T>
void MyVector<T>::push_back(const T& value) {
if (size >= capacity) {
allocate_memory(capacity * 2); // doubles capacity when size is equal or bigger
}
elements[size] = value;
size++;
}
// Removes last element
template<typename T>
T MyVector<T>::pop_back(void) {
if (size > 0) {
if (size - 1 < capacity / 2) {
allocate_memory(capacity / 2);
}
return elements[--size]; // Pre-decrements the value of size
}
else {
// Throw an exception
throw "The vector is empty!";
}
}
// Prints Vector
template<typename T>
void MyVector<T>::print() const {
std::cout << "[ ";
for (int i = 0; i < size; i++) {
std::cout << elements[i] << ' ';
}
std::cout << "]\n";
}
// Allocates new memory when capacity is low
template<typename T>
void MyVector<T>::allocate_memory(int memory_size) {
capacity = memory_size;
T *old = elements;
// Allocate a new memory (bigger or smaller)
elements = new T[memory_size];
for (int i = 0; i < size; i++) {
elements[i] = old[i];
}
// Deallocate the old memory
delete [] old;
}
// Points at current element
template<typename T>
T& MyVector<T>::at(int index) {
// Check if index is positive or negative
if (index < 0){ // Negative
return elements[index + size];
}
else{ // Positive
return elements[index];
}
}
// Clears all elements
template<typename T>
void MyVector<T>::clear() {
// Remove all elements
delete[] elements;
size = 0;
capacity = 1;
elements = new T[capacity];
}
// Inserts new element to the first position
template<typename T>
void MyVector<T>::push_front(const T& value) {
// Resize if capacity is exceeded
if (size >= capacity){
allocate_memory(capacity * 2);
}
// Move all values one to the right
for (int i = size; i > 0; i--){
elements[i] = elements[i-1];
}
// Change new size
size++;
// Add new value at index 0
elements[0] = value;
}
// Adds element at a certain index
template<typename T>
void MyVector<T>::insert(int pos, const T& value) {
// Resize if capacity is exceeded
if (size >= capacity){
allocate_memory(capacity * 2);
}
// Check if pos is positive or negative
if (pos < 0){ // Update negative index (starting from left)
pos = (size + pos) + 1;
}
// Move values to the right starting from pos
for (int i = size; i >= pos; i--){
elements[i] = elements[i-1];
}
// Update size
size++;
// Insert value at pos
elements[pos] = value;
}
// Erases element at a certain index
template<typename T>
void MyVector<T>::erase(int index){
// Support for negative index
if(index < 0) {index = size + index;}
// Index is out of range
if(index < 0 || index >= capacity){
throw "Index is out of range!";
}
// Shift elements left
for(int i = index; i < size - 1; i++){
elements[i] = elements[i + 1];
}
--size;
// Memory management
if(size <= capacity / 2){
allocate_memory(capacity / 2);
}
}
// Removes duplicates
template<typename T>
void MyVector<T>::remove_dups(){
// Remove duplicate elements
for(int i = 0; i < size; i++){
for(int j = i + 1; j < size; ){
if(elements[i] == elements[j]){
erase(j);
}
else{
j++;
}
}
}
}