blob: 0bf8d38dce5ce8dd1239580f2fdb68d4329214a0 [file] [log] [blame]
John Zulauf79f06582021-02-27 18:38:39 -07001/* Copyright (c) 2020-2021 The Khronos Group Inc.
2 * Copyright (c) 2020-2021 Valve Corporation
3 * Copyright (c) 2020-2021 LunarG, Inc.
4 * Copyright (C) 2020-2021 Google Inc.
John Zulauff660ad62019-03-23 07:16:05 -06005 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * John Zulauf <jzulauf@lunarg.com>
19 *
20 */
21#ifndef SPARSE_CONTAINERS_H_
22#define SPARSE_CONTAINERS_H_
John Zulauff660ad62019-03-23 07:16:05 -060023#include <cassert>
24#include <memory>
John Zulauff660ad62019-03-23 07:16:05 -060025#include <vector>
26
27namespace sparse_container {
28// SparseVector:
29//
30// Defines a sparse single-dimensional container which is targeted for three distinct use cases
31// 1) Large range of indices sparsely populated ("Sparse access" below)
32// 2) Large range of indices where all values are the same ("Sparse access" below)
33// 3) Large range of values densely populated (more that 1/4 full) ("Dense access" below)
34// 4) Small range of values where direct access is most efficient ("Dense access" below)
35//
36// To update semantics are supported bases on kSetReplaces:
37// true -- updates to already set (valid) indices replace current value
38// false -- updates to already set (valid) indicies are ignored.
39//
40// Theory of operation:
41//
42// When created, a sparse vector is created (based on size relative to
43// the kSparseThreshold) in either Sparse or Dense access mode.
44//
45// In "Sparse access" mode individual values are stored in a map keyed
46// by the index. A "full range" value (if set) defines the value of all
47// entries not present in the map. Setting a full range value via
48//
49// SetRange(range_min, range_max, full_range_value )
50//
51// either clears the map (kSetReplaces==true) or prevents further
52// updates to the vector (kSetReplaces==false). If the map becomes
Petr Kraus6c4bdce2019-08-27 17:35:01 +020053// more than 1/kConversionThreshold (=4) full, the SparseVector is
54// converted into "Dense access" mode. Entries are copied from map,
John Zulauff660ad62019-03-23 07:16:05 -060055// with non-present indices set to the default value (kDefaultValue)
56// or the full range value (if present).
57//
58// In "Dense access" mode, values are stored in a vector the size of
59// the valid range indexed by the incoming index value minus range_min_.
60// The same upate semantic applies bases on kSetReplaces.
61//
Mark Lobodzinski0875f0c2019-09-18 14:15:17 -060062// Note that when kSparseThreshold is zero, the map is always in "Dense access" mode.
John Zulauff660ad62019-03-23 07:16:05 -060063//
64// Access:
65//
66// NOTE all "end" indices (in construction or access) are *exclusive*.
67//
68// Given the variable semantics and effective compression of Sparse
69// access mode, all access is through Get, Set, and SetRange functions
70// and a constant iterator. Get return either the value found (using
Petr Kraus6c4bdce2019-08-27 17:35:01 +020071// the current access mode) or the kDefaultValue. Set and SetRange
John Zulauff660ad62019-03-23 07:16:05 -060072// return whether or not state was updated, in order to support dirty
73// bit updates for any dependent state.
74//
75// The iterator ConstIterator provides basic, "by value" access. The
76// "by value" nature of the access reflect the compressed nature
77// operators *, ++, ==, and != are provided, with the latter two only
Petr Kraus6c4bdce2019-08-27 17:35:01 +020078// suitable for comparisons vs. cend. The iterator skips all
John Zulauff660ad62019-03-23 07:16:05 -060079// kDefaultValue entries in either access mode, returning a std::pair
80// containing {IndexType, ValueType}. The multiple access modes give
81// the iterator a bit more complexity than is optimal, but hides the
82// underlying complexity from the callers.
83//
84// TODO: Update iterator to use a reference (likely using
85// reference_wrapper...)
86
87template <typename IndexType_, typename T, bool kSetReplaces, T kDefaultValue = T(), size_t kSparseThreshold = 16>
88class SparseVector {
Petr Kraus4ed81e32019-09-02 23:41:19 +020089 public:
John Zulauff660ad62019-03-23 07:16:05 -060090 typedef IndexType_ IndexType;
91 typedef T value_type;
92 typedef value_type ValueType;
Jeremy Gebbencbf22862021-03-03 12:01:22 -070093 typedef layer_data::unordered_map<IndexType, ValueType> SparseType;
John Zulauff660ad62019-03-23 07:16:05 -060094 typedef std::vector<ValueType> DenseType;
95
96 SparseVector(IndexType start, IndexType end)
97 : range_min_(start), range_max_(end), threshold_((end - start) / kConversionThreshold) {
98 assert(end > start);
99 Reset();
100 }
101
102 // Initial access mode is set based on range size vs. kSparseThreshold. Either sparse_ or dense_ is always set, but only
103 // ever one at a time
104 void Reset() {
105 has_full_range_value_ = false;
106 full_range_value_ = kDefaultValue;
107 size_t count = range_max_ - range_min_;
Petr Kraus6c4bdce2019-08-27 17:35:01 +0200108 if (kSparseThreshold > 0 && (count > kSparseThreshold)) {
John Zulauff660ad62019-03-23 07:16:05 -0600109 sparse_.reset(new SparseType());
110 dense_.reset();
111 } else {
112 sparse_.reset();
113 dense_.reset(new DenseType(count, kDefaultValue));
114 }
115 }
116
117 const ValueType &Get(const IndexType index) const {
118 // Note that here (and similarly below, the 'IsSparse' clause is
119 // eliminated as dead code in release builds if kSparseThreshold==0
120 if (IsSparse()) {
121 if (!sparse_->empty()) { // Don't attempt lookup in empty map
122 auto it = sparse_->find(index);
123 if (it != sparse_->cend()) {
124 return it->second;
125 }
126 }
127 // If there is a full_range_value, return it, but there isn't a full_range_value_, it's set to kDefaultValue
128 // so it's still the correct this to return
129 return full_range_value_;
130 } else {
131 // Direct access
132 assert(dense_.get());
133 const ValueType &value = (*dense_)[index - range_min_];
134 return value;
135 }
136 }
137
138 // Set a indexes value, based on the access mode, update semantics are enforced within the access mode specific function
139 bool Set(const IndexType index, const ValueType &value) {
140 bool updated = false;
141 if (IsSparse()) {
142 updated = SetSparse(index, value);
143 } else {
144 assert(dense_.get());
145 updated = SetDense(index, value);
146 }
147 return updated;
148 }
149
150 // Set a range of values based on access mode, with some update semantics applied a the range level
151 bool SetRange(const IndexType start, IndexType end, ValueType value) {
152 bool updated = false;
153 if (IsSparse()) {
154 if (!kSetReplaces && HasFullRange()) return false; // We have full coverage, we can change this no more
155
156 bool is_full_range = IsFullRange(start, end);
157 if (kSetReplaces && is_full_range) {
158 updated = value != full_range_value_;
159 full_range_value_ = value;
160 if (HasSparseSubranges()) {
161 updated = true;
162 sparse_->clear(); // full range replaces all subranges
163 }
164 // has_full_range_value_ state of the full_range_value_ to avoid ValueType comparisons
165 has_full_range_value_ = value != kDefaultValue;
166 } else if (!kSetReplaces && (value != kDefaultValue) && is_full_range && !HasFullRange()) {
167 // With the update only invalid semantics, the value becomes the fallback, and will prevent other updates
168 full_range_value_ = value;
169 has_full_range_value_ = true;
170 updated = true;
171 // Clean up the sparse map a bit
172 for (auto it = sparse_->begin(); it != sparse_->end();) { // no increment clause because of erase below
173 if (it->second == value) {
174 it = sparse_->erase(it); // remove redundant entries
175 } else {
176 ++it;
177 }
178 }
179 } else {
180 for (IndexType index = start; index < end; ++index) {
181 // NOTE: We can't use SetSparse here, because this may be converted to dense access mid update
182 updated |= Set(index, value);
183 }
184 }
185 } else {
186 // Note that "Dense Access" does away with the full_range_value_ logic, storing empty entries using kDefaultValue
187 assert(dense_);
188 for (IndexType index = start; index < end; ++index) {
189 updated = SetDense(index, value);
190 }
191 }
192 return updated;
193 }
194
195 // Set only the non-default values from another sparse vector
196 bool Merge(const SparseVector &from) {
197 // Must not set from Sparse arracy with larger bounds...
198 assert((range_min_ <= from.range_min_) && (range_max_ >= from.range_max_));
199 bool updated = false;
200 if (from.IsSparse()) {
201 if (from.HasFullRange() && !from.HasSparseSubranges()) {
202 // Short cut to copy a full range if that's all we have
203 updated |= SetRange(from.range_min_, from.range_max_, from.full_range_value_);
204 } else {
205 // Have to do it the complete (potentially) slow way
206 // TODO add sorted keys to iterator to reduce hash lookups
207 for (auto it = from.cbegin(); it != from.cend(); ++it) {
208 const IndexType index = (*it).first;
209 const ValueType &value = (*it).second;
210 Set(index, value);
211 }
212 }
213 } else {
214 assert(from.dense_);
215 DenseType &ray = *from.dense_;
216 for (IndexType entry = from.range_min_; entry < from.range_max_; ++entry) {
217 IndexType index = entry - from.range_min_;
218 if (ray[index] != kDefaultValue) {
219 updated |= Set(entry, ray[index]);
220 }
221 }
222 }
223 return updated;
224 }
225
226 friend class ConstIterator;
227 class ConstIterator {
Petr Kraus4ed81e32019-09-02 23:41:19 +0200228 public:
John Zulauff660ad62019-03-23 07:16:05 -0600229 using SparseType = typename SparseVector::SparseType;
230 using SparseIterator = typename SparseType::const_iterator;
231 using IndexType = typename SparseVector::IndexType;
232 using ValueType = typename SparseVector::ValueType;
233 using IteratorValueType = std::pair<IndexType, ValueType>;
234 const IteratorValueType &operator*() const { return current_value_; }
235
236 ConstIterator &operator++() {
237 if (delegated_) { // implies sparse
238 ++it_sparse_;
239 if (it_sparse_ == vec_->sparse_->cend()) {
240 the_end_ = true;
241 current_value_.first = vec_->range_max_;
242 current_value_.second = SparseVector::DefaultValue();
243 } else {
244 current_value_.first = it_sparse_->first;
245 current_value_.second = it_sparse_->second;
246 }
247 } else {
248 index_++;
249 SetCurrentValue();
250 }
251 return *this;
252 }
253 bool operator!=(const ConstIterator &rhs) const {
254 return (the_end_ != rhs.the_end_); // Just good enough for cend checks
255 }
256
257 bool operator==(const ConstIterator &rhs) const {
258 return (the_end_ == rhs.the_end_); // Just good enough for cend checks
259 }
260
261 // The iterator has two modes:
262 // delegated:
263 // where we are in sparse access mode and have no full_range_value
264 // and thus can delegate our iteration to underlying map
265 // non-delegated:
266 // either dense mode or we have a full range value and thus
267 // must iterate over the whole range
268 ConstIterator(const SparseVector &vec) : vec_(&vec) {
269 if (!vec_->IsSparse() || vec_->HasFullRange()) {
270 // Must iterated over entire ranges skipping (in the case of dense access), invalid entries
271 delegated_ = false;
272 index_ = vec_->range_min_;
273 SetCurrentValue(); // Skips invalid and sets the_end_
274 } else if (vec_->HasSparseSubranges()) {
275 // The subranges store the non-default values... and their is no full range value
276 delegated_ = true;
277 it_sparse_ = vec_->sparse_->cbegin();
278 current_value_.first = it_sparse_->first;
279 current_value_.second = it_sparse_->second;
280 the_end_ = false; // the sparse map is non-empty (per HasSparseSubranges() above)
281 } else {
282 // Sparse, but with no subranges
283 the_end_ = true;
284 }
285 }
286
287 ConstIterator() : vec_(nullptr), the_end_(true) {}
288
Petr Kraus4ed81e32019-09-02 23:41:19 +0200289 protected:
John Zulauff660ad62019-03-23 07:16:05 -0600290 const SparseVector *vec_;
291 bool the_end_;
292 SparseIterator it_sparse_;
293 bool delegated_;
294 IndexType index_;
295 ValueType value_;
296
297 IteratorValueType current_value_;
298
299 // in the non-delegated case we use normal accessors and skip default values.
300 void SetCurrentValue() {
301 the_end_ = true;
302 while (index_ < vec_->range_max_) {
303 value_ = vec_->Get(index_);
304 if (value_ != SparseVector::DefaultValue()) {
305 the_end_ = false;
306 current_value_ = IteratorValueType(index_, value_);
307 break;
308 }
309 index_++;
310 }
311 }
312 };
313 typedef ConstIterator const_iterator;
314
315 ConstIterator cbegin() const { return ConstIterator(*this); }
316 ConstIterator cend() const { return ConstIterator(); }
317
318 IndexType RangeMax() const { return range_max_; }
319 IndexType RangeMin() const { return range_min_; }
320
321 static const unsigned kConversionThreshold = 4;
322 const IndexType range_min_; // exclusive
323 const IndexType range_max_; // exclusive
324 const IndexType threshold_; // exclusive
325
326 // Data for sparse mode
327 // We have a short cut for full range values when in sparse mode
328 bool has_full_range_value_;
329 ValueType full_range_value_;
330 std::unique_ptr<SparseType> sparse_;
331
332 // Data for dense mode
333 std::unique_ptr<DenseType> dense_;
334
335 static const ValueType &DefaultValue() {
336 static ValueType value = kDefaultValue;
337 return value;
338 }
339 // Note that IsSparse is compile-time reducible if kSparseThreshold is zero...
Petr Kraus6c4bdce2019-08-27 17:35:01 +0200340 inline bool IsSparse() const { return kSparseThreshold > 0 && sparse_.get(); }
John Zulauff660ad62019-03-23 07:16:05 -0600341 bool IsFullRange(IndexType start, IndexType end) const { return (start == range_min_) && (end == range_max_); }
342 bool IsFullRangeValue(const ValueType &value) const { return has_full_range_value_ && (value == full_range_value_); }
343 bool HasFullRange() const { return IsSparse() && has_full_range_value_; }
344 bool HasSparseSubranges() const { return IsSparse() && !sparse_->empty(); }
345
346 // This is called unconditionally, to encapsulate the conversion criteria and logic here
347 void SparseToDenseConversion() {
348 // If we're using more threshold of the sparse range, convert to dense_
349 if (IsSparse() && (sparse_->size() > threshold_)) {
350 ValueType default_value = HasFullRange() ? full_range_value_ : kDefaultValue;
351 dense_.reset(new DenseType((range_max_ - range_min_), default_value));
352 DenseType &ray = *dense_;
John Zulauf79f06582021-02-27 18:38:39 -0700353 for (const auto &item : *sparse_) {
John Zulauff660ad62019-03-23 07:16:05 -0600354 ray[item.first - range_min_] = item.second;
355 }
356 sparse_.reset();
357 has_full_range_value_ = false;
358 }
359 }
360
361 // Dense access mode setter with update semantics implemented
362 bool SetDense(IndexType index, const ValueType &value) {
363 bool updated = false;
364 ValueType &current_value = (*dense_)[index - range_min_];
365 if ((kSetReplaces || current_value == kDefaultValue) && (value != current_value)) {
366 current_value = value;
367 updated = true;
368 }
369 return updated;
370 }
371
372 // Sparse access mode setter with update full range and update semantics implemented
373 bool SetSparse(IndexType index, const ValueType &value) {
374 if (!kSetReplaces && HasFullRange()) {
375 return false; // We have full coverage, we can change this no more
376 }
377
378 if (kSetReplaces && IsFullRangeValue(value) && HasSparseSubranges()) {
379 auto erasure = sparse_->erase(index); // Remove duplicate record from map
380 return erasure > 0;
381 }
382
383 // Use insert to reduce the number of hash lookups
384 auto map_pair = std::make_pair(index, value);
385 auto insert_pair = sparse_->insert(map_pair);
386 auto &it = insert_pair.first; // use references to avoid nested pair accesses
387 const bool inserted = insert_pair.second;
388 bool updated = false;
389 if (inserted) {
390 updated = true;
391 SparseToDenseConversion();
392 } else if (kSetReplaces && value != it->second) {
393 // Only replace value if semantics allow it and it has changed.
394 it->second = value;
395 updated = true;
396 }
397 return updated;
398 }
399};
400
401} // namespace sparse_container
402#endif