-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedArray.h
More file actions
127 lines (97 loc) · 3.03 KB
/
SortedArray.h
File metadata and controls
127 lines (97 loc) · 3.03 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
#ifndef SORTEDARRAY_H
#define SORTEDARRAY_H
#include <iostream>
#include <iomanip>
#include <string>
enum exception_type { INVALID_ARGUMENT, MEMORY_ALLOCATION_FAILURE, EMPTY_ENTRY };
const unsigned int ARRAY_INITIALIZER = INT_MAX;
//class SortedArray;
class CustomInt;
class SortedArray
{
public:
explicit SortedArray(int size_ = 10); // works as both default and integer constructor
SortedArray(const SortedArray& array_to_copy); // Copy constructor
SortedArray(SortedArray&& array_to_move); // Move constructor
void setat(int index, int value);
unsigned int getat(int index) const;
// Overloaded Operators
// Assignment Operator
const SortedArray& operator= (const SortedArray& right_arr); // Copy Semantics
const SortedArray& operator= (SortedArray&& right_arr); // Move Semantics
bool operator== (const SortedArray& right_arr) const; // Equality Operator
// Index Operator
CustomInt& operator[](int index);
unsigned int operator[](int index) const;
~SortedArray();
private:
int size;
unsigned int* arr;
bool* is_valid_element; // to indicate if a specific index contains a valid entry or not
CustomInt* index_arr; // this will be an array of indices, will be used inside overloading of [] operator
void debug_print(std::string operation_name) const;
void bubbleSort();
friend std::ostream& operator<<(std::ostream& output, const SortedArray& out_arr);
};
class CustomInt
{
public:
// inline functions
// Integer and Default Constructor
CustomInt(int index = 0, SortedArray* arr_obj = nullptr) : my_int(index), my_sorted_arr(arr_obj)
{
}
// Copy Constructor
CustomInt(const CustomInt& obj_to_copy)
{
my_int = obj_to_copy.my_int;
my_sorted_arr = obj_to_copy.my_sorted_arr;
}
// Move Constructor
CustomInt(CustomInt&& obj_to_move)
{
my_int = obj_to_move.my_int;
my_sorted_arr = obj_to_move.my_sorted_arr;
obj_to_move.my_sorted_arr = nullptr;
obj_to_move.my_int = 0;
}
~CustomInt()
{
}
// Overloading the assignment operator to int
const CustomInt& operator=(int value)
{
my_sorted_arr->setat(my_int, value);
return *this;
}
// Move assignment operator to CustomInt object
const CustomInt& operator=(CustomInt&& right_obj)
{
my_int = right_obj.my_int;
my_sorted_arr = right_obj.my_sorted_arr;
return *this;
}
// Copy assignment operator to CustomInt object
const CustomInt& operator=(const CustomInt& right_obj)
{
my_int = right_obj.my_int;
my_sorted_arr = right_obj.my_sorted_arr;
return *this;
}
// Overloading unsigned int type converter
operator unsigned int()
{
unsigned int temp = my_sorted_arr->getat(my_int);
return temp;
}
friend inline std::ostream& operator<<(std::ostream& output, const CustomInt& out_element)
{
unsigned int temp = out_element.my_sorted_arr->getat(out_element.my_int);
output << temp;
return output;
}
private:
int my_int;
SortedArray* my_sorted_arr;
};
#endif // !SORTEDARRAY_H