Debian Codegraph @

src/atlas/interpolation/Cache.cc

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #include "atlas/interpolation/Cache.h"
12 #include "atlas/interpolation/Interpolation.h"
13 namespace atlas {
14 namespace interpolation {
15
16 InterpolationCacheEntry::~InterpolationCacheEntry() = default;
17
18 Cache::Cache(std::shared_ptr<InterpolationCacheEntry> cache) {
19 auto& new_cache = cache_[cache->type()];
20 new_cache = cache;
21 }
22
23 Cache::Cache(const Cache& other) {
24 add(other);
25 }
26
27 Cache::Cache(const Cache& other, const std::string& filter): Cache(other) {
28 std::shared_ptr<InterpolationCacheEntry> filtered;
29 for (auto& entry : cache_) {
30 if (entry.first == filter) {
31 filtered = entry.second;
32 }
33 }
34 cache_.clear();
35 if (filtered) {
36 cache_[filtered->type()] = filtered;
37 }
38 }
39
40
41 Cache::Cache(const Interpolation& interpolation): Cache(interpolation.createCache()) {}
42
43 Cache::~Cache() = default;
44
45 Cache& Cache::operator=(const Cache& other) {
46 cache_.clear();
47 add(other);
48 return *this;
49 }
50
51 Cache& Cache::operator=(Cache&& other) {
52 cache_.clear();
53 add(other);
54 other.cache_.clear();
55 return *this;
56 }
57
58 void Cache::add(const Cache& other) {
59 for (auto& entry : other.cache_) {
60 auto& new_cache = cache_[entry.first];
61 new_cache = entry.second;
62 }
63 }
64
65 MatrixCacheEntry::~MatrixCacheEntry() = default;
66
67 class MatrixCacheEntryOwned : public MatrixCacheEntry {
68 public:
69 MatrixCacheEntryOwned(Matrix&& matrix): MatrixCacheEntry(&matrix_) {
70 const_cast<Matrix&>(matrix_) = std::move(matrix);
71 }
72
73 private:
74 const Matrix matrix_;
75 };
76
77 class MatrixCacheEntryShared : public MatrixCacheEntry {
78 public:
79 MatrixCacheEntryShared(std::shared_ptr<const Matrix> matrix, const std::string& uid = ""):
80 MatrixCacheEntry(matrix.get(), uid), matrix_{matrix} {}
81
82 private:
83 const std::shared_ptr<const Matrix> matrix_;
84 };
85
86
87 MatrixCache::MatrixCache(const Cache& c):
88 Cache(c, MatrixCacheEntry::static_type()),
89 matrix_{dynamic_cast<const MatrixCacheEntry*>(c.get(MatrixCacheEntry::static_type()))} {}
90
91 MatrixCache::MatrixCache(Matrix&& m): MatrixCache(std::make_shared<MatrixCacheEntryOwned>(std::move(m))) {}
92
93 MatrixCache::MatrixCache(std::shared_ptr<const Matrix> m, const std::string& uid):
94 MatrixCache(std::make_shared<MatrixCacheEntryShared>(m, uid)) {}
95
96 MatrixCache::MatrixCache(const Matrix* m): MatrixCache(std::make_shared<MatrixCacheEntry>(m)) {}
97
98 MatrixCache::MatrixCache(const Interpolation& interpolation): MatrixCache(Cache(interpolation)) {}
99
100 MatrixCache::operator bool() const {
101 return matrix_ && !matrix().empty();
102 }
103
104 const MatrixCache::Matrix& MatrixCache::matrix() const {
105 ATLAS_ASSERT(matrix_);
106 return matrix_->matrix();
107 }
108
109 const std::string& MatrixCache::uid() const {
110 ATLAS_ASSERT(matrix_);
111 return matrix_->uid();
112 }
113
114 size_t MatrixCache::footprint() const {
115 if (matrix_) {
116 return matrix_->footprint();
117 }
118 return 0;
119 }
120
121 MatrixCache::MatrixCache(std::shared_ptr<InterpolationCacheEntry> entry):
122 Cache(entry), matrix_{dynamic_cast<const MatrixCacheEntry*>(entry.get())} {}
123
124
125 IndexKDTreeCacheEntry::~IndexKDTreeCacheEntry() = default;
126
127 const IndexKDTreeCacheEntry::IndexKDTree& IndexKDTreeCacheEntry::tree() const {
128 ATLAS_ASSERT(tree_);
129 return tree_;
130 }
131
132 IndexKDTreeCache::IndexKDTreeCache(const Cache& c):
133 Cache(c, IndexKDTreeCacheEntry::static_type()),
134 tree_{dynamic_cast<const IndexKDTreeCacheEntry*>(c.get(IndexKDTreeCacheEntry::static_type()))} {}
135
136 IndexKDTreeCache::IndexKDTreeCache(const IndexKDTree& tree): Cache(std::make_shared<IndexKDTreeCacheEntry>(tree)) {
137 tree_ = dynamic_cast<const IndexKDTreeCacheEntry*>(get(IndexKDTreeCacheEntry::static_type()));
138 }
139
140 IndexKDTreeCache::IndexKDTreeCache(const Interpolation& interpolation): IndexKDTreeCache(Cache(interpolation)) {}
141
142 IndexKDTreeCache::operator bool() const {
143 return tree_; //&& !tree().empty();
144 }
145
146 const IndexKDTreeCache::IndexKDTree& IndexKDTreeCache::tree() const {
147 ATLAS_ASSERT(tree_);
148 return tree_->tree();
149 }
150
151 size_t IndexKDTreeCache::footprint() const {
152 if (tree_) {
153 return tree_->footprint();
154 }
155 return 0;
156 }
157
158
159 } // namespace interpolation
160 } // namespace atlas