More stuff on chapter 2's array stack.

This commit is contained in:
Kyle Isom 2018-02-08 15:05:19 -08:00
parent e0d8dc0171
commit 707d033d9f
7 changed files with 114 additions and 46 deletions

33
.gitignore vendored
View File

@ -2,8 +2,41 @@
*.fasl *.fasl
*.pyc *.pyc
*.o *.o
*.pdf
.d
# ignore build systems # ignore build systems
.ninja_deps .ninja_deps
.ninja_log .ninja_log
*.ninja *.ninja
# editor crap
.vscode/*
!.vscode/settings.json
# data files
ods/data/corpus.txt
ods/data/cpp
ods/data/ods-cpp.tgz
ods/data/ods-cpp.pdf
# autotools junk files
.deps
Makefile
Makefile.in
aclocal.m4
autom4te.cache/
compile
config.*
configure
depcomp
install-sh
libtool
ltmain.sh
m4/
missing
ch??ex??
*_test
*.la
*_bench

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"cppcheck.includePaths": ["${workspaceRoot}/ods/src"]
}

26
ods/.gitignore vendored
View File

@ -1,26 +0,0 @@
# data files
data/corpus.txt
data/cpp
data/ods-cpp.tgz
# autotools junk files
.deps
Makefile
Makefile.in
aclocal.m4
autom4te.cache/
compile
config.*
configure
depcomp
install-sh
libtool
ltmain.sh
m4/
missing
# build artifacts
src/*.o
src/ch??ex??
src/*.la
src/*_bench

View File

@ -1,4 +1,4 @@
pkgdata_DATA = corpus.txt ods-cpp/main.cpp pkgdata_DATA = corpus.txt ods-cpp/main.cpp ods-cpp.pdf
corpus.txt: corpus.txt.gz corpus.txt: corpus.txt.gz
gzip -d -k $@.gz gzip -d -k $@.gz
@ -10,3 +10,9 @@ ODS_SAMPLE_CODE_URL := http://opendatastructures.org/ods-cpp.tgz
ods-cpp.tgz: ods-cpp.tgz:
if command -v curl 2>&1 > /dev/null ; then curl -L -O $(ODS_SAMPLE_CODE_URL) ; \ if command -v curl 2>&1 > /dev/null ; then curl -L -O $(ODS_SAMPLE_CODE_URL) ; \
elif command -v curl 2>&1 > /dev/null ; then wget $(ODS_SAMPLE_CODE_URL) ; fi elif command -v curl 2>&1 > /dev/null ; then wget $(ODS_SAMPLE_CODE_URL) ; fi
ODS_PDF_URL := http://opendatastructures.org/ods-cpp.pdf
ods-cpp.pdf:
if command -v curl 2>&1 > /dev/null ; then curl -L -O $(ODS_PDF_URL) ; \
elif command -v curl 2>&1 > /dev/null ; then wget $(ODS_PDF_URL) ; fi

View File

@ -1,6 +1,8 @@
#include <iostream> #include <iostream>
#include <ods/array.h> #include <ods/array.h>
#include <ods/array_stack.h>
using namespace std; using namespace std;
using namespace ods;
int int
main(void) main(void)
@ -10,5 +12,15 @@ main(void)
cout << "a[0] " << a[0] << endl; cout << "a[0] " << a[0] << endl;
b = a; b = a;
cout << "a[0] " << a[0] << endl; cout << "b[0] " << b[0] << endl;
ArrayStack<int> as(5);
for (int i = 0; i < 3; i++) {
as.set(i, i+1);
}
as.add(1, 42);
cout << "as[0] " << as.get(0) << endl;
cout << "as[1] " << as.get(1) << endl;
as.remove(0);
cout << "as[0] " << as.get(0) << endl;
} }

View File

@ -1,6 +1,7 @@
#ifndef __ODS_ODS_ARRAY__ #ifndef __ODS_ODS_ARRAY__
#define __ODS_ODS_ARRAY__ #define __ODS_ODS_ARRAY__
#include <cassert>
#include <cstdint> #include <cstdint>
namespace ods { namespace ods {
@ -8,14 +9,19 @@ namespace ods {
template<typename T> template<typename T>
class Array { class Array {
public: public:
Array(int len) : length(len) { a = new T[length]; } T *a;
int length;
Array(int len) : a(new T[len]), length(len) {}
T& operator[](int i) { T& operator[](int i) {
assert(i >= 0 && static_cast<std::size_t>(i) < length); assert(i >= 0 && i < length);
return a[i]; return a[i];
} }
Array<T>& operator=(Array<T> &rhs) { Array<T>& operator=(Array<T> &rhs) {
if (a != nullptr) { if (a != nullptr) {
delete a; delete[] a;
} }
a = rhs.a; a = rhs.a;
@ -23,18 +29,12 @@ public:
// I don't understand why this is done, but it's how the book defines it. // I don't understand why this is done, but it's how the book defines it.
rhs.a = nullptr; rhs.a = nullptr;
length = rhs.length;
// I put this in to prevent segfaults. // I put this in to prevent segfaults.
rhs.length = 0; rhs.length = 0;
length = rhs.length;
return *this; return *this;
} }
std::size_t size(void) { return length; }
private:
T *a;
std::size_t length;
}; };
} // namespace ods } // namespace ods

View File

@ -1,18 +1,58 @@
#ifndef __ODS_ODS_ARRAY_STACK__ #ifndef __ODS_ODS_ARRAY_STACK__
#define __ODS_ODS_ARRAY_STACK__ #define __ODS_ODS_ARRAY_STACK__
#include <iostream>
#include <ods/array.h>
namespace ods { namespace ods {
template<typename T> template<typename T>
class ArrayStack { class ArrayStack {
int size(void) { return static_cast<int>(this->a->size()); } public:
private: ArrayStack(int len) : n(0), a(Array<T>(len)) {}
Array<T> a; int size(void) { return a.length; }
int n; T get(int i) { return a[i]; }
T set(int i, T x) {
T y = a[i];
a[i] = x;
return y;
} }
void add(int i, T x) {
if (n+1 > a.length) {
resize();
}
for (int j = n; j > i; j--) {
a[j] = a[j-1];
}
a[i] = x;
n++;
}
T remove(int i) {
T x = a[i];
for (int j = i; j < (n-1); j++) {
std::cout << j << "\t" << i << "\t" << a[j] << std::endl;
a[j] = a[j+1];
}
n--;
if (a.length > 3*n) {
resize();
}
return x;
}
void resize(void) {}
private:
int n;
Array<T> a;
};
} // namespace ods } // namespace ods
#endif // __ODS_ODS_ARRAY_STACK__ #endif // __ODS_ODS_ARRAY_STACK__