-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayBag.cpp
More file actions
186 lines (146 loc) · 3.28 KB
/
Copy pathArrayBag.cpp
File metadata and controls
186 lines (146 loc) · 3.28 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
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2016 __Pearson Education__. All rights reserved.
//Modified by TAA to remove Template and Inheritance
/** @file ArrayBag.cpp */
#include "ArrayBag.h"
#include <iostream>
using namespace std;
ArrayBag::ArrayBag() : itemCount(0), maxItems(DEFAULT_BAG_SIZE)
{
} // end default constructor
int ArrayBag::getCurrentSize() const
{
return itemCount;
} // end getCurrentSize
bool ArrayBag::isEmpty() const
{
return itemCount == 0;
} // end isEmpty
bool ArrayBag::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
} // end if
return hasRoomToAdd;
} // end add
bool ArrayBag::remove(const ItemType& anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
} // end if
return canRemoveItem;
} // end remove
void ArrayBag::clear()
{
itemCount = 0;
} // end clear
int ArrayBag::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int searchIndex = 0;
while (searchIndex < itemCount)
{
if (items[searchIndex] == anEntry)
{
frequency++;
} // end if
searchIndex++;
} // end while
return frequency;
} // end getFrequencyOf
bool ArrayBag::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
std::vector<ItemType> ArrayBag::toVector() const
{
std::vector<ItemType> ArrayBagContents;
for (int i = 0; i < itemCount; i++)
{
ArrayBagContents.push_back(items[i]);
}
return ArrayBagContents;
} // end toVector
ArrayBag ArrayBag :: operator + (const ArrayBag &b)
{
ArrayBag temp;
//Adding both arrays to the temp array
for (int i = 0; i < itemCount; i++)
{
temp.add(items[i]);
}
int size = b.getCurrentSize();
for (int index = 0; index < size; index++)
{
temp.add(b.items[index]);
}
//Removing duplicates
int tempSize = temp.getCurrentSize();
int frequency = 1;
int secondFrequency = 1;
for (int newIndex = 0; newIndex < tempSize; newIndex++)
{
frequency = temp.getFrequencyOf(temp.items[newIndex]);
if (frequency > 1)
{
temp.remove(items[newIndex]);
secondFrequency = temp.getFrequencyOf(temp.items[newIndex]);
if (secondFrequency > 1)
{
temp.remove(items[newIndex]);
}
}
}
return temp;
}
ArrayBag ArrayBag:: operator - (const ArrayBag &b)
{
ArrayBag temp;
//removing any items in array bag1 that also appear in array bag b
for (int i = 0; i < itemCount; i++)
{
if (b.contains(items[i]))
{
remove(items[i]);
}
}
//placing the updated arrays into temp
for (int newI = 0; newI < itemCount; newI++)
{
temp.add(items[newI]);
}
int size = b.getCurrentSize();
for (int index = 0; index < size; index++)
{
temp.add(b.items[index]);
}
return temp;
}
// private
int ArrayBag::getIndexOf(const ItemType& target) const
{
bool found = false;
int result = -1;
int searchIndex = 0;
// if the ArrayBag is empty, itemCount is zero, so loop is skipped
while (!found && (searchIndex < itemCount))
{
if (items[searchIndex] == target)
{
found = true;
result = searchIndex;
}
else
{
searchIndex++;
} // end if
} // end while
return result;
} // end getIndexOf