From f1220734a4378451c2e86f76ad904533b7bb5674 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Tue, 19 Dec 2017 23:07:51 +0000 Subject: [PATCH] NB broken. Working on the list implementation. --- include/list.h | 22 +++++++++++---------- include/simplist.h | 24 +++++++++++++++++++++++ notes/chapter1.txt | 2 ++ src/Makefile.am | 6 ++++-- src/ch01ex05.cc | 27 ++++++++++++++++---------- src/ch01ex06.cc | 22 +++++++++++++++++++++ src/simplist.cc | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 129 insertions(+), 22 deletions(-) create mode 100644 include/simplist.h create mode 100644 src/ch01ex06.cc create mode 100644 src/simplist.cc diff --git a/include/list.h b/include/list.h index 2b7efe2..fd77522 100644 --- a/include/list.h +++ b/include/list.h @@ -4,17 +4,19 @@ #include +namespace ods { // Lists are sequences of values. -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); -}; + 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); + }; +} // end namespace ods -#endif \ No newline at end of file +#endif diff --git a/include/simplist.h b/include/simplist.h new file mode 100644 index 0000000..70b1472 --- /dev/null +++ b/include/simplist.h @@ -0,0 +1,24 @@ +#ifndef __ODS_SIMPLIST__ +#define __ODS_SIMPLIST__ + +#include "list.h" + +namespace ods { + + template + class SimpList : public List { + public: + SimpList(); + std::size_t size(void); + T get(std::size_t); + T set(std::size_t, T); + void add(std::size_t, T); + T remove(std::size_t); + private: + T arr[]; + std::size_t cap; + std::size_t len; + }; + +} // end namespace ods +#endif diff --git a/notes/chapter1.txt b/notes/chapter1.txt index 6285997..de987a5 100644 --- a/notes/chapter1.txt +++ b/notes/chapter1.txt @@ -142,6 +142,8 @@ Run times come in three flavours: is equal to x. In addition, a Bag supports the findAll(x) operation that returns a list of all elements in the Bag that are equal to x. + In progress: src/ch01ex05.cc. + 6. From scratch, write and test implementations of the List, USet and SSet interfaces. These do not have to be efficient. They can be used later to test the correctness and performance of more efficient implementations. diff --git a/src/Makefile.am b/src/Makefile.am index 1215fc1..e0da8fc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,9 +1,11 @@ AM_CPPFLAGS = -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align 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 += -Wunused-variable -std=c++14 -D_XOPEN_SOURCE -O0 -g -I. -I../include AM_CPPFLAGS += -fno-elide-constructors -Weffc++ -bin_PROGRAMS := ch01ex01 ch01ex03 ch01ex04 +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 := simplist.cc ch01ex06.cc diff --git a/src/ch01ex05.cc b/src/ch01ex05.cc index 0339421..ebe585a 100644 --- a/src/ch01ex05.cc +++ b/src/ch01ex05.cc @@ -1,15 +1,22 @@ #include #include -// Using a USet, implement a Bag. A Bag is like a USet supports the add(x), -// remove(x), and find (x) methods — but it allows duplicate -// elements to be stored. The find(x) operation in a Bag returns some element (if any) that is equal to x. In addition, -// a Bag supports the findAll(x) operation that returns a list of all elements in the Bag that are equal to x. +// Using a USet, implement a Bag. A Bag is like a USet — it supports the add(x), +// remove(x), and find (x) methods — but it allows duplicate elements to be +// stored. The find(x) operation in a Bag returns some element (if any) that is +// equal to x. In addition, a Bag supports the findAll(x) operation that returns +// a list of all elements in the Bag that are equal to x. -template -class Bag { -public: +// template +// class Bag { +// public: +// +// private: +// vector v; +// }; -private: - vector v; -}; +int +main(void) +{ + return 0; +} diff --git a/src/ch01ex06.cc b/src/ch01ex06.cc new file mode 100644 index 0000000..ec19e5f --- /dev/null +++ b/src/ch01ex06.cc @@ -0,0 +1,22 @@ +#include +#include +using namespace std; + + +static void +check_simplist(void) +{ + ods::SimpList sl; + + sl.add(0, 1); + sl.add(1, 2); + sl.add(2, 3); + assert(sl.size() == 3); +} + + +int +main(void) +{ + check_simplist(); +} diff --git a/src/simplist.cc b/src/simplist.cc new file mode 100644 index 0000000..988fd92 --- /dev/null +++ b/src/simplist.cc @@ -0,0 +1,48 @@ +#include "simplist.h" +#include +#include +#include + +constexpr std::size_t DEFAULT_SIZE = 8; + +namespace ods { + + SimpList::SimpList() + { + this->arr = new T[DEFAULT_SIZE]; + this->cap = 0; + this->len = 0; + } + + + std::size_t + SimpList size(void) + { + std::assert(len <= cap); + return this->len(); + } + + T + SimpList::get(std::size_t i) + { + throw std::invalid_argument("invalid argument"); + } + + T + SimpList::set(std::size_t, T) + { + throw std::invalid_argument("invalid argument"); + } + + void + SimpList::add(std::size_t, T) + { + return; + } + + T + SimpList::remove(std::size_t) + { + throw std::invalid_argument("invalid argument"); + } +};