From dad664f2459b9851ed3773a3e275f981390e7f29 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 21 Dec 2017 18:38:31 +0000 Subject: [PATCH] C++ stuff sorted out. --- .gitignore | 1 + src/Makefile.am | 5 --- src/ods/list.h | 18 +++++----- src/ods/simplist.h | 90 +++++++++++++++++++++++++++++++++++++++++++--- src/simplist.cc | 37 ++++++++++++------- 5 files changed, 121 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 95677fd..b10102f 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,5 @@ missing # build artifacts src/*.o src/ch??ex?? +src/*.la diff --git a/src/Makefile.am b/src/Makefile.am index 25f63aa..b9b101b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,14 +3,9 @@ AM_CPPFLAGS += -Wwrite-strings -Wmissing-declarations -Wno-long-long -Werror AM_CPPFLAGS += -Wunused-variable -std=c++14 -D_XOPEN_SOURCE -O0 -g -I. AM_CPPFLAGS += -fno-elide-constructors -Weffc++ -lib_LTLIBRARIES := libods.la -libods_la_SOURCES := simplist.cc -libods_la_CPPFLAGS := $(AM_CPPFLAGS) - bin_PROGRAMS := ch01ex01 ch01ex03 ch01ex04 ch01ex05 ch01ex06 ch01ex01_SOURCES := ch01ex01.cc ch01ex03_SOURCES := ch01ex03.cc ch01ex04_SOURCES := ch01ex04.cc ch01ex05_SOURCES := ch01ex05.cc ch01ex06_SOURCES := ch01ex06.cc -ch01ex06_LDADD := .libs/libods.a \ No newline at end of file diff --git a/src/ods/list.h b/src/ods/list.h index 20f6e37..6fc6781 100644 --- a/src/ods/list.h +++ b/src/ods/list.h @@ -1,8 +1,7 @@ -#ifndef __ODS_LIST__ -#define __ODS_LIST__ +#ifndef __ODS_ODS_LIST__ +#define __ODS_ODS_LIST__ - -#include +#include namespace ods { @@ -10,11 +9,12 @@ namespace ods { template class List { public: - virtual std::size_t size(void); - virtual T get(std::size_t); - virtual T set(std::size_t, T); - virtual void add(std::size_t, T); - virtual T remove(std::size_t); + virtual ~List(void) {}; + virtual std::size_t size(void) =0; + virtual T get(std::size_t) =0; + virtual T set(std::size_t, T) =0; + virtual void add(std::size_t, T) =0; + virtual T remove(std::size_t) =0; }; } // end namespace ods diff --git a/src/ods/simplist.h b/src/ods/simplist.h index 02fc930..fa573d3 100644 --- a/src/ods/simplist.h +++ b/src/ods/simplist.h @@ -1,15 +1,18 @@ -#ifndef __ODS_SIMPLIST__ -#define __ODS_SIMPLIST__ +#ifndef __ODS_ODS_SIMPLIST__ +#define __ODS_ODS_SIMPLIST__ #include -#include +#include +#include +#include namespace ods { template -class SimpList { +class SimpList : List { public: SimpList(); + ~SimpList(void); std::size_t size(void); T get(std::size_t); T set(std::size_t, T); @@ -19,7 +22,86 @@ private: T *arr; std::size_t cap; std::size_t len; + + const std::size_t DEFAULT_SIZE = 8; }; +template +SimpList::SimpList() +{ + arr = new T[DEFAULT_SIZE]; + cap = DEFAULT_SIZE; + len = 0; +} + +template +SimpList::~SimpList() +{ + delete this->arr; + this->cap = 0; + this->len = 0; +} + + +template +std::size_t +SimpList::size(void) +{ + assert(len <= cap); + return this->len; +} + + +template +T +SimpList::get(std::size_t i) +{ + if (i >= this->len) { + throw std::invalid_argument("index out of range"); + } + + return this->arr[i]; +} + + +template +T +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; +} + + +template +void +SimpList::add(std::size_t i, T value) +{ + if (i >= this->len) { + throw std::invalid_argument("index out of range"); + } + assert(value); + + return; +} + + +template +T +SimpList::remove(std::size_t i) +{ + if (i >= this->len) { + throw std::invalid_argument("index out of range"); + } + + return this->arr[i]; +} + } // end namespace ods + #endif diff --git a/src/simplist.cc b/src/simplist.cc index 905eeea..ff92981 100644 --- a/src/simplist.cc +++ b/src/simplist.cc @@ -10,18 +10,18 @@ namespace ods { template SimpList::SimpList() { - this->arr = new T[DEFAULT_SIZE]; - this->cap = 0; - this->len = 0; + arr = new T[DEFAULT_SIZE]; + cap = 0; + len = 0; } -template +template std::size_t SimpList::size(void) { assert(len <= cap); - return this->len(); + return this->len; } @@ -29,22 +29,31 @@ template T SimpList::get(std::size_t i) { - i = 0; - throw std::invalid_argument("invalid argument"); + if (i >= this->len) { + throw std::invalid_argument("index out of range"); + } + + return this->arr[i]; } template T -SimpList::set(std::size_t, T) +SimpList::set(std::size_t i, T value) { - throw std::invalid_argument("invalid argument"); + 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; } template void -SimpList::add(std::size_t, T) +SimpList::add(std::size_t i, T value) { return; } @@ -52,9 +61,13 @@ SimpList::add(std::size_t, T) template T -SimpList::remove(std::size_t) +SimpList::remove(std::size_t i) { - throw std::invalid_argument("invalid argument"); + if (i >= this->len) { + throw std::invalid_argument("index out of range"); + } + + return this->arr[i]; } } // end namespace ods