diff --git a/.gitignore b/.gitignore index c1bf33e..70a8511 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,41 @@ *.fasl *.pyc *.o +*.pdf +.d # ignore build systems .ninja_deps .ninja_log *.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 diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..11ba3e7 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "cppcheck.includePaths": ["${workspaceRoot}/ods/src"] +} \ No newline at end of file diff --git a/ods/.gitignore b/ods/.gitignore deleted file mode 100644 index c61ba60..0000000 --- a/ods/.gitignore +++ /dev/null @@ -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 diff --git a/ods/data/Makefile.am b/ods/data/Makefile.am index dbd6f14..e324dbe 100644 --- a/ods/data/Makefile.am +++ b/ods/data/Makefile.am @@ -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 gzip -d -k $@.gz @@ -10,3 +10,9 @@ ODS_SAMPLE_CODE_URL := http://opendatastructures.org/ods-cpp.tgz ods-cpp.tgz: 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 + +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 + diff --git a/ods/src/array_test.cc b/ods/src/array_test.cc index 54db1c5..ef3c8c8 100644 --- a/ods/src/array_test.cc +++ b/ods/src/array_test.cc @@ -1,6 +1,8 @@ #include #include +#include using namespace std; +using namespace ods; int main(void) @@ -10,5 +12,15 @@ main(void) cout << "a[0] " << a[0] << endl; b = a; - cout << "a[0] " << a[0] << endl; -} \ No newline at end of file + cout << "b[0] " << b[0] << endl; + + ArrayStack 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; +} diff --git a/ods/src/ods/array.h b/ods/src/ods/array.h index fe1a55b..dd56ecc 100644 --- a/ods/src/ods/array.h +++ b/ods/src/ods/array.h @@ -1,6 +1,7 @@ #ifndef __ODS_ODS_ARRAY__ #define __ODS_ODS_ARRAY__ +#include #include namespace ods { @@ -8,35 +9,34 @@ namespace ods { template class Array { 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) { - assert(i >= 0 && static_cast(i) < length); + assert(i >= 0 && i < length); return a[i]; } + Array& operator=(Array &rhs) { if (a != nullptr) { - delete a; + 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; + + // I put this in to prevent segfaults. + rhs.length = 0; return *this; } - - std::size_t size(void) { return length; } -private: - T *a; - std::size_t length; }; } // namespace ods -#endif // __ODS_ODS_ARRAY__ \ No newline at end of file +#endif // __ODS_ODS_ARRAY__ diff --git a/ods/src/ods/array_stack.h b/ods/src/ods/array_stack.h index 0b4931b..952a0c5 100644 --- a/ods/src/ods/array_stack.h +++ b/ods/src/ods/array_stack.h @@ -1,18 +1,58 @@ #ifndef __ODS_ODS_ARRAY_STACK__ #define __ODS_ODS_ARRAY_STACK__ +#include +#include namespace ods { template class ArrayStack { - int size(void) { return static_cast(this->a->size()); } +public: + ArrayStack(int len) : n(0), a(Array(len)) {} + int size(void) { return a.length; } + 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: - Array a; int n; -} + Array a; +}; } // namespace ods -#endif // __ODS_ODS_ARRAY_STACK__ \ No newline at end of file +#endif // __ODS_ODS_ARRAY_STACK__