move weak_ptr list of tile_data objects to a map for faster lookups

This commit is contained in:
Konstantin Käfer
2014-05-13 12:01:29 +02:00
parent 654520ea34
commit d8fa9aa322
3 changed files with 26 additions and 10 deletions

View File

@@ -12,6 +12,7 @@
#include <vector>
#include <string>
#include <map>
#include <set>
namespace llmr {
@@ -69,7 +70,7 @@ private:
const int32_t max_zoom;
std::forward_list<Tile> tiles;
std::forward_list<std::weak_ptr<TileData>> tile_data;
std::map<Tile::ID, std::weak_ptr<TileData>> tile_data;
};
}

View File

@@ -11,6 +11,23 @@ template<typename T, typename ...Args>
return ::std::unique_ptr<T>(new T(::std::forward<Args>(args)...));
}
template <typename Container, typename ForwardIterator, typename Predicate>
void erase_if(Container &container, ForwardIterator it, const ForwardIterator end, Predicate pred) {
while (it != end) {
if (pred(*it)) {
container.erase(it++);
} else {
++it;
}
}
}
template <typename Container, typename Predicate>
void erase_if(Container &container, Predicate pred) {
erase_if(container, container.begin(), container.end(), pred);
}
}
#endif

View File

@@ -7,6 +7,7 @@
#include <llmr/util/string.hpp>
#include <llmr/util/texturepool.hpp>
#include <llmr/util/vec.hpp>
#include <llmr/util/std.hpp>
#include <llmr/geometry/glyph_atlas.hpp>
@@ -110,13 +111,10 @@ TileData::State Source::addTile(const Tile::ID& id) {
// Try to find the associated TileData object.
const Tile::ID normalized_id = id.normalized();
auto it = std::find_if(tile_data.begin(), tile_data.end(), [normalized_id](const std::weak_ptr<TileData>& tile_data) {
return !tile_data.expired() && tile_data.lock()->id == normalized_id;
});
auto it = tile_data.find(normalized_id);
if (it != tile_data.end()) {
// Create a shared_ptr handle. Note that this might be empty!
new_tile.data = it->lock();
new_tile.data = it->second.lock();
}
if (new_tile.data && new_tile.data->state == TileData::State::obsolete) {
@@ -138,7 +136,7 @@ TileData::State Source::addTile(const Tile::ID& id) {
}
new_tile.data->request();
tile_data.push_front(new_tile.data);
tile_data.emplace(new_tile.data->id, new_tile.data);
}
return new_tile.data->state;
@@ -269,9 +267,9 @@ bool Source::updateTiles() {
return a.id.z < b.id.z;
});
// Remove all the expired pointers from the list.
tile_data.remove_if([&retain_data](const std::weak_ptr<TileData>& tile_data) {
const std::shared_ptr<TileData> tile = tile_data.lock();
// Remove all the expired pointers from the set.
std::erase_if(tile_data, [&retain_data](const std::pair<Tile::ID, std::weak_ptr<TileData>>& pair) {
const std::shared_ptr<TileData> tile = pair.second.lock();
if (!tile) {
return true;
}