Lagrange
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
31template <typename Index>
33{
35 std::vector<Index> data;
36
38 std::vector<Index> offsets;
39};
40
60template <typename Index, typename Function>
62 Index num_source_elements,
63 Function old_to_new,
64 Index num_target_elements = invalid<Index>())
65{
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);
69
70 for (Index i = 0; i < num_source_elements; ++i) {
71 Index j = old_to_new(i);
72 if (j == invalid<Index>()) {
73 continue;
74 }
76 j < static_cast<Index>(mapping.offsets.size()),
77 fmt::format(
78 "Mapped element index cannot exceeds {} number of elements!",
79 has_target_count ? "target" : "source"));
80 ++mapping.offsets[j + 1];
81 }
82
83 if (!has_target_count) {
84 // If the number of target elements is not provided, we need to resize our offset array now
85 num_target_elements = num_source_elements;
86 while (num_target_elements != 0 && mapping.offsets[num_target_elements] == 0) {
87 --num_target_elements;
88 }
89 mapping.offsets.resize(num_target_elements + 1);
90 }
91
92 std::partial_sum(mapping.offsets.begin(), mapping.offsets.end(), mapping.offsets.begin());
93 la_debug_assert(mapping.offsets.back() <= num_source_elements);
94 mapping.data.resize(mapping.offsets.back());
95 for (Index i = 0; i < num_source_elements; i++) {
96 Index j = old_to_new(i);
97 if (j == invalid<Index>()) {
98 continue;
99 }
100 mapping.data[mapping.offsets[j]++] = i;
101 }
102
103 std::rotate(mapping.offsets.begin(), std::prev(mapping.offsets.end()), mapping.offsets.end());
104 mapping.offsets[0] = 0;
105
106 return mapping;
107}
108
128template <typename Index>
130 span<const Index> old_to_new,
131 Index num_target_elements = invalid<Index>())
132{
133 Index num_source_elements = static_cast<Index>(old_to_new.size());
134 return invert_mapping(
135 num_source_elements,
136 [&](Index i) { return old_to_new[i]; },
137 num_target_elements);
138}
139
140} // namespace lagrange::internal
#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