-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockPosHashList.cpp
61 lines (60 loc) · 1.19 KB
/
BlockPosHashList.cpp
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
#include "BlockPosHashList.h"
#include <stdexcept>
void BlockPosHashList::addNode(BlockPos pos)
{
buckets[pos.hash() % buckets.size()].push_back(pos);
numElements++;
}
bool BlockPosHashList::contains(BlockPos pos)
{
auto& bucket = buckets[pos.hash() % buckets.size()];
auto endIter = bucket.end();
auto iter = bucket.begin();
while (iter != endIter)
{
BlockPos curEntry = *iter;
if (curEntry == pos)
return true;
iter++;
}
return false;
}
void BlockPosHashList::remove(BlockPos pos)
{
auto& bucket = buckets[pos.hash() % buckets.size()];
auto endIter = bucket.end();
auto iter = bucket.begin();
while (iter != endIter)
{
BlockPos curEntry = *iter;
if (curEntry == pos)
{
bucket.erase(iter);
numElements--;
return;
}
iter++;
}
throw std::invalid_argument("No such position.");
}
void BlockPosHashList::clearAndSetNumBuckets(size_t num)
{
buckets.clear();
buckets.reserve(num);
for (size_t i = 0; i < num; i++)
{
buckets.push_back(std::list<BlockPos>());
}
}
void BlockPosHashList::clear()
{
auto numBuckets = buckets.size();
for (size_t i = 0; i < numBuckets; i++)
{
buckets[numBuckets].clear();
}
}
size_t BlockPosHashList::size()
{
return numElements;
}