From eb379731475a95a7d895e317d7192c39ffe3d736 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 21 Dec 2017 14:02:32 -0800 Subject: [PATCH] Start simple USet implementation. --- src/ods/simplist.h | 64 +++++++++++++++++++++++++++++++++++++------- src/ods/simpuset.h | 66 ++++++++++++++++++++++++++++++++++++++++++++++ src/ods/uset.h | 20 ++++++++++++++ 3 files changed, 141 insertions(+), 9 deletions(-) create mode 100644 src/ods/simpuset.h create mode 100644 src/ods/uset.h diff --git a/src/ods/simplist.h b/src/ods/simplist.h index fa573d3..f832aae 100644 --- a/src/ods/simplist.h +++ b/src/ods/simplist.h @@ -22,6 +22,8 @@ private: T *arr; std::size_t cap; std::size_t len; + void grow(); + void shrink(); const std::size_t DEFAULT_SIZE = 8; }; @@ -71,10 +73,10 @@ SimpList::set(std::size_t i, T value) if (i >= this->len) { throw std::invalid_argument("index out of range"); } - // check size, grow as needed - // simple case: check append - // complex case: insertion - return value; + + T old = this->arr[i]; + this->arr[i] = value; + return old; } @@ -82,12 +84,28 @@ template void SimpList::add(std::size_t i, T value) { - if (i >= this->len) { + if (i > this->len) { throw std::invalid_argument("index out of range"); } assert(value); - - return; + + // check size, grow as needed + if (len == (cap - 1)) { + this->grow(); + } + + // simple case: check append + if (i == len) { + this->arr[len++] = value; + return; + } + + // complex case: insertion + for (std::size_t j = this->len; j > i; j--) { + this->arr[j] = this->arr[j-1]; + } + this->arr[i] = value; + this->len++; } @@ -95,11 +113,39 @@ template T SimpList::remove(std::size_t i) { - if (i >= this->len) { + if (i > this->len) { throw std::invalid_argument("index out of range"); } + + T old = this->arr[i]; + + if (i == this->len) { + this->len--; + return old; + } + + for (std::size_t j = i; j < this->len; j++) { + this->arr[j] = this->arr[j+1]; + } + this->len--; - return this->arr[i]; + return old; +} + +template +void +SimpList::grow() +{ + std::size_t new_cap = this->cap * 2; + T *new_arr = new T[new_cap]; + + for (std::size_t i = 0; i < this->len; i++) { + new_arr[i] = this->arr[i]; + } + + delete this->arr; + this->arr = new_arr; + this->cap = new_cap; } } // end namespace ods diff --git a/src/ods/simpuset.h b/src/ods/simpuset.h new file mode 100644 index 0000000..bf9cc05 --- /dev/null +++ b/src/ods/simpuset.h @@ -0,0 +1,66 @@ +#ifndef __ODS_ODS_SIMPUSET__ +#define __ODS_ODS_SIMPUSET__ + + +#include +#include + +namespace ods { +template +class SimpUSet : USet { +public: + SimpUSet(void); + ~SimpUSet(void); + std::size_t size(void); + bool add(T); + std::optional remove(T); + std::optional find(T); +}; + +template +SimpUSet::SimpUSet() +{ +} + + +template +SimpUSet::~SimpUSet() +{ +} + +template +std::size_t +SimpUSet::size() +{ + return 0; +} + +template +bool +SimpUSet::add(T value) +{ + assert(value); + return false; +} + +template +std::optional +SimpUSet::remove(T value) +{ + assert(value); + return std::nullopt; +} + +template +std::optional +SimpUSet::find(T value) +{ + assert(value); + return std::nullopt; +} + + +} // namespace ods + + +#endif // __ODS_ODS_SIMPUSET__ \ No newline at end of file diff --git a/src/ods/uset.h b/src/ods/uset.h new file mode 100644 index 0000000..0872d11 --- /dev/null +++ b/src/ods/uset.h @@ -0,0 +1,20 @@ +#ifndef __ODS_ODS_USET__ +#define __ODS_ODS_USET__ + + +#include +#include + +namespace ods { +template +class USet { +public: + virtual ~USet(void) {}; + virtual std::size_t size(void) =0; + virtual bool add(T) = 0; + virtual std::optional remove(T) = 0; + virtual std::optional find(T) = 0; +}; +} // namespace ods + +#endif // __ODS_ODS_USET__ \ No newline at end of file