-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.cpp
More file actions
131 lines (117 loc) · 3.02 KB
/
vector.cpp
File metadata and controls
131 lines (117 loc) · 3.02 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
#ifndef VECTOR_H
#define VECTOR_H
// The class declaration must have doxygen comments – put these in
template <class T>
class Vector
{
public:
Vector(){
array = new T[1];
containerSize = 1;
filledLength = 0;
}
/**
* \brief Get value at given index
*
* \param [in] index index of value to get
* \pre Index > 0 and index < length
* \post If index < 0 then index = 0. If index > length then index
* \return Value at given index = length
*/
T& get(int index){
if (index > filledLength) index = filledLength;
else if (index < 0) index = 0;
return array[index];
}
/**
* \brief Add value at specified index
*
* \param [in] t Value to add
* \param [in] index Index to add value into
* \return
* \pre Index > 0 and index < length
* \post If index < 0 then index = 0. If index > length then index = length
*/
void insert(T t, int index){
if (index < 0) index = 0;
if (index > filledLength) index = filledLength;
filledLength++;
T* tempArray;
if (filledLength > containerSize){
containerSize = containerSize*2;
tempArray = new T[containerSize];
}
else tempArray = new T[containerSize];
for (int i = 0; i < index; i++){
tempArray[i] = array[i];
}
tempArray[index] = t;
for (int i = index; i < filledLength-1; i++){
tempArray[i+1] = array[i];
}
delete[] array;
array = tempArray;
}
/**
* \brief Remove value at specified index
*
* \param [in] index Index to remove value from
* \return
* \pre Index > 0 and index < length
* \post If index < 0 then index = 0. If index > length then index = length
*/
void remove(int index){
if (index < 0) index = 0;
if (index > filledLength) index = filledLength;
T* tempArray = new T[containerSize];
for (int i = 0; i < index; i++){
tempArray[i] = array[i];
}
for (int i = index+1; i < filledLength; i++){
tempArray[i-1] = array[i];
}
delete[] array;
array = tempArray;
filledLength--;
}
/**
* \brief Add value to end of vector
*
* \param [in] t Value to add
* \return
* \pre
* \post
*/
void add(T t){
if (filledLength == containerSize) insert(t, filledLength);
else{
array[filledLength] = t;
filledLength++;
}
}
/**
* \brief Get length of vector
*
* \return Vector actual length (not container size)
* \pre
* \post
*/
int getLength(){
return filledLength;
}
/**
* \brief Get container size
*
* \return Size of container (not actual vector length)
* \pre
* \post
*/
int getContainerSize(){
return containerSize;
}
private:
T* array;
int containerSize;
int filledLength;
};
#endif