| 58 | */ |
| 59 | template < typename T > |
| 60 | class SparseAttribute : public ReadOnlyAttribute< T > |
| 61 | { |
| 62 | friend class bitsery::Access; |
| 63 | |
| 64 | public: |
| 65 | SparseAttribute( T default_value, |
| 66 | AttributeProperties properties, |
| 67 | AttributeBase::AttributeKey /*key*/ ) |
| 68 | : SparseAttribute( |
| 69 | std::move( default_value ), std::move( properties ) ) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | [[nodiscard]] const T& value( index_t element ) const override |
| 74 | { |
| 75 | const auto value_it = values_.find( element ); |
| 76 | if( value_it != values_.end() ) |
| 77 | { |
| 78 | return value_it->second; |
| 79 | } |
| 80 | return default_value_; |
| 81 | } |
| 82 | |
| 83 | void set_value( index_t element, T value ) |
| 84 | { |
| 85 | values_[element] = std::move( value ); |
| 86 | } |
| 87 | |
| 88 | [[nodiscard]] const T& default_value() const |
| 89 | { |
| 90 | return default_value_; |
| 91 | } |
| 92 | |
| 93 | template < typename Modifier > |
| 94 | void modify_value( index_t element, Modifier modifier ) |
| 95 | { |
| 96 | auto [value_it, _] = values_.emplace( element, default_value_ ); |
| 97 | modifier( value_it->second ); |
| 98 | } |
| 99 | |
| 100 | public: |
| 101 | void compute_value( index_t from_element, |
| 102 | index_t to_element, |
| 103 | AttributeBase::AttributeKey /*key*/ ) override |
| 104 | { |
| 105 | set_value( to_element, value( from_element ) ); |
| 106 | } |
| 107 | |
| 108 | void compute_value( const AttributeLinearInterpolation& interpolation, |
| 109 | index_t to_element, |
| 110 | AttributeBase::AttributeKey /*key*/ ) override |
| 111 | { |
| 112 | set_value( to_element, interpolation.compute_value( *this ) ); |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | SparseAttribute( T default_value, AttributeProperties properties ) |
| 117 | : ReadOnlyAttribute< T >( std::move( properties ) ), |