-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayList.cpp
185 lines (164 loc) · 4.15 KB
/
ArrayList.cpp
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
#include <iostream>
#include "ArrayList.h"
// default constructor
ArrayList::ArrayList()
{
// std::cout << "Arraylist default constructor is called." << std::endl;
this->capacity = 1;
this->used = 0;
this->pArray = new int[capacity];
}
// copy constructor
ArrayList::ArrayList(const ArrayList &arrayToCopy)
{
// std::cout << "Arraylist copy constructor is called." << std::endl;
this->capacity = arrayToCopy.getCapacity();
this->used = arrayToCopy.size();
this->pArray = new int[capacity];
for (int i = 0; i < capacity; i++)
{
this->pArray[i] = arrayToCopy.getPArray()[i];
}
}
// move constructor
ArrayList::ArrayList(ArrayList&& arrayToMove)
{
// std::cout << "Arraylist move constructor is called." << std::endl;
this->capacity = arrayToMove.getCapacity();
this->used = arrayToMove.size();
this->pArray = arrayToMove.getPArray();
arrayToMove.capacity = 0;
arrayToMove.used = 0;
arrayToMove.pArray = nullptr;
}
// copy assignment operator
ArrayList& ArrayList::operator=(const ArrayList &rhs)
{
// std::cout << "Arraylist copy assignment operator is called." << std::endl;
// if the addresses are not the same, proceed with copying
if (&rhs != this)
{
this->capacity = rhs.getCapacity();
this->used = rhs.size();
delete[] this->pArray;
this->pArray = new int[capacity];
for (int i = 0; i < capacity; i++)
{
this->pArray[i] = rhs.getPArray()[i];
}
}
return *this;
}
// move assignment operator
ArrayList& ArrayList::operator=(ArrayList &&rhs)
{
// std::cout << "Arraylist move assignment operator is called." << std::endl;
if (&rhs != this)
{
delete[] this->pArray;
this->capacity = rhs.getCapacity();
this->used = rhs.size();
this->pArray = rhs.getPArray();
rhs.capacity = 0;
rhs.used = 0;
rhs.pArray = nullptr;
}
return *this;
}
// destructor
ArrayList::~ArrayList()
{
// std::cout << "Arraylist destructor is called." << std::endl;
delete[] this->pArray;
}
// determines whether used equals zero
bool ArrayList::empty() const
{
return this->used == 0;
}
// determines whether used equals capacity
bool ArrayList::full() const
{
return this->used == this->capacity;
}
// getters for all member variables
int ArrayList::getCapacity() const
{
return this->capacity;
}
int *ArrayList::getPArray() const
{
return this->pArray;
}
int ArrayList::size() const
{
return this->used;
}
// inserts x at position used and then increments used by 1
void ArrayList::pushBack(int x)
{
// if we are at capacity
if (this->capacity == this->used)
{
resize();
}
this->pArray[this->used] = x;
this->used++;
}
// determines whether x occurs in the list
bool ArrayList::contains(int x) const
{
for (int i = 0; i < this->used; i++)
{
if (x == this->pArray[i])
{
return true;
}
}
return false;
}
// places the value stored at position in the reference parameter value and returns true
// if position is out of range, returns false
bool ArrayList::get(int position, int& value) const
{
if(position > this->capacity)
{
return false;
}
value = this->pArray[position];
return true;
}
// double the capacity of pArray
void ArrayList::resize()
{
int newCapacity = this->capacity * 2;
int* newPArray = new int[newCapacity];
for (int i = 0; i < this->capacity; i++)
{
newPArray[i] = this->pArray[i];
}
this->capacity = newCapacity;
delete[] this->pArray;
this->pArray = newPArray;
}
// << operator overload
std::ostream &operator<<(std::ostream &output, const ArrayList &listToPrint)
{
// if used is 0, return empty string because we can't print anything
if (listToPrint.size() == 0)
{
return output;
}
for (int i = 0; i < listToPrint.size(); i++)
{
output << listToPrint.pArray[i] << " ";
}
return output;
}
void ArrayList::print(std::ostream &output) const
{
for (int i = 0; i < this->used; i++)
{
output << pArray[i] << " ";
}
}