手写 UE4中的 TArray

2024-03-15 19:12
文章标签 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.返回特定元素的索引,如果没有改元素,返回-1int 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];
}

这篇关于手写 UE4中的 TArray的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/812985

相关文章

stl的sort和手写快排的运行效率哪个比较高?

STL的sort必然要比你自己写的快排要快,因为你自己手写一个这么复杂的sort,那就太闲了。STL的sort是尽量让复杂度维持在O(N log N)的,因此就有了各种的Hybrid sort algorithm。 题主你提到的先quicksort到一定深度之后就转为heapsort,这种是introsort。 每种STL实现使用的算法各有不同,GNU Standard C++ Lib

JS手写实现深拷贝

手写深拷贝 一、通过JSON.stringify二、函数库lodash三、递归实现深拷贝基础递归升级版递归---解决环引用爆栈问题最终版递归---解决其余类型拷贝结果 一、通过JSON.stringify JSON.parse(JSON.stringify(obj))是比较常用的深拷贝方法之一 原理:利用JSON.stringify 将JavaScript对象序列化成为JSO

T1打卡——mnist手写数字识别

🍨 本文为🔗365天深度学习训练营中的学习记录博客🍖 原作者:K同学啊 1.定义GPU import tensorflow as tfgpus=tf.config.list_physical_devices("GPU")if gpus:gpu0=gpus[0]tf.config.experimental.set_memort_groth(gpu0,True) #设置GPU现存用量按需

【DL--22】实现神经网络算法NeuralNetwork以及手写数字识别

1.NeuralNetwork.py #coding:utf-8import numpy as np#定义双曲函数和他们的导数def tanh(x):return np.tanh(x)def tanh_deriv(x):return 1.0 - np.tanh(x)**2def logistic(x):return 1/(1 + np.exp(-x))def logistic_derivati

【tensorflow CNN】构建cnn网络,识别mnist手写数字识别

#coding:utf8"""构建cnn网络,识别mnistinput conv1 padding max_pool([2,2],strides=[2,2]) conv2 x[-1,28,28,1] 卷积 [5,5,1,32] -> [-1,24,24,32]->[-1,28,

【tensorflow 全连接神经网络】 minist 手写数字识别

主要内容: 使用tensorflow构建一个三层全连接传统神经网络,作为字符识别的多分类器。通过字符图片预测对应的数字,对mnist数据集进行预测。 # coding: utf-8from tensorflow.examples.tutorials.mnist import input_dataimport tensorflow as tfimport matplotlib.pyplot

【UE4源代码观察】观察Core模块

话题 Core模块是整个引擎中最核心的模块,在之前的博客【UE4源代码观察】可视化所有模块的依赖情况中有统计,它被983个模块引用,恐怕除了第三方的模块外基本所有模块都有引用。我想首先观察其中的内容,然后再做测试:将Core模块拷贝到之前【UE4源代码观察】手动建立一个使用UBT进行编译的空白工程建立的空白工程中,看能否将它成功编译,理论上讲,“核心”不应再依赖太多其他的东西,所以我应该不会再需

【UE4源代码观察】在空白工程中测试跨模块调用函数

目的 在之前的博客【UE4源代码观察】手动建立一个使用UBT进行编译的空白工程中,成功让UBT工作起来了。现在我想要测试编译出的多个模块之间是否能互相调用,我记录下测试的过程。最终工程见 工程GIT链接。 实践 第一部分 首先仿照TestA模块建立TestB模块。 在TestB.h文件中声明了一个函数 int TestBFunc(int x, int y); 并在TestB.cpp中

【UE4源代码观察】观察FEngineLoop中各函数的调用顺序

当启动编辑器时,Launch模块是启动模块,在Windows平台中 LaunchWindows.cpp 的 WinMain函数是入口,而 FEngineLoop 管理了程序的初始化与主循环。他的成员函数如下: public:/*** Pre-Initialize the main loop, and generates the commandline from standard ArgC/Arg

【UE4源代码观察】手动建立一个使用UBT进行编译的空白工程

我想观察UE4是怎么编译的,于是查阅官方文档,了解到UE4有一套自己的编译工具:UnrealBuildTool,简称UBT。关于UBT的官方文档参阅:虚幻编译工具。我想尝试自己手动建立一个使用UBT进行编译的空白工程。不过首先,先了解下UBT的编译流程中一些文件所扮演的角色 UBT的编译流程中一些文件所扮演的角色 模块 每个模块都由一个 .build.cs 文件声明,它存储在 Source