Loading...
Searching...
No Matches
irrList.h
Go to the documentation of this file.
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#ifndef __IRR_LIST_H_INCLUDED__
#define __IRR_LIST_H_INCLUDED__
#include "irrTypes.h"
#include "irrAllocator.h"
#include "irrMath.h"
namespace irr
{
namespace core
{
template <class T>
class list
{
private:
struct SKListNode
{
SKListNode(const T& e) : Next(0), Prev(0), Element(e) {}
SKListNode* Next;
SKListNode* Prev;
T Element;
};
public:
class ConstIterator;
class Iterator
{
public:
Iterator() : Current(0) {}
Iterator& operator ++() { Current = Current->Next; return *this; }
Iterator& operator --() { Current = Current->Prev; return *this; }
Iterator operator ++(s32) { Iterator tmp = *this; Current = Current->Next; return tmp; }
Iterator operator --(s32) { Iterator tmp = *this; Current = Current->Prev; return tmp; }
Iterator& operator +=(s32 num)
{
if(num > 0)
{
while (num-- && this->Current != 0) ++(*this);
}
else
{
while(num++ && this->Current != 0) --(*this);
}
return *this;
}
Iterator operator + (s32 num) const { Iterator tmp = *this; return tmp += num; }
Iterator& operator -=(s32 num) { return (*this)+=(-num); }
Iterator operator - (s32 num) const { return (*this)+ (-num); }
bool operator ==(const Iterator& other) const { return Current == other.Current; }
bool operator !=(const Iterator& other) const { return Current != other.Current; }
bool operator ==(const ConstIterator& other) const { return Current == other.Current; }
bool operator !=(const ConstIterator& other) const { return Current != other.Current; }
#if defined (_MSC_VER) && (_MSC_VER < 1300)
#pragma warning(disable:4284) // infix notation problem when using iterator operator ->
#endif
T & operator * () { return Current->Element; }
T * operator ->() { return &Current->Element; }
private:
explicit Iterator(SKListNode* begin) : Current(begin) {}
SKListNode* Current;
friend class list<T>;
friend class ConstIterator;
};
class ConstIterator
{
public:
ConstIterator() : Current(0) {}
ConstIterator(const Iterator& iter) : Current(iter.Current) {}
ConstIterator& operator ++() { Current = Current->Next; return *this; }
ConstIterator& operator --() { Current = Current->Prev; return *this; }
ConstIterator operator ++(s32) { ConstIterator tmp = *this; Current = Current->Next; return tmp; }
ConstIterator operator --(s32) { ConstIterator tmp = *this; Current = Current->Prev; return tmp; }
ConstIterator& operator +=(s32 num)
{
if(num > 0)
{
while(num-- && this->Current != 0) ++(*this);
}
else
{
while(num++ && this->Current != 0) --(*this);
}
return *this;
}
ConstIterator operator + (s32 num) const { ConstIterator tmp = *this; return tmp += num; }
ConstIterator& operator -=(s32 num) { return (*this)+=(-num); }
ConstIterator operator - (s32 num) const { return (*this)+ (-num); }
bool operator ==(const ConstIterator& other) const { return Current == other.Current; }
bool operator !=(const ConstIterator& other) const { return Current != other.Current; }
bool operator ==(const Iterator& other) const { return Current == other.Current; }
bool operator !=(const Iterator& other) const { return Current != other.Current; }
const T & operator * () { return Current->Element; }
const T * operator ->() { return &Current->Element; }
ConstIterator & operator =(const Iterator & iterator) { Current = iterator.Current; return *this; }
private:
explicit ConstIterator(SKListNode* begin) : Current(begin) {}
SKListNode* Current;
friend class Iterator;
friend class list<T>;
};
list()
: First(0), Last(0), Size(0) {}
list(const list<T>& other) : First(0), Last(0), Size(0)
{
*this = other;
}
~list()
{
clear();
}
void operator=(const list<T>& other)
{
if(&other == this)
{
return;
}
clear();
SKListNode* node = other.First;
while(node)
{
push_back(node->Element);
node = node->Next;
}
}
u32 size() const
{
return Size;
}
u32 getSize() const
{
return Size;
}
void clear()
{
while(First)
{
SKListNode * next = First->Next;
allocator.destruct(First);
allocator.deallocate(First);
First = next;
}
//First = 0; handled by loop
Last = 0;
Size = 0;
}
bool empty() const
{
return (First == 0);
}
void push_back(const T& element)
{
SKListNode* node = allocator.allocate(1);
allocator.construct(node, element);
++Size;
if (First == 0)
First = node;
node->Prev = Last;
if (Last != 0)
Last->Next = node;
Last = node;
}
void push_front(const T& element)
{
SKListNode* node = allocator.allocate(1);
allocator.construct(node, element);
++Size;
if (First == 0)
{
Last = node;
First = node;
}
else
{
node->Next = First;
First->Prev = node;
First = node;
}
}
Iterator begin()
{
return Iterator(First);
}
ConstIterator begin() const
{
return ConstIterator(First);
}
Iterator end()
{
return Iterator(0);
}
ConstIterator end() const
{
return ConstIterator(0);
}
Iterator getLast()
{
return Iterator(Last);
}
ConstIterator getLast() const
{
return ConstIterator(Last);
}
void insert_after(const Iterator& it, const T& element)
{
SKListNode* node = allocator.allocate(1);
allocator.construct(node, element);
node->Next = it.Current->Next;
if (it.Current->Next)
it.Current->Next->Prev = node;
node->Prev = it.Current;
it.Current->Next = node;
++Size;
if (it.Current == Last)
Last = node;
}
void insert_before(const Iterator& it, const T& element)
{
SKListNode* node = allocator.allocate(1);
allocator.construct(node, element);
node->Prev = it.Current->Prev;
if (it.Current->Prev)
it.Current->Prev->Next = node;
node->Next = it.Current;
it.Current->Prev = node;
++Size;
if (it.Current == First)
First = node;
}
Iterator erase(Iterator& it)
{
// suggest changing this to a const Iterator& and
// working around line: it.Current = 0 (possibly with a mutable, or just let it be garbage?)
Iterator returnIterator(it);
++returnIterator;
if(it.Current == First)
{
First = it.Current->Next;
}
else
{
it.Current->Prev->Next = it.Current->Next;
}
if(it.Current == Last)
{
Last = it.Current->Prev;
}
else
{
it.Current->Next->Prev = it.Current->Prev;
}
allocator.destruct(it.Current);
allocator.deallocate(it.Current);
it.Current = 0;
--Size;
return returnIterator;
}
void swap(list<T>& other)
{
core::swap(First, other.First);
core::swap(Last, other.Last);
core::swap(Size, other.Size);
core::swap(allocator, other.allocator); // memory is still released by the same allocator used for allocation
}
private:
SKListNode* First;
SKListNode* Last;
u32 Size;
irrAllocator<SKListNode> allocator;
};
} // end namespace core
}// end namespace irr
#endif
irr::core::irrAllocator
Very simple allocator implementation, containers using it can be used across dll boundaries.
Definition irrAllocator.h:26
irr::core::list::ConstIterator
List iterator for const access.
Definition irrList.h:88
irr::core::list::ConstIterator::operator->
const T * operator->()
Definition irrList.h:122
irr::core::list::ConstIterator::operator++
ConstIterator & operator++()
Definition irrList.h:94
irr::core::list::ConstIterator::operator-=
ConstIterator & operator-=(s32 num)
Definition irrList.h:113
irr::core::list::ConstIterator::operator--
ConstIterator & operator--()
Definition irrList.h:95
irr::core::list::ConstIterator::operator!=
bool operator!=(const ConstIterator &other) const
Definition irrList.h:117
irr::core::list::ConstIterator::operator=
ConstIterator & operator=(const Iterator &iterator)
Definition irrList.h:124
irr::core::list::ConstIterator::operator-
ConstIterator operator-(s32 num) const
Definition irrList.h:114
irr::core::list::ConstIterator::operator==
bool operator==(const ConstIterator &other) const
Definition irrList.h:116
irr::core::list::ConstIterator::ConstIterator
ConstIterator()
Definition irrList.h:91
irr::core::list::ConstIterator::operator+=
ConstIterator & operator+=(s32 num)
Definition irrList.h:99
irr::core::list::ConstIterator::Iterator
friend class Iterator
Definition irrList.h:131
irr::core::list::ConstIterator::operator*
const T & operator*()
Definition irrList.h:121
irr::core::list::ConstIterator::operator+
ConstIterator operator+(s32 num) const
Definition irrList.h:112
irr::core::list::ConstIterator::ConstIterator
ConstIterator(const Iterator &iter)
Definition irrList.h:92
irr::core::list::Iterator::operator+
Iterator operator+(s32 num) const
Definition irrList.h:61
irr::core::list::Iterator::Iterator
Iterator()
Definition irrList.h:41
irr::core::list::Iterator::operator->
T * operator->()
Definition irrList.h:75
irr::core::list::Iterator::operator*
T & operator*()
Definition irrList.h:74
irr::core::list::Iterator::ConstIterator
friend class ConstIterator
Definition irrList.h:83
irr::core::list::Iterator::operator--
Iterator & operator--()
Definition irrList.h:44
irr::core::list::Iterator::operator==
bool operator==(const Iterator &other) const
Definition irrList.h:65
irr::core::list::Iterator::operator-=
Iterator & operator-=(s32 num)
Definition irrList.h:62
irr::core::list::Iterator::operator-
Iterator operator-(s32 num) const
Definition irrList.h:63
irr::core::list::Iterator::operator!=
bool operator!=(const Iterator &other) const
Definition irrList.h:66
irr::core::list::Iterator::operator++
Iterator & operator++()
Definition irrList.h:43
irr::core::list::Iterator::operator+=
Iterator & operator+=(s32 num)
Definition irrList.h:48
irr::core::list::push_back
void push_back(const T &element)
Adds an element at the end of the list.
Definition irrList.h:213
irr::core::list::list
list(const list< T > &other)
Copy constructor.
Definition irrList.h:141
irr::core::list::insert_before
void insert_before(const Iterator &it, const T &element)
Inserts an element before an element.
Definition irrList.h:332
irr::core::list::size
u32 size() const
Returns amount of elements in list.
Definition irrList.h:175
irr::core::list::erase
Iterator erase(Iterator &it)
Erases an element.
Definition irrList.h:354
irr::core::list::operator=
void operator=(const list< T > &other)
Assignment operator.
Definition irrList.h:155
irr::core::list::getSize
u32 getSize() const
Definition irrList.h:179
irr::core::list::~list
~list()
Destructor.
Definition irrList.h:148
irr::core::list::end
ConstIterator end() const
Gets end node.
Definition irrList.h:281
irr::core::list::getLast
Iterator getLast()
Gets last element.
Definition irrList.h:289
irr::core::list::getLast
ConstIterator getLast() const
Gets last element.
Definition irrList.h:297
irr::core::list::swap
void swap(list< T > &other)
Swap the content of this list container with the content of another list.
Definition irrList.h:393
irr::core::list::begin
ConstIterator begin() const
Gets first node.
Definition irrList.h:265
irr::core::list::empty
bool empty() const
Checks for empty list.
Definition irrList.h:205
irr::core::list::insert_after
void insert_after(const Iterator &it, const T &element)
Inserts an element after an element.
Definition irrList.h:308
irr::core::list::list
list()
Default constructor for empty list.
Definition irrList.h:136
irr::core::list::end
Iterator end()
Gets end node.
Definition irrList.h:273
irr::core::list::clear
void clear()
Clears the list, deletes all elements in the list.
Definition irrList.h:187
irr::core::list::begin
Iterator begin()
Gets first node.
Definition irrList.h:257
irr::core::list::push_front
void push_front(const T &element)
Adds an element at the begin of the list.
Definition irrList.h:234
irrAllocator.h
irrMath.h
irrTypes.h
irr::core
Basic classes such as vectors, planes, arrays, lists, and so on can be found in this namespace.
Definition aabbox3d.h:15
irr::core::swap
void swap(T1 &a, T2 &b)
swaps the content of the passed parameters
Definition irrMath.h:177
irr
Everything in the Irrlicht Engine can be found in this namespace.
Definition aabbox3d.h:13
irr::u32
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.h:58
irr::s32
signed int s32
32 bit signed variable.
Definition irrTypes.h:66
- Irrlicht Engine Documentation © 2003-2012 by Nikolaus Gebhardt. Generated on for Irrlicht 3D Engine by Doxygen 1.15.0