13#include <lagrange/utils/assert.h>
14#include <lagrange/utils/fmt/format.h>
15#include <lagrange/utils/invalid.h>
16#include <lagrange/utils/span.h>
34template <
typename Index>
44 template <
typename Func>
54 template <
typename Func>
83template <
typename Index,
typename Function>
85 Index num_source_elements,
89 const bool has_target_count = num_target_elements !=
invalid<Index>();
91 mapping.
offsets.assign(has_target_count ? num_target_elements + 1 : num_source_elements + 1, 0);
93 for (Index i = 0; i < num_source_elements; ++i) {
94 Index j = old_to_new(i);
99 j <
static_cast<Index
>(mapping.
offsets.size()),
101 "Mapped element index cannot exceeds {} number of elements!",
102 has_target_count ?
"target" :
"source"));
106 if (!has_target_count) {
108 num_target_elements = num_source_elements;
109 while (num_target_elements != 0 && mapping.
offsets[num_target_elements] == 0) {
110 --num_target_elements;
112 mapping.
offsets.resize(num_target_elements + 1);
118 for (Index i = 0; i < num_source_elements; i++) {
119 Index j = old_to_new(i);
151template <
typename Index>
156 Index num_source_elements =
static_cast<Index
>(old_to_new.size());
159 [&](Index i) {
return old_to_new[i]; },
160 num_target_elements);
#define la_runtime_assert(...)
Runtime assertion check.
Definition assert.h:175
#define la_debug_assert(...)
Debug assertion check.
Definition assert.h:195
::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: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:84
A simple struct representing the inverse of a 1-to-many mapping.
Definition invert_mapping.h:36
void foreach_mapped_to(Index i, Func &&func)
Iterate over all source elements mapped to target element i.
Definition invert_mapping.h:55
void foreach_mapped_to(Index i, Func &&func) const
Iterate over all source elements mapped to target element i.
Definition invert_mapping.h:45
std::vector< Index > data
A flat array of indices of the source elements.
Definition invert_mapping.h:38
std::vector< Index > offsets
An array of data offset indices. It is of size num_target_elements + 1.
Definition invert_mapping.h:41