📄 ac_array.h
字号:
/*
ac_array.h - version 1.0, June 25h, 2003
acCArray is a templated dynamic array.
Copyright (c) 2003 Andreas J鰊sson
This software is provided 'as-is', without any form of
warranty. In no case will the author be held responsible
for any damage caused by its use.
Permission is granted to anyone to use the software for
for any purpose, including commercial. It is also allowed
to modify the software and redistribute it free of charge.
The permission is granted with the following restrictions:
1. The origin of the software may not be misrepresented.
It must be plainly understandable who is the author of
the original software.
2. Altered versions must not be misrepresented as the
original software, i.e. must be plainly marked as
altered.
3. This notice may not be removed or altered from any
distribution of the software, altered or not.
Andreas J鰊sson
andreas@angelcode.com
*/
#ifndef AC_ARRAY_H
#define AC_ARRAY_H
#include <memory.h>
#include <assert.h>
template <class T> class acCArray
{
public:
acCArray();
acCArray(const acCArray<T> &);
~acCArray();
void Allocate(int numElements, bool keepData);
int GetCapacity() const;
void PushLast(T element);
T PopLast();
void SetLength(int numElements);
int GetLength() const;
void Copy(const T*, int count);
acCArray<T> &operator =(const acCArray<T> &);
T &operator [](int index) const;
T *AddressOf();
protected:
T *array;
int length;
int maxLength;
};
// Implementation
template <class T>
T *acCArray<T>::AddressOf()
{
return array;
}
template <class T>
acCArray<T>::acCArray(void)
{
array = 0;
length = 0;
maxLength = 0;
}
template <class T>
acCArray<T>::acCArray(const acCArray<T> ©)
{
array = 0;
length = 0;
maxLength = 0;
*this = copy;
}
template <class T>
acCArray<T>::~acCArray(void)
{
if( array )
{
delete[] array;
array = 0;
}
}
template <class T>
int acCArray<T>::GetLength() const
{
return length;
}
template <class T>
T &acCArray<T>::operator [](int index) const
{
assert(index >= 0);
assert(index < maxLength);
return array[index];
}
template <class T>
void acCArray<T>::PushLast(T element)
{
if( length == maxLength )
Allocate(int(maxLength*1.5f) + 1, true);
array[length++] = element;
}
template <class T>
T acCArray<T>::PopLast()
{
assert(length > 0);
return array[--length];
}
template <class T>
void acCArray<T>::Allocate(int numElements, bool keepData)
{
assert(numElements >= 0);
T *tmp = new T[numElements];
if( array )
{
if( keepData )
{
if( length > numElements )
length = numElements;
memcpy(tmp, array, length*sizeof(T));
}
else
length = 0;
delete[] array;
}
array = tmp;
maxLength = numElements;
}
template <class T>
int acCArray<T>::GetCapacity() const
{
return maxLength;
}
template <class T>
void acCArray<T>::SetLength(int numElements)
{
assert(numElements >= 0);
if( numElements > maxLength )
Allocate(numElements, true);
length = numElements;
}
template <class T>
void acCArray<T>::Copy(const T *data, int count)
{
if( maxLength < count )
Allocate(count, false);
memcpy(array, data, count*sizeof(T));
}
template <class T>
acCArray<T> &acCArray<T>::operator =(const acCArray<T> ©)
{
Copy(copy.array, copy.length);
return *this;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -