List Template
// Dynamically sized list using po2b memory. Uses versioning to allow checking if original item still
// exists. Also see class ListMem in Memory section for additional functions used by this template.
template<typename T> class List : public ListMem
{
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++() { list->GetNext(n); return *this; }
self_type operator++(int ignore) { self_type i = *this; list->GetNext(n); return i; }
reference operator*() const { return (*list)[n]; }
pointer operator->() const { return &(*list)[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 List<T> &list, int n) : list(&list), n(n) {}
const_iterator() : list(0), n(0) {}
private:
const List<T> *list;
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++() { list->GetNext(n); return *this; }
self_type operator++(int ignore) { self_type i = *this; list->GetNext(n); return i; }
reference operator*() const { return (*list)[n]; }
pointer operator->() const { return &(*list)[n]; }
bool operator==(const self_type &rhs) const { return n == rhs.n; }
bool operator!=(const self_type &rhs) const { return n != rhs.n; }
iterator(List<T> &list, int n) : list(&list), n(n) {}
iterator() : list(0), n(0) {}
private:
List<T> *list;
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++() { list->GetPrev(n); return *this; }
self_type operator++(int ignore) { self_type i = *this; list->GetPrev(n); return i; }
reference operator*() const { return (*list)[n]; }
pointer operator->() const { return &(*list)[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 List<T> &list, int n) : list(&list), n(n) {}
const_reverse_iterator() : list(0), n(0) {}
private:
const List<T> *list;
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++() { list->GetPrev(n); return *this; }
self_type operator++(int ignore) { self_type i = *this; list->GetPrev(n); return i; }
reference operator*() const { return (*list)[n]; }
pointer operator->() const { return &(*list)[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(List<T> &list, int n) : list(&list), n(n) {}
reverse_iterator() : list(0), n(0) {}
private:
List<T> *list;
int n;
};
// Get constant begin iterator.
const_iterator begin() const { int index = 0; GetFirst(index); return const_iterator(*this, index); }
// Get constant end iterator.
const_iterator end() const { int index = 0; GetLast(index); GetNext(index); return const_iterator(*this, index); }
// Get begin iterator.
iterator begin() { int index = 0; GetFirst(index); return iterator(*this, index); }
// Get end iterator.
iterator end() { int index = 0; GetLast(index); GetNext(index); return iterator(*this, index); }
// Get constant reverse begin iterator.
const_reverse_iterator rbegin() const { int index = 0; GetLast(index); return const_reverse_iterator(*this, index); }
// Get constant reverse end iterator.
const_reverse_iterator rend() const { int index = 0; GetFirst(index); GetPrev(index); return const_reverse_iterator(*this, index); }
// Get reverse begin iterator.
reverse_iterator rbegin() { int index = 0; GetLast(index); return reverse_iterator(*this, index); }
// Get reverse end iterator.
reverse_iterator rend() { int index = 0; GetFirst(index); GetPrev(index); return reverse_iterator(*this, index); }
// Get first item. Initializes index iterator.
T *GetFirst(int &index) const { return (T *)ListMem::GetFirst(index); }
// Get last item. Initializes index iterator.
T *GetLast(int &index) const { return (T *)ListMem::GetLast(index); }
// Get previous item. Index iterator must be intialized with GetLast().
T *GetPrev(int &index) const { return (T *)ListMem::GetPrev(index); }
// Get next item. Index iterator must be intialized with GetFirst().
T *GetNext(int &index) const { return (T *)ListMem::GetNext(index); }
// Get item at [index].
T &operator [] (int index) const { return *(T *)GetItem(index); }
// Constructor. Use estimated max items for [incr].
List(uint incr = 8) : ListMem((uint)sizeof(T), incr, Destruct) {}
// Destructor.
~List() {}
private:
static void Destruct(void *ptr) { ((T *)ptr)->~T(); }
};