static void shrink_cache() { #ifdef DEBUG_MINIMAP_CACHE std::cerr << "\nShrink cache from " << cache.size(); #else DBG_GUI_D << "Shrinking the minimap cache.\n"; #endif std::vector<std::pair<unsigned, tcache::iterator> > items; for(tcache::iterator itor = cache.begin(); itor != cache.end(); ++itor) { itor->second.age /= 2; items.push_back(std::make_pair(itor->second.age, itor)); } std::partial_sort(items.begin() , items.begin() + cache_max_size / 4 , items.end() , compare); for(std::vector<std::pair<unsigned, tcache::iterator> >::iterator vitor = items.begin() ; vitor < items.begin() + cache_max_size / 4 ; ++vitor) { cache.erase(vitor->second); } #ifdef DEBUG_MINIMAP_CACHE std::cerr << " to " << cache.size() << ".\n"; #endif }
const surface tminimap::get_image(const int w, const int h) const { if(!terrain_) { return NULL; } if(terrain_ != terrain) { #ifdef DEBUG_MINIMAP_CACHE std::cerr << "\nFlush cache.\n"; #else DBG_GUI_D << "Flushing the minimap cache.\n"; #endif terrain = terrain_; cache.clear(); } const tkey key(w, h, map_data_); tcache::iterator itor = cache.find(key); if(itor != cache.end()) { #ifdef DEBUG_MINIMAP_CACHE std::cerr << '+'; #endif itor->second.age++; return itor->second.surf; } if(cache.size() >= cache_max_size) { shrink_cache(); } try { const gamemap map(boost::make_shared<terrain_type_data>(*terrain_), map_data_); const surface surf = image::getMinimap(w, h, map, NULL); cache.insert(std::make_pair(key, tvalue(surf))); #ifdef DEBUG_MINIMAP_CACHE std::cerr << '-'; #endif return surf; } catch(incorrect_map_format_error& e) { ERR_CF << "Error while loading the map: " << e.message << '\n'; #ifdef DEBUG_MINIMAP_CACHE std::cerr << 'X'; #endif } return NULL; }