#include
"Bag.h"
templatetypename
Type>
Bag::Bag() {
arrayNumbers = 0;
}
templatetypename
Type>
Bag::Bag(const
Type& item) {
arrayNumbers = 0;
items[arrayNumbers++] = item;
}
templatetypename
Type>
Bag::Bag(const
Bag& bag) {
arrayNumbers = bag.arrayNumbers;
for(int
value = 0;value < arraynumbers;="">
items[value] = bag.items[value];
}
}
templatetypename
Type>
Bag::~Bag() {
arrayNumbers= 0;
}
templatetypename
Type>
const
BagType>&
Bag::operator=(const
BagType>& bag) {
if
(this
!= &bag)
{
arrayNumbers = bag.arrayNumbers;
for
(int
i = 0; i < arraynumbers;="" ++i)="">
items[i] = bag.items[i];
}
}
return
*this;
}
templatetypename
Type>
bool
Bag::addItem(const
Type& item) {
if
(isFull()) {
return
false;
}
items[arrayNumbers++] = item;
return
true;
}
templatetypename
Type>
bool
Bag::removeItem(const
Type& item) {
int
index = -1;
int
value = 0;
while(value < arraynumbers)="">
if
(items[value] == item) {
index = value;
break;
}
++value;
}
if
(index == -1)
return
false;
--arrayNumbers;
int
number = index;
while( number < arraynumbers)="">
items[value] = items[number + 1];
++number;
}
return
true;
}
templatetypename
Type>
int
Bag::removeAllItems(const
Type& item) {
int
cnt = 0;
while
(removeItem(item)) ++cnt;
return
cnt;
}
templatetypename
Type>
void
Bag:: empty(){
arrayNumbers = 0;
}
templatetypename
Type>
int
Bag::nmbrItems() {
return
arrayNumbers;
}
templatetypename
Type>
int
Bag::capacity() {
return
BAG_SIZE;
}
templatetypename
Type>
bool
Bag::isEmpty() {
return
arrayNumbers == 0;
}
templatetypename
Type>
bool
Bag::isFull() {
return
arrayNumbers ==
BAG_SIZE;
}
templatetypename
Type>
bool
Bag::contains(const
Type& item) {
int
value = 0;
while( value < arraynumbers)="">
if
(items[value] == item) {
return
true;
}
++value;
}
return
false;
}
templatetypename
Type>
int
Bag::itemOccurrences(const
Type& item) {
int
count = 0;
int
value = 0;
while( value < arraynumbers)="">
if
(items[value] == item) {
++count;
}
++value;
}
return
count;
}
templatetypename
Type>
BagType>
Bag::bagsUnion(const
Bag& bag ) {
BagType> unionBag;
unionBag.arrayNumbers = bag.arrayNumbers;
for
(int
i = 0; i < unionbag.arraynumbers;="" ++i)="">
unionBag.items[i] = bag.items[i];
unionBag.contains(bag.items[i]);
}
return
unionBag;
}
templatetypename
Type>
string
Bag::toString() {
if
(isEmpty())
return
"[]";
string number = "[" + std::to_string(items[0]);
int
value = 1;
while( value < arraynumbers)="">
number += ", " + std::to_string(items[value]);
++value;
}
number += "]";
return
number;
}