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