UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
VVMBitMap.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#if WITH_VERSE_VM || defined(__INTELLISENSE__)
5
6#include "VerseVM/VVMInt.h"
8
9namespace Verse
10{
11
12// Static bit-map that uses VInt as its storage (Bits are 0-indexed)
13// Thus, its max size is currently `VHeapInt::MaxLength`
14struct VBitMap
15{
16 static const uint32 NumDigitBits = 32;
17
18 TWriteBarrier<VInt> Storage;
19
20 // one must call init after default construction before using other functionality otherwise one is likely to crash
21 VBitMap() = default;
22
23 VBitMap(FAllocationContext Context, uint32 InSize, bool bSet = false) { Init(Context, InSize, bSet); }
24 VBitMap(FAllocationContext Context, const VBitMap& Other)
25 {
26 if (VHeapInt* HeapInt = Other.Storage.Get().DynamicCast<VHeapInt>())
27 {
28 Storage.Set(Context, VInt(VValue(*VHeapInt::Copy(Context, *HeapInt))));
29 }
30 else
31 {
32 Storage = Other.Storage;
33 }
34 }
35
36 void Init(FAllocationContext Context, uint32 InSize, bool bSet = false)
37 {
38 if (InSize < NumDigitBits)
39 {
40 int32 Int32 = 0;
41 if (bSet)
42 {
43 Int32 = (1 << InSize) - 1;
44 }
45 Storage.Set(Context, VInt(Int32));
46 }
47 else
48 {
49 uint64 Size = FMath::DivideAndRoundUp<uint64>(InSize, NumDigitBits);
50 VHeapInt* HeapInt = VHeapInt::CreateWithLength(Context, Size);
51 HeapInt->Initialize(VHeapInt::InitializationType::WithZero);
52 if (bSet)
53 {
54 for (uint32 Index = 0; Index < HeapInt->GetLength() - 1; ++Index)
55 {
56 HeapInt->SetDigit(Index, UINT32_MAX);
57 }
58 HeapInt->SetDigit(HeapInt->GetLength() - 1, InSize % NumDigitBits == 0 ? UINT32_MAX : (1 << InSize % NumDigitBits) - 1);
59 }
60 Storage.Set(Context, VInt(VValue(*HeapInt))); // We need to construct this way to avoid having the VInt ctor convert the heap int into a plain VValue
61 }
62 }
63
64 bool Equals(FAllocationContext Context, const VBitMap& Other) const
65 {
66 if (!Storage || !Other.Storage)
67 {
68 return Storage == Other.Storage;
69 }
70 return VInt::Eq(Context, Storage.Get(), Other.Storage.Get());
71 }
72
73 void SetBit(FAllocationContext Context, uint32 BitIndex)
74 {
75 checkSlow(BitIndex < Size());
76 if (IsInt32())
77 {
78 Storage.Set(Context, VInt(AsInt32() | (1 << BitIndex)));
79 }
80 else
81 {
82 DigitRef(BitIndex) |= (1 << BitIndex % NumDigitBits);
83 }
84 }
85
86 void UnsetBit(FAllocationContext Context, uint32 BitIndex)
87 {
88 checkSlow(BitIndex < Size());
89 if (IsInt32())
90 {
91 Storage.Set(Context, VInt(AsInt32() & ~(1 << BitIndex)));
92 }
93 else
94 {
95 DigitRef(BitIndex) &= ~(1 << BitIndex % NumDigitBits);
96 }
97 }
98
99 // returns true if bit is set
100 bool CheckBit(uint32 BitIndex)
101 {
102 checkSlow(BitIndex < Size());
103 if (IsInt32())
104 {
105 return AsInt32() & (1 << BitIndex);
106 }
107 else
108 {
109 return DigitRef(BitIndex) & (1 << BitIndex % NumDigitBits);
110 }
111 }
112
113 void Clear(FAllocationContext Context)
114 {
115 if (IsInt32())
116 {
117 Storage.Set(Context, VInt(0));
118 }
119 else
120 {
121 VHeapInt& HeapInt = Storage.Get().StaticCast<VHeapInt>();
122 memset(HeapInt.DataStorage(), 0, HeapInt.GetLength() * sizeof(VHeapInt::Digit));
123 }
124 }
125
126 bool IsZero()
127 {
128 return Storage.Get().IsZero();
129 }
130
131 uint64 Size()
132 {
133 if (IsInt32())
134 {
135 return NumDigitBits;
136 }
137 else
138 {
139 VHeapInt& HeapInt = Storage.Get().StaticCast<VHeapInt>();
140 return HeapInt.GetLength() * NumDigitBits;
141 }
142 }
143
144 template <typename TVisitor>
145 friend void Visit(TVisitor& Visitor, VBitMap& Value)
146 {
147 Visitor.Visit(Value.Storage, TEXT("Storage"));
148 }
149
150private:
151 uint32& DigitRef(uint64 BitIndex)
152 {
153 return Storage.Get().StaticCast<VHeapInt>().DataStorage()[BitIndex / NumDigitBits];
154 }
155
156 int32 AsInt32()
157 {
158 return Storage.Get().AsInt32();
159 }
160
161 bool IsInt32()
162 {
163 return Storage.Get().IsInt32();
164 }
165};
166
167inline uint32 GetTypeHash(const VBitMap& BitMap)
168{
169 return GetTypeHash(BitMap.Storage);
170}
171
172}; // namespace Verse
173
174#endif // WITH_VERSE_VM
#define checkSlow(expr)
Definition AssertionMacros.h:332
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
void Init()
Definition LockFreeList.h:4
decltype(auto) Visit(Func &&Callable, Variants &&... Args)
Definition TVariant.h:271
uint32 Size
Definition VulkanMemory.cpp:4034
uint32_t uint32
Definition binka_ue_file_header.h:6
@ Visitor
Definition XmppMultiUserChat.h:94
Definition Archive.h:36
uint32 GetTypeHash(TPtrVariant< Ts... > Ptr)
Definition VVMPtrVariant.h:83
U16 Index
Definition radfft.cpp:71