/*Container.h 所有容器的基类
/*MemoryObject 内存申请基类 我使用TBB申请内存
*/
template<typename T>
class Container :public MemoryObject
{
protected:
T* C_New(unsigned int number) {
if (!number) return nullptr;
T* ptr = (T*)GetMemoryDispatcher().Allocate(number * sizeof(T), 0, true);
if (!ptr) return nullptr;
return ptr;
}
void C_Delete(T*& ptr, unsigned int number) {
if (!ptr) return;
if (number > 0)
{
if (ValueBase<T>::NeedsConstructor) {
for (unsigned int i = 0; i < number; i++)
{
(ptr + i)->~T();
}
}
}
GetMemoryDispatcher().Deallocate((char*)ptr, 0, true);
ptr = nullptr;
}
};
/*Array.h
*/
template<typename T>
class Array :public Container<T>
{
public:
Array(unsigned int length = DEFAULT_LENGTH);
Array(const Array<T>& array);
~Array();
unsigned int GetLength() const;
unsigned int GetCount() const;
T* GetPtr() const;
unsigned int AddValue(const T& element);
void Sort();
unsigned int IndexOf(const T& element);
void Union(const Array<T> array);
void Clear();
void Destroy();
T& operator[] (unsigned int i)const;
void operator=(const Array<T>& array);
private:
const static unsigned int DEFAULT_LENGTH = 4;
protected:
T* m_Buffer;
unsigned int m_Length;
unsigned int m_Count;
public:
class ArrayIterator
{
public:
ArrayIterator(T* node) :m_Node(node) {}
ArrayIterator(const ArrayIterator& iterator) { m_Node = iterator.m_Node; }
~ArrayIterator() = default;
//运算符重载
inline void operator= (const ArrayIterator& iterator) { m_Node = iterator.m_Node; }
inline bool operator!=(const ArrayIterator& iterator) { return (m_Node != iterator.m_Node); }
inline bool operator==(const ArrayIterator& iterator) { return (m_Node == iterator.m_Node); }
inline ArrayIterator operator++() { m_Node++; return (*this); }
inline ArrayIterator operator++(int) { ArrayIterator temp = *this; ++* this; return temp; }
inline ArrayIterator operator--() { m_Node--; return (*this); }
inline ArrayIterator operator--(int) { ArrayIterator temp = *this; --* this; return temp; }
inline T& operator*() const { return *m_Node; };
inline T* operator->() const { return m_Node; };
protected:
T* m_Node;
};
inline ArrayIterator Begin() const { return (ArrayIterator)m_Buffer; }
inline ArrayIterator End() const { return (ArrayIterator(m_Buffer + GetLength())); }
};
#include "Array.inl"
/*Array.inl
/*部分函数待更新 先够用就可以了
*/
#include "Array.h"
template<typename T>
inline Array<T>::Array(unsigned int length)
{
MAC_ASSERT(length);
m_Buffer = this->C_New(length);
ZeroMemory(m_Buffer, sizeof(T) * length);
m_Length = length;
m_Count = 0;
}
template<typename T>
inline Array<T>::Array(const Array<T>& array)
{
m_Buffer = nullptr;
m_Length = 0;
m_Count = 0;
*this = array;
}
template<typename T>
inline Array<T>::~Array()
{
this->C_Delete(m_Buffer, m_Length);
m_Length = 0;
m_Count = 0;
}
template<typename T>
inline unsigned int Array<T>::GetLength() const
{
return m_Length;
}
template<typename T>
inline unsigned int Array<T>::GetCount() const
{
return m_Count;
}
template<typename T>
inline T* Array<T>::GetPtr() const
{
return m_Buffer;
}
template<typename T>
inline unsigned int Array<T>::AddValue(const T& element)
{
if (m_Length >= m_Count) {
S_NEW(m_Buffer + m_Count)T(element);
m_Count++;
return m_Count - 1;
}
return m_Length;
//TODO 抛异常
}
template<typename T>
inline void Array<T>::Clear()
{
if (m_Count == 0) return;
if (ValueBase<T>::NeedsDestructor) {
for (unsigned int i = 0; i < m_Count; i++)
{
m_Buffer[i].~T();
}
}
m_Count = 0;
}
template<typename T>
inline void Unknown::Array<T>::Destroy()
{
this->Clear();
m_Count = 0;
Delete(m_Buffer, m_Length);
m_Length = 0;
}
template<typename T>
inline void Array<T>::Sort()
{
}
template<typename T>
inline unsigned int Array<T>::IndexOf(const T& element)
{
for (unsigned int i = 0; i < m_Count; i++)
{
if (element == m_Buffer[i]) return i;
}
return m_Length-1; //返回最后一个数据索引
}
template<typename T>
inline void Array<T>::Union(const Array<T> array)
{
}
template<typename T>
inline T& Array<T>::operator[](unsigned int i) const
{
MAC_ASSERT(i < m_Count);
return m_Buffer[i];
}
template<typename T>
inline void Array<T>::operator=(const Array<T>& array)
{
if (m_Length >= array.GetLength())
{
this->Clear();
m_Count = array.GetCount();
m_Length = array.GetLength();
T* tPtr = array.GetPtr();
for (unsigned int i = 0; i < m_Count; i++)
{
S_NEW(m_Buffer + i)T(tPtr[i]);
}
}
else
{
this->Clear();
m_Count = array.GetCount();
m_Length = array.GetLength();
T* tPtr = array.GetPtr();
m_Buffer = this->C_New(m_Length);
ZeroMemory(m_Buffer, sizeof(T) * m_Length);
if (!m_Buffer) return;
for (unsigned int i = 0; i <m_Count ; i++)
{
S_NEW(m_Buffer + i)T(tPtr[i]);
}
}
标签:const,int,unsigned,C++,return,数组,inline,Array,模板 From: https://www.cnblogs.com/zjr0/p/16759928.html