-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOpVector.h
More file actions
234 lines (198 loc) · 5.97 KB
/
OpVector.h
File metadata and controls
234 lines (198 loc) · 5.97 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//
// OpVector.h
// GProject
//
// Created by DFTL on 4/20/18.
// Copyright © 2018 DFXTL. All rights reserved.
//
#ifndef OpVector_h
#define OpVector_h
//includes
//#include
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
//uml
//vector template class type Type
template<class Type>
class OpVector{
private:
int numElemSize; //number of elements in vecList
int maxSize; //max number of elements that vecList can Hold
Type *vecList; //vecList
void Reserve();
public:
//Default constructor
OpVector();
//Non Default constructor for maxSize
OpVector(int pmaxSize);
//Non default constructor
OpVector(int numElemSize, int maxSize, const Type & initial);
//Copy constructor
OpVector(const OpVector<Type> & copyOpVector);
//destructor
~OpVector();
typedef Type *traverse; //pointer Type
//Operators 0
Type & operator[] (int index); //get reference of indexed element (what element do we want to return)
OpVector<Type> & operator = (const OpVector<Type> & rhs);
//OpVector& operator += (const OpVector<Type> & rhs);
//Setters
void PushBack(const Type & value); //add new element
//update maxsize = elemesize * 2
//void Resize(int numElemSize); //update new elements size
//Getters
int NumElemSize() const; //get number of elements
int MaxSize() const; //get maxsize
bool IsEmpty() const; //check if v is empty
Type elemAt(int index)const; //return element at specific index
void clear(); //clear list
traverse Begin(); //get pointer to the first element
traverse End(); //get pointer to the last element
Type & Front(); //get a reference of first element
Type & Back(); //get a reference of last element
};//End class declarations
//******** OpVector Implementations ****************************************************************
//Default constructor
template<class Type>
OpVector<Type>::OpVector(){
numElemSize = 0;
maxSize = 5; //by defaul max capacity is 4
vecList = new Type[maxSize];
}
//Non Default Constructor for maxSize
template <class Type>
OpVector<Type>::OpVector(int pmaxSize){
numElemSize = 0;
maxSize = pmaxSize;
vecList = new Type[maxSize];
}
//Non default constructor
template<class Type>
OpVector<Type>::OpVector(int numElemSize, int maxSize, const Type & initial):
numElemSize(numElemSize),
maxSize(maxSize){
if(maxSize < 0){
cout << "negative size vector..." << endl;
}else{
vecList = new Type[maxSize];
for(int index = 0; index < numElemSize; index++){
vecList[index] = initial;
}
}
}
//Destructor
template<class Type>
OpVector<Type>::~OpVector(){
delete[] vecList;
}
//Copy constructor
template<class Type>
OpVector<Type>::OpVector(const OpVector<Type> & copyOpVector){
numElemSize = copyOpVector.numElemSize;
maxSize = copyOpVector.maxSize;
vecList = new Type[copyOpVector.maxSize];
for(int index = 0; index < copyOpVector.numElemSize; index++){
vecList[index] = copyOpVector.vecList[index];
}
}
//subscription
template <class Type>
Type& OpVector<Type>::operator[](int index){
return vecList[index];
}
//clear();
template<class Type>
void OpVector<Type>::clear(){
delete[] vecList;
numElemSize = 0;
maxSize = 5;
vecList = new Type[maxSize];
}
template <class Type>
OpVector<Type> & OpVector<Type>::operator=(const OpVector<Type> & rhs){
if(this!=&rhs){
numElemSize = rhs.numElemSize;
maxSize = rhs.maxSize;
delete[] vecList;
vecList = new Type[maxSize];
for(int index = 0; index < rhs.numElemSize; index++){
vecList[index] =rhs.vecList[index];
}
}
return *this;
}
//template <class Type>
//OpVector<Type> & OpVector<Type>::operator+=(const OpVector<Type> & rhs){
// this->PushBack(rhs);
// return *this;
//}
//PushBack
template<class Type>
void OpVector<Type>::PushBack(const Type & addElement){
if(numElemSize >= maxSize){
Reserve();
vecList[numElemSize++] = addElement; //Add element after reserve
}else{
vecList[numElemSize++] = addElement;
}
}
//Reserve memory if maxvec is full
template<class Type>//needs work
void OpVector<Type>::Reserve(){
maxSize = numElemSize * 2; //duplicate space
Type *tempList = new Type[maxSize]; //we create a new list
//now we need to copy all elements and delete old list
for(int index = 0; index < numElemSize; index++){
tempList[index] = vecList[index]; //copy all elements
}
delete[] vecList; //dealloc
vecList = tempList; //assign with updated size
}
//**********
//Getters
//get numElemSize
template<class Type>
int OpVector<Type>::NumElemSize()const{
return numElemSize;
}
//get maxSize
template<class Type>
int OpVector<Type>::MaxSize()const{
return maxSize;
}
//IsEmpty
template<class Type>
bool OpVector<Type>::IsEmpty()const{
return numElemSize == 0;
}
//Type elemAt(int index); //return element at specific index
template <class Type>
Type OpVector<Type>::elemAt(int index)const{
return vecList[index];
}
//Begin returns address of first element in case..
template<class Type>
typename OpVector<Type>::traverse OpVector<Type>::Begin(){
return vecList;
}
//End return address of last element
template<class Type>
typename OpVector<Type>::traverse OpVector<Type>::End(){
return vecList + numElemSize -1;
}
//Front
template<class Type>
Type& OpVector<Type>::Front(){
if(!(IsEmpty()))
return vecList[0];
throw *this; //add scpecific exception
}
//Back
template<class Type>
Type& OpVector<Type>::Back(){
return vecList[NumElemSize() - 1];
}
//
#endif /* OpVector_h */