Finish ArrayStack.

This commit is contained in:
Kyle Isom 2018-02-20 10:09:12 -08:00
parent 707d033d9f
commit 214142583b
4 changed files with 45 additions and 22 deletions

View File

@ -1,18 +0,0 @@
pkgdata_DATA = corpus.txt ods-cpp/main.cpp ods-cpp.pdf
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
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

@ -19,3 +19,15 @@ resizing isn't too bad.
* 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.
* Add and remove requires shifting all the elements after i (O(n - i)
complexity), ignoring potential calls to resize
* Resizing is triggered when we run out of room in an add or when a remove
brings us to the point where the array is more than 3n elements
* Resizing creates a new array of size 2n and copies all the elements over;
this then has complexity O(n).
* The analysis of add and remove didn't consider cost of resize.
* An amortised analysis is done instead that considers the cost of all calls
to add and remove, given a sequence of *m* calls to either.
* Lemma: if an empty ArrayStack is created and any sequence of *m* >= 1 calls
to add and remove are performed, the total time spent in calls to resize is
O(m).

View File

@ -23,4 +23,11 @@ main(void)
cout << "as[1] " << as.get(1) << endl;
as.remove(0);
cout << "as[0] " << as.get(0) << endl;
cout << "size: " << as.size() << ", cap: " << as.cap() << endl;
as.add(0, 47);
as.add(0, 11);
as.add(0, 17);
as.add(0, 1);
cout << "size: " << as.size() << ", cap: " << as.cap() << endl;
}

View File

@ -11,9 +11,13 @@ template<typename T>
class ArrayStack {
public:
ArrayStack(int len) : n(0), a(Array<T>(len)) {}
int size(void) { return a.length; }
int size(void) { return n; }
int cap(void) { return a.length; }
T get(int i) { return a[i]; }
T set(int i, T x) {
T set(int i, T x) {
if (i > n) {
n = i+1;
}
T y = a[i];
a[i] = x;
return y;
@ -35,7 +39,6 @@ public:
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--;
@ -47,7 +50,26 @@ public:
return x;
}
void resize(void) {}
static inline int max(int x, int y) {
return x > y ? x : y;
}
void resize(void) {
int nlen = max(2 * n, 1);
Array<T> b(nlen);
for (int i = 0; i < n; i++) {
b[i] = a[i];
}
a = b;
}
T pop(void) {
return a.remove(n - 1);
}
void push(T x) {
return a.add(n, x);
}
private:
int n;
Array<T> a;