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 {