-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamicvector.h
More file actions
127 lines (110 loc) · 3.33 KB
/
dynamicvector.h
File metadata and controls
127 lines (110 loc) · 3.33 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
//
// Created by razvan on 24.03.2018.
//
template <class TElement>
class DynamicVector {
private:
int capacity;
int size;
TElement *elems;
public:
// Class constructor, initializing the array with a capacity of 10
explicit DynamicVector(int capacity = 10);
// Copy constructor
DynamicVector(const DynamicVector& dv);
// Destructor
~DynamicVector();
// Assignment operator
DynamicVector& operator=(const DynamicVector& dv);
// Minus operator
DynamicVector<TElement>& operator-(const TElement& element);
// Adds an element to the array
void add(const TElement &element);
// Removes an element from the array
void remove(int pos);
// Inserts an element in the array, at a given position
void insert(const TElement &element, int pos);
// Returns the size of the array
int getSize() const;
// Returns element from a given position
TElement getElement(int pos);
// Returns all array elements
TElement *getAllElements() const;
protected:
// Expands array's capacity
void resize(int scale = 2);
};
template<class TElement>
DynamicVector<TElement>::DynamicVector(int capacity) {
this->capacity = capacity;
this->size = 0;
this->elems = new TElement[this->capacity];
}
template<class TElement>
DynamicVector<TElement>::DynamicVector(const DynamicVector &dv) {
this->capacity = dv.capacity;
this->size = dv.size;
this->elems = new TElement[dv.capacity];
for ( int i = 0; i < this->size; ++i )
this->elems[i] = dv.elems[i];
}
template<class TElement>
DynamicVector<TElement>::~DynamicVector() {
delete[] elems;
}
template<class TElement>
DynamicVector<TElement> &DynamicVector<TElement>::operator=(const DynamicVector &dv) {
this->capacity = dv.capacity;
this->size = dv.size;
if (this != &dv) {
delete[] this->elems;
this->elems = new TElement[this->capacity];
for ( int i = 0; i < this->size; ++i )
this->elems[i] = dv.elems[i];
}
return *this;
}
template<class TElement>
void DynamicVector<TElement>::add(const TElement &element) {
if (this->capacity == this->size)
this->resize();
this->elems[this->size++] = element;
}
template<class TElement>
void DynamicVector<TElement>::remove(int pos) {
for ( int i = pos; i < this->size - 1; ++i )
this->elems[i] = this->elems[i+1];
this->size--;
}
template<class TElement>
int DynamicVector<TElement>::getSize() const {
return this->size;
}
template<class TElement>
TElement *DynamicVector<TElement>::getAllElements() const {
return this->elems;
}
template<class TElement>
void DynamicVector<TElement>::resize(int scale) {
this->capacity *= scale;
TElement *aux = new TElement[this->capacity];
for ( int i = 0; i < this->size; ++i )
aux[i] = this->elems[i];
delete[] this->elems;
this->elems = aux;
}
template<class TElement>
void DynamicVector<TElement>::insert(const TElement &element, int pos) {
this->elems[pos] = element;
}
template<class TElement>
DynamicVector<TElement> &DynamicVector<TElement>::operator-(const TElement &element) {
for ( int i = 0; i < getSize(); ++i )
if ( this->elems[i] == element )
remove(i);
return *this;
}
template<class TElement>
TElement DynamicVector<TElement>::getElement(int pos) {
return this->elems[pos];
}