14#include <lagrange/utils/assert.h>
15#include <lagrange/utils/fmt/format.h>
16#include <lagrange/utils/invalid.h>
17#include <lagrange/utils/span.h>
35template <
typename Index>
45 template <
typename Func>
55 template <
typename Func>
84template <
typename Index,
typename Function>
86 Index num_source_elements,
90 const bool has_target_count = num_target_elements !=
invalid<Index>();
92 mapping.
offsets.assign(has_target_count ? num_target_elements + 1 : num_source_elements + 1, 0);
94 for (Index i = 0; i < num_source_elements; ++i) {
95 Index j = old_to_new(i);
100 j <
static_cast<Index
>(mapping.
offsets.size()),
102 "Mapped element index cannot exceeds {} number of elements!",
103 has_target_count ?
"target" :
"source"));
107 if (!has_target_count) {
109 num_target_elements = num_source_elements;
110 while (num_target_elements != 0 && mapping.
offsets[num_target_elements] == 0) {
111 --num_target_elements;
113 mapping.
offsets.resize(num_target_elements + 1);
119 for (Index i = 0; i < num_source_elements; i++) {
120 Index j = old_to_new(i);
152template <
typename Index>
157 Index num_source_elements =
static_cast<Index
>(old_to_new.size());
160 [&](Index i) {
return old_to_new[i]; },
161 num_target_elements);
#define la_runtime_assert(...)
Runtime assertion check.
Definition assert.h:177
#define la_debug_assert(...)
Debug assertion check.
Definition assert.h:197
::nonstd::span< T, Extent > span
A bounds-safe view for sequences of objects.
Definition span.h:27
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:22
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:85
A simple struct representing the inverse of a 1-to-many mapping.
Definition invert_mapping.h:37
void foreach_mapped_to(Index i, Func &&func)
Iterate over all source elements mapped to target element i.
Definition invert_mapping.h:56
void foreach_mapped_to(Index i, Func &&func) const
Iterate over all source elements mapped to target element i.
Definition invert_mapping.h:46
std::vector< Index > data
A flat array of indices of the source elements.
Definition invert_mapping.h:39
std::vector< Index > offsets
An array of data offset indices. It is of size num_target_elements + 1.
Definition invert_mapping.h:42