-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayList.cpp
More file actions
211 lines (175 loc) · 4.88 KB
/
Copy pathArrayList.cpp
File metadata and controls
211 lines (175 loc) · 4.88 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
//
// Created by Sean Blackford on 10/14/19.
//
#include <iostream>
#include "ArrayList.h"
#include "ArrayLib.h"
ArrayList::ArrayList(int initialCapacity) {
if(initialCapacity < 1){
throw std::invalid_argument("Size can't be less than 1");
}
else{
this -> currCapacity = initialCapacity;
this -> array = new Song[currCapacity];
this -> currItemCount = 0;
}
}
//assignment operator
//combining two objects listA = listB
//using destructor and copy constructor
ArrayList& ArrayList::operator=(const ArrayList& arrayListToCopy){
if(this != &arrayListToCopy) {
delete[] array;
this->currCapacity = arrayListToCopy.currCapacity;
this->array = new Song[currCapacity];
for (int i = 0; i < arrayListToCopy.currItemCount; i++) {
array[i] = arrayListToCopy.array[i];
}
this->currItemCount = arrayListToCopy.currItemCount;
return *this;
}return *this;
}
//Copy constructor
ArrayList::ArrayList(const ArrayList& arrayListToCopy) {
this -> currCapacity = arrayListToCopy.currCapacity;
this -> array = new Song[currCapacity];
for(int i = 0; i < arrayListToCopy.currItemCount; i++){
array[i] = arrayListToCopy.array[i];
}
this -> currItemCount = arrayListToCopy.currItemCount;
}
//Destructor
ArrayList::~ArrayList(){
delete[] array;
array = nullptr;
}
void ArrayList::doubleCapacity() {
Song *arrayList = new Song[currItemCount * 2];
currCapacity = currCapacity * 2;
for(int i = 0; i < currItemCount;i++){
arrayList[i] = array[i];
}
delete[] array;
array = new Song[currItemCount*2];
for(int i = 0; i < currItemCount;i++){
array[i] = arrayList[i];
}
delete[] arrayList;
}
void ArrayList::insertAtEnd(Song itemToAdd) {
currItemCount+=1;
if (currItemCount > currCapacity){
doubleCapacity();
}
array[currItemCount-1] = itemToAdd;
}
void ArrayList::insertAtAlphabetized(Song itemToAdd) {
int index = 0;
Song temp = array[0];
while (temp.getArtist().compare(itemToAdd.getArtist()) <= -1 && index < currItemCount) {
index += 1;
temp = array[index];
}
if (temp.getArtist().compare(itemToAdd.getArtist()) == 0){
while ((temp.getTitle().compare(itemToAdd.getTitle()) <= -1) and (temp.getArtist().compare(itemToAdd.getArtist()) == 0)){
index+=1;
temp = array[index];
}
}
this->insertAt(itemToAdd,index);
}
Song ArrayList::getValueAt(int index) {
if(index < 0 or index >= currItemCount){
throw std::out_of_range("e");
}
return array[index];
}
std::string ArrayList::toString(){
//return ::toString(array,currItemCount);
return "not done yet";
}
bool ArrayList::isEmpty() {
if(currItemCount < 1){
return true;
}
return false;
}
int ArrayList::itemCount() {
return currItemCount;
}
void ArrayList::clearList() {
for(int i = 0; i < currItemCount;i++){
removeValueAtFront();
}
currItemCount = 0;
}
void ArrayList::insertAtFront(Song itemToAdd) {
if (currItemCount +1 > currCapacity){
doubleCapacity();
}
for (int i = 0; i<currItemCount+1;i++){
Song temp = array[i];
array[i] = itemToAdd;
itemToAdd = temp;
}
currItemCount ++;
}
int ArrayList::findArtistandTitle(const int size, std::string artistIn, std::string titleIn) {
for(int i = 0; i < size; i++){
if(artistIn == array[i].getArtist()){
if(titleIn == array[i].getTitle()){
return i;
}
}
}
return -1;
}
void ArrayList::insertAt(Song itemToAdd, int index) {
if (index < 0 or index > currItemCount){
throw std::out_of_range("Bad index given to insertAt: " + std::to_string(index));
}
if (currItemCount +1 > currCapacity){
doubleCapacity();
}
for (int i = index; i<currItemCount+1;i++){
Song temp = array[i];
array[i] = itemToAdd;
itemToAdd = temp;
}
currItemCount ++;
}
Song ArrayList::removeValueAtEnd() {
if(currItemCount <1){
throw std::out_of_range("e");
}
currItemCount-=1;
return array[currItemCount];
}
Song ArrayList::removeValueAtFront() {
if(currItemCount<=0){
throw std::out_of_range("In removeValueAtFront, List must have items");
}
Song temp = array[0];
for (int i =0; i<currItemCount-1;i++){
array[i] = array[i+1];
}
currItemCount--;
return temp;
}
Song ArrayList::removeValueAt(int index) {
if(index < 0 or index > currItemCount){
throw std::out_of_range("e");
}
if(currItemCount < 0){
throw std::out_of_range("e");
}
Song copy = array[index];
for(int i = 0; i < index;i++){
array[i] = array[i];
}
for(int i = index+1; i < currItemCount;i++){
array[i-1] = array[i];
}
currItemCount--;
return copy;
}