Start working on ArrayDeque.
This commit is contained in:
		
							parent
							
								
									214142583b
								
							
						
					
					
						commit
						6311eaf378
					
				| 
						 | 
					@ -1,6 +1,8 @@
 | 
				
			||||||
#include <iostream>
 | 
					#include <iostream>
 | 
				
			||||||
#include <ods/array.h>
 | 
					#include <ods/array.h>
 | 
				
			||||||
#include <ods/array_stack.h>
 | 
					#include <ods/array_stack.h>
 | 
				
			||||||
 | 
					#include <ods/array_queue.h>
 | 
				
			||||||
 | 
					#include <ods/array_deque.h>
 | 
				
			||||||
using namespace std;
 | 
					using namespace std;
 | 
				
			||||||
using namespace ods;
 | 
					using namespace ods;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,12 +10,15 @@ int
 | 
				
			||||||
main(void)
 | 
					main(void)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	Array<int>	a(2);
 | 
						Array<int>	a(2);
 | 
				
			||||||
	Array<int>	b(5);;
 | 
						Array<int>	b(5);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cout << "=== Array ===" << endl;
 | 
				
			||||||
	cout << "a[0] " << a[0] << endl;
 | 
						cout << "a[0] " << a[0] << endl;
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	b = a;
 | 
						b = a;
 | 
				
			||||||
	cout << "b[0] " << b[0] << endl;
 | 
						cout << "b[0] " << b[0] << endl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cout << "=== ArrayStack ===" << endl;
 | 
				
			||||||
	ArrayStack<int>	as(5);
 | 
						ArrayStack<int>	as(5);
 | 
				
			||||||
	for (int i = 0; i < 3; i++) {
 | 
						for (int i = 0; i < 3; i++) {
 | 
				
			||||||
		as.set(i, i+1);
 | 
							as.set(i, i+1);
 | 
				
			||||||
| 
						 | 
					@ -30,4 +35,32 @@ main(void)
 | 
				
			||||||
	as.add(0, 17);
 | 
						as.add(0, 17);
 | 
				
			||||||
	as.add(0, 1);
 | 
						as.add(0, 1);
 | 
				
			||||||
	cout << "size: " << as.size() << ", cap: " << as.cap() << endl;
 | 
						cout << "size: " << as.size() << ", cap: " << as.cap() << endl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cout << "=== ArrayQueue ===" << endl;
 | 
				
			||||||
 | 
						ArrayQueue<int> 	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<int>	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;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,75 @@
 | 
				
			||||||
 | 
					#ifndef __ODS_ODS_ARRAY_DEQUE_
 | 
				
			||||||
 | 
					#define __ODS_ODS_ARRAY_DEQUE_
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <ods/array.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace ods {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					template<typename T>
 | 
				
			||||||
 | 
					class ArrayDeque {
 | 
				
			||||||
 | 
					public:
 | 
				
			||||||
 | 
						ArrayDeque(int nlen) : a(Array<T>(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<T> b(max(2 * n, 1));
 | 
				
			||||||
 | 
							for (int k = 0; k < n; k++) {
 | 
				
			||||||
 | 
								b[k] = a[index(k)];
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							j = 0;
 | 
				
			||||||
 | 
							a = b;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					private:
 | 
				
			||||||
 | 
						Array<T>	a;
 | 
				
			||||||
 | 
						int		j;
 | 
				
			||||||
 | 
						int		n;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					} // namespace ods
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif // __ODS_ODS_ARRAY_DEQUE_
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,59 @@
 | 
				
			||||||
 | 
					#ifndef __ODS_ODS_ARRAY_QUEUE__
 | 
				
			||||||
 | 
					#define __ODS_ODS_ARRAY_QUEUE__
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <ods/array.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace ods {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					template<typename T>
 | 
				
			||||||
 | 
					class ArrayQueue {
 | 
				
			||||||
 | 
					public:
 | 
				
			||||||
 | 
						ArrayQueue(int nlen) : a(Array<T>(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<T> 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<T>	a;
 | 
				
			||||||
 | 
						int		j;
 | 
				
			||||||
 | 
						int		n;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					} // namespace ods
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif // __ODS_ODS_ARRAY_QUEUE__
 | 
				
			||||||
| 
						 | 
					@ -1,7 +1,6 @@
 | 
				
			||||||
#ifndef __ODS_ODS_ARRAY_STACK__
 | 
					#ifndef __ODS_ODS_ARRAY_STACK__
 | 
				
			||||||
#define __ODS_ODS_ARRAY_STACK__
 | 
					#define __ODS_ODS_ARRAY_STACK__
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <iostream>
 | 
					 | 
				
			||||||
#include <ods/array.h>
 | 
					#include <ods/array.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace ods {
 | 
					namespace ods {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue