From 9bba1bcbd12ddd13b6752995a69095c8e7d94d36 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Tue, 19 Dec 2017 21:53:51 +0000 Subject: [PATCH] Automake the data files. --- .gitignore | 17 ++++++++++++++--- Makefile.am | 2 +- configure.ac | 2 +- data/Makefile.am | 12 ++++++++++++ notes/chapter1.txt | 6 +++++- src/Makefile.am | 5 +++-- src/ch01ex04.cc | 43 +++++++++++++++++++++++++++++++++++++++++++ src/ch01ex05.cc | 15 +++++++++++++++ 8 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 data/Makefile.am create mode 100644 src/ch01ex04.cc create mode 100644 src/ch01ex05.cc diff --git a/.gitignore b/.gitignore index cafb952..95677fd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,14 +1,25 @@ +# data files data/corpus.txt +data/cpp +data/ods-cpp.tgz + +# autotools junk files +.deps +Makefile Makefile.in aclocal.m4 autom4te.cache/ compile -config.guess -config.sub +config.* configure depcomp install-sh +libtool ltmain.sh m4/ missing -src/Makefile.in + +# build artifacts +src/*.o +src/ch??ex?? + diff --git a/Makefile.am b/Makefile.am index 7b571e6..11c2cc1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,2 +1,2 @@ ACLOCAL_AMFLAGS = -I m4 -SUBDIRS = src \ No newline at end of file +SUBDIRS = src data diff --git a/configure.ac b/configure.ac index fb3e1a7..1888319 100644 --- a/configure.ac +++ b/configure.ac @@ -9,7 +9,7 @@ AC_INIT([ods], AM_INIT_AUTOMAKE([1.11 foreign]) AC_CONFIG_SRCDIR([src/ch01ex01.cc]) -AC_CONFIG_FILES([Makefile src/Makefile]) +AC_CONFIG_FILES([Makefile src/Makefile data/Makefile]) AC_CONFIG_MACRO_DIR([m4]) PKG_PROG_PKG_CONFIG diff --git a/data/Makefile.am b/data/Makefile.am new file mode 100644 index 0000000..fe964cc --- /dev/null +++ b/data/Makefile.am @@ -0,0 +1,12 @@ +pkgdata_DATA = corpus.txt ods-cpp/main.cpp + +corpus.txt: corpus.txt.gz + gzip -d -k $@.gz + +ods-cpp/main.cpp: ods-cpp.tgz + tar xzf ods-cpp.tgz + +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 diff --git a/notes/chapter1.txt b/notes/chapter1.txt index 4e26dfd..6285997 100644 --- a/notes/chapter1.txt +++ b/notes/chapter1.txt @@ -132,6 +132,10 @@ Run times come in three flavours: and pop() operations. Show how, using only a FIFO Queue, q, you can reverse the order of all elements in s. + See src/ch01ex04.cc: you just pop each element from the stack → queue, + then pop the elements off the queue. If you wanted to reverse the stack + itself, you'd just push the elements back onto the stack. + 5. 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 @@ -148,4 +152,4 @@ Run times come in three flavours: remove(i) in your List implementation. Think about how you could improve the performance of the find(x) operation in your USet and SSet implementations. This exercise is designed to give you a feel for how - difficult it can be to obtain efficient implementations of these interfaces \ No newline at end of file + difficult it can be to obtain efficient implementations of these interfaces diff --git a/src/Makefile.am b/src/Makefile.am index 63b2e69..1215fc1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,6 +3,7 @@ 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 += -fno-elide-constructors -Weffc++ -bin_PROGRAMS := ch01ex01 ch01ex03 +bin_PROGRAMS := ch01ex01 ch01ex03 ch01ex04 ch01ex01_SOURCES := ch01ex01.cc -ch01ex03_SOURCES := ch01ex03.cc \ No newline at end of file +ch01ex03_SOURCES := ch01ex03.cc +ch01ex04_SOURCES := ch01ex04.cc diff --git a/src/ch01ex04.cc b/src/ch01ex04.cc new file mode 100644 index 0000000..8015c53 --- /dev/null +++ b/src/ch01ex04.cc @@ -0,0 +1,43 @@ +#include +#include +#include +#include +using namespace std; + + + +static void +print_queue(queue& q) +{ + while (!q.empty()) { + cout << q.front() << " "; + q.pop(); + } + cout << endl; +} + + +int +main(int argc, char *argv[]) +{ + stack s; + queue q; + + for (int i = 1; i < argc; i++) { + string line(argv[i]); + int arg = stoi(line); + + s.push(arg); + } + + while (!s.empty()) { + // NB: the pop() interface in the book returns the topmost + // element while removing it; the C++ STL interface does + // not, requiring the separate top() and pop() invocations. + int arg = s.top(); + s.pop(); + q.push(arg); + } + + print_queue(q); +} diff --git a/src/ch01ex05.cc b/src/ch01ex05.cc new file mode 100644 index 0000000..0339421 --- /dev/null +++ b/src/ch01ex05.cc @@ -0,0 +1,15 @@ +#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. + +template +class Bag { +public: + +private: + vector v; +};