diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-02-23 12:15:46 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-02-23 12:15:46 -0500 |
commit | 3504fd5080dbcfd0172299c5c6d13895e53ad163 (patch) | |
tree | caab8e868566262b3aa4d80aa6a06cdf13e7c21d /src/grid_cache.h | |
parent | 15b511e694f976686fdec1fb9f959f8a92f3b594 (diff) | |
download | dispatcher-3504fd5080dbcfd0172299c5c6d13895e53ad163.tar.gz dispatcher-3504fd5080dbcfd0172299c5c6d13895e53ad163.tar.bz2 dispatcher-3504fd5080dbcfd0172299c5c6d13895e53ad163.zip |
Removed position caches
There aren't going to be enough entities at once for position checking to ever really be a bottleneck, I don't think. Removing the caches makes the range logic a bit more intuitive, and it removes the possibility of accidentally not updating a cache when it needs to be.
Diffstat (limited to 'src/grid_cache.h')
-rw-r--r-- | src/grid_cache.h | 59 |
1 files changed, 0 insertions, 59 deletions
diff --git a/src/grid_cache.h b/src/grid_cache.h deleted file mode 100644 index e837f55..0000000 --- a/src/grid_cache.h +++ /dev/null | |||
@@ -1,59 +0,0 @@ | |||
1 | #ifndef GRID_CACHE_H_67BBE74D | ||
2 | #define GRID_CACHE_H_67BBE74D | ||
3 | |||
4 | #include <unordered_map> | ||
5 | #include <unordered_set> | ||
6 | #include "level.h" | ||
7 | |||
8 | template <typename T> | ||
9 | class GridCache { | ||
10 | public: | ||
11 | |||
12 | explicit GridCache(const Level& level) : width_(level.getSize().w()) | ||
13 | { | ||
14 | } | ||
15 | |||
16 | void set(T value, vec2s pos) | ||
17 | { | ||
18 | if (reverse_.count(value)) | ||
19 | { | ||
20 | size_t oldPosIndex = reverse_.at(value); | ||
21 | lookup_[oldPosIndex].erase(value); | ||
22 | } | ||
23 | |||
24 | size_t newPosIndex = getIndex(pos); | ||
25 | lookup_[newPosIndex].insert(value); | ||
26 | reverse_[value] = newPosIndex; | ||
27 | } | ||
28 | |||
29 | void remove(T value) | ||
30 | { | ||
31 | if (reverse_.count(value)) | ||
32 | { | ||
33 | size_t index = reverse_.at(value); | ||
34 | lookup_[index].erase(value); | ||
35 | reverse_.erase(value); | ||
36 | } | ||
37 | } | ||
38 | |||
39 | const std::unordered_set<T>& at(vec2s pos) const | ||
40 | { | ||
41 | size_t index = getIndex(pos); | ||
42 | |||
43 | return lookup_[index]; | ||
44 | } | ||
45 | |||
46 | private: | ||
47 | |||
48 | inline size_t getIndex(const vec2s& pos) const | ||
49 | { | ||
50 | return pos.x() + pos.y() * width_; | ||
51 | } | ||
52 | |||
53 | size_t width_; | ||
54 | |||
55 | mutable std::unordered_map<size_t, std::unordered_set<T>> lookup_; | ||
56 | std::unordered_map<T, size_t> reverse_; | ||
57 | }; | ||
58 | |||
59 | #endif /* end of include guard: GRID_CACHE_H_67BBE74D */ | ||