美文网首页
9 UE5 TSparseArray介绍

9 UE5 TSparseArray介绍

作者: 游戏开发程序员 | 来源:发表于2024-02-26 16:59 被阅读0次

TSparseArray 稀疏数组

  • 他是TMap和TSet内存放元素的容器
  • 动态数组,可以认为和TArray差不多
  • 下标不一定是连续的
  • 删除元素消耗为 O(1)
  • 用TBitArray来存储每个元素索引是否使用
  • 用TArray来存储元素
template<typename InElementType,typename Allocator /*= FDefaultSparseArrayAllocator */>
class TSparseArray
{
    using ElementType = InElementType;

    template <typename, typename>
    friend class TScriptSparseArray;
....
....

private:
    typedef TSparseArrayElementOrFreeListLink<
        TAlignedBytes<sizeof(ElementType), alignof(ElementType)>> 
FElementOrFreeListLink;

        // 数据的存储
    typedef TArray<FElementOrFreeListLink,typename Allocator::ElementAllocator> DataType;
    DataType Data;
      
        // // 索引标记存储
    typedef TBitArray<typename Allocator::BitArrayAllocator> AllocationBitArrayType;
    AllocationBitArrayType AllocationFlags;

    int32 FirstFreeIndex;

    int32 NumFreeIndices;

链表结构 TSparseArrayElementOrFreeListLink

  • 在有元素时,这个ElementData就是元素本身,
  • 没有元素时,是前一个和后一个空索引/指针。
  • 当删除某个元素时,这个元素的内存不动,而是将这个元素插入空闲元素链表,通过索引将他们链起来,
  • 在下次插入时,如果链表里有空闲元素,只要找空闲元素,并把这个元素从链表中删除即可。
  • 如下代码
/** Allocated elements are overlapped with free element info in the element list. */
template<typename ElementType>
union TSparseArrayElementOrFreeListLink
{
    /** If the element is allocated, its value is stored here. */
    ElementType ElementData;

    struct
    {
        /** If the element isn't allocated, this is a link to the previous element in the array's free list. */
        int32 PrevFreeIndex;

        /** If the element isn't allocated, this is a link to the next element in the array's free list. */
        int32 NextFreeIndex;
    };
};

删除图解,参考知乎

image.png

代码实操

  • Add 和 RemoveAt
image.png image.png

相关文章

  • 程序打包

    关于UE5打包问题[https://www.bilibili.com/read/cv11679358] UE5 P...

  • 目录、资产命名规范

    【UE5】目录、资产命名规范[https://zhuanlan.zhihu.com/p/484119115]

  • 地理坐标转换

    关联GIS:条条道路通UE5城[https://zhuanlan.zhihu.com/p/528244402] 关...

  • 天登野外课

    自我介绍 9:00-9:15 记忆介绍9:15-9:25 抛物介绍 线路介绍 9:25-9:35距离,路况紧急...

  • 通讯

    开源篇-WebSocket搭建UE5通信桥梁[https://zhuanlan.zhihu.com/p/54621...

  • 调试

    UE4/UE5的崩溃,卡死等问题处理[https://zhuanlan.zhihu.com/p/565680732]

  • 源代码

    从零开始:编译UE5 source code[https://www.jianshu.com/p/4a6b8603...

  • UE 命名规范

    资产命名表格链接:UE5项目命名规则[https://link.zhihu.com/?target=https%3...

  • UE5蓝图-动态创建和查找模型

    UE5蓝图-动态创建和查找模型,并控制其显隐性 蓝图 BP_RedEarth 蓝图 BP_CreateRedEar...

  • 【UE5】Nanite解析

    Epic外放的两大特性Nanite跟Lumen,构成了UE版本升级的基石,关于这两大技术,已经有了众多的分享,不过...

网友评论

      本文标题:9 UE5 TSparseArray介绍

      本文链接:https://www.haomeiwen.com/subject/knbzadtx.html