Blender V5.0
BLI_array_state.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include <optional>
12
13#include "BLI_array.hh"
15#include "BLI_virtual_array.hh"
16
17namespace blender {
18
27template<typename T> class ArrayState {
28 private:
33 Span<T> values_;
35 ImplicitSharingPtr<> sharing_info_;
37 std::optional<Array<T, 0>> cached_values_;
38
39 public:
40 ArrayState() = default;
41
42 ArrayState(const VArray<T> &values, const ImplicitSharingInfo *sharing_info)
43 {
44 if (values.is_span() && sharing_info) {
45 /* Don't create a copy of the array and just take shared ownership. */
46 values_ = values.get_internal_span();
47 sharing_info->add_user();
48 sharing_info_ = ImplicitSharingPtr(sharing_info);
49 return;
50 }
51 /* Create a copy of the array because sharing is not possible. */
52 cached_values_.emplace(values.size(), NoInitialization{});
53 values.materialize_to_uninitialized(*cached_values_);
54 values_ = *cached_values_;
55 }
56
60 bool is_empty() const
61 {
62 return values_.is_empty();
63 }
64
71 bool same_as(const VArray<T> &other_values, const ImplicitSharingInfo *other_sharing_info) const
72 {
73 if (sharing_info_ && other_sharing_info) {
74 if (sharing_info_ == other_sharing_info) {
75 /* The data is still shared. */
76 return true;
77 }
78 }
79 if (values_.size() != other_values.size()) {
80 /* The arrays can't be the same if their sizes differ. */
81 return false;
82 }
83 /* Need to actually compare all elements. */
84 VArraySpan<T> other_values_span(other_values);
85 return values_ == other_values_span;
86 }
87};
88
89} // namespace blender
ArrayState(const VArray< T > &values, const ImplicitSharingInfo *sharing_info)
bool same_as(const VArray< T > &other_values, const ImplicitSharingInfo *other_sharing_info) const
void materialize_to_uninitialized(MutableSpan< T > r_span) const
Span< T > get_internal_span() const