-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicIntArray.cpp
More file actions
262 lines (205 loc) · 4.92 KB
/
DynamicIntArray.cpp
File metadata and controls
262 lines (205 loc) · 4.92 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
252
253
254
255
256
257
258
259
260
261
262
// ===========================================================================
// DynamicIntArray.cpp
// ===========================================================================
#include <stdexcept>
#include <string>
#include "DynamicIntArray.h"
// c'tor(s) / d'tor
DynamicIntArray::DynamicIntArray()
{
m_size = 0;
m_data = nullptr;
}
DynamicIntArray::DynamicIntArray(size_t size)
{
m_size = size;
m_data = new int[size];
// initialize buffer with default values
for (int i = 0; i < m_size; ++i) {
m_data[i] = 0;
}
}
DynamicIntArray::DynamicIntArray(int* values, size_t count)
{
m_size = count;
m_data = new int[count];
// initialize buffer with provided values
for (int i = 0; i < m_size; ++i) {
m_data[i] = values[i];
}
}
DynamicIntArray::~DynamicIntArray()
{
delete[] m_data;
}
// getter / setter
size_t DynamicIntArray::size() const
{
return m_size;
}
int& DynamicIntArray::at(size_t index)
{
// bounds checking
if (index >= m_size) {
std::string msg = "Wrong Index: " + std::to_string(index);
throw std::out_of_range(msg);
}
return m_data[index];
}
// public interface
void DynamicIntArray::fill(int value)
{
for (int i = 0; i < m_size; ++i) {
m_data[i] = value;
}
}
void DynamicIntArray::resize(size_t newSize)
{
if (newSize <= m_size)
{
// just shorten length, keep buffer
m_size = newSize;
}
else
{
// allocate new (temporary) buffer
int* data = new int[newSize];
// copy current buffer into new one
for (size_t i = 0; i < m_size; ++i) {
data[i] = m_data[i];
}
// initialize rest of buffer with default values
for (size_t i = m_size; i < newSize; ++i) {
data[i] = 0;
}
// release old buffer
delete[] m_data;
// switch to new buffer
m_size = newSize;
m_data = data;
}
}
void DynamicIntArray::shrinkToFit()
{
// allocate new - temporary and fitting - buffer
int* data = new int[m_size];
// copy current buffer into new one
for (size_t i = 0; i < m_size; ++i) {
data[i] = m_data[i];
}
// release old buffer
delete[] m_data;
// switch to new buffer
m_data = data;
}
int DynamicIntArray::minimum() const
{
if (m_size == 0) {
return 0;
}
int min = m_data[0];
for (int i = 1; i < m_size; i++) {
if (min > m_data[i]) {
min = m_data[i];
}
}
return min;
}
int DynamicIntArray::maximum() const
{
if (m_size == 0) {
return 0;
}
int max = m_data[0];
for (int i = 1; i < m_size; i++) {
if (max < m_data[i]) {
max = m_data[i];
}
}
return max;
}
int DynamicIntArray::indexOf(int value) const
{
// perform a linear search
for (int i = 0; i < m_size; i++) {
if (m_data[i] == value) {
return i;
}
}
return -1;
}
bool DynamicIntArray::contains(int value) const
{
for (int i = 0; i < m_size; i++) {
if (m_data[i] == value) {
return 1;
}
}
return 0;
}
void DynamicIntArray::release() {
if (m_data != nullptr) {
delete[] m_data;
m_data = nullptr;
m_size = 0;
}
}
void DynamicIntArray::print() const
{
for (size_t i = 0; i < m_size; ++i) {
std::cout << m_data[i] << ' ';
}
std::cout << "[Length: " << m_size << ']' << std::endl;
}
// operators
int& DynamicIntArray::operator[] (size_t index)
{
// no bounds checking - intentionally !!!
return m_data[index];
}
// copy semantics - copy constructor
DynamicIntArray::DynamicIntArray(const DynamicIntArray& data) {
// allocate buffer
m_size = data.m_size;
m_data = new int[m_size];
// copy object
for (size_t i = 0; i < data.m_size; ++i) {
m_data[i] = data.m_data[i];
}
}
// copy semantics - assignment operator
DynamicIntArray& DynamicIntArray::operator= (const DynamicIntArray& data) {
// prevent self-assignment
if (this == &data)
return *this;
// delete old buffer
delete[] m_data;
// allocate a new buffer
m_size = data.m_size;
m_data = new int[m_size];
// copy buffer
for (size_t i = 0; i < data.m_size; ++i) {
m_data[i] = data.m_data[i];
}
return *this;
}
// comparison operators
bool operator== (const DynamicIntArray& left, DynamicIntArray right)
{
if (left.m_size != right.m_size) {
return false;
}
for (int i = 0; i < left.m_size; ++i) {
if (left.m_data[i] != right.m_data[i]) {
return false;
}
}
return true;
}
bool operator!= (const DynamicIntArray& left, DynamicIntArray right)
{
return ! (left == right);
}
// ===========================================================================
// End-of-File
// ===========================================================================