summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/wavm/Include/WAVM/Inline/Impl/HashSetImpl.h
blob: ff7d712be560314a742b50fb4c02f298fd6e2201 (plain) (blame)
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
// IWYU pragma: private, include "WAVM/Inline/HashSet.h"
// You should only include this file indirectly by including HashMap.h.

template<typename Element>
bool HashSetIterator<Element>::operator!=(const HashSetIterator& other) const
{
	return bucket != other.bucket;
}

template<typename Element>
bool HashSetIterator<Element>::operator==(const HashSetIterator& other) const
{
	return bucket == other.bucket;
}

template<typename Element> HashSetIterator<Element>::operator bool() const
{
	return bucket < endBucket && bucket->hashAndOccupancy;
}

template<typename Element> void HashSetIterator<Element>::operator++()
{
	do
	{
		++bucket;
	} while(bucket < endBucket && !bucket->hashAndOccupancy);
}

template<typename Element> const Element& HashSetIterator<Element>::operator*() const
{
	WAVM_ASSERT(bucket->hashAndOccupancy);
	return bucket->storage.get();
}

template<typename Element> const Element* HashSetIterator<Element>::operator->() const
{
	WAVM_ASSERT(bucket->hashAndOccupancy);
	return &bucket->storage.get();
}

template<typename Element>
HashSetIterator<Element>::HashSetIterator(const HashTableBucket<Element>* inBucket,
										  const HashTableBucket<Element>* inEndBucket)
: bucket(inBucket), endBucket(inEndBucket)
{
}

template<typename Element, typename ElementHashPolicy>
HashSet<Element, ElementHashPolicy>::HashSet(Uptr reserveNumElements) : table(reserveNumElements)
{
}

template<typename Element, typename ElementHashPolicy>
HashSet<Element, ElementHashPolicy>::HashSet(const std::initializer_list<Element>& initializerList)
: table(initializerList.size())
{
	for(const Element& element : initializerList)
	{
		const bool result = add(element);
		WAVM_ASSERT(result);
	}
}

template<typename Element, typename ElementHashPolicy>
bool HashSet<Element, ElementHashPolicy>::add(const Element& element)
{
	const Uptr hash = ElementHashPolicy::getKeyHash(element);
	HashTableBucket<Element>& bucket = table.getBucketForAdd(hash, element);
	if(bucket.hashAndOccupancy != 0) { return false; }
	else
	{
		bucket.hashAndOccupancy = hash | HashTableBucket<Element>::isOccupiedMask;
		bucket.storage.construct(element);
		return true;
	}
}

template<typename Element, typename ElementHashPolicy>
void HashSet<Element, ElementHashPolicy>::addOrFail(const Element& element)
{
	const Uptr hash = ElementHashPolicy::getKeyHash(element);
	HashTableBucket<Element>& bucket = table.getBucketForAdd(hash, element);
	WAVM_ASSERT(!bucket.hashAndOccupancy);
	bucket.hashAndOccupancy = hash | HashTableBucket<Element>::isOccupiedMask;
	bucket.storage.construct(element);
}

template<typename Element, typename ElementHashPolicy>
bool HashSet<Element, ElementHashPolicy>::remove(const Element& element)
{
	return table.remove(ElementHashPolicy::getKeyHash(element), element);
}

template<typename Element, typename ElementHashPolicy>
void HashSet<Element, ElementHashPolicy>::removeOrFail(const Element& element)
{
	const bool removed = table.remove(ElementHashPolicy::getKeyHash(element), element);
	WAVM_ASSERT(removed);
}

template<typename Element, typename ElementHashPolicy>
const Element& HashSet<Element, ElementHashPolicy>::operator[](const Element& element) const
{
	const Uptr hash = ElementHashPolicy::getKeyHash(element);
	const HashTableBucket<Element>* bucket = table.getBucketForRead(hash, element);
	WAVM_ASSERT(bucket);
	if(!bucket)
	{
		// In addition to the assert, use WAVM_UNREACHABLE to ensure that GCC's -Wnull-dereference
		// warning isn't triggered because it thinks this function might return nullptr.
		WAVM_UNREACHABLE();
	}
	WAVM_ASSERT(bucket->hashAndOccupancy == (hash | HashTableBucket<Element>::isOccupiedMask));
	return bucket->storage.get();
}

template<typename Element, typename ElementHashPolicy>
bool HashSet<Element, ElementHashPolicy>::contains(const Element& element) const
{
	const Uptr hash = ElementHashPolicy::getKeyHash(element);
	const HashTableBucket<Element>* bucket = table.getBucketForRead(hash, element);
	WAVM_ASSERT(!bucket
				|| bucket->hashAndOccupancy == (hash | HashTableBucket<Element>::isOccupiedMask));
	return bucket != nullptr;
}

template<typename Element, typename ElementHashPolicy>
const Element* HashSet<Element, ElementHashPolicy>::get(const Element& element) const
{
	const Uptr hash = ElementHashPolicy::getKeyHash(element);
	const HashTableBucket<Element>* bucket = table.getBucketForRead(hash, element);
	if(!bucket) { return nullptr; }
	else
	{
		WAVM_ASSERT(bucket->hashAndOccupancy == (hash | HashTableBucket<Element>::isOccupiedMask));
		return &bucket->storage.get();
	}
}

template<typename Element, typename ElementHashPolicy>
void HashSet<Element, ElementHashPolicy>::clear()
{
	table.clear();
}

template<typename Element, typename ElementHashPolicy>
HashSetIterator<Element> HashSet<Element, ElementHashPolicy>::begin() const
{
	// Find the first occupied bucket.
	HashTableBucket<Element>* beginBucket = table.getBuckets();
	HashTableBucket<Element>* endBucket = table.getBuckets() + table.numBuckets();
	while(beginBucket < endBucket && !beginBucket->hashAndOccupancy) { ++beginBucket; };
	return HashSetIterator<Element>(beginBucket, endBucket);
}

template<typename Element, typename ElementHashPolicy>
HashSetIterator<Element> HashSet<Element, ElementHashPolicy>::end() const
{
	return HashSetIterator<Element>(table.getBuckets() + table.numBuckets(),
									table.getBuckets() + table.numBuckets());
}

template<typename Element, typename ElementHashPolicy>
Uptr HashSet<Element, ElementHashPolicy>::size() const
{
	return table.size();
}

template<typename Element, typename ElementHashPolicy>
void HashSet<Element, ElementHashPolicy>::analyzeSpaceUsage(Uptr& outTotalMemoryBytes,
															Uptr& outMaxProbeCount,
															F32& outOccupancy,
															F32& outAverageProbeCount) const
{
	return table.analyzeSpaceUsage(
		outTotalMemoryBytes, outMaxProbeCount, outOccupancy, outAverageProbeCount);
}