C++ stuff sorted out.

This commit is contained in:
Kyle Isom 2017-12-21 18:38:31 +00:00
parent 54be01d113
commit dad664f245
5 changed files with 121 additions and 30 deletions

1
.gitignore vendored
View File

@ -22,4 +22,5 @@ missing
# build artifacts
src/*.o
src/ch??ex??
src/*.la

View File

@ -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

View File

@ -1,8 +1,7 @@
#ifndef __ODS_LIST__
#define __ODS_LIST__
#ifndef __ODS_ODS_LIST__
#define __ODS_ODS_LIST__
#include <cstddef>
#include <cstdlib>
namespace ods {
@ -10,11 +9,12 @@ namespace ods {
template<typename T>
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

View File

@ -1,15 +1,18 @@
#ifndef __ODS_SIMPLIST__
#define __ODS_SIMPLIST__
#ifndef __ODS_ODS_SIMPLIST__
#define __ODS_ODS_SIMPLIST__
#include <ods/list.h>
#include <cstddef>
#include <cassert>
#include <cstdlib>
#include <stdexcept>
namespace ods {
template<typename T>
class SimpList {
class SimpList : List<T> {
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<typename T>
SimpList<T>::SimpList()
{
arr = new T[DEFAULT_SIZE];
cap = DEFAULT_SIZE;
len = 0;
}
template<typename T>
SimpList<T>::~SimpList()
{
delete this->arr;
this->cap = 0;
this->len = 0;
}
template<typename T>
std::size_t
SimpList<T>::size(void)
{
assert(len <= cap);
return this->len;
}
template <typename T>
T
SimpList<T>::get(std::size_t i)
{
if (i >= this->len) {
throw std::invalid_argument("index out of range");
}
return this->arr[i];
}
template <typename T>
T
SimpList<T>::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 <typename T>
void
SimpList<T>::add(std::size_t i, T value)
{
if (i >= this->len) {
throw std::invalid_argument("index out of range");
}
assert(value);
return;
}
template <typename T>
T
SimpList<T>::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

View File

@ -10,18 +10,18 @@ namespace ods {
template<typename T>
SimpList<T>::SimpList()
{
this->arr = new T[DEFAULT_SIZE];
this->cap = 0;
this->len = 0;
arr = new T[DEFAULT_SIZE];
cap = 0;
len = 0;
}
template <typename T>
template<typename T>
std::size_t
SimpList<T>::size(void)
{
assert(len <= cap);
return this->len();
return this->len;
}
@ -29,22 +29,31 @@ template <typename T>
T
SimpList<T>::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 <typename T>
T
SimpList<T>::set(std::size_t, T)
SimpList<T>::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 <typename T>
void
SimpList<T>::add(std::size_t, T)
SimpList<T>::add(std::size_t i, T value)
{
return;
}
@ -52,9 +61,13 @@ SimpList<T>::add(std::size_t, T)
template <typename T>
T
SimpList<T>::remove(std::size_t)
SimpList<T>::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