From 89ea1fadf291201530a4af05b6eb55dc837559f5 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 21 Dec 2017 14:13:48 -0800 Subject: [PATCH] Finish SimpUSet implementation. --- src/ch01ex06.cc | 14 ++++++++++++++ src/ods/simpuset.h | 31 ++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/ch01ex06.cc b/src/ch01ex06.cc index d186043..1b5961a 100644 --- a/src/ch01ex06.cc +++ b/src/ch01ex06.cc @@ -62,6 +62,20 @@ check_simpuset(void) ods::SimpUSet 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)); } diff --git a/src/ods/simpuset.h b/src/ods/simpuset.h index bf9cc05..f9066e4 100644 --- a/src/ods/simpuset.h +++ b/src/ods/simpuset.h @@ -3,6 +3,7 @@ #include +#include #include namespace ods { @@ -15,6 +16,8 @@ public: bool add(T); std::optional remove(T); std::optional find(T); +private: + SimpList list; }; template @@ -32,22 +35,35 @@ template std::size_t SimpUSet::size() { - return 0; + return this->list.size(); } template bool SimpUSet::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 std::optional SimpUSet::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 std::optional SimpUSet::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; }