// see apset.h const int SDEFAULT_SIZE = 10; // default initial set size template apset::apset() : myList(SDEFAULT_SIZE), myCount(0) { } template bool apset::contains(const itemType & item) const { int loc = find(item); return loc != -1; } template int apset::size() const { return myCount; } template void apset::print(ostream & output) const { int k; output << "(" << endl; for(k=0; k < myCount; k++) { output << "\t" << myList[k] << endl; } output << ")" << endl; } template void apset::add(const itemType & item) { int loc = find(item); if (loc == -1) { if (myCount >= myList.length()) { myList.resize(myCount * 2); } loc = myCount - 1; while (0 <= loc && item < myList[loc]) { myList[loc+1] = myList[loc]; loc--; } myList[loc+1] = item; myCount++; } } template void apset::remove(const itemType & item) { int loc = find(item); if (loc != -1) { int k; for(k=loc; k < myCount-1; k++) { myList[k] = myList[k+1]; } myCount--; } } template void apset::makeEmpty() { myCount = 0; } template int apset::find(const itemType & item) const { int low = 0; int high = myCount-1; int mid; // middle of current range while (low <= high) { mid = (low + high)/2; if (myList[mid] == item) // found key, exit { return mid; } else if (myList[mid] < item) // key in upper half { low = mid + 1; } else // key in lower half { high = mid - 1; } } return -1; } template const apset& apset::operator *= (const apset & rhs) { apset result; int k; int length; if (size() < rhs.size()) { length = size(); for(k=0; k < length; k++) { if (rhs.contains(myList[k])) { result.add(myList[k]); } } } else { length = rhs.size(); for(k=0; k < length; k++) { if (contains(rhs.myList[k])) { result.add(myList[k]); } } } *this = result; return *this; } template const apset& apset::operator += (const apset & rhs) { int k; int length = rhs.size(); for(k=0; k < length; k++) { if (! contains(rhs.myList[k])) { add(rhs.myList[k]); } } return *this; }