From e0d8dc01719a5645a7114ee495976d7c1e82ed7d Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 8 Feb 2018 08:38:06 -0800 Subject: [PATCH] More chapter 2 work. --- ods/notes/chapter2.txt | 8 +++++++- ods/src/Makefile.am | 31 ++++++------------------------- ods/src/array_test.cc | 14 ++++++++++++++ ods/src/build.yaml | 1 + ods/src/ods/array.h | 37 +++++++++++++++++++++++++++++++++++-- ods/src/ods/array_stack.h | 18 ++++++++++++++++++ 6 files changed, 81 insertions(+), 28 deletions(-) create mode 100644 ods/src/array_test.cc create mode 100644 ods/src/ods/array_stack.h diff --git a/ods/notes/chapter2.txt b/ods/notes/chapter2.txt index f996518..9ef8889 100644 --- a/ods/notes/chapter2.txt +++ b/ods/notes/chapter2.txt @@ -12,4 +12,10 @@ These data structures have common advantages and limitations: array requires shifting all the following elements. With some careful management, the additional *amortised* complexity added by -resizing isn't too bad. \ No newline at end of file +resizing isn't too bad. + +## Array stack + +* Uses backing array *a*. +* Typically, the array will be larger than necessary, so an element *n* is + used to track the actual number of elements stored in the stack. diff --git a/ods/src/Makefile.am b/ods/src/Makefile.am index b1f2881..0a6e850 100644 --- a/ods/src/Makefile.am +++ b/ods/src/Makefile.am @@ -1,27 +1,8 @@ -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++17 -D_XOPEN_SOURCE -O0 -g -I. -AM_CPPFLAGS += -fno-elide-constructors -Weffc++ -fPIC +all: build.ninja + ninja -bin_PROGRAMS := ch01ex01 ch01ex03 ch01ex04 ch01ex05 ch01ex06 \ - list_bench uset_bench sset_bench -ch01ex01_SOURCES := ch01ex01.cc -ch01ex03_SOURCES := ch01ex03.cc -ch01ex04_SOURCES := ch01ex04.cc -ch01ex05_SOURCES := ch01ex05.cc -ch01ex06_SOURCES := ch01ex06.cc -list_bench_SOURCES := list_bench.cc -uset_bench_SOURCES := uset_bench.cc -sset_bench_SOURCES := sset_bench.cc +build.ninja: build.yaml + ng -f build.yaml $@ -BENCH_OPS ?= 1000000 - -benchmarks: list_bench uset_bench sset_bench - @echo "LIST BENCHMARKS" - @./list_bench $(BENCH_OPS) > /dev/null - @echo - @echo "USET BENCHMARKS" - @./uset_bench $(BENCH_OPS) > /dev/null - @echo - @echo "SSET BENCHMARKS" - @./sset_bench $(BENCH_OPS) > /dev/null \ No newline at end of file +clean: + ninja -t clean diff --git a/ods/src/array_test.cc b/ods/src/array_test.cc new file mode 100644 index 0000000..54db1c5 --- /dev/null +++ b/ods/src/array_test.cc @@ -0,0 +1,14 @@ +#include +#include +using namespace std; + +int +main(void) +{ + Array a(2); + Array b(5);; + cout << "a[0] " << a[0] << endl; + + b = a; + cout << "a[0] " << a[0] << endl; +} \ No newline at end of file diff --git a/ods/src/build.yaml b/ods/src/build.yaml index 89b0527..6b6abd1 100644 --- a/ods/src/build.yaml +++ b/ods/src/build.yaml @@ -8,6 +8,7 @@ targets: ch01ex03: [] ch01ex04: [] ch01ex05: [] + array_test: [] # disabled due to flakey C++17 support. # ch01ex06: [] diff --git a/ods/src/ods/array.h b/ods/src/ods/array.h index f1cb844..fe1a55b 100644 --- a/ods/src/ods/array.h +++ b/ods/src/ods/array.h @@ -1,9 +1,42 @@ #ifndef __ODS_ODS_ARRAY__ #define __ODS_ODS_ARRAY__ +#include -class array { +namespace ods { + +template +class Array { +public: + Array(int len) : length(len) { a = new T[length]; } + T& operator[](int i) { + assert(i >= 0 && static_cast(i) < length); + return a[i]; + } + Array& operator=(Array &rhs) { + if (a != nullptr) { + delete a; + } + + a = rhs.a; + + // I don't understand why this is done, but it's how the book defines it. + rhs.a = nullptr; + + // I put this in to prevent segfaults. + rhs.length = 0; + + + length = rhs.length; + return *this; + } + std::size_t size(void) { return length; } +private: + T *a; + std::size_t length; }; -#endif __ODS_ODS_ARRAY__ \ No newline at end of file +} // namespace ods + +#endif // __ODS_ODS_ARRAY__ \ No newline at end of file diff --git a/ods/src/ods/array_stack.h b/ods/src/ods/array_stack.h new file mode 100644 index 0000000..0b4931b --- /dev/null +++ b/ods/src/ods/array_stack.h @@ -0,0 +1,18 @@ +#ifndef __ODS_ODS_ARRAY_STACK__ +#define __ODS_ODS_ARRAY_STACK__ + + +namespace ods { + + +template +class ArrayStack { + int size(void) { return static_cast(this->a->size()); } +private: + Array a; + int n; +} + +} // namespace ods + +#endif // __ODS_ODS_ARRAY_STACK__ \ No newline at end of file