Lagrange
Loading...
Searching...
No Matches
compact_chart_ids.h
1/*
2 * Copyright 2026 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/span.h>
15
16// clang-format off
17#include <lagrange/utils/warnoff.h>
18#include <tbb/parallel_for.h>
19#include <tbb/parallel_sort.h>
20#include <lagrange/utils/warnon.h>
21// clang-format on
22
23#include <algorithm>
24#include <utility>
25#include <vector>
26
27namespace lagrange::internal {
28
39template <typename Index>
40std::pair<std::vector<Index>, Index> compact_chart_ids(span<const Index> chart_ids)
41{
42 std::vector<Index> sorted_ids(chart_ids.begin(), chart_ids.end());
43 tbb::parallel_sort(sorted_ids.begin(), sorted_ids.end());
44 sorted_ids.erase(std::unique(sorted_ids.begin(), sorted_ids.end()), sorted_ids.end());
45 const Index num_charts = static_cast<Index>(sorted_ids.size());
46
47 std::vector<Index> dense_chart_ids(chart_ids.size());
48 tbb::parallel_for(Index(0), static_cast<Index>(chart_ids.size()), [&](Index f) {
49 dense_chart_ids[f] = static_cast<Index>(
50 std::lower_bound(sorted_ids.begin(), sorted_ids.end(), chart_ids[f]) -
51 sorted_ids.begin());
52 });
53 return {std::move(dense_chart_ids), num_charts};
54}
55
56} // namespace lagrange::internal
::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
std::pair< std::vector< Index >, Index > compact_chart_ids(span< const Index > chart_ids)
Compact chart ids to a dense [0, N) range so per-chart storage is bounded by the number of distinct c...
Definition compact_chart_ids.h:40