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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
#include "slang-uint-set.h"
namespace Slang
{
Index UIntSet::getLSBZero()
{
uint64_t offset = 0;
for (Element& element : this->m_buffer)
{
// Flip all bits so bitscanForward can find a 0 bit
Element flippedElement = ~element;
// continue if we don't have 0 bits
if (flippedElement == 0)
{
offset += sizeof(Element) * 8;
continue;
}
// Get LSBZero of current Block, add with offset
return bitscanForward(flippedElement) + offset;
}
return offset;
}
UIntSet& UIntSet::operator=(UIntSet&& other)
{
m_buffer = _Move(other.m_buffer);
return *this;
}
UIntSet& UIntSet::operator=(const UIntSet& other)
{
m_buffer = other.m_buffer;
return *this;
}
HashCode UIntSet::getHashCode() const
{
int rs = 0;
for (auto val : m_buffer)
rs ^= val;
return rs;
}
void UIntSet::resizeAndClear(UInt val)
{
// TODO(JS): This could be faster in that if the resize is larger the additional area is cleared twice
resize(val);
clear();
}
void UIntSet::setAll()
{
::memset(m_buffer.getBuffer(), -1, m_buffer.getCount() * sizeof(Element));
}
void UIntSet::resize(UInt size)
{
const Index newCount = Index((size + kElementMask) >> kElementShift);
resizeBackingBufferDirectly(newCount);
}
void UIntSet::clear()
{
::memset(m_buffer.getBuffer(), 0, m_buffer.getCount() * sizeof(Element));
}
bool UIntSet::isEmpty() const
{
return _areAllZero(m_buffer.getBuffer(), m_buffer.getCount());
}
void UIntSet::clearAndDeallocate()
{
m_buffer.clearAndDeallocate();
}
void UIntSet::unionWith(const UIntSet& set)
{
const Index minCount = Math::Min(set.m_buffer.getCount(), m_buffer.getCount());
for (Index i = 0; i < minCount; i++)
{
m_buffer[i] |= set.m_buffer[i];
}
if (set.m_buffer.getCount() > m_buffer.getCount())
m_buffer.addRange(set.m_buffer.getBuffer() + m_buffer.getCount(), set.m_buffer.getCount() - m_buffer.getCount());
}
bool UIntSet::operator==(const UIntSet& set) const
{
const Index aCount = m_buffer.getCount();
const auto aElems = m_buffer.getBuffer();
const Index bCount = set.m_buffer.getCount();
const auto bElems = set.m_buffer.getBuffer();
const Index minCount = Math::Min(aCount, bCount);
return ::memcmp(aElems, bElems, minCount*sizeof(Element)) == 0 &&
_areAllZero(aElems + minCount, aCount - minCount) &&
_areAllZero(bElems + minCount, bCount - minCount);
}
void UIntSet::intersectWith(const UIntSet& set)
{
if (set.m_buffer.getCount() < m_buffer.getCount())
::memset(m_buffer.getBuffer() + set.m_buffer.getCount(), 0, (m_buffer.getCount() - set.m_buffer.getCount()) * sizeof(Element));
const Index minCount = Math::Min(set.m_buffer.getCount(), m_buffer.getCount());
for (Index i = 0; i < minCount; i++)
{
m_buffer[i] &= set.m_buffer[i];
}
}
void UIntSet::subtractWith(const UIntSet& set)
{
const Index minCount = Math::Min(this->m_buffer.getCount(), set.m_buffer.getCount());
for (Index i = 0; i < minCount; i++)
{
this->m_buffer[i] = this->m_buffer[i] & (~set.m_buffer[i]);
}
}
/* static */void UIntSet::calcUnion(UIntSet& outRs, const UIntSet& set1, const UIntSet& set2)
{
outRs.resizeBackingBufferDirectly(Math::Max(set1.m_buffer.getCount(), set2.m_buffer.getCount()));
outRs.clear();
for (Index i = 0; i < set1.m_buffer.getCount(); i++)
outRs.m_buffer[i] |= set1.m_buffer[i];
for (Index i = 0; i < set2.m_buffer.getCount(); i++)
outRs.m_buffer[i] |= set2.m_buffer[i];
}
/* static */void UIntSet::calcIntersection(UIntSet& outRs, const UIntSet& set1, const UIntSet& set2)
{
const Index minCount = Math::Min(set1.m_buffer.getCount(), set2.m_buffer.getCount());
outRs.resizeBackingBufferDirectly(minCount);
for (Index i = 0; i < minCount; i++)
outRs.m_buffer[i] = set1.m_buffer[i] & set2.m_buffer[i];
}
/* static */void UIntSet::calcSubtract(UIntSet& outRs, const UIntSet& set1, const UIntSet& set2)
{
outRs.resizeBackingBufferDirectly(set1.m_buffer.getCount());
const Index minCount = Math::Min(set1.m_buffer.getCount(), set2.m_buffer.getCount());
for (Index i = 0; i < minCount; i++)
outRs.m_buffer[i] = set1.m_buffer[i] & (~set2.m_buffer[i]);
}
/* static */bool UIntSet::hasIntersection(const UIntSet& set1, const UIntSet& set2)
{
const Index minCount = Math::Min(set1.m_buffer.getCount(), set2.m_buffer.getCount());
for (Index i = 0; i < minCount; i++)
{
if (set1.m_buffer[i] & set2.m_buffer[i])
return true;
}
return false;
}
Index UIntSet::countElements() const
{
// TODO: This can be made faster using SIMD intrinsics to count set bits.
uint64_t tmp;
constexpr Index loopSize = ((sizeof(Element) / sizeof(tmp)) != 0) ? sizeof(Element) / sizeof(tmp) : 1;
Index count = 0;
for (auto index = 0; index < this->m_buffer.getCount(); index++)
{
for (auto i = 0; i < loopSize; i++)
{
tmp = m_buffer[index] >> (sizeof(tmp) * i);
tmp = tmp - ((tmp >> 1) & 0x5555555555555555);
tmp = (tmp & 0x3333333333333333) + ((tmp >> 2) & 0x3333333333333333);
count += ((tmp + (tmp >> 4) & 0xF0F0F0F0F0F0F0F) * 0x101010101010101) >> 56;
}
}
return count;
}
}
|