This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
tile_cache.cpp
73 lines (56 loc) · 1.54 KB
/
tile_cache.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
62
63
64
65
66
67
68
69
70
71
72
73
#include <mbgl/tile/tile_cache.hpp>
#include <cassert>
namespace mbgl {
void TileCache::setSize(size_t size_) {
size = size_;
while (orderedKeys.size() > size) {
auto key = orderedKeys.front();
orderedKeys.pop_front();
tiles.erase(key);
}
assert(orderedKeys.size() <= size);
}
void TileCache::add(const OverscaledTileID& key, std::unique_ptr<Tile> tile) {
if (!tile->isRenderable() || !size) {
return;
}
// insert new or query existing tile
if (tiles.emplace(key, std::move(tile)).second) {
// remove existing tile key
orderedKeys.remove(key);
}
// (re-)insert tile key as newest
orderedKeys.push_back(key);
// purge oldest key/tile if necessary
if (orderedKeys.size() > size) {
pop(orderedKeys.front());
}
assert(orderedKeys.size() <= size);
}
Tile* TileCache::get(const OverscaledTileID& key) {
auto it = tiles.find(key);
if (it != tiles.end()) {
return it->second.get();
} else {
return nullptr;
}
}
std::unique_ptr<Tile> TileCache::pop(const OverscaledTileID& key) {
std::unique_ptr<Tile> tile;
auto it = tiles.find(key);
if (it != tiles.end()) {
tile = std::move(it->second);
tiles.erase(it);
orderedKeys.remove(key);
assert(tile->isRenderable());
}
return tile;
}
bool TileCache::has(const OverscaledTileID& key) {
return tiles.find(key) != tiles.end();
}
void TileCache::clear() {
orderedKeys.clear();
tiles.clear();
}
} // namespace mbgl