From 6311eaf3785af4df9b983563299e3c2a26ffb0e7 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Wed, 21 Feb 2018 08:05:47 -0800 Subject: [PATCH] Start working on ArrayDeque. --- ods/src/array_test.cc | 35 +++++++++++++++++- ods/src/ods/array_deque.h | 75 +++++++++++++++++++++++++++++++++++++++ ods/src/ods/array_queue.h | 59 ++++++++++++++++++++++++++++++ ods/src/ods/array_stack.h | 1 - 4 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 ods/src/ods/array_deque.h create mode 100644 ods/src/ods/array_queue.h diff --git a/ods/src/array_test.cc b/ods/src/array_test.cc index aaefb9c..a184dbf 100644 --- a/ods/src/array_test.cc +++ b/ods/src/array_test.cc @@ -1,6 +1,8 @@ #include #include #include +#include +#include using namespace std; using namespace ods; @@ -8,12 +10,15 @@ int main(void) { Array a(2); - Array b(5);; + Array b(5); + + cout << "=== Array ===" << endl; cout << "a[0] " << a[0] << endl; b = a; cout << "b[0] " << b[0] << endl; + cout << "=== ArrayStack ===" << endl; ArrayStack as(5); for (int i = 0; i < 3; i++) { as.set(i, i+1); @@ -30,4 +35,32 @@ main(void) as.add(0, 17); as.add(0, 1); cout << "size: " << as.size() << ", cap: " << as.cap() << endl; + + cout << "=== ArrayQueue ===" << endl; + ArrayQueue aq(5); + cout << "size: " << aq.size() << ", cap: " << aq.cap() << endl; + for (int i = 0; i < 100; i++) { + aq.add(i); + } + cout << "size: " << aq.size() << ", cap: " << aq.cap() << endl; + for (int i = 0; i < 100; i++) { + aq.remove(); + } + cout << "size: " << aq.size() << ", cap: " << aq.cap() << endl; + + cout << "=== ArrayDeque ===" << endl; + ArrayDeque ad(1); + for (int i = 0; i < 5; i++) { + ad.add(0, i); + } + cout << "size: " << ad.size() << ", cap: " << ad.cap() << endl; + + for (int i = 0; i < 5; i++) { + ad.add(ad.size() - 1, i); + } + cout << "size: " << ad.size() << ", cap: " << ad.cap() << endl; + + for (int i = 0; i < ad.size(); i++) { + cout << i << "\t" << ad.get(i) << endl; + } } diff --git a/ods/src/ods/array_deque.h b/ods/src/ods/array_deque.h new file mode 100644 index 0000000..d4830fc --- /dev/null +++ b/ods/src/ods/array_deque.h @@ -0,0 +1,75 @@ +#ifndef __ODS_ODS_ARRAY_DEQUE_ +#define __ODS_ODS_ARRAY_DEQUE_ + +#include + +namespace ods { + + +template +class ArrayDeque { +public: + ArrayDeque(int nlen) : a(Array(nlen)), j(0), n(0) {} + int size(void) { return n; } + int cap(void) { return a.length; } + + static inline int max(int x, int y) { + return x > y ? x : y; + } + + inline int index(int i) { + auto v = (j+i) % a.length; + return v; + } + + T get(int i) { + return a[index(i)]; + } + + T set(int i, T x) { + T y = a[index(i)]; + a[index(i)] = x; + return y; + } + + void add(int i, T x) { + if (n + 1 > a.length) { + resize(); + } + + // if i is in the leftmost half, shift left. + if (i < n/2) { + j = (j == 0) ? a.length - 1 : j - 1; + for (int k = 0; k <= i-1; k++) { + a[index(k)] = a[index(k+1)]; + } + } + // otherwise, shift right. + else { + for (int k = n; k > i; k--) { + a[index(k)] = a[index(k)-1]; + } + } + + a[index(i)] = x; + n++; + } + + void resize(void) { + Array b(max(2 * n, 1)); + for (int k = 0; k < n; k++) { + b[k] = a[index(k)]; + } + + j = 0; + a = b; + } +private: + Array a; + int j; + int n; +}; + +} // namespace ods + +#endif // __ODS_ODS_ARRAY_DEQUE_ \ No newline at end of file diff --git a/ods/src/ods/array_queue.h b/ods/src/ods/array_queue.h new file mode 100644 index 0000000..cd3b4fb --- /dev/null +++ b/ods/src/ods/array_queue.h @@ -0,0 +1,59 @@ +#ifndef __ODS_ODS_ARRAY_QUEUE__ +#define __ODS_ODS_ARRAY_QUEUE__ + +#include + +namespace ods { + + +template +class ArrayQueue { +public: + ArrayQueue(int nlen) : a(Array(nlen)), j(0), n(0) {} + int size(void) { return n; } + int cap(void) { return a.length; } + + static inline int max(int x, int y) { + return x > y ? x : y; + } + + bool add(T x) { + if (n + 1 > a.length) { + resize(); + } + + a[(j+n) % a.length] = x; + n++; + return true; + } + + T remove() { + T x = a[j]; + j = (j+1) % a.length; + n--; + + if (a.length >= 3*n) { + resize(); + } + + return x; + } + + void resize(void) { + Array b(max(2 * n, 1)); + for (int k = 0; k < n; k++) { + b[k] = a[(j+k) % a.length]; + } + + j = 0; + a = b; + } +private: + Array a; + int j; + int n; +}; + +} // namespace ods + +#endif // __ODS_ODS_ARRAY_QUEUE__ \ No newline at end of file diff --git a/ods/src/ods/array_stack.h b/ods/src/ods/array_stack.h index 5449576..617c67a 100644 --- a/ods/src/ods/array_stack.h +++ b/ods/src/ods/array_stack.h @@ -1,7 +1,6 @@ #ifndef __ODS_ODS_ARRAY_STACK__ #define __ODS_ODS_ARRAY_STACK__ -#include #include namespace ods {