Lagrange
Loading...
Searching...
No Matches
set_invalid_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
34template <typename ValueType, typename Index>
36{
37 auto indices = attr.indices().ref_all();
38 const size_t num_indices = attr.indices().get_num_elements();
39
40 // Count the number of invalid indices.
41 size_t num_invalid = 0;
42 for (size_t i = 0; i < num_indices; ++i) {
43 if (indices[i] == invalid<Index>()) {
44 ++num_invalid;
45 }
46 }
47
48 if (num_invalid == 0) return;
49
50 // Append invalid<ValueType>() values for each invalid index.
51 const size_t num_valid = attr.values().get_num_elements();
52
53 const ValueType old_default_value = attr.values().get_default_value();
54 auto scope = make_scope_guard([&] { attr.values().set_default_value(old_default_value); });
55 attr.values().set_default_value(invalid<ValueType>());
56 attr.values().insert_elements(num_invalid);
57
58 // Assign each invalid index to its own new value.
59 Index next_value = static_cast<Index>(num_valid);
60 for (size_t i = 0; i < num_indices; ++i) {
61 if (indices[i] == invalid<Index>()) {
62 indices[i] = next_value++;
63 }
64 }
65}
66
68
69} // namespace lagrange::internal
Derived attribute class that stores the actual information.
Definition IndexedAttribute.h:29
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_invalid_indexed_values.h:35
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