2008-05-22 04:51:37 -07:00
|
|
|
// Copyright (C) 2002-2008 Nikolaus Gebhardt
|
2007-05-20 11:03:49 -07:00
|
|
|
// This file is part of the "Irrlicht Engine" and the "irrXML" project.
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
|
|
|
|
|
|
|
|
#ifndef __IRR_ARRAY_H_INCLUDED__
|
|
|
|
#define __IRR_ARRAY_H_INCLUDED__
|
|
|
|
|
|
|
|
#include "irrTypes.h"
|
|
|
|
#include "heapsort.h"
|
|
|
|
#include "irrAllocator.h"
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace core
|
|
|
|
{
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Self reallocating template array (like stl vector) with additional features.
|
2007-05-20 11:03:49 -07:00
|
|
|
/** Some features are: Heap sorting, binary search methods, easier debugging.
|
|
|
|
*/
|
|
|
|
template <class T, typename TAlloc = irrAllocator<T> >
|
|
|
|
class array
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Default constructor for empty array.
|
2007-05-20 11:03:49 -07:00
|
|
|
array()
|
|
|
|
: data(0), allocated(0), used(0),
|
|
|
|
free_when_destroyed(true), is_sorted(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Constructs an array and allocates an initial chunk of memory.
|
|
|
|
/** \param start_count Amount of elements to pre-allocate. */
|
2007-05-20 11:03:49 -07:00
|
|
|
array(u32 start_count)
|
|
|
|
: data(0), allocated(0), used(0),
|
|
|
|
free_when_destroyed(true), is_sorted(true)
|
|
|
|
{
|
|
|
|
reallocate(start_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Copy constructor
|
|
|
|
array(const array<T>& other)
|
|
|
|
: data(0)
|
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Destructor.
|
|
|
|
/** Frees allocated memory, if set_free_when_destroyed was not set to
|
|
|
|
false by the user before. */
|
2007-05-20 11:03:49 -07:00
|
|
|
~array()
|
|
|
|
{
|
|
|
|
if (free_when_destroyed)
|
|
|
|
{
|
|
|
|
for (u32 i=0; i<used; ++i)
|
|
|
|
allocator.destruct(&data[i]);
|
|
|
|
|
|
|
|
allocator.deallocate(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Reallocates the array, make it bigger or smaller.
|
2008-05-22 04:51:37 -07:00
|
|
|
/** \param new_size New size of array. */
|
2007-05-20 11:03:49 -07:00
|
|
|
void reallocate(u32 new_size)
|
|
|
|
{
|
|
|
|
T* old_data = data;
|
|
|
|
|
|
|
|
data = allocator.allocate(new_size); //new T[new_size];
|
|
|
|
allocated = new_size;
|
|
|
|
|
|
|
|
// copy old data
|
|
|
|
s32 end = used < new_size ? used : new_size;
|
2008-05-22 04:51:37 -07:00
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
for (s32 i=0; i<end; ++i)
|
|
|
|
{
|
|
|
|
// data[i] = old_data[i];
|
|
|
|
allocator.construct(&data[i], old_data[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// destruct old data
|
|
|
|
for (u32 j=0; j<used; ++j)
|
|
|
|
allocator.destruct(&old_data[j]);
|
|
|
|
|
|
|
|
if (allocated < used)
|
|
|
|
used = allocated;
|
|
|
|
|
|
|
|
allocator.deallocate(old_data); //delete [] old_data;
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
|
|
|
|
//! Adds an element at back of array.
|
|
|
|
/** If the array is too small to add this new element it is made bigger.
|
|
|
|
\param element: Element to add at the back of the array. */
|
2007-05-20 11:03:49 -07:00
|
|
|
void push_back(const T& element)
|
|
|
|
{
|
|
|
|
if (used + 1 > allocated)
|
|
|
|
{
|
|
|
|
// reallocate(used * 2 +1);
|
|
|
|
// this doesn't work if the element is in the same array. So
|
|
|
|
// we'll copy the element first to be sure we'll get no data
|
|
|
|
// corruption
|
|
|
|
|
|
|
|
T e(element);
|
|
|
|
reallocate(used * 2 +1); // increase data block
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
allocator.construct(&data[used++], e); // data[used++] = e; // push_back
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//data[used++] = element;
|
|
|
|
// instead of using this here, we copy it the safe way:
|
|
|
|
allocator.construct(&data[used++], element);
|
|
|
|
}
|
|
|
|
|
|
|
|
is_sorted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Adds an element at the front of the array.
|
|
|
|
/** If the array is to small to add this new element, the array is
|
|
|
|
made bigger. Please note that this is slow, because the whole array
|
|
|
|
needs to be copied for this.
|
|
|
|
\param element Element to add at the back of the array. */
|
2007-05-20 11:03:49 -07:00
|
|
|
void push_front(const T& element)
|
|
|
|
{
|
2008-01-20 16:24:28 -08:00
|
|
|
insert(element);
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Insert item into array at specified position.
|
|
|
|
/** Please use this only if you know what you are doing (possible
|
|
|
|
performance loss). The preferred method of adding elements should be
|
|
|
|
push_back().
|
|
|
|
\param element: Element to be inserted
|
|
|
|
\param index: Where position to insert the new element. */
|
2007-05-20 11:03:49 -07:00
|
|
|
void insert(const T& element, u32 index=0)
|
|
|
|
{
|
|
|
|
_IRR_DEBUG_BREAK_IF(index>used) // access violation
|
|
|
|
|
|
|
|
if (used + 1 > allocated)
|
|
|
|
reallocate(used +1);
|
|
|
|
|
2008-01-20 16:24:28 -08:00
|
|
|
for (u32 i=used; i>index; --i)
|
|
|
|
{
|
|
|
|
if (i<used)
|
|
|
|
allocator.destruct(&data[i]);
|
2007-05-20 11:03:49 -07:00
|
|
|
allocator.construct(&data[i], data[i-1]); // data[i] = data[i-1];
|
2008-01-20 16:24:28 -08:00
|
|
|
}
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-01-20 16:24:28 -08:00
|
|
|
if (used > index)
|
|
|
|
allocator.destruct(&data[index]);
|
2007-05-20 11:03:49 -07:00
|
|
|
allocator.construct(&data[index], element); // data[index] = element;
|
|
|
|
is_sorted = false;
|
2008-01-20 16:24:28 -08:00
|
|
|
++used;
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Clears the array and deletes all allocated memory.
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
for (u32 i=0; i<used; ++i)
|
|
|
|
allocator.destruct(&data[i]);
|
|
|
|
|
|
|
|
allocator.deallocate(data); // delete [] data;
|
|
|
|
data = 0;
|
|
|
|
used = 0;
|
|
|
|
allocated = 0;
|
|
|
|
is_sorted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Sets pointer to new array, using this as new workspace.
|
2008-05-22 04:51:37 -07:00
|
|
|
/** \param newPointer: Pointer to new array of elements.
|
|
|
|
\param size: Size of the new array. */
|
2007-05-20 11:03:49 -07:00
|
|
|
void set_pointer(T* newPointer, u32 size)
|
|
|
|
{
|
|
|
|
for (u32 i=0; i<used; ++i)
|
|
|
|
allocator.destruct(&data[i]);
|
|
|
|
|
|
|
|
allocator.deallocate(data); // delete [] data;
|
|
|
|
data = newPointer;
|
|
|
|
allocated = size;
|
|
|
|
used = size;
|
|
|
|
is_sorted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Sets if the array should delete the memory it uses upon destruction.
|
|
|
|
/** \param f If true, the array frees the allocated memory in its
|
|
|
|
destructor, otherwise not. The default is true. */
|
2007-05-20 11:03:49 -07:00
|
|
|
void set_free_when_destroyed(bool f)
|
|
|
|
{
|
|
|
|
free_when_destroyed = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Sets the size of the array and allocates new elements if necessary.
|
|
|
|
/** Please note: This is only secure when using it with simple types,
|
|
|
|
because no default constructor will be called for the added elements.
|
|
|
|
\param usedNow Amount of elements now used. */
|
2007-05-20 11:03:49 -07:00
|
|
|
void set_used(u32 usedNow)
|
|
|
|
{
|
|
|
|
if (allocated < usedNow)
|
|
|
|
reallocate(usedNow);
|
|
|
|
|
|
|
|
used = usedNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Assignment operator
|
|
|
|
void operator=(const array<T>& other)
|
|
|
|
{
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
for (u32 i=0; i<used; ++i)
|
|
|
|
allocator.destruct(&data[i]);
|
|
|
|
|
|
|
|
allocator.deallocate(data); // delete [] data;
|
|
|
|
}
|
|
|
|
|
|
|
|
//if (allocated < other.allocated)
|
|
|
|
if (other.allocated == 0)
|
|
|
|
data = 0;
|
|
|
|
else
|
|
|
|
data = allocator.allocate(other.allocated); // new T[other.allocated];
|
|
|
|
|
|
|
|
used = other.used;
|
|
|
|
free_when_destroyed = other.free_when_destroyed;
|
|
|
|
is_sorted = other.is_sorted;
|
|
|
|
allocated = other.allocated;
|
|
|
|
|
|
|
|
for (u32 i=0; i<other.used; ++i)
|
|
|
|
allocator.construct(&data[i], other.data[i]); // data[i] = other.data[i];
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
|
|
|
|
//! Equality operator
|
2007-05-20 11:03:49 -07:00
|
|
|
bool operator == (const array<T>& other) const
|
|
|
|
{
|
|
|
|
if (used != other.used)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (u32 i=0; i<other.used; ++i)
|
|
|
|
if (data[i] != other[i])
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Inequality operator
|
2007-05-20 11:03:49 -07:00
|
|
|
bool operator != (const array<T>& other) const
|
|
|
|
{
|
|
|
|
return !(*this==other);
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
//! Direct access operator
|
|
|
|
T& operator [](u32 index)
|
|
|
|
{
|
|
|
|
_IRR_DEBUG_BREAK_IF(index>=used) // access violation
|
|
|
|
|
|
|
|
return data[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Direct const access operator
|
2007-05-20 11:03:49 -07:00
|
|
|
const T& operator [](u32 index) const
|
|
|
|
{
|
|
|
|
_IRR_DEBUG_BREAK_IF(index>=used) // access violation
|
|
|
|
|
|
|
|
return data[index];
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
|
|
|
|
//! Gets last element.
|
2007-05-20 11:03:49 -07:00
|
|
|
T& getLast()
|
|
|
|
{
|
|
|
|
_IRR_DEBUG_BREAK_IF(!used) // access violation
|
|
|
|
|
|
|
|
return data[used-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Gets last element
|
2007-05-20 11:03:49 -07:00
|
|
|
const T& getLast() const
|
|
|
|
{
|
|
|
|
_IRR_DEBUG_BREAK_IF(!used) // access violation
|
|
|
|
|
|
|
|
return data[used-1];
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
|
|
|
|
//! Gets a pointer to the array.
|
|
|
|
/** \return Pointer to the array. */
|
2007-05-20 11:03:49 -07:00
|
|
|
T* pointer()
|
|
|
|
{
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Gets a const pointer to the array.
|
|
|
|
/** \return Pointer to the array. */
|
2007-05-20 11:03:49 -07:00
|
|
|
const T* const_pointer() const
|
|
|
|
{
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Get size of array.
|
|
|
|
/** \return Size of elements used in the array. */
|
2007-05-20 11:03:49 -07:00
|
|
|
u32 size() const
|
|
|
|
{
|
|
|
|
return used;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Get amount of memory allocated.
|
|
|
|
/** \return Amount of memory allocated. The amount of bytes
|
|
|
|
allocated would be allocated_size() * sizeof(ElementsUsed); */
|
2007-05-20 11:03:49 -07:00
|
|
|
u32 allocated_size() const
|
|
|
|
{
|
|
|
|
return allocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Check if array is empty.
|
|
|
|
/** \return True if the array is empty false if not. */
|
2007-05-20 11:03:49 -07:00
|
|
|
bool empty() const
|
|
|
|
{
|
|
|
|
return used == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Sorts the array using heapsort.
|
|
|
|
/** There is no additional memory waste and the algorithm performs
|
|
|
|
O(n*log n) in worst case. */
|
2007-05-20 11:03:49 -07:00
|
|
|
void sort()
|
|
|
|
{
|
|
|
|
if (is_sorted || used<2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
heapsort(data, used);
|
|
|
|
is_sorted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Performs a binary search for an element, returns -1 if not found.
|
2008-05-22 04:51:37 -07:00
|
|
|
/** The array will be sorted before the binary search if it is not
|
|
|
|
already sorted.
|
|
|
|
\param element Element to search for.
|
|
|
|
\return Position of the searched element if it was found,
|
|
|
|
otherwise -1 is returned. */
|
2007-05-20 11:03:49 -07:00
|
|
|
s32 binary_search(const T& element)
|
|
|
|
{
|
|
|
|
sort();
|
|
|
|
return binary_search(element, 0, used-1);
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
//! Performs a binary search for an element, returns -1 if not found.
|
2008-05-22 04:51:37 -07:00
|
|
|
/** The array must be sorted prior
|
|
|
|
\param element Element to search for.
|
|
|
|
\return Position of the searched element if it was found, otherwise -1
|
|
|
|
is returned. */
|
2007-05-20 11:03:49 -07:00
|
|
|
s32 binary_search_const(const T& element) const
|
|
|
|
{
|
|
|
|
return binary_search(element, 0, used-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Performs a binary search for an element, returns -1 if not found.
|
2008-05-22 04:51:37 -07:00
|
|
|
/** \param element: Element to search for.
|
|
|
|
\param left First left index
|
|
|
|
\param right Last right index.
|
|
|
|
\return Position of the searched element if it was found, otherwise -1
|
|
|
|
is returned. */
|
2007-05-20 11:03:49 -07:00
|
|
|
s32 binary_search(const T& element, s32 left, s32 right) const
|
|
|
|
{
|
|
|
|
if (!used)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
s32 m;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
m = (left+right)>>1;
|
|
|
|
|
|
|
|
if (element < data[m])
|
|
|
|
right = m - 1;
|
|
|
|
else
|
|
|
|
left = m + 1;
|
|
|
|
|
|
|
|
} while((element < data[m] || data[m] < element) && left<=right);
|
|
|
|
// this last line equals to:
|
|
|
|
// " while((element != array[m]) && left<=right);"
|
|
|
|
// but we only want to use the '<' operator.
|
|
|
|
// the same in next line, it is "(element == array[m])"
|
|
|
|
|
2007-12-03 02:10:13 -08:00
|
|
|
|
2007-05-20 11:03:49 -07:00
|
|
|
if (!(element < data[m]) && !(data[m] < element))
|
|
|
|
return m;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Finds an element in linear time, which is very slow.
|
|
|
|
/** Use binary_search for faster finding. Only works if ==operator is
|
|
|
|
implemented.
|
|
|
|
\param element Element to search for.
|
|
|
|
\return Position of the searched element if it was found, otherwise -1
|
|
|
|
is returned. */
|
2007-05-20 11:03:49 -07:00
|
|
|
s32 linear_search(const T& element) const
|
|
|
|
{
|
|
|
|
for (u32 i=0; i<used; ++i)
|
2007-06-27 22:10:23 -07:00
|
|
|
if (element == data[i])
|
2007-05-20 11:03:49 -07:00
|
|
|
return (s32)i;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Finds an element in linear time, which is very slow.
|
|
|
|
/** Use binary_search for faster finding. Only works if ==operator is
|
|
|
|
implemented.
|
|
|
|
\param element: Element to search for.
|
|
|
|
\return Position of the searched element if it was found, otherwise -1
|
|
|
|
is returned. */
|
2007-05-20 11:03:49 -07:00
|
|
|
s32 linear_reverse_search(const T& element) const
|
|
|
|
{
|
|
|
|
for (s32 i=used-1; i>=0; --i)
|
|
|
|
if (data[i] == element)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Erases an element from the array.
|
|
|
|
/** May be slow, because all elements following after the erased
|
|
|
|
element have to be copied.
|
|
|
|
\param index: Index of element to be erased. */
|
2007-05-20 11:03:49 -07:00
|
|
|
void erase(u32 index)
|
|
|
|
{
|
|
|
|
_IRR_DEBUG_BREAK_IF(index>=used) // access violation
|
|
|
|
|
|
|
|
for (u32 i=index+1; i<used; ++i)
|
|
|
|
{
|
|
|
|
allocator.destruct(&data[i-1]);
|
|
|
|
allocator.construct(&data[i-1], data[i]); // data[i-1] = data[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
allocator.destruct(&data[used-1]);
|
|
|
|
|
|
|
|
--used;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Erases some elements from the array.
|
|
|
|
/** May be slow, because all elements following after the erased
|
|
|
|
element have to be copied.
|
|
|
|
\param index: Index of the first element to be erased.
|
|
|
|
\param count: Amount of elements to be erased. */
|
2007-05-20 11:03:49 -07:00
|
|
|
void erase(u32 index, s32 count)
|
|
|
|
{
|
|
|
|
_IRR_DEBUG_BREAK_IF(index>=used || count<1 || index+count>used) // access violation
|
|
|
|
|
|
|
|
u32 i;
|
|
|
|
for (i=index; i<index+count; ++i)
|
|
|
|
allocator.destruct(&data[i]);
|
|
|
|
|
|
|
|
for (i=index+count; i<used; ++i)
|
|
|
|
{
|
|
|
|
if (i > index+count)
|
|
|
|
allocator.destruct(&data[i-count]);
|
|
|
|
|
|
|
|
allocator.construct(&data[i-count], data[i]); // data[i-count] = data[i];
|
|
|
|
|
|
|
|
if (i >= used-count)
|
|
|
|
allocator.destruct(&data[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
used-= count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! Sets if the array is sorted
|
|
|
|
void set_sorted(bool _is_sorted)
|
|
|
|
{
|
|
|
|
is_sorted = _is_sorted;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
T* data;
|
|
|
|
u32 allocated;
|
|
|
|
u32 used;
|
|
|
|
bool free_when_destroyed;
|
|
|
|
bool is_sorted;
|
|
|
|
TAlloc allocator;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // end namespace core
|
|
|
|
} // end namespace irr
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|