File tree 5 files changed +510
-382
lines changed
5 files changed +510
-382
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments