31#if defined _MSC_VER && _MSC_VER<1900
32#define snprintf _snprintf
39#if defined(HAVE_UNORDERED_MAP)
40#include <unordered_map>
41#define HASHMAP std::unordered_map
43#elif defined(HAVE_TR1_UNORDERED_MAP)
44#include <tr1/unordered_map>
45#define HASHMAP std::tr1::unordered_map
48#elif defined(HAVE_EXT_HASH_MAP)
49#include <ext/hash_map>
50#define HASHMAP __gnu_cxx::hash_map
59 struct hash<std::string> {
60 size_t operator() (
const std::string& x)
const {
61 return hash<const char*>()(x.c_str());
69#define HASHMAP std::map
76#ifdef HAVE_EXT_POOL_ALLOCATOR
77#include <ext/pool_allocator.h>
99 unsigned long maxSize;
102 unsigned long currentSize;
105#ifdef HAVE_EXT_POOL_ALLOCATOR
106 typedef std::list < std::pair<const std::string,RawTile>,
107 __gnu_cxx::__pool_alloc< std::pair<const std::string,RawTile> > > TileList;
109 typedef std::list < std::pair<const std::string,RawTile> > TileList;
113 typedef std::list < std::pair<const std::string,RawTile> >::iterator List_Iter;
116#ifdef HAVE_EXT_POOL_ALLOCATOR
117 typedef HASHMAP < std::string, List_Iter,
118 __gnu_cxx::hash< const std::string >,
119 std::equal_to< const std::string >,
120 __gnu_cxx::__pool_alloc< std::pair<const std::string, List_Iter> >
123 typedef HASHMAP < std::string,List_Iter > TileMap;
139 TileMap::iterator _touch(
const std::string &key ) {
140 TileMap::iterator miter = tileMap.find( key );
141 if( miter == tileMap.end() )
return miter;
143 tileList.splice( tileList.begin(), tileList, miter->second );
153 void _remove(
const TileMap::iterator &miter ) {
155 currentSize -= ( (miter->second->second).dataLength +
156 ( (miter->second->second).filename.capacity() + (miter->second->first).capacity() )*
sizeof(
char) +
158 tileList.erase( miter->second );
159 tileMap.erase( miter );
165 void _remove(
const std::string &key ) {
166 TileMap::iterator miter = tileMap.find( key );
167 this->_remove( miter );
177 maxSize = (
unsigned long)(max*1024000) ; currentSize = 0;
179 tileSize =
sizeof(
RawTile ) +
sizeof( std::pair<const std::string,RawTile> ) +
180 sizeof( std::pair<const std::string, List_Iter> ) +
sizeof(
char)*64 +
sizeof(List_Iter);
202 if( maxSize == 0 )
return;
208 TileMap::iterator miter = this->_touch( key );
211 if( miter != tileMap.end() ){
213 if( miter->second->second.timestamp < r.
timestamp ){
214 this->_remove( miter );
222 tileList.push_front( std::make_pair(key,r) );
225 List_Iter liter = tileList.begin();
226 tileMap[ key ] = liter;
231 currentSize += (r.
dataLength + (r.
filename.capacity()+key.capacity())*
sizeof(
char) + tileSize);
234 while( currentSize > maxSize ) {
236 liter = tileList.end();
238 this->_remove( liter->first );
263 RawTile*
getTile(
const std::string& f,
int r,
int t,
int h,
int v, CompressionType c,
int q ) {
265 if( maxSize == 0 )
return NULL;
267 std::string key = this->
getIndex( f, r, t, h, v, c, q );
269 TileMap::iterator miter = tileMap.find( key );
270 if( miter == tileMap.end() )
return NULL;
273 return &(miter->second->second);
288 std::string
getIndex(
const std::string& f,
int r,
int t,
int h,
int v, CompressionType c,
int q )
const {
290 snprintf( tmp, 1024,
"%s:%d:%d:%d:%d:%d:%d", f.c_str(), r, t, h, v, c, q );
291 return std::string( tmp );
Cache to store raw tile data.
Definition: Cache.h:90
unsigned int getNumElements() const
Return the number of tiles in the cache.
Definition: Cache.h:245
float getMemorySize() const
Return the number of MB stored.
Definition: Cache.h:249
RawTile * getTile(const std::string &f, int r, int t, int h, int v, CompressionType c, int q)
Get a tile from the cache.
Definition: Cache.h:263
void insert(const RawTile &r)
Insert a tile.
Definition: Cache.h:200
void clear()
Empty the cache.
Definition: Cache.h:191
Cache(const float max)
Constructor.
Definition: Cache.h:176
~Cache()
Destructor.
Definition: Cache.h:185
std::string getIndex(const std::string &f, int r, int t, int h, int v, CompressionType c, int q) const
Create a hash index.
Definition: Cache.h:288
Class to represent a single image tile.
Definition: RawTile.h:47
uint32_t dataLength
The size of the data pointed to by the data pointer in bytes.
Definition: RawTile.h:94
std::string filename
Name of the file from which this tile comes.
Definition: RawTile.h:52
time_t timestamp
Tile timestamp.
Definition: RawTile.h:76
int vSequence
The vertical angle to which this tile belongs.
Definition: RawTile.h:88
int tileNum
The tile number for this tile.
Definition: RawTile.h:79
int resolution
The resolution number to which this tile belongs.
Definition: RawTile.h:82
CompressionType compressionType
Compression type.
Definition: RawTile.h:70
int quality
Compression rate or quality.
Definition: RawTile.h:73
int hSequence
The horizontal angle to which this tile belongs.
Definition: RawTile.h:85