-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.hxx
More file actions
159 lines (141 loc) · 2.33 KB
/
Copy pathmethods.hxx
File metadata and controls
159 lines (141 loc) · 2.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
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
//:: scope resolution operator, defines function out of the class
//return number of elements
template <class T>
int Vector<T>::getVectorSize()
{
return vecSize;
}
//returns capacity of vector
template <class T>
int Vector<T>::getVectorCapacity()
{
return capacity;
}
//print vector
template <class T>
Vector<T>::printVector()
{
for (int i = 0; i < vecSize; i++)
{
cout << arrayPointer[i] << " ";
}
cout << "\n";
}
//default constructor
template <class T>
Vector<T>::Vector()
{
vecSize = 0; //vectory is empty
arrayPointer = new T[capacity]; //create vector with 10 capacity
}
//initialize vector of size all with same val
template <class T>
Vector<T>::Vector(int size, int val)
{
arrayPointer = new T[capacity];
if(size > capacity)
{
resize();
}
vecSize = size;
for(int i = 0; i < size; i++)
{
arrayPointer[i] = val;
}
}
//resize double the capacity size
template <class T>
Vector<T>::resize()
{
capacity = capacity*2;
//cout << capacity << endl;
T * newArr = new T[capacity];
for(int i = 0; i < vecSize; i++)
{
newArr[i] = arrayPointer[i];
}
delete [] arrayPointer;
arrayPointer = newArr;
}
//insert one item
template <class T>
Vector<T>::insert(int index, T item)
{
vecSize += 1; //insert another element into vector
if(vecSize > capacity)
{
resize();
}
int* newArr = new int[vecSize];
bool insert = false;
for(int i = 0; i < vecSize; i++)
{
if(i == index)
{
newArr[i] = item;
insert = true;
}
else if(insert)
{
newArr[i] = arrayPointer[i-1];
}
else
{
newArr[i] = arrayPointer[i];
}
}
arrayPointer = newArr;
}
//push_back
template <class T>
Vector<T>::push_back(T item)
{
vecSize += 1; //insert element to end of vector
if(vecSize > capacity)
{
resize();
}
for(int i = 0; i < vecSize; i++)
{
if( i == vecSize-1 )
arrayPointer[i] = item;
}
}
//delete at index
template <class T>
Vector<T>::deleteAtIndex(int index)
{
bool skipped = false;
int i = 0;
while(i < vecSize)
{
if(i == index)
{
skipped = true;
i++;
}
else if(skipped)
{
arrayPointer[i-1] = arrayPointer[i];
i++;
}
else
{
arrayPointer[i] = arrayPointer[i];
i++;
}
}
vecSize -= 1; //taking away element
}
//pop_back
template <class T>
Vector<T>::pop_back()
{
for(int i = 0; i < vecSize; i++)
{
if( i == vecSize-1 )
{
deleteAtIndex(i);
}
}
}