当前位置:   article > 正文

手写 UE4中的 TArray

手写 UE4中的 TArray
#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;
	}
	// 1.返回特定元素的索引,如果没有改元素,返回-1
	int GetElenmentIndex(const ElementType& Element);
	// 2.在特定位置插入元素
	void InsertElenmentByIndex(const int& Index, const ElementType& Element);
	// 3.删除特定位置元素
	void DeleteElementByIndex(const int& index);
	// 4.将特定索引处的元素和另外一个特定索引处的元素互换
	void SwapElementByIndex(const int&IndexA,const int &  IndexB);
	// 5.反转整个数组
	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()
{
	// 方法一:创建一个新的数组
	/*auto NewData = new ElementType[ElementCount];
	for (int i = 0,j=ElementCount-1; i < ElementCount; i++,j--)
	{
		NewData[i] = Data[j];
	}
	delete Data;
	Data = NewData;*/

	// 方法二:将第一个和最后一个交换 ,第二个和倒数第二个交换,以此类推
	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];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/260876
推荐阅读
相关标签
  

闽ICP备14008679号