This repository has been archived on 2023-10-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blender-archive/source/blender/editors/space_spreadsheet/spreadsheet_cache.cc
Jacques Lucke 2fd63efd0e BLI: simplify removing elements from containers with predicate
Previously removing elements based on a predicate was a bit cumbersome,
especially for hash tables. Now there is a new `remove_if` method in some
data structures which is similar to `std::erase_if`. We could consider adding
`blender::erase_if` in the future to more closely mimic the standard library,
but for now this is using the api design of the surrounding code is used.
2022-09-25 17:57:49 +02:00

65 lines
1.5 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later */
#include "spreadsheet_cache.hh"
namespace blender::ed::spreadsheet {
void SpreadsheetCache::add(std::unique_ptr<Key> key, std::unique_ptr<Value> value)
{
key->is_used = true;
cache_map_.add_overwrite(*key, std::move(value));
keys_.append(std::move(key));
}
SpreadsheetCache::Value *SpreadsheetCache::lookup(const Key &key)
{
std::unique_ptr<Value> *value = cache_map_.lookup_ptr(key);
if (value == nullptr) {
return nullptr;
}
const Key &stored_cache_key = cache_map_.lookup_key(key);
stored_cache_key.is_used = true;
return value->get();
}
SpreadsheetCache::Value &SpreadsheetCache::lookup_or_add(
std::unique_ptr<Key> key, FunctionRef<std::unique_ptr<Value>()> create_value)
{
Value *value = this->lookup(*key);
if (value != nullptr) {
return *value;
}
std::unique_ptr<Value> new_value = create_value();
value = new_value.get();
this->add(std::move(key), std::move(new_value));
return *value;
}
void SpreadsheetCache::set_all_unused()
{
for (std::unique_ptr<Key> &key : keys_) {
key->is_used = false;
}
}
void SpreadsheetCache::remove_all_unused()
{
/* First remove the keys from the map and free the values. */
cache_map_.remove_if([&](auto item) {
const Key &key = item.key;
return !key.is_used;
});
/* Then free the keys. */
for (int i = 0; i < keys_.size();) {
if (keys_[i]->is_used) {
i++;
}
else {
keys_.remove_and_reorder(i);
}
}
}
} // namespace blender::ed::spreadsheet