Lagrange
Loading...
Searching...
No Matches
set_indexed_values.h
1/*
2 * Copyright 2026 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/IndexedAttribute.h>
15#include <lagrange/utils/invalid.h>
16#include <lagrange/utils/scope_guard.h>
17
18namespace lagrange::internal {
19
24
36template <typename ValueType, typename Index, typename Predicate>
38{
39 auto indices = attr.indices().ref_all();
40 const size_t num_indices = attr.indices().get_num_elements();
41
42 // Count the number of indices to remap.
43 size_t num_remap = 0;
44 for (size_t i = 0; i < num_indices; ++i) {
45 if (predicate(indices[i])) {
46 ++num_remap;
47 }
48 }
49
50 if (num_remap == 0) return;
51
52 // Append an invalid<ValueType>() value for each index to remap.
53 const size_t num_valid = attr.values().get_num_elements();
54
55 const ValueType old_default_value = attr.values().get_default_value();
56 auto scope = make_scope_guard([&] { attr.values().set_default_value(old_default_value); });
57 attr.values().set_default_value(invalid<ValueType>());
58 attr.values().insert_elements(num_remap);
59
60 // Assign each remapped index to its own new value.
61 Index next_value = static_cast<Index>(num_valid);
62 for (size_t i = 0; i < num_indices; ++i) {
63 if (predicate(indices[i])) {
64 indices[i] = next_value++;
65 }
66 }
67}
68
78template <typename ValueType, typename Index>
80{
81 set_predicated_indexed_values(attr, [](Index index) { return index == invalid<Index>(); });
82}
83
93template <typename ValueType, typename Index>
95{
96 const size_t num_values = attr.values().get_num_elements();
97 set_predicated_indexed_values(attr, [num_values](Index index) {
98 return static_cast<size_t>(index) >= num_values;
99 });
100}
101
103
104} // namespace lagrange::internal
Derived attribute class that stores the actual information.
Definition IndexedAttribute.h:29
void set_out_of_range_indexed_values(IndexedAttribute< ValueType, Index > &attr)
For each element in the index buffer pointing outside the value buffer, appends a new value set to in...
Definition set_indexed_values.h:94
void set_predicated_indexed_values(IndexedAttribute< ValueType, Index > &attr, Predicate &&predicate)
For each index matching the given predicate, appends a new value set to invalid<ValueType>() and reas...
Definition set_indexed_values.h:37
void set_invalid_indexed_values(IndexedAttribute< ValueType, Index > &attr)
For each element in the index buffer set to invalid<Index>(), appends a new value set to invalid<Valu...
Definition set_indexed_values.h:79
sg_detail::scope_guard< Callback > make_scope_guard(Callback &&callback) noexcept(std::is_nothrow_constructible< Callback, Callback && >::value)
Creates a scope guard around a callable object.
constexpr T invalid()
You can use invalid<T>() to get a value that can represent "invalid" values, such as invalid indices ...
Definition invalid.h:40
nullptr_t, size_t, ptrdiff_t basic_ostream bad_weak_ptr extent, remove_extent, is_array,...
Definition attribute_string_utils.h:21