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
71 #endif // End of #if defined
76 #ifdef HAVE_EXT_POOL_ALLOCATOR
77 #include <ext/pool_allocator.h>
100 unsigned long maxSize;
103 unsigned long currentSize;
106 #ifdef HAVE_EXT_POOL_ALLOCATOR
107 typedef std::list < std::pair<const std::string,RawTile>,
108 __gnu_cxx::__pool_alloc< std::pair<const std::string,RawTile> > > TileList;
110 typedef std::list < std::pair<const std::string,RawTile> > TileList;
114 typedef std::list < std::pair<const std::string,RawTile> >::iterator List_Iter;
117 #ifdef HAVE_EXT_POOL_ALLOCATOR
118 typedef HASHMAP < std::string, List_Iter,
119 __gnu_cxx::hash< const std::string >,
120 std::equal_to< const std::string >,
121 __gnu_cxx::__pool_alloc< std::pair<const std::string, List_Iter> >
124 typedef HASHMAP < std::string,List_Iter > TileMap;
140 TileMap::iterator _touch(
const std::string &key ) {
141 TileMap::iterator miter = tileMap.find( key );
142 if( miter == tileMap.end() )
return miter;
144 tileList.splice( tileList.begin(), tileList, miter->second );
154 void _remove(
const TileMap::iterator &miter ) {
156 currentSize -= ( (miter->second->second).dataLength +
157 ( (miter->second->second).filename.capacity() + (miter->second->first).capacity() )*
sizeof(
char) +
159 tileList.erase( miter->second );
160 tileMap.erase( miter );
166 void _remove(
const std::string &key ) {
167 TileMap::iterator miter = tileMap.find( key );
168 this->_remove( miter );
178 maxSize = (
unsigned long)(max*1024000) ; currentSize = 0;
180 tileSize =
sizeof(
RawTile ) +
sizeof( std::pair<const std::string,RawTile> ) +
181 sizeof( std::pair<const std::string, List_Iter> ) +
sizeof(
char)*64 +
sizeof(List_Iter);
203 if( maxSize == 0 )
return;
209 TileMap::iterator miter = this->_touch( key );
212 if( miter != tileMap.end() ){
214 if( miter->second->second.timestamp < r.
timestamp ){
215 this->_remove( miter );
223 tileList.push_front( std::make_pair(key,r) );
226 List_Iter liter = tileList.begin();
227 tileMap[ key ] = liter;
232 currentSize += (r.
dataLength + (r.
filename.capacity()+key.capacity())*
sizeof(
char) + tileSize);
235 while( currentSize > maxSize ) {
237 liter = tileList.end();
239 this->_remove( liter->first );
264 RawTile*
getTile( std::string f,
int r,
int t,
int h,
int v, CompressionType c,
int q ) {
266 if( maxSize == 0 )
return NULL;
268 std::string key = this->
getIndex( f, r, t, h, v, c, q );
270 TileMap::iterator miter = tileMap.find( key );
271 if( miter == tileMap.end() )
return NULL;
274 return &(miter->second->second);
289 std::string
getIndex( std::string f,
int r,
int t,
int h,
int v, CompressionType c,
int q ) {
291 snprintf( tmp, 1024,
"%s:%d:%d:%d:%d:%d:%d", f.c_str(), r, t, h, v, c, q );
292 return std::string( tmp );