File: tile-cache.hpp

package info (click to toggle)
tippecanoe 2.53.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 148,236 kB
  • sloc: cpp: 44,069; ansic: 2,057; makefile: 454; perl: 129; python: 62; sh: 4
file content (46 lines) | stat: -rw-r--r-- 1,022 bytes parent folder | download | duplicates (2)
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
#include <functional>
#include "mvt.hpp"
#include "dirtiles.hpp"	 // for zxy

struct mvt_tile_seq {
	mvt_tile tile;
	size_t seq;
};

struct tile_cache {
	std::map<zxy, mvt_tile_seq> overzoom_cache;
	std::atomic<size_t> seq;
	size_t capacity = 1000;

	mvt_tile get(zxy parent_tile, std::function<mvt_tile(zxy)> getter) {
		mvt_tile source;
		auto f = overzoom_cache.find(parent_tile);
		if (f == overzoom_cache.end()) {
			if (overzoom_cache.size() >= capacity) {
				// evict the oldest tile to make room

				auto to_erase = overzoom_cache.begin();
				for (auto here = overzoom_cache.begin(); here != overzoom_cache.end(); ++here) {
					if (here->second.seq < to_erase->second.seq) {
						to_erase = here;
					}
				}

				overzoom_cache.erase(to_erase);
			}

			source = getter(parent_tile);

			mvt_tile_seq to_cache;
			to_cache.tile = source;
			to_cache.seq = seq++;

			overzoom_cache.emplace(parent_tile, to_cache);
		} else {
			f->second.seq = seq++;
			source = f->second.tile;
		}

		return source;
	}
};