-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedArray.cpp
More file actions
235 lines (189 loc) · 6.38 KB
/
SortedArray.cpp
File metadata and controls
235 lines (189 loc) · 6.38 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
#include "SortedArray.h"
// Integer and default constructor
SortedArray::SortedArray(int size_) : size(size_ > 0 ? size_ : throw INVALID_ARGUMENT), arr(new unsigned int[size]), is_valid_element(new bool [size])
{
debug_print("SortedArray::SortedArray(int)");
if (arr == nullptr || is_valid_element == nullptr)
throw MEMORY_ALLOCATION_FAILURE;
index_arr = new CustomInt[size];
if (index_arr == nullptr)
throw MEMORY_ALLOCATION_FAILURE;
for (int i = 0; i < size; i++)
{
arr[i] = ARRAY_INITIALIZER;
index_arr[i] = CustomInt(i, this);
is_valid_element[i] = false;
}
}
// Copy Constructor
SortedArray::SortedArray(const SortedArray& array_to_copy) : size(array_to_copy.size), arr(new unsigned int[size]), is_valid_element(new bool[size])
{
debug_print("SortedArray::SortedArray(const SortedArray&)");
if (arr == nullptr || is_valid_element == nullptr)
throw MEMORY_ALLOCATION_FAILURE;
index_arr = new CustomInt[size];
if (index_arr == nullptr)
throw MEMORY_ALLOCATION_FAILURE;
for (int i = 0; i < size; i++)
{
arr[i] = array_to_copy.arr[i];
index_arr[i] = array_to_copy.index_arr[i];
is_valid_element[i] = array_to_copy.is_valid_element[i];
}
}
// Move Constructor
SortedArray::SortedArray(SortedArray&& array_to_move) : size(array_to_move.size), arr(array_to_move.arr),
index_arr(array_to_move.index_arr), is_valid_element(array_to_move.is_valid_element)
{
debug_print("SortedArray::SortedArray(SortedArray&&)");
array_to_move.arr = nullptr;
array_to_move.index_arr = nullptr;
array_to_move.is_valid_element = nullptr;
array_to_move.size = 0;
}
unsigned int SortedArray::getat(int index) const
{
debug_print("unsigned int SortedArray::getat(int)");
// check that the index is within the range of the array or else throw an exception
if (index < 0 || index >= size)
throw INVALID_ARGUMENT;
if (is_valid_element[index] == false) // this array entry is empty, thus can't be retrieved
throw EMPTY_ENTRY;
return arr[index];
}
void SortedArray::setat(int index, int value)
{
debug_print("void SortedArray::setat(int, int)");
if (value < 0 || value >= INT_MAX)
throw INVALID_ARGUMENT;
if (index < 0 || index >= size)
throw INVALID_ARGUMENT;
arr[index] = value;
is_valid_element[index] = true; // the index is set so became valid
bubbleSort(); // to maintain the property of sorted Array elements
}
// Overloaded Operators Definitions
// Assignment Operator Copy Semantics
const SortedArray& SortedArray::operator= (const SortedArray& right_arr)
{
debug_print("const SortedArray& SortedArray::operator= ((const SortedArray&)");
// to avoid self-assignment
if (&right_arr != this) // check if the passed array is pointing to my current array object
{
if (size != right_arr.size) // free the old memory and allocate new memory with the new proper size
{
delete[] arr; // free the old memory
delete[] index_arr;
delete[] is_valid_element;
size = right_arr.size;
// allocate new memories with the new size
arr = new unsigned int[size];
index_arr = new CustomInt[size];
is_valid_element = new bool[size];
if (arr == nullptr || index_arr == nullptr)
throw MEMORY_ALLOCATION_FAILURE;
}
for (int i = 0; i < size; i++)
{
arr[i] = right_arr.arr[i];
index_arr[i] = right_arr.index_arr[i];
is_valid_element[i] = right_arr.is_valid_element[i];
}
}
return *this; // to allow for cascaded assignment
}
// Assignment Operator Move Semantics
const SortedArray& SortedArray::operator= (SortedArray&& right_arr)
{
debug_print("const SortedArray& SortedArray::operator= ((SortedArray&&)");
// to avoid self-assignment
if (&right_arr != this) // check if the passed array is pointing to my current array object
{
delete[] arr;
delete[] index_arr;
delete[] is_valid_element;
arr = right_arr.arr;
index_arr = right_arr.index_arr;
is_valid_element = right_arr.is_valid_element;
size = right_arr.size;
right_arr.arr = nullptr;
right_arr.index_arr = nullptr;
right_arr.is_valid_element = nullptr;
right_arr.size = 0;
}
return *this; // to allow for cascaded assignment
}
// Equality Operator
bool SortedArray::operator== (const SortedArray& right_arr) const
{
debug_print("bool SortedArray::operator== (const SortedArray&)");
if (size != right_arr.size) // sizes aren't compatible, so blindly return false
return false;
// in case sizes are compatible , do element-wise check
for (int i = 0; i < size; i++)
{
if (arr[i] != right_arr.arr[i] || is_valid_element[i] != right_arr.is_valid_element[i] )
return false;
}
return true;
}
// Index Operators
CustomInt& SortedArray::operator[] (int index) // sets the value of the element
{
debug_print("CustomInt& SortedArray::operator[] (int)");
if (index < 0 || index >= size)
throw INVALID_ARGUMENT;
return index_arr[index];
}
unsigned int SortedArray::operator[](int index) const
{
debug_print("unsigned int SortedArray::operator[](int) const");
if (index < 0 || index >= size)
throw INVALID_ARGUMENT;
if (is_valid_element[index] == false) // this array entry is empty, thus can't be retrieved
throw EMPTY_ENTRY;
return arr[index];
}
// Destructor
SortedArray::~SortedArray()
{
debug_print("SortedArray::~SortedArray()");
if (arr != nullptr)
delete[] arr;
if (index_arr != nullptr)
delete[] index_arr;
if (is_valid_element != nullptr)
delete[] is_valid_element;
}
// Output stream global function definition
std::ostream& operator<<(std::ostream& output, const SortedArray& out_arr)
{
for (int i = 0; i < out_arr.size; i++)
{
output << std::setw(12) << out_arr.arr[i];
if ((i + 1) % 4 == 0) // to have 4 elements per row
output << std::endl;
}
return output;
}
// Private Functions Definitions
void SortedArray::debug_print(std::string operation_name) const
{
std::cout << operation_name << " size==" << size << " arr==" << arr << std::endl;
}
void SortedArray::bubbleSort()
{
for (int i = 0; i < size - 1; i++)
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
unsigned int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
bool temp_flag = is_valid_element[j];
is_valid_element[j] = is_valid_element[j+1];
is_valid_element[j+1] = temp_flag;
}
}
}