More chapter 2 work.

This commit is contained in:
Kyle Isom 2018-02-08 08:38:06 -08:00
parent acbb50d43c
commit e0d8dc0171
6 changed files with 81 additions and 28 deletions

View File

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

View File

@ -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
clean:
ninja -t clean

14
ods/src/array_test.cc Normal file
View File

@ -0,0 +1,14 @@
#include <iostream>
#include <ods/array.h>
using namespace std;
int
main(void)
{
Array<int> a(2);
Array<int> b(5);;
cout << "a[0] " << a[0] << endl;
b = a;
cout << "a[0] " << a[0] << endl;
}

View File

@ -8,6 +8,7 @@ targets:
ch01ex03: []
ch01ex04: []
ch01ex05: []
array_test: []
# disabled due to flakey C++17 support.
# ch01ex06: []

View File

@ -1,9 +1,42 @@
#ifndef __ODS_ODS_ARRAY__
#define __ODS_ODS_ARRAY__
#include <cstdint>
class array {
namespace ods {
template<typename T>
class Array {
public:
Array(int len) : length(len) { a = new T[length]; }
T& operator[](int i) {
assert(i >= 0 && static_cast<std::size_t>(i) < length);
return a[i];
}
Array<T>& operator=(Array<T> &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__
} // namespace ods
#endif // __ODS_ODS_ARRAY__

18
ods/src/ods/array_stack.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef __ODS_ODS_ARRAY_STACK__
#define __ODS_ODS_ARRAY_STACK__
namespace ods {
template<typename T>
class ArrayStack {
int size(void) { return static_cast<int>(this->a->size()); }
private:
Array<T> a;
int n;
}
} // namespace ods
#endif // __ODS_ODS_ARRAY_STACK__