Created
July 28, 2017 18:30
-
-
Save AlexBolotsin/c5ecaf5aafc7f81c596229ec3959f1cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Image | |
{ | |
public: | |
int sizeInBytes | |
//... | |
}; | |
Image makeMiniatureImage(Image img) { | |
//... | |
return img; | |
} | |
void removeImageFromDisk(Image img) { | |
//... | |
} | |
class ImageStorage | |
{ | |
std::map<std::string, Image> _imageMap; | |
Image GetImage(std::string& name) { | |
auto itr = _imageMap.find(name); | |
if (itr == _imageMap.end()) | |
throw std::exception(); | |
// developer thought that name is a local variable | |
name = name.substr(5); | |
// some kind of preview image | |
if (name.size() < 2) | |
return makeMiniatureImage(itr->second); | |
return itr->second; | |
} | |
bool AddImage(std::string& name, Image image) | |
{ | |
auto itr = _imageMap.find(name); | |
if (itr == _imageMap.end()) | |
return false; | |
_imageMap[name] = image; | |
return true; | |
} | |
size_t CalculateImagesDiskSpace() | |
{ | |
size_t space = 0; | |
for (auto& pair : _imageMap) | |
{ | |
space += pair.second.sizeInBytes; | |
if (pair.second.sizeInBytes == 0) | |
{ | |
removeImageFromDisk(pair.second); | |
_imageMap.erase(_imageMap.find(pair.first)); | |
} | |
} | |
return space; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment