array.h 2.97 KB
Newer Older
Sam Moore's avatar
Sam Moore committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef ARRAY_H
#define ARRAY_H

typedef long unsigned int LUint;
#include <cassert>

template <class T>
class Array
{
	public:
		Array() : start(NULL), size(0), reserved(0) {}
		Array(LUint newSize) : start(new T[newSize]), size(newSize), reserved(newSize) {}
		~Array() {delete [] start;}

		void Empty() {size = 0;}
		void Add(const T & add);
		void Reserve(LUint reserve);
		void Resize(LUint newSize);
		void RemoveBack();

		LUint Size() const {return size;}
		LUint Capacity() const {return reserved;}

		void operator=(const Array & equ);
		bool operator==(const Array & equ) const;
		bool operator!=(const Array & equ) const {return !operator==(equ);}

		class Iterator
		{
			public:
				Iterator(const Array & from) : parent(from), index(0) {}
				Iterator(const Iterator & cpy) : parent(cpy.parent), index(cpy.index) {}
				~Iterator() {}

				bool Good() const {return index < parent.Size();}

				T & operator*() const {return parent.start[index];}

				void operator++() {++index;}
				void operator--() {--index;}
				void operator++(int) {operator++();}
				void operator--(int) {operator--();}
				Iterator & operator+=(int amount) {index += amount;}
				Iterator & operator-=(int amount) {index -= amount;}
				Iterator operator+(int amount) {return Iterator(*this) += amount;}
				Iterator operator-(int amount) {return Iterator(*this) -= amount;}

				void operator=(const Iterator & set) {index = set.index;}
				bool operator==(const Iterator & set) {return (&parent == &(set.parent) && index == set.index);}
			private:
				const Array & parent;
				LUint index;
		};

		Iterator First() const {return Iterator(*this);}
		Iterator Last() const {return Iterator(*this) -= (size-1);}
		

		T & operator[](LUint at) const 
		{
			#ifdef DEBUGALL
				printf("	Array<T>::operator[] - called for index %lu/%lu (reserved %lu)\n", at, size, reserved);
				
			#endif //DEBUG
			assert(at < size); return start[at];
		}
		
		int Find(const T & find)
		{

			LUint result;
			for (result = 0; result < size; result++)
			{	
				//printf("%p %lu/%lu\n", (void*)(start), result, size);
				if (start[result] == find)
					return (int)(result);
			}		
			return -1;
		}

	private:
		T * start;
		LUint size; LUint reserved;
};

template <class T> void Array<T>::Add(const T & add)
{
	if (size >= reserved)
	{
		T * old = start; 
		reserved *= 2; ++reserved;
		start = new T[reserved];
		for (LUint ii=0; ii < size; ++ii)
			start[ii] = old[ii];
		delete [] old;
	}
	start[size++] = add;
}

template <class T> void Array<T>::RemoveBack()
{
	if (size > 0)
		--size;
}

template <class T> void Array<T>::Resize(LUint newSize)
{
	T * old = start;
	start = new T[newSize];
	for (LUint ii=0; ii < size; ++ii)
		start[ii] = old[ii];
	size = newSize; reserved = newSize;
	delete [] old;
}

template <class T> void Array<T>::Reserve(LUint newReserve)
{
	if (newReserve > reserved)
	{
		T * old = start;
		start = new T[newReserve];
		for (LUint ii=0; ii < size; ++ii)
			start[ii] = old[ii];
		reserved = newReserve;
	}
}

#endif //ARRAY_H