mirror of
https://github.com/kevinbentley/Descent3.git
synced 2026-04-07 11:00:04 -04:00
clang-format on everything.
This commit is contained in:
@@ -37,327 +37,256 @@
|
||||
#include <string.h> // some compilers declare memcpy() here
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4345) // warning about a change in how the code is handled in this version
|
||||
#pragma warning(disable : 4345) // warning about a change in how the code is handled in this version
|
||||
#endif
|
||||
|
||||
BEGIN_AS_NAMESPACE
|
||||
|
||||
template <class T> class asCArray
|
||||
{
|
||||
template <class T> class asCArray {
|
||||
public:
|
||||
asCArray();
|
||||
asCArray(const asCArray<T> &);
|
||||
asCArray(int reserve);
|
||||
~asCArray();
|
||||
asCArray();
|
||||
asCArray(const asCArray<T> &);
|
||||
asCArray(int reserve);
|
||||
~asCArray();
|
||||
|
||||
void Allocate(size_t numElements, bool keepData);
|
||||
size_t GetCapacity() const;
|
||||
void Allocate(size_t numElements, bool keepData);
|
||||
size_t GetCapacity() const;
|
||||
|
||||
void PushLast(const T &element);
|
||||
T PopLast();
|
||||
void PushLast(const T &element);
|
||||
T PopLast();
|
||||
|
||||
void SetLength(size_t numElements);
|
||||
size_t GetLength() const;
|
||||
void SetLength(size_t numElements);
|
||||
size_t GetLength() const;
|
||||
|
||||
void Copy(const T*, size_t count);
|
||||
asCArray<T> &operator =(const asCArray<T> &);
|
||||
void Copy(const T *, size_t count);
|
||||
asCArray<T> &operator=(const asCArray<T> &);
|
||||
|
||||
const T &operator [](size_t index) const;
|
||||
T &operator [](size_t index);
|
||||
T *AddressOf();
|
||||
const T &operator[](size_t index) const;
|
||||
T &operator[](size_t index);
|
||||
T *AddressOf();
|
||||
|
||||
void Concatenate(const asCArray<T> &);
|
||||
void Concatenate(T*, unsigned int count);
|
||||
void Concatenate(const asCArray<T> &);
|
||||
void Concatenate(T *, unsigned int count);
|
||||
|
||||
bool Exists(const T &element);
|
||||
int IndexOf(const T &element);
|
||||
void RemoveIndex(size_t index); // Removes the entry without reordering the array
|
||||
void RemoveValue(const T &element);
|
||||
bool Exists(const T &element);
|
||||
int IndexOf(const T &element);
|
||||
void RemoveIndex(size_t index); // Removes the entry without reordering the array
|
||||
void RemoveValue(const T &element);
|
||||
|
||||
bool operator==(const asCArray<T> &) const;
|
||||
bool operator!=(const asCArray<T> &) const;
|
||||
bool operator==(const asCArray<T> &) const;
|
||||
bool operator!=(const asCArray<T> &) const;
|
||||
|
||||
protected:
|
||||
T *array;
|
||||
size_t length;
|
||||
size_t maxLength;
|
||||
char buf[8];
|
||||
T *array;
|
||||
size_t length;
|
||||
size_t maxLength;
|
||||
char buf[8];
|
||||
};
|
||||
|
||||
// Implementation
|
||||
|
||||
template <class T>
|
||||
T *asCArray<T>::AddressOf()
|
||||
{
|
||||
return array;
|
||||
template <class T> T *asCArray<T>::AddressOf() { return array; }
|
||||
|
||||
template <class T> asCArray<T>::asCArray(void) {
|
||||
array = 0;
|
||||
length = 0;
|
||||
maxLength = 0;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
asCArray<T>::asCArray(void)
|
||||
{
|
||||
array = 0;
|
||||
length = 0;
|
||||
maxLength = 0;
|
||||
template <class T> asCArray<T>::asCArray(const asCArray<T> ©) {
|
||||
array = 0;
|
||||
length = 0;
|
||||
maxLength = 0;
|
||||
|
||||
*this = copy;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
asCArray<T>::asCArray(const asCArray<T> ©)
|
||||
{
|
||||
array = 0;
|
||||
length = 0;
|
||||
maxLength = 0;
|
||||
template <class T> asCArray<T>::asCArray(int reserve) {
|
||||
array = 0;
|
||||
length = 0;
|
||||
maxLength = 0;
|
||||
|
||||
*this = copy;
|
||||
Allocate(reserve, false);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
asCArray<T>::asCArray(int reserve)
|
||||
{
|
||||
array = 0;
|
||||
length = 0;
|
||||
maxLength = 0;
|
||||
|
||||
Allocate(reserve, false);
|
||||
template <class T> asCArray<T>::~asCArray(void) {
|
||||
// Allocating a zero length array will free all memory
|
||||
Allocate(0, 0);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
asCArray<T>::~asCArray(void)
|
||||
{
|
||||
// Allocating a zero length array will free all memory
|
||||
Allocate(0,0);
|
||||
template <class T> size_t asCArray<T>::GetLength() const { return length; }
|
||||
|
||||
template <class T> const T &asCArray<T>::operator[](size_t index) const {
|
||||
asASSERT(index < length);
|
||||
|
||||
return array[index];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
size_t asCArray<T>::GetLength() const
|
||||
{
|
||||
return length;
|
||||
template <class T> T &asCArray<T>::operator[](size_t index) {
|
||||
asASSERT(index < length);
|
||||
|
||||
return array[index];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
const T &asCArray<T>::operator [](size_t index) const
|
||||
{
|
||||
asASSERT(index < length);
|
||||
template <class T> void asCArray<T>::PushLast(const T &element) {
|
||||
if (length == maxLength) {
|
||||
if (maxLength == 0)
|
||||
Allocate(1, false);
|
||||
else
|
||||
Allocate(2 * maxLength, true);
|
||||
}
|
||||
|
||||
return array[index];
|
||||
array[length++] = element;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T &asCArray<T>::operator [](size_t index)
|
||||
{
|
||||
asASSERT(index < length);
|
||||
template <class T> T asCArray<T>::PopLast() {
|
||||
asASSERT(length > 0);
|
||||
|
||||
return array[index];
|
||||
return array[--length];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void asCArray<T>::PushLast(const T &element)
|
||||
{
|
||||
if( length == maxLength )
|
||||
{
|
||||
if( maxLength == 0 )
|
||||
Allocate(1, false);
|
||||
else
|
||||
Allocate(2*maxLength, true);
|
||||
}
|
||||
template <class T> void asCArray<T>::Allocate(size_t numElements, bool keepData) {
|
||||
// We have 4 situations
|
||||
// 1. The previous array is 8 bytes or smaller and the new array is also 8 bytes or smaller
|
||||
// 2. The previous array is 8 bytes or smaller and the new array is larger than 8 bytes
|
||||
// 3. The previous array is larger than 8 bytes and the new array is 8 bytes or smaller
|
||||
// 4. The previous array is larger than 8 bytes and the new array is also larger than 8 bytes
|
||||
|
||||
array[length++] = element;
|
||||
T *tmp = 0;
|
||||
if (numElements) {
|
||||
if (sizeof(T) * numElements <= 8)
|
||||
// Use the internal buffer
|
||||
tmp = (T *)buf;
|
||||
else
|
||||
// Allocate the array and construct each of the elements
|
||||
tmp = asNEWARRAY(T, numElements);
|
||||
|
||||
if (array == tmp) {
|
||||
// Construct only the newly allocated elements
|
||||
for (size_t n = length; n < numElements; n++)
|
||||
new (&tmp[n]) T();
|
||||
} else {
|
||||
// Construct all elements
|
||||
for (size_t n = 0; n < numElements; n++)
|
||||
new (&tmp[n]) T();
|
||||
}
|
||||
}
|
||||
|
||||
if (array) {
|
||||
size_t oldLength = length;
|
||||
|
||||
if (array == tmp) {
|
||||
if (keepData) {
|
||||
if (length > numElements)
|
||||
length = numElements;
|
||||
} else
|
||||
length = 0;
|
||||
|
||||
// Call the destructor for elements that are no longer used
|
||||
for (size_t n = length; n < oldLength; n++)
|
||||
array[n].~T();
|
||||
} else {
|
||||
if (keepData) {
|
||||
if (length > numElements)
|
||||
length = numElements;
|
||||
|
||||
for (size_t n = 0; n < length; n++)
|
||||
tmp[n] = array[n];
|
||||
} else
|
||||
length = 0;
|
||||
|
||||
// Call the destructor for all elements
|
||||
for (size_t n = 0; n < oldLength; n++)
|
||||
array[n].~T();
|
||||
|
||||
if (array != (T *)buf)
|
||||
asDELETEARRAY(array);
|
||||
}
|
||||
}
|
||||
|
||||
array = tmp;
|
||||
maxLength = numElements;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T asCArray<T>::PopLast()
|
||||
{
|
||||
asASSERT(length > 0);
|
||||
template <class T> size_t asCArray<T>::GetCapacity() const { return maxLength; }
|
||||
|
||||
return array[--length];
|
||||
template <class T> void asCArray<T>::SetLength(size_t numElements) {
|
||||
if (numElements > maxLength)
|
||||
Allocate(numElements, true);
|
||||
|
||||
length = numElements;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void asCArray<T>::Allocate(size_t numElements, bool keepData)
|
||||
{
|
||||
// We have 4 situations
|
||||
// 1. The previous array is 8 bytes or smaller and the new array is also 8 bytes or smaller
|
||||
// 2. The previous array is 8 bytes or smaller and the new array is larger than 8 bytes
|
||||
// 3. The previous array is larger than 8 bytes and the new array is 8 bytes or smaller
|
||||
// 4. The previous array is larger than 8 bytes and the new array is also larger than 8 bytes
|
||||
template <class T> void asCArray<T>::Copy(const T *data, size_t count) {
|
||||
if (maxLength < count)
|
||||
Allocate(count, false);
|
||||
|
||||
T *tmp = 0;
|
||||
if( numElements )
|
||||
{
|
||||
if( sizeof(T)*numElements <= 8 )
|
||||
// Use the internal buffer
|
||||
tmp = (T*)buf;
|
||||
else
|
||||
// Allocate the array and construct each of the elements
|
||||
tmp = asNEWARRAY(T,numElements);
|
||||
for (size_t n = 0; n < count; n++)
|
||||
array[n] = data[n];
|
||||
|
||||
if( array == tmp )
|
||||
{
|
||||
// Construct only the newly allocated elements
|
||||
for( size_t n = length; n < numElements; n++ )
|
||||
new (&tmp[n]) T();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Construct all elements
|
||||
for( size_t n = 0; n < numElements; n++ )
|
||||
new (&tmp[n]) T();
|
||||
}
|
||||
}
|
||||
|
||||
if( array )
|
||||
{
|
||||
size_t oldLength = length;
|
||||
|
||||
if( array == tmp )
|
||||
{
|
||||
if( keepData )
|
||||
{
|
||||
if( length > numElements )
|
||||
length = numElements;
|
||||
}
|
||||
else
|
||||
length = 0;
|
||||
|
||||
// Call the destructor for elements that are no longer used
|
||||
for( size_t n = length; n < oldLength; n++ )
|
||||
array[n].~T();
|
||||
}
|
||||
else
|
||||
{
|
||||
if( keepData )
|
||||
{
|
||||
if( length > numElements )
|
||||
length = numElements;
|
||||
|
||||
for( size_t n = 0; n < length; n++ )
|
||||
tmp[n] = array[n];
|
||||
}
|
||||
else
|
||||
length = 0;
|
||||
|
||||
// Call the destructor for all elements
|
||||
for( size_t n = 0; n < oldLength; n++ )
|
||||
array[n].~T();
|
||||
|
||||
if( array != (T*)buf )
|
||||
asDELETEARRAY(array);
|
||||
}
|
||||
}
|
||||
|
||||
array = tmp;
|
||||
maxLength = numElements;
|
||||
length = count;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
size_t asCArray<T>::GetCapacity() const
|
||||
{
|
||||
return maxLength;
|
||||
template <class T> asCArray<T> &asCArray<T>::operator=(const asCArray<T> ©) {
|
||||
Copy(copy.array, copy.length);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void asCArray<T>::SetLength(size_t numElements)
|
||||
{
|
||||
if( numElements > maxLength )
|
||||
Allocate(numElements, true);
|
||||
template <class T> bool asCArray<T>::operator==(const asCArray<T> &other) const {
|
||||
if (length != other.length)
|
||||
return false;
|
||||
|
||||
length = numElements;
|
||||
for (asUINT n = 0; n < length; n++)
|
||||
if (array[n] != other.array[n])
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void asCArray<T>::Copy(const T *data, size_t count)
|
||||
{
|
||||
if( maxLength < count )
|
||||
Allocate(count, false);
|
||||
template <class T> bool asCArray<T>::operator!=(const asCArray<T> &other) const { return !(*this == other); }
|
||||
|
||||
for( size_t n = 0; n < count; n++ )
|
||||
array[n] = data[n];
|
||||
template <class T> void asCArray<T>::Concatenate(const asCArray<T> &other) {
|
||||
if (maxLength < length + other.length)
|
||||
Allocate(length + other.length, true);
|
||||
|
||||
length = count;
|
||||
for (size_t n = 0; n < other.length; n++)
|
||||
array[length + n] = other.array[n];
|
||||
|
||||
length += other.length;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
asCArray<T> &asCArray<T>::operator =(const asCArray<T> ©)
|
||||
{
|
||||
Copy(copy.array, copy.length);
|
||||
|
||||
return *this;
|
||||
template <class T> void asCArray<T>::Concatenate(T *array, unsigned int count) {
|
||||
for (unsigned int c = 0; c < count; c++)
|
||||
PushLast(array[c]);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool asCArray<T>::operator ==(const asCArray<T> &other) const
|
||||
{
|
||||
if( length != other.length ) return false;
|
||||
template <class T> bool asCArray<T>::Exists(const T &e) { return IndexOf(e) == -1 ? false : true; }
|
||||
|
||||
for( asUINT n = 0; n < length; n++ )
|
||||
if( array[n] != other.array[n] )
|
||||
return false;
|
||||
template <class T> int asCArray<T>::IndexOf(const T &e) {
|
||||
for (size_t n = 0; n < length; n++)
|
||||
if (array[n] == e)
|
||||
return (int)n;
|
||||
|
||||
return true;
|
||||
return -1;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool asCArray<T>::operator !=(const asCArray<T> &other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
template <class T> void asCArray<T>::RemoveIndex(size_t index) {
|
||||
if (index < length) {
|
||||
for (size_t n = index; n < length - 1; n++)
|
||||
array[n] = array[n + 1];
|
||||
|
||||
PopLast();
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void asCArray<T>::Concatenate(const asCArray<T> &other)
|
||||
{
|
||||
if( maxLength < length + other.length )
|
||||
Allocate(length + other.length, true);
|
||||
|
||||
for( size_t n = 0; n < other.length; n++ )
|
||||
array[length+n] = other.array[n];
|
||||
|
||||
length += other.length;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void asCArray<T>::Concatenate(T* array, unsigned int count)
|
||||
{
|
||||
for( unsigned int c = 0; c < count; c++ )
|
||||
PushLast(array[c]);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool asCArray<T>::Exists(const T &e)
|
||||
{
|
||||
return IndexOf(e) == -1 ? false : true;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
int asCArray<T>::IndexOf(const T &e)
|
||||
{
|
||||
for( size_t n = 0; n < length; n++ )
|
||||
if( array[n] == e ) return (int)n;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void asCArray<T>::RemoveIndex(size_t index)
|
||||
{
|
||||
if( index < length )
|
||||
{
|
||||
for( size_t n = index; n < length-1; n++ )
|
||||
array[n] = array[n+1];
|
||||
|
||||
PopLast();
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void asCArray<T>::RemoveValue(const T &e)
|
||||
{
|
||||
for( size_t n = 0; n < length; n++ )
|
||||
{
|
||||
if( array[n] == e )
|
||||
{
|
||||
RemoveIndex(n);
|
||||
break;
|
||||
}
|
||||
}
|
||||
template <class T> void asCArray<T>::RemoveValue(const T &e) {
|
||||
for (size_t n = 0; n < length; n++) {
|
||||
if (array[n] == e) {
|
||||
RemoveIndex(n);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
END_AS_NAMESPACE
|
||||
|
||||
Reference in New Issue
Block a user