Finish SimpUSet implementation.

This commit is contained in:
Kyle Isom 2017-12-21 14:13:48 -08:00
parent 8ea78af90b
commit 89ea1fadf2
2 changed files with 40 additions and 5 deletions

View File

@ -62,6 +62,20 @@ check_simpuset(void)
ods::SimpUSet<int> us;
assert(us.add(1));
assert(us.size() == 1);
assert(us.find(1));
assert(!us.add(1));
assert(us.size() == 1);
assert(us.add(2));
assert(us.find(2));
assert(us.add(3));
assert(us.size() == 3);
assert(us.find(3));
auto removed = us.remove(2);
assert(removed == 2);
assert(us.size() == 2);
assert(!us.find(2));
}

View File

@ -3,6 +3,7 @@
#include <optional>
#include <ods/simplist.h>
#include <ods/uset.h>
namespace ods {
@ -15,6 +16,8 @@ public:
bool add(T);
std::optional<T> remove(T);
std::optional<T> find(T);
private:
SimpList<T> list;
};
template<typename T>
@ -32,22 +35,35 @@ template<typename T>
std::size_t
SimpUSet<T>::size()
{
return 0;
return this->list.size();
}
template<typename T>
bool
SimpUSet<T>::add(T value)
{
assert(value);
return false;
for (std::size_t i = 0; i < this->list.size(); i++) {
if (this->list.get(i) == value) {
return false;
}
}
this->list.add(this->list.size(), value);
return true;
}
template<typename T>
std::optional<T>
SimpUSet<T>::remove(T value)
{
assert(value);
for (std::size_t i = 0; i < this->list.size(); i++) {
if (this->list.get(i) == value) {
auto removed = this->list.get(i);
this->list.remove(i);
return removed;
}
}
return std::nullopt;
}
@ -55,7 +71,12 @@ template<typename T>
std::optional<T>
SimpUSet<T>::find(T value)
{
assert(value);
for (std::size_t i = 0; i < this->list.size(); i++) {
if (this->list.get(i) == value) {
return this->list.get(i);
}
}
return std::nullopt;
}