13#include <lagrange/utils/assert.h>
14#include <lagrange/utils/invalid.h>
15#include <lagrange/utils/span.h>
31template <
typename Index>
60template <
typename Index,
typename Function>
62 Index num_source_elements,
64 Index num_target_elements = invalid<Index>())
66 const bool has_target_count = num_target_elements != invalid<Index>();
68 mapping.
offsets.assign(has_target_count ? num_target_elements + 1 : num_source_elements + 1, 0);
70 for (Index i = 0; i < num_source_elements; ++i) {
71 Index j = old_to_new(i);
72 if (j == invalid<Index>()) {
76 j <
static_cast<Index
>(mapping.
offsets.size()),
78 "Mapped element index cannot exceeds {} number of elements!",
79 has_target_count ?
"target" :
"source"));
83 if (!has_target_count) {
85 num_target_elements = num_source_elements;
86 while (num_target_elements != 0 && mapping.
offsets[num_target_elements] == 0) {
87 --num_target_elements;
89 mapping.
offsets.resize(num_target_elements + 1);
95 for (Index i = 0; i < num_source_elements; i++) {
96 Index j = old_to_new(i);
97 if (j == invalid<Index>()) {
128template <
typename Index>
131 Index num_target_elements = invalid<Index>())
133 Index num_source_elements =
static_cast<Index
>(old_to_new.size());
136 [&](Index i) {
return old_to_new[i]; },
137 num_target_elements);
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
#define la_debug_assert(...)
Debug assertion check.
Definition: assert.h:189
::nonstd::span< T, Extent > span
A bounds-safe view for sequences of objects.
Definition: span.h:27
nullptr_t, size_t, ptrdiff_t basic_ostream bad_weak_ptr extent, remove_extent, is_array,...
Definition: attribute_string_utils.h:21
InverseMapping< Index > invert_mapping(Index num_source_elements, Function old_to_new, Index num_target_elements=invalid< Index >())
Compute the target-to-source (i.e.
Definition: invert_mapping.h:61
A simple struct representing the inverse of a 1-to-many mapping.
Definition: invert_mapping.h:33
std::vector< Index > data
A flat array of indices of the source elements.
Definition: invert_mapping.h:35
std::vector< Index > offsets
An array of data offset indices. It is of size num_target_elements + 1.
Definition: invert_mapping.h:38