1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
/** \file
* \ingroup bli
*
* This is separate from `BLI_index_mask.hh` because it includes headers just `IndexMask` shouldn't
* depend on.
*/
#include "BLI_enumerable_thread_specific.hh"
#include "BLI_index_mask.hh"
#include "BLI_task.hh"
#include "BLI_vector.hh"
#include "BLI_virtual_array.hh"
namespace blender::index_mask_ops {
namespace detail {
IndexMask find_indices_based_on_predicate__merge(
IndexMask indices_to_check,
threading::EnumerableThreadSpecific<Vector<Vector<int64_t>>> &sub_masks,
Vector<int64_t> &r_indices);
} // namespace detail
/**
* Evaluate the #predicate for all indices in #indices_to_check and return a mask that contains all
* indices where the predicate was true.
*
* #r_indices indices is only used if necessary.
*/
template<typename Predicate>
inline IndexMask find_indices_based_on_predicate(const IndexMask indices_to_check,
const int64_t parallel_grain_size,
Vector<int64_t> &r_indices,
const Predicate &predicate)
{
/* Evaluate predicate in parallel. Since the size of the final mask is not known yet, many
* smaller vectors have to be filled with all indices where the predicate is true. Those smaller
* vectors are joined afterwards. */
threading::EnumerableThreadSpecific<Vector<Vector<int64_t>>> sub_masks;
threading::parallel_for(
indices_to_check.index_range(), parallel_grain_size, [&](const IndexRange range) {
const IndexMask sub_mask = indices_to_check.slice(range);
Vector<int64_t> masked_indices;
for (const int64_t i : sub_mask) {
if (predicate(i)) {
masked_indices.append(i);
}
}
if (!masked_indices.is_empty()) {
sub_masks.local().append(std::move(masked_indices));
}
});
/* This part doesn't have to be in the header. */
return detail::find_indices_based_on_predicate__merge(indices_to_check, sub_masks, r_indices);
}
/**
* Find the true indices in a virtual array. This is a version of
* #find_indices_based_on_predicate optimized for a virtual array input.
*
* \param parallel_grain_size: The grain size for when the virtual array isn't a span or a single
* value internally. This should be adjusted based on the expected cost of evaluating the virtual
* array-- more expensive virtual arrays should have smaller grain sizes.
*/
IndexMask find_indices_from_virtual_array(IndexMask indices_to_check,
const VArray<bool> &virtual_array,
int64_t parallel_grain_size,
Vector<int64_t> &r_indices);
} // namespace blender::index_mask_ops
|