Welcome to mirror list, hosted at ThFree Co, Russian Federation.

spreadsheet_cache.cc « space_spreadsheet « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 931a00ab583ab881f28c97f9bb0713fd1a45f1b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* 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. */
  for (auto it = cache_map_.keys().begin(); it != cache_map_.keys().end(); ++it) {
    const Key &key = *it;
    if (!key.is_used) {
      cache_map_.remove(it);
    }
  }
  /* 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