forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket_map.cpp
54 lines (45 loc) · 1.44 KB
/
bucket_map.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
#include "stdafx.h"
#include "bucket_map.h"
#include "creature.h"
template <class T>
template <class Archive>
void BucketMap<T>::serialize(Archive& ar, const unsigned int version) {
ar& SVAR(bucketSize)
& SVAR(buckets);
CHECK_SERIAL;
}
SERIALIZABLE(BucketMap<Creature*>);
template <class T>
SERIALIZATION_CONSTRUCTOR_IMPL2(BucketMap<T>, BucketMap);
template<class T>
BucketMap<T>::BucketMap(int w, int h, int size)
: bucketSize(size), buckets((w + size - 1) / size, (h + size - 1) / size) {
}
template<class T>
void BucketMap<T>::addElement(Vec2 v, T elem) {
CHECK(!buckets[v.x / bucketSize][v.y / bucketSize].count(elem));
buckets[v.x / bucketSize][v.y / bucketSize].insert(elem);
}
template<class T>
void BucketMap<T>::removeElement(Vec2 v, T elem) {
CHECK(buckets[v.x / bucketSize][v.y / bucketSize].count(elem));
buckets[v.x / bucketSize][v.y / bucketSize].erase(elem);
}
template<class T>
void BucketMap<T>::moveElement(Vec2 from, Vec2 to, T elem) {
removeElement(from, elem);
addElement(to, elem);
}
template<class T>
vector<T> BucketMap<T>::getElements(Rectangle area) const {
vector<T> ret;
Rectangle allBuckets = Rectangle(
area.getPX() / bucketSize, area.getPY() / bucketSize,
area.getKX() / bucketSize, area.getKY() / bucketSize)
.intersection(Rectangle(buckets.getBounds()));
for (Vec2 v : allBuckets)
for (T elem : buckets[v])
ret.push_back(elem);
return ret;
}
template class BucketMap<Creature*>;