Array Template
// Dynamically sized array using po2b memory. Also see class ArrayMem in Memory section for
// additional functions used by this template.
template<typename T> class Array : public ArrayMem
{
public:
class const_iterator
{
public:
typedef const_iterator self_type;
typedef const T value_type;
typedef const T &reference;
typedef const T *pointer;
typedef int difference_type;
int index() const { return n; };
self_type operator++() { n++; return *this; }
self_type operator++(int ignore) { self_type i = *this; n++; return i; }
reference operator*() const { return (*array)[n]; }
pointer operator->() const { return &(*array)[n]; }
bool operator==(const self_type &rhs) const { return n == rhs.n; }
bool operator!=(const self_type &rhs) const { return n != rhs.n; }
const_iterator(const Array<T> &array, int n) : array(&array), n(n) {}
const_iterator() : array(0), n(0) {}
private:
const Array<T> *array;
int n;
};
class iterator
{
public:
typedef iterator self_type;
typedef T value_type;
typedef T &reference;
typedef T *pointer;
typedef int difference_type;
int index() const { return n; };
self_type operator++() { n++; return *this; }
self_type operator++(int ignore) { self_type i = *this; n++; return i; }
reference operator*() const { return (*array)[n]; }
pointer operator->() const { return &(*array)[n]; }
bool operator==(const self_type &rhs) const { return n == rhs.n; }
bool operator!=(const self_type &rhs) const { return n != rhs.n; }
iterator(Array<T> &array, int n) : array(&array), n(n) {}
iterator() : array(0), n(0) {}
private:
Array<T> *array;
int n;
};
class const_reverse_iterator
{
public:
typedef const_reverse_iterator self_type;
typedef const T value_type;
typedef const T &reference;
typedef const T *pointer;
typedef int difference_type;
int index() const { return n; };
self_type operator++() { n--; return *this; }
self_type operator++(int ignore) { self_type i = *this; n--; return i; }
reference operator*() const { return (*array)[n]; }
pointer operator->() const { return &(*array)[n]; }
bool operator==(const self_type &rhs) const { return n == rhs.n; }
bool operator!=(const self_type &rhs) const { return n != rhs.n; }
const_reverse_iterator(const Array<T> &array, int n) : array(&array), n(n) {}
const_reverse_iterator() : array(0), n(0) {}
private:
const Array<T> *array;
int n;
};
class reverse_iterator
{
public:
typedef reverse_iterator self_type;
typedef T value_type;
typedef T &reference;
typedef T *pointer;
typedef int difference_type;
int index() const { return n; };
self_type operator++() { n--; return *this; }
self_type operator++(int ignore) { self_type i = *this; n--; return i; }
reference operator*() const { return (*array)[n]; }
pointer operator->() const { return &(*array)[n]; }
bool operator==(const self_type &rhs) const { return n == rhs.n; }
bool operator!=(const self_type &rhs) const { return n != rhs.n; }
reverse_iterator(Array<T> &array, int n) : array(&array), n(n) {}
reverse_iterator() : array(0), n(0) {}
private:
Array<T> *array;
int n;
};
// Get constant begin iterator.
const_iterator begin() const { return const_iterator(*this, 0); }
// Get constant end iterator.
const_iterator end() const { return const_iterator(*this, GetCount()); }
// Get begin iterator.
iterator begin() { return iterator(*this, 0); }
// Get end iterator.
iterator end() { return iterator(*this, GetCount()); }
// Get constant reverse begin iterator.
const_reverse_iterator rbegin() const { return const_reverse_iterator(*this, GetCount() - 1); }
// Get constant reverse end iterator.
const_reverse_iterator rend() const { return const_reverse_iterator(*this, -1); }
// Get reverse begin iterator.
reverse_iterator rbegin() { return reverse_iterator(*this, GetCount() - 1); }
// Get reverse end iterator.
reverse_iterator rend() { return reverse_iterator(*this, -1); }
// Get item using [index]. Negative index offsets from end.
T &operator [] (int index) const { return *(T *)GetItem(index); }
// Construct array. Use estimated max items for [incr].
Array(uint incr = 8) : ArrayMem((uint)sizeof(T), incr, Destruct) {}
// Destructor.
~Array() {}
private:
static void Destruct(void *ptr) { ((T *)ptr)->~T(); }
};