13#include <lagrange/utils/assert.h>
14#include <lagrange/utils/invalid.h>
15#include <lagrange/utils/span.h>
33template <
typename Index>
43 template <
typename Func>
53 template <
typename Func>
82template <
typename Index,
typename Function>
84 Index num_source_elements,
88 const bool has_target_count = num_target_elements !=
invalid<Index>();
90 mapping.
offsets.assign(has_target_count ? num_target_elements + 1 : num_source_elements + 1, 0);
92 for (Index i = 0; i < num_source_elements; ++i) {
93 Index j = old_to_new(i);
98 j <
static_cast<Index
>(mapping.
offsets.size()),
100 "Mapped element index cannot exceeds {} number of elements!",
101 has_target_count ?
"target" :
"source"));
105 if (!has_target_count) {
107 num_target_elements = num_source_elements;
108 while (num_target_elements != 0 && mapping.
offsets[num_target_elements] == 0) {
109 --num_target_elements;
111 mapping.
offsets.resize(num_target_elements + 1);
117 for (Index i = 0; i < num_source_elements; i++) {
118 Index j = old_to_new(i);
150template <
typename Index>
155 Index num_source_elements =
static_cast<Index
>(old_to_new.size());
158 [&](Index i) {
return old_to_new[i]; },
159 num_target_elements);
#define la_runtime_assert(...)
Runtime assertion check.
Definition assert.h:174
#define la_debug_assert(...)
Debug assertion check.
Definition assert.h:194
::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:83
A simple struct representing the inverse of a 1-to-many mapping.
Definition invert_mapping.h:35
void foreach_mapped_to(Index i, Func &&func)
Iterate over all source elements mapped to target element i.
Definition invert_mapping.h:54
void foreach_mapped_to(Index i, Func &&func) const
Iterate over all source elements mapped to target element i.
Definition invert_mapping.h:44
std::vector< Index > data
A flat array of indices of the source elements.
Definition invert_mapping.h:37
std::vector< Index > offsets
An array of data offset indices. It is of size num_target_elements + 1.
Definition invert_mapping.h:40