Skip to content

Commit 320d676

Browse files
authored
added support for caching the pair of coordinates for transformation (#520)
* partial implementation * initial implementation * fixed pointer * unique ptr
1 parent a2ecbb3 commit 320d676

File tree

5 files changed

+510
-382
lines changed

5 files changed

+510
-382
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
#include "coordinate_transform_cache.hxx"
3+
#include <iostream>
4+
5+
void CoordinateTransformCache::put(TransformKey key, void* psInfo) {
6+
remove(key);
7+
if( coordLookup.size() >= maxCapacity ) {
8+
int minCount = std::numeric_limits<int>::max();
9+
auto minIt = coordLookup.begin();
10+
for(auto it = coordLookup.begin(); it != coordLookup.end(); it++ ) {
11+
if( it->second->useCount < minCount ) {
12+
minCount = it->second->useCount;
13+
minIt = it;
14+
}
15+
}
16+
remove(minIt->first);
17+
}
18+
19+
coordLookup[key] = std::make_unique<CacheBlock>(psInfo);
20+
}
21+
22+
void* CoordinateTransformCache::get(TransformKey key) {
23+
auto it = coordLookup.find(key);
24+
if( it != coordLookup.end() ) {
25+
it->second->useCount++;
26+
return it->second->item;
27+
}
28+
return nullptr;
29+
}
30+
31+
void CoordinateTransformCache::remove(TransformKey key) {
32+
auto it = coordLookup.find(key);
33+
if( it != coordLookup.end() ) {
34+
coordLookup.erase(it);
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef COORD_TRANSFORM_H
2+
#define COORD_TRANSFORM_H
3+
4+
#include "gdal_alg.h"
5+
#include <map>
6+
#include <limits>
7+
#include <memory>
8+
#include <utility>
9+
10+
typedef std::pair<std::string, std::string> TransformKey;
11+
12+
struct CacheBlock {
13+
void* item;
14+
int useCount;
15+
CacheBlock(void* item) {
16+
this->item = item;
17+
this->useCount = 1;
18+
}
19+
~CacheBlock() {
20+
if( item != nullptr ) {
21+
GDALDestroyGenImgProjTransformer(item);
22+
}
23+
}
24+
};
25+
26+
class CoordinateTransformCache {
27+
public:
28+
void put(TransformKey, void* psInfo);
29+
void* get(TransformKey key);
30+
void remove(TransformKey key);
31+
private:
32+
std::map<TransformKey, std::unique_ptr<CacheBlock>> coordLookup;
33+
size_t maxCapacity = 1024;
34+
};
35+
36+
#endif

0 commit comments

Comments
 (0)