Blender V5.3
BLI_offset_indices.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include <algorithm>
12#include <optional>
13
14#include "BLI_array.hh"
15#include "BLI_index_mask_fwd.hh"
16#include "BLI_index_range.hh"
17#include "BLI_span.hh"
18
19namespace blender {
20
21namespace offset_indices {
22
24struct NoSortCheck {};
25
37template<typename T> class OffsetIndices {
38 private:
39 static_assert(std::is_integral_v<T>);
40
41 Span<T> offsets_;
42
43 public:
44 OffsetIndices() = default;
45 OffsetIndices(const Span<T> offsets) : offsets_(offsets)
46 {
47 BLI_assert(offsets_.size() < 2 || std::is_sorted(offsets_.begin(), offsets_.end()));
48 }
49
55 OffsetIndices(const Span<T> offsets, NoSortCheck /*no_sort_check*/) : offsets_(offsets) {}
56
58 T total_size() const
59 {
60 return offsets_.size() > 1 ? offsets_.last() - offsets_.first() : 0;
61 }
62
67 int64_t size() const
68 {
69 return std::max<int64_t>(offsets_.size() - 1, 0);
70 }
71
72 bool is_empty() const
73 {
74 return this->size() == 0;
75 }
76
78 {
79 return IndexRange(this->size());
80 }
81
83 {
84 BLI_assert(index >= 0);
85 BLI_assert(index < offsets_.size() - 1);
86 const int64_t begin = offsets_[index];
87 const int64_t end = offsets_[index + 1];
89 }
90
91 IndexRange operator[](const IndexRange indices) const
92 {
93 const int64_t begin = offsets_[indices.start()];
94 const int64_t end = offsets_[indices.one_after_last()];
96 }
97
103 OffsetIndices slice(const IndexRange range) const
104 {
105 BLI_assert(range.is_empty() || offsets_.index_range().drop_back(1).contains(range.last()));
106 return OffsetIndices(offsets_.slice(range.start(), range.size() + 1));
107 }
108
109 Span<T> data() const
110 {
111 return offsets_;
112 }
113};
114
123template<typename T> struct GroupedSpan {
126
127 GroupedSpan() = default;
129 {
130 BLI_assert(this->offsets.total_size() == this->data.size());
131 }
132
134 {
135 return this->data.slice(this->offsets[index]);
136 }
137
138 int64_t size() const
139 {
140 return this->offsets.size();
141 }
142
144 {
145 return this->offsets.index_range();
146 }
147
148 bool is_empty() const
149 {
150 return this->data.size() == 0;
151 }
152};
153
158 int start_offset = 0);
159
160std::optional<OffsetIndices<int>> accumulate_counts_to_offsets_with_overflow_check(
161 MutableSpan<int> counts_to_offsets, int start_offset = 0);
162
164void fill_constant_group_size(int size, int start_offset, MutableSpan<int> offsets);
165
168
172
175int sum_group_sizes(OffsetIndices<int> offsets, Span<int> indices);
176
179 const IndexMask &selection,
180 int start_offset,
181 MutableSpan<int> dst_offsets);
183 const IndexMask &selection,
184 MutableSpan<int> dst_offsets)
185{
186 return gather_selected_offsets(src_offsets, selection, 0, dst_offsets);
187}
189 Span<int> selection,
190 int start_offset,
191 MutableSpan<int> dst_offsets);
193 const Span<int> selection,
194 MutableSpan<int> dst_offsets)
195{
196 return gather_selected_offsets(src_offsets, selection, 0, dst_offsets);
197}
198
206
213
219
228void reverse_indices_in_groups(Span<int> group_indices,
229 OffsetIndices<int> offsets,
230 MutableSpan<int> results,
231 bool sort = true);
232
239void reverse_indices_in_groups(Span<int> group_indices,
240 OffsetIndices<int> offsets,
241 MutableSpan<int> results,
242 OffsetIndices<int> value_groups);
243
249void reverse_indices_in_groups(Span<int> group_indices,
250 OffsetIndices<int> offsets,
251 MutableSpan<int> results,
252 int value_group_size);
253
262 int groups_num,
263 Array<int> &offset_data,
264 Array<int> &index_data);
265
274 int groups_num,
275 Array<int> &offset_data,
276 Array<int> &index_data,
277 const IndexMask &values);
278
279} // namespace offset_indices
280
281using offset_indices::GroupedSpan;
282using offset_indices::OffsetIndices;
283
284} // namespace blender
void sort(btMatrix3x3 &U, btVector3 &sigma, btMatrix3x3 &V, int t)
Helper function of 3X3 SVD for sorting singular values.
constexpr int64_t one_after_last() const
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr bool is_empty() const
static constexpr IndexRange from_begin_end(const int64_t begin, const int64_t end)
constexpr IndexRange()=default
constexpr int64_t start() const
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr int64_t size() const
Definition BLI_span.hh:256
OffsetIndices(const Span< T > offsets, NoSortCheck)
IndexRange operator[](const int64_t index) const
OffsetIndices slice(const IndexRange range) const
IndexRange operator[](const IndexRange indices) const
#define T
void sort_groups(OffsetIndices< int > groups, MutableSpan< int > indices)
void copy_group_sizes(OffsetIndices< int > offsets, const IndexMask &mask, MutableSpan< int > sizes)
void build_reverse_map(OffsetIndices< int > offsets, MutableSpan< int > r_map)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
GroupedSpan< int > build_groups_from_indices(Span< int > indices, int groups_num, Array< int > &offset_data, Array< int > &index_data)
std::optional< OffsetIndices< int > > accumulate_counts_to_offsets_with_overflow_check(MutableSpan< int > counts_to_offsets, int start_offset=0)
void gather_group_sizes(OffsetIndices< int > offsets, const IndexMask &mask, MutableSpan< int > sizes)
OffsetIndices< int > build_reverse_offsets(Span< int > indices, MutableSpan< int > offsets)
void fill_constant_group_size(int size, int start_offset, MutableSpan< int > offsets)
void reverse_indices_in_groups(Span< int > group_indices, OffsetIndices< int > offsets, MutableSpan< int > results, bool sort=true)
int sum_group_sizes(OffsetIndices< int > offsets, const IndexMask &mask)
OffsetIndices< int > gather_selected_offsets(OffsetIndices< int > src_offsets, const IndexMask &selection, int start_offset, MutableSpan< int > dst_offsets)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
iter begin(iter)
GroupedSpan(OffsetIndices< int > offsets, Span< T > data)
Span< T > operator[](const int64_t index) const