#pragma once
#include<iostream>
#include<stdexcept>
#define CHECK_INDEX_RANGE(Index) if (Index >= ElementCount) throw std::out_of_range("索引超出界限")template<typename ElementType>
class TArray
{typedef unsigned int uint;
private:uint Capacity;uint ElementCount;ElementType* Data;
public:TArray();void Add(const ElementType& NewElement);inline uint GetSize(){return ElementCount;}int GetElenmentIndex(const ElementType& Element);void InsertElenmentByIndex(const int& Index, const ElementType& Element);void DeleteElementByIndex(const int& index);void SwapElementByIndex(const int&IndexA,const int & IndexB);void Reverse();void Shuffle();void CheckCapacity();ElementType& operator[](uint Index);
};template<typename ElementType>
inline TArray<ElementType>::TArray()
{ElementCount = 0u;Capacity = 8u;Data = new ElementType[Capacity];
}template<typename ElementType>
inline void TArray<ElementType>::Add(const ElementType& NewElement)
{CheckCapacity();Data[ElementCount] = NewElement;ElementCount++;
}template<typename ElementType>
inline int TArray<ElementType>::GetElenmentIndex(const ElementType& Element)
{for (int i = 0; i < GetSize(); i++){if (Data[i] == Element) {return i;}}return -1;
}template<typename ElementType>
inline void TArray<ElementType>::InsertElenmentByIndex(const int& Index, const ElementType& Element)
{CHECK_INDEX_RANGE(Index);ElementCount++;CheckCapacity();for (int i = ElementCount; i>=Index;i--){Data[i] = Data[i - 1];}Data[Index] = Element;
}template<typename ElementType>
inline void TArray<ElementType>::DeleteElementByIndex(const int& index)
{for (int i = index; i < ElementCount; i++) {Data[i] = Data[i + 1];}ElementCount -= 1;
}template<typename ElementType>
inline void TArray<ElementType>::SwapElementByIndex(const int& IndexA, const int& IndexB)
{CHECK_INDEX_RANGE(IndexA);CHECK_INDEX_RANGE(IndexB);ElementType TempElement;TempElement = Data[IndexA];Data[IndexA] = Data[IndexB];Data[IndexB] = TempElement;
}template<typename ElementType>
inline void TArray<ElementType>::Reverse()
{int EndIndex = ElementCount-1;for (int i = 0; i < ElementCount/2; i++){SwapElementByIndex(i,EndIndex--);}
}template<typename ElementType>
inline void TArray<ElementType>::Shuffle()
{for (int i = 0; i < ElementCount; i++){int RandomIndex = rand() % (ElementCount-1);SwapElementByIndex(i, RandomIndex);}
}template<typename ElementType>
inline void TArray<ElementType>::CheckCapacity()
{if (Capacity <= ElementCount) {auto NewData = new ElementType[Capacity * 2];memcpy(NewData, Data, sizeof(ElementType) * ElementCount);delete Data;Data = NewData;}
}template<typename ElementType>
inline ElementType& TArray<ElementType>::operator[](uint Index)
{CHECK_INDEX_RANGE(Index);return Data[Index];
}