69 lines
1.4 KiB
C++
69 lines
1.4 KiB
C++
#include "ListExceptions.h"
|
|
#include "listE.h"
|
|
|
|
template <class E> class Set {
|
|
private:
|
|
List<E> _list;
|
|
|
|
public:
|
|
Set();
|
|
Set(const E &_newItem);
|
|
Set(const E *_newItem, const int count);
|
|
// Add an element.
|
|
Set<E> &add(const E &_newItem);
|
|
// Remove an element.
|
|
Set<E> &remove(const E &target);
|
|
// Clear all elements.
|
|
Set<E> &clear();
|
|
|
|
int size();
|
|
void show();
|
|
};
|
|
|
|
template <class E> Set<E>::Set() : _list(){};
|
|
|
|
template <class E> Set<E>::Set(const E &_newItem) : _list(_newItem){};
|
|
|
|
template <class E> Set<E>::Set(const E *_newItem, const int count) : _list() {
|
|
for (int i = 0; i < count; i++) {
|
|
try {
|
|
this->add(_newItem[i]);
|
|
}
|
|
catch (DuplicateError) {
|
|
continue;
|
|
}
|
|
}
|
|
};
|
|
|
|
template <class E> Set<E> &Set<E>::add(const E &_newItem) {
|
|
if (_list.contains(_newItem)) {
|
|
throw(DuplicateError());
|
|
}
|
|
|
|
_list.append(_newItem);
|
|
return *this;
|
|
};
|
|
|
|
template <class E> Set<E> &Set<E>::remove(const E &target) {
|
|
_list.remove(target);
|
|
return *this;
|
|
}
|
|
|
|
template <class E> Set<E> &Set<E>::clear() {
|
|
_list.clear();
|
|
return *this;
|
|
}
|
|
|
|
template <class E> int Set<E>::size() {
|
|
return _list.length();
|
|
}
|
|
|
|
template <class E> void Set<E>::show() {
|
|
for (int i = 0; i < _list.length(); i++) {
|
|
std::cout << _list[i] << ' ';
|
|
}
|
|
if (_list.length() == 0) {
|
|
std::cout << "(Empty)";
|
|
}
|
|
std::cout << std::endl;
|
|
} |