Start simple USet implementation.

This commit is contained in:
Kyle Isom 2017-12-21 14:02:32 -08:00
parent a303676084
commit eb37973147
3 changed files with 141 additions and 9 deletions

View File

@ -22,6 +22,8 @@ private:
T *arr; T *arr;
std::size_t cap; std::size_t cap;
std::size_t len; std::size_t len;
void grow();
void shrink();
const std::size_t DEFAULT_SIZE = 8; const std::size_t DEFAULT_SIZE = 8;
}; };
@ -71,10 +73,10 @@ SimpList<T>::set(std::size_t i, T value)
if (i >= this->len) { if (i >= this->len) {
throw std::invalid_argument("index out of range"); throw std::invalid_argument("index out of range");
} }
// check size, grow as needed
// simple case: check append T old = this->arr[i];
// complex case: insertion this->arr[i] = value;
return value; return old;
} }
@ -82,12 +84,28 @@ template <typename T>
void void
SimpList<T>::add(std::size_t i, T value) SimpList<T>::add(std::size_t i, T value)
{ {
if (i >= this->len) { if (i > this->len) {
throw std::invalid_argument("index out of range"); throw std::invalid_argument("index out of range");
} }
assert(value); assert(value);
// check size, grow as needed
if (len == (cap - 1)) {
this->grow();
}
// simple case: check append
if (i == len) {
this->arr[len++] = value;
return; 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 <typename T>
T T
SimpList<T>::remove(std::size_t i) SimpList<T>::remove(std::size_t i)
{ {
if (i >= this->len) { if (i > this->len) {
throw std::invalid_argument("index out of range"); throw std::invalid_argument("index out of range");
} }
return this->arr[i]; 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 old;
}
template<typename T>
void
SimpList<T>::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 } // end namespace ods

66
src/ods/simpuset.h Normal file
View File

@ -0,0 +1,66 @@
#ifndef __ODS_ODS_SIMPUSET__
#define __ODS_ODS_SIMPUSET__
#include <optional>
#include <ods/uset.h>
namespace ods {
template<typename T>
class SimpUSet : USet<T> {
public:
SimpUSet(void);
~SimpUSet(void);
std::size_t size(void);
bool add(T);
std::optional<T> remove(T);
std::optional<T> find(T);
};
template<typename T>
SimpUSet<T>::SimpUSet()
{
}
template<typename T>
SimpUSet<T>::~SimpUSet()
{
}
template<typename T>
std::size_t
SimpUSet<T>::size()
{
return 0;
}
template<typename T>
bool
SimpUSet<T>::add(T value)
{
assert(value);
return false;
}
template<typename T>
std::optional<T>
SimpUSet<T>::remove(T value)
{
assert(value);
return std::nullopt;
}
template<typename T>
std::optional<T>
SimpUSet<T>::find(T value)
{
assert(value);
return std::nullopt;
}
} // namespace ods
#endif // __ODS_ODS_SIMPUSET__

20
src/ods/uset.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef __ODS_ODS_USET__
#define __ODS_ODS_USET__
#include <cstdlib>
#include <optional>
namespace ods {
template<typename T>
class USet {
public:
virtual ~USet(void) {};
virtual std::size_t size(void) =0;
virtual bool add(T) = 0;
virtual std::optional<T> remove(T) = 0;
virtual std::optional<T> find(T) = 0;
};
} // namespace ods
#endif // __ODS_ODS_USET__