& bag) { std::cout bagItems = bag.toVector(); int numberOfEntries = (int) bagItems.size(); for (int i = 0; i void displayBagOfStrings(ArrayBag<:string>& bag) { std::cout bagItems = bag.toVector(); int numberOfEntries = (int) bagItems.size(); for (int i = 0; i
ArrayBag.h
#ifndef ARRAY_BAG_ #define ARRAY_BAG_
#include "BagInterface.h"
template class ArrayBag : public BagInterface { private: static const int DEFAULT_CAPACITY = 20; ItemType items[DEFAULT_CAPACITY]; // Array of bag items int itemCount; // Current count of bag items int maxItems; // Max capacity of the bag // Returns either the index of the element in the array items that // contains the given target or -1, if the array does not contain // the target. int getIndexOf(const ItemType& target) const;
public: ArrayBag(); int getCurrentSize() const; bool isEmpty() const; bool add(const ItemType& newEntry); bool remove(const ItemType& anEntry); void clear(); bool contains(const ItemType& anEntry) const; int getFrequencyOf(const ItemType& anEntry) const; std::vector toVector() const; }; // end ArrayBag
#include "ArrayBag.cpp" #endif
ArrayBag.cpp
#include "ArrayBag.h" #include
template ArrayBag::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY) { } // end default constructor
template int ArrayBag::getCurrentSize() const { return itemCount; } // end getCurrentSize
template bool ArrayBag::isEmpty() const { return itemCount == 0; } // end isEmpty
template bool ArrayBag::add(const ItemType& newEntry) { bool hasRoomToAdd = (itemCount 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
template void ArrayBag::clear() { itemCount = 0; } // end clear
template int ArrayBag::getFrequencyOf(const ItemType& anEntry) const { int frequency = 0; int curIndex = 0; // Current array index while (curIndex template bool ArrayBag::contains(const ItemType& anEntry) const { return getIndexOf(anEntry) > -1; } // end contains
template std::vector ArrayBag::toVector() const { std::vector bagContents; for (int i = 0; i // private template int ArrayBag::getIndexOf(const ItemType& target) const { bool found = false; int result = -1; int searchIndex = 0; // If the bag is empty, itemCount is zero, so loop is skipped while (!found && (searchIndex BagInterface.h
template class BagInterface { public: /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ virtual int getCurrentSize() const = 0; /** Sees whether this bag is empty. @return True if the bag is empty, or false if not. */ virtual bool isEmpty() const = 0; /** Adds a new entry to this bag. @post If successful, newEntry is stored in the bag and the count of items in the bag has increased by 1. @param newEntry The object to be added as a new entry. @return True if addition was successful, or false if not. */ virtual bool add(const ItemType& newEntry) = 0; /** Removes one occurrence of a given entry from this bag, if possible. @post If successful, anEntry has been removed from the bag and the count of items in the bag has decreased by 1. @param anEntry The entry to be removed. @return True if removal was successful, or false if not. */ virtual bool remove(const ItemType& anEntry) = 0; /** Removes all entries from this bag. @post Bag contains no items, and the count of items is 0. */ virtual void clear() = 0; /** Counts the number of times a given entry appears in bag. @param anEntry The entry to be counted. @return The number of times anEntry appears in the bag. */ virtual int getFrequencyOf(const ItemType& anEntry) const = 0; /** Tests whether this bag contains a given entry. @param anEntry The entry to locate. @return True if bag contains anEntry, or false otherwise. */ virtual bool contains(const ItemType& anEntry) const = 0; /** Empties and then fills a given vector with all entries that are in this bag. @return A vector containing all the entries in the bag. */ virtual std::vector toVector() const = 0; /** Destroys object and frees memory allocated by object. (See C++ Interlude 2) */ virtual ~BagInterface () { } }; // end BagInterface #endif
The following 3 methods are to be added to the Array-based implementation of the Bag ADT. Methods must compose a new Bag object by accessing the elements of the underlying array of the Bag objects. You may not convert the Bags to vectors in the completion of these methods. The main method must also be updated to thoroughly test the newly added methods. Your output does not need to match the samples shown. 1) bagUnion: The union of two bags is a new bag containing the combined contents of the original two bags. Design and specify a method union for the ArrayBag that returns as a new bag the union of the bag receiving the call to the method and the bag that is the method's parameter. The method signature is: ArrayBag bagUnion (const ArrayBag &otherBag) const Note that the union of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the union of these bags contains x seven times. The union does not affect the contents of the original bags Example run First bag: The bag contains 5 items: 1 2 34 5 Second bag: The bag contains 5 items: 20 30 40 50 1 The bag containing the union of these bags: The bag contains 10 items: 1 2 3 4 5 20 30 40 50 1 First bag: The bag contains 5 items one two three four five second bag: The bag contains 5 items: twenty thirty fourty fifty one The bag containing the union of these bags: The bag contains 10 items: one two three four five twenty thirty fourty fifty one 2) bagIntersection: The intersection of two bags is a new bag containing the entries that occur in both of the original bags. Design and specify a method intersection for the ArrayBag that returns as a new bag the intersection of the bag receiving the call to the method and the bag that is the method's parameter. The method signature is: ArrayBag bagIntersection (const ArrayBag bagDifference (const ArrayBag otherBag) consti Note that the difference of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the difference of these bags contains x three times. Example run First bag: The bag contains 5 items: 1 23 4 5 Second bag: The bag contains 5 items: 20 30 40 50 1 The bag containing the difference of these bags: The bag contains 4 items: 52 First bag: The bag contains 5 items: one two three four five Second bag: The bag contains 5 items: twenty thirty fourty fifty one The bag containing the difference of these bags: The bag contains 4 items: five two three four