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#pragma once
13
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>
18
19#include <algorithm>
20#include <numeric>
21#include <optional>
22#include <vector>
23
24namespace lagrange::internal {
25
35template <typename Index>
37{
39 std::vector<Index> data;
40
42 std::vector<Index> offsets;
43
45 template <typename Func>
46 void foreach_mapped_to(Index i, Func&& func) const
47 {
48 la_debug_assert(i >= 0 && i < static_cast<Index>(offsets.size() - 1));
49 for (Index j = offsets[i]; j < offsets[i + 1]; ++j) {
50 func(data[j]);
51 }
52 }
53
55 template <typename Func>
56 void foreach_mapped_to(Index i, Func&& func)
57 {
58 la_debug_assert(i >= 0 && i < static_cast<Index>(offsets.size() - 1));
59 for (Index j = offsets[i]; j < offsets[i + 1]; ++j) {
60 func(data[j]);
61 }
62 }
63};
64
84template <typename Index, typename Function>
86 Index num_source_elements,
87 Function old_to_new,
88 Index num_target_elements = invalid<Index>())
89{
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);
93
94 for (Index i = 0; i < num_source_elements; ++i) {
95 Index j = old_to_new(i);
96 if (j == invalid<Index>()) {
97 continue;
98 }
100 j < static_cast<Index>(mapping.offsets.size()),
101 format(
102 "Mapped element index cannot exceeds {} number of elements!",
103 has_target_count ? "target" : "source"));
104 ++mapping.offsets[j + 1];
105 }
106
107 if (!has_target_count) {
108 // If the number of target elements is not provided, we need to resize our offset array now
109 num_target_elements = num_source_elements;
110 while (num_target_elements != 0 && mapping.offsets[num_target_elements] == 0) {
111 --num_target_elements;
112 }
113 mapping.offsets.resize(num_target_elements + 1);
114 }
115
116 std::partial_sum(mapping.offsets.begin(), mapping.offsets.end(), mapping.offsets.begin());
117 la_debug_assert(mapping.offsets.back() <= num_source_elements);
118 mapping.data.resize(mapping.offsets.back());
119 for (Index i = 0; i < num_source_elements; i++) {
120 Index j = old_to_new(i);
121 if (j == invalid<Index>()) {
122 continue;
123 }
124 mapping.data[mapping.offsets[j]++] = i;
125 }
126
127 std::rotate(mapping.offsets.begin(), std::prev(mapping.offsets.end()), mapping.offsets.end());
128 mapping.offsets[0] = 0;
129
130 return mapping;
131}
132
152template <typename Index>
154 span<const Index> old_to_new,
155 Index num_target_elements = invalid<Index>())
156{
157 Index num_source_elements = static_cast<Index>(old_to_new.size());
158 return invert_mapping(
159 num_source_elements,
160 [&](Index i) { return old_to_new[i]; },
161 num_target_elements);
162}
163
164} // namespace lagrange::internal
#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