Welcome to mirror list, hosted at ThFree Co, Russian Federation.

index_mask.cc « intern « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1e301bc5fb94b3a95ed12504e40825f7e366d3d7 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include "BLI_index_mask.hh"
#include "BLI_index_mask_ops.hh"

namespace blender {

IndexMask IndexMask::slice(int64_t start, int64_t size) const
{
  return this->slice(IndexRange(start, size));
}

IndexMask IndexMask::slice(IndexRange slice) const
{
  return IndexMask(indices_.slice(slice));
}

IndexMask IndexMask::slice_and_offset(const IndexRange slice, Vector<int64_t> &r_new_indices) const
{
  const int slice_size = slice.size();
  if (slice_size == 0) {
    return {};
  }
  IndexMask sliced_mask{indices_.slice(slice)};
  if (sliced_mask.is_range()) {
    return IndexMask(slice_size);
  }
  const int64_t offset = sliced_mask.indices().first();
  if (offset == 0) {
    return sliced_mask;
  }
  r_new_indices.resize(slice_size);
  for (const int i : IndexRange(slice_size)) {
    r_new_indices[i] = sliced_mask[i] - offset;
  }
  return IndexMask(r_new_indices.as_span());
}

IndexMask IndexMask::invert(const IndexRange full_range, Vector<int64_t> &r_new_indices) const
{
  BLI_assert(this->contained_in(full_range));
  if (full_range.size() == indices_.size()) {
    return {};
  }
  if (indices_.is_empty()) {
    return full_range;
  }
  r_new_indices.clear();

  const Vector<IndexRange> ranges = this->extract_ranges_invert(full_range, nullptr);
  for (const IndexRange &range : ranges) {
    for (const int64_t index : range) {
      r_new_indices.append(index);
    }
  }
  return r_new_indices.as_span();
}

Vector<IndexRange> IndexMask::extract_ranges() const
{
  Vector<IndexRange> ranges;
  int64_t range_start = 0;
  while (range_start < indices_.size()) {
    int64_t current_range_end = range_start + 1;
    int64_t step_size = 1;

    while (true) {
      const int64_t possible_range_end = current_range_end + step_size;
      if (possible_range_end > indices_.size()) {
        break;
      }
      if (!this->slice(range_start, possible_range_end - range_start).is_range()) {
        break;
      }
      current_range_end = possible_range_end;
      step_size *= 2;
    }

    /* This step size was tried already, no need to try it again. */
    step_size /= 2;

    while (step_size > 0) {
      const int64_t possible_range_end = current_range_end + step_size;
      step_size /= 2;
      if (possible_range_end > indices_.size()) {
        continue;
      }
      if (!this->slice(range_start, possible_range_end - range_start).is_range()) {
        continue;
      }
      current_range_end = possible_range_end;
    }

    ranges.append(IndexRange{indices_[range_start], current_range_end - range_start});
    range_start = current_range_end;
  }
  return ranges;
}

Vector<IndexRange> IndexMask::extract_ranges_invert(const IndexRange full_range,
                                                    Vector<int64_t> *r_skip_amounts) const
{
  BLI_assert(this->contained_in(full_range));
  const Vector<IndexRange> ranges = this->extract_ranges();
  Vector<IndexRange> inverted_ranges;

  int64_t skip_amount = 0;
  int64_t next_start = full_range.start();
  for (const int64_t i : ranges.index_range()) {
    const IndexRange range = ranges[i];
    if (range.start() > next_start) {
      inverted_ranges.append({next_start, range.start() - next_start});
      if (r_skip_amounts != nullptr) {
        r_skip_amounts->append(skip_amount);
      }
    }
    next_start = range.one_after_last();
    skip_amount += range.size();
  }
  if (next_start < full_range.one_after_last()) {
    inverted_ranges.append({next_start, full_range.one_after_last() - next_start});
    if (r_skip_amounts != nullptr) {
      r_skip_amounts->append(skip_amount);
    }
  }
  return inverted_ranges;
}

}  // namespace blender

namespace blender::index_mask_ops::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)
{
  /* Gather vectors that have been generated by possibly multiple threads. */
  Vector<Vector<int64_t> *> all_vectors;
  int64_t result_mask_size = 0;
  for (Vector<Vector<int64_t>> &local_sub_masks : sub_masks) {
    for (Vector<int64_t> &sub_mask : local_sub_masks) {
      all_vectors.append(&sub_mask);
      result_mask_size += sub_mask.size();
    }
  }

  if (all_vectors.is_empty()) {
    /* Special case when the predicate was false for all elements. */
    return {};
  }
  if (result_mask_size == indices_to_check.size()) {
    /* Special case when the predicate was true for all elements. */
    return indices_to_check;
  }
  if (all_vectors.size() == 1) {
    /* Special case when all indices for which the predicate is true happen to be in a single
     * vector. */
    r_indices = std::move(*all_vectors[0]);
    return r_indices.as_span();
  }

  /* Indices in separate vectors don't overlap. So it is ok to sort the vectors just by looking at
   * the first element. */
  std::sort(all_vectors.begin(),
            all_vectors.end(),
            [](const Vector<int64_t> *a, const Vector<int64_t> *b) { return (*a)[0] < (*b)[0]; });

  /* Precompute the offsets for the individual vectors, so that the indices can be copied into the
   * final vector in parallel. */
  Vector<int64_t> offsets;
  offsets.reserve(all_vectors.size() + 1);
  offsets.append(0);
  for (Vector<int64_t> *vector : all_vectors) {
    offsets.append(offsets.last() + vector->size());
  }

  r_indices.resize(result_mask_size);

  /* Fill the final index mask in parallel again. */
  threading::parallel_for(all_vectors.index_range(), 100, [&](const IndexRange all_vectors_range) {
    for (const int64_t vector_index : all_vectors_range) {
      Vector<int64_t> &vector = *all_vectors[vector_index];
      const int64_t offset = offsets[vector_index];
      threading::parallel_for(vector.index_range(), 1024, [&](const IndexRange range) {
        initialized_copy_n(vector.data() + range.start(),
                           range.size(),
                           r_indices.data() + offset + range.start());
      });
    }
  });

  return r_indices.as_span();
}

}  // namespace blender::index_mask_ops::detail