Skip to content

Instantly share code, notes, and snippets.

@Jxrgxn
Created July 24, 2015 19:48
Show Gist options
  • Save Jxrgxn/744b1edeb6d5400aad89 to your computer and use it in GitHub Desktop.
Save Jxrgxn/744b1edeb6d5400aad89 to your computer and use it in GitHub Desktop.
//Lucas Larson
//7/19/15
//problem 1
//assignment #2
#ifndef _ARRAY_BAG
#define _ARRAY_BAG
#include "BagInterface.h"
#include <vector>
#include <cstddef>
#include <string>
template<class ItemType>
class ArrayBag : public BagInterface<ItemType>
{
private:
static const int DEFAULT_CAPACITY = 24; // the bag size a bit bigger to allow
// adding bags.
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;
int& operator[](size_t x) const { return items[x];};
public:
ArrayBag();
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
void clear();
bool remove(const ItemType& anEntry);
bool contains(const ItemType& target) const;
int getFrequencyOf(const ItemType& anEntry) const;
int merge(ArrayBag<string> a, ArrayBag<string> b);
std::vector<ItemType> toVector() const;
}; // end ArrayBag
#endif
@Jxrgxn
Copy link
Author

Jxrgxn commented Jul 24, 2015

template
int ArrayBag::merge(ArrayBag a, ArrayBag b) {
int newsz = a.getCurrentSize() + b.getCurrentSize();
if (newsz > a.DEFAULT_CAPACITY)

    ArrayBag<string> result;

for (int c=0; c<a.getCurrentSize(); ++c){
    result.add(a.items[c]);
}

for (int c=0; c<b.getCurrentSize(); ++c){
    result.add(b.items[c]);
}

return result;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment