-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_Base.cpp
More file actions
251 lines (215 loc) · 5.34 KB
/
Array_Base.cpp
File metadata and controls
251 lines (215 loc) · 5.34 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// -*- C++ -*-
// Honor Pledge:
//
// I pledge that I have neither given nor receieved any help
// on this assignment.
//
// Array
//
template <typename T>
Array_Base <T>::Array_Base (void)
: data_ (new T [5]),
cur_size_ (0),
max_size_ (5)
{
// nothing needs to be done
} // end default constructor
//
// Array (size_t)
//
template <typename T>
Array_Base <T>::Array_Base (size_t length)
: data_ (new T [length]),
cur_size_ (length),
max_size_ (length)
{
// nothing needs to be done
} // end initialization constructor
//
// Array (size_t, char)
//
template <typename T>
Array_Base <T>::Array_Base (size_t length, T element)
: Array_Base <T> (length)
{
fill (element);
} // end initialization constructor
//
// Array (const Array &)
//
template <typename T>
Array_Base <T>::Array_Base (const Array_Base <T> & array)
: Array_Base <T> (array.cur_size_)
{
for (size_t i = 0; i < array.cur_size_; i++) {
data_[i] = array.data_[i];
} // end for
} // end copy constructor
//
// ~Array
//
template <typename T>
Array_Base <T>::~Array_Base (void)
{
try {
delete [] data_;
} catch (...) {
throw;
} // end try-catch
} // end destructor
//
// operator []
//
template <typename T>
T & Array_Base <T>::operator [] (size_t index)
{
if (cur_size_ <= index) {
throw std::out_of_range("std::out_of_range: The index passed in is greater than the size of the array.");
} else {
return data_[index];
} // end if-else
} // end operator [] for mutability
//
// operator []
//
template <typename T>
const T & Array_Base <T>::operator [] (size_t index) const
{
// reuse of code -- mobility
return this[index];
} // end operator [] for access
//
// get
//
template <typename T>
T Array_Base <T>::get (size_t index) const
{
if (cur_size_ <= index) {
throw std::out_of_range("std::out_of_range: The index passed in is greater than the size of the array.");
} else {
return data_[index];
} // end if-else
} // end get
//
// set
//
template <typename T>
void Array_Base <T>::set (size_t index, T value)
{
if (cur_size_ <= index) {
throw std::out_of_range("std::out_of_range: The index passed in is greater than the size of the array.");
} else {
data_[index] = value;
} // end if-else
} // end set
//
// find (char)
//
template <typename T>
int Array_Base <T>::find (T value) const
{
try {
return find(value, 0);
} catch (const std::out_of_range & ex) {
// since this method doesn't take in an index, the exception means just return -1
return -1;
} // end try-catch
} // end find
//
// find (char, size_t)
//
template <typename T>
int Array_Base <T>::find (T val, size_t start) const
{
int returnVal = -1;
if (start >= cur_size_) {
throw std::out_of_range("std::out_of_range: The start index is larger than the size of the array");
} else {
for (size_t i = start; i < cur_size_; i++) {
if (data_[i] == val) {
returnVal = (int) i;
break;
} // end if
} // end for
} // end if-else
return returnVal;
} // end find with start
//
// operator ==
//
template <typename T>
bool Array_Base <T>::operator == (const Array_Base <T> & rhs) const
{
bool returnVal = true;
if (this == &rhs) {
returnVal = true;
} else {
if (cur_size_ == rhs.cur_size_) {
for (size_t i = 0; i < cur_size_; i++) {
if (data_[i] != rhs.data_[i]) {
returnVal = false;
break;
} // end if
} // end for
} else {
returnVal = false;
} // end if-else
} // end else
return returnVal;
} // end operator ==
//
// operator !=
//
template <typename T>
bool Array_Base <T>::operator != (const Array_Base <T> & rhs) const
{
return !((*this) == rhs);
} // end operator !=
//
// fill
//
template <typename T>
void Array_Base <T>::fill (T value)
{
for (size_t i = 0; i < cur_size_; i++) {
data_[i] = value;
} // end for
} // end fill
//
// reverse
//
template <typename T>
void Array_Base <T>::reverse ( void ) {
T temp;
for (size_t i = 0; i < (cur_size_/2); i++) {
temp = data_[i];
data_[i] = data_[cur_size_ - 1 - i];
data_[cur_size_ - 1 - i] = temp;
} // end for
} // end reverse
//
// slice
//
template <typename T>
Array_Base <T> Array_Base <T>::slice (size_t begin) const {
// same as the slice method with begin and end index, with end being equal to the size of the array
Array_Base <T> subArray = slice (begin, cur_size_-1);
return subArray;
} // end slice
//
// slice
//
template <typename T>
Array_Base <T> Array_Base <T>::slice (size_t begin, size_t end) const {
if (begin > cur_size_ - 1 || end > cur_size_ - 1) {
throw std::out_of_range("std::out_of_range: Either or both indexes passed in are greater than the size of the array.");
} else {
// assuming that "begin" and "end" is the zero based location of the starting element of the slice
size_t subArray_size = end - begin;
Array_Base <T> subArray(subArray_size); // on the stack
for (size_t i = begin; i < subArray_size; i++) {
subArray[i-begin] = data_[i];
} // end for
return subArray;
} // end if-else
} // end slice