Lagrange
Loading...
Searching...
No Matches
invert_mapping.h
1/*
2 * Copyright 2023 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
13#include <lagrange/utils/assert.h>
14#include <lagrange/utils/invalid.h>
15#include <lagrange/utils/span.h>
16
17#include <algorithm>
18#include <numeric>
19#include <optional>
20#include <vector>
21
22namespace lagrange::internal {
23
33template <typename Index>
35{
37 std::vector<Index> data;
38
40 std::vector<Index> offsets;
41
43 template <typename Func>
44 void foreach_mapped_to(Index i, Func&& func) const
45 {
46 la_debug_assert(i >= 0 && i < static_cast<Index>(offsets.size() - 1));
47 for (Index j = offsets[i]; j < offsets[i + 1]; ++j) {
48 func(data[j]);
49 }
50 }
51
53 template <typename Func>
54 void foreach_mapped_to(Index i, Func&& func)
55 {
56 la_debug_assert(i >= 0 && i < static_cast<Index>(offsets.size() - 1));
57 for (Index j = offsets[i]; j < offsets[i + 1]; ++j) {
58 func(data[j]);
59 }
60 }
61};
62
82template <typename Index, typename Function>
84 Index num_source_elements,
85 Function old_to_new,
86 Index num_target_elements = invalid<Index>())
87{
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);
91
92 for (Index i = 0; i < num_source_elements; ++i) {
93 Index j = old_to_new(i);
94 if (j == invalid<Index>()) {
95 continue;
96 }
98 j < static_cast<Index>(mapping.offsets.size()),
99 fmt::format(
100 "Mapped element index cannot exceeds {} number of elements!",
101 has_target_count ? "target" : "source"));
102 ++mapping.offsets[j + 1];
103 }
104
105 if (!has_target_count) {
106 // If the number of target elements is not provided, we need to resize our offset array now
107 num_target_elements = num_source_elements;
108 while (num_target_elements != 0 && mapping.offsets[num_target_elements] == 0) {
109 --num_target_elements;
110 }
111 mapping.offsets.resize(num_target_elements + 1);
112 }
113
114 std::partial_sum(mapping.offsets.begin(), mapping.offsets.end(), mapping.offsets.begin());
115 la_debug_assert(mapping.offsets.back() <= num_source_elements);
116 mapping.data.resize(mapping.offsets.back());
117 for (Index i = 0; i < num_source_elements; i++) {
118 Index j = old_to_new(i);
119 if (j == invalid<Index>()) {
120 continue;
121 }
122 mapping.data[mapping.offsets[j]++] = i;
123 }
124
125 std::rotate(mapping.offsets.begin(), std::prev(mapping.offsets.end()), mapping.offsets.end());
126 mapping.offsets[0] = 0;
127
128 return mapping;
129}
130
150template <typename Index>
152 span<const Index> old_to_new,
153 Index num_target_elements = invalid<Index>())
154{
155 Index num_source_elements = static_cast<Index>(old_to_new.size());
156 return invert_mapping(
157 num_source_elements,
158 [&](Index i) { return old_to_new[i]; },
159 num_target_elements);
160}
161
162} // namespace lagrange::internal
#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