Blender  V3.3
BLI_index_mask.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
26 #include "BLI_index_range.hh"
27 #include "BLI_span.hh"
28 #include "BLI_vector.hh"
29 
30 namespace blender {
31 
32 class IndexMask {
33  private:
35  Span<int64_t> indices_;
36 
37  public:
39  IndexMask() = default;
40 
47  {
49  }
50 
55  IndexMask(IndexRange range) : indices_(range.as_span())
56  {
57  }
58 
69  IndexMask(const std::initializer_list<int64_t> &indices) : IndexMask(Span<int64_t>(indices))
70  {
71  }
72 
77  {
78  }
79 
82  {
83  if (!indices.is_empty()) {
84  if (indices.first() < 0) {
85  return false;
86  }
87  }
88  for (int64_t i = 1; i < indices.size(); i++) {
89  if (indices[i - 1] >= indices[i]) {
90  return false;
91  }
92  }
93  return true;
94  }
95 
96  operator Span<int64_t>() const
97  {
98  return indices_;
99  }
100 
101  const int64_t *begin() const
102  {
103  return indices_.begin();
104  }
105 
106  const int64_t *end() const
107  {
108  return indices_.end();
109  }
110 
116  {
117  return indices_[n];
118  }
119 
125  {
126  if (indices_.size() == 0) {
127  return 0;
128  }
129  else {
130  return indices_.last() + 1;
131  }
132  }
133 
135  {
136  return indices_;
137  }
138 
142  bool is_range() const
143  {
144  return indices_.size() > 0 && indices_.last() - indices_.first() == indices_.size() - 1;
145  }
146 
152  {
153  BLI_assert(this->is_range());
154  return IndexRange{indices_.first(), indices_.size()};
155  }
156 
164  template<typename CallbackT> void foreach_index(const CallbackT &callback) const
165  {
166  this->to_best_mask_type([&](const auto &mask) {
167  for (const int64_t i : mask) {
168  callback(i);
169  }
170  });
171  }
172 
181  template<typename Fn> void to_best_mask_type(const Fn &fn) const
182  {
183  if (this->is_range()) {
184  const IndexRange masked_range = this->as_range();
185  fn(masked_range);
186  }
187  else {
188  const Span<int64_t> masked_indices = indices_;
189  fn(masked_indices);
190  }
191  }
192 
201  {
202  return indices_.index_range();
203  }
204 
208  int64_t last() const
209  {
210  return indices_.last();
211  }
212 
216  int64_t size() const
217  {
218  return indices_.size();
219  }
220 
221  bool is_empty() const
222  {
223  return indices_.is_empty();
224  }
225 
226  bool contained_in(const IndexRange range) const
227  {
228  if (indices_.is_empty()) {
229  return true;
230  }
231  if (range.size() < indices_.size()) {
232  return false;
233  }
234  return indices_.first() >= range.first() && indices_.last() <= range.last();
235  }
236 
237  IndexMask slice(int64_t start, int64_t size) const;
258 
263  IndexMask invert(const IndexRange full_range, Vector<int64_t> &r_new_indices) const;
264 
269 
281  Vector<int64_t> *r_skip_amounts = nullptr) const;
282 };
283 
284 } // namespace blender
#define BLI_assert(a)
Definition: BLI_assert.h:46
const int64_t * end() const
static bool indices_are_valid_index_mask(Span< int64_t > indices)
IndexMask(const std::initializer_list< int64_t > &indices)
int64_t last() const
int64_t size() const
IndexMask(IndexRange range)
int64_t operator[](int64_t n) const
bool is_empty() const
bool is_range() const
Vector< IndexRange > extract_ranges_invert(const IndexRange full_range, Vector< int64_t > *r_skip_amounts=nullptr) const
Definition: index_mask.cc:100
IndexMask slice_and_offset(IndexRange slice, Vector< int64_t > &r_new_indices) const
Definition: index_mask.cc:18
IndexMask(Span< int64_t > indices)
void to_best_mask_type(const Fn &fn) const
Span< int64_t > indices() const
int64_t min_array_size() const
IndexRange index_range() const
void foreach_index(const CallbackT &callback) const
Vector< IndexRange > extract_ranges() const
Definition: index_mask.cc:59
IndexMask invert(const IndexRange full_range, Vector< int64_t > &r_new_indices) const
Definition: index_mask.cc:39
bool contained_in(const IndexRange range) const
IndexMask slice(int64_t start, int64_t size) const
Definition: index_mask.cc:8
const int64_t * begin() const
IndexRange as_range() const
constexpr int64_t first() const
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr const T & first() const
Definition: BLI_span.hh:303
constexpr const T & last(const int64_t n=0) const
Definition: BLI_span.hh:313
constexpr int64_t size() const
Definition: BLI_span.hh:240
constexpr IndexRange index_range() const
Definition: BLI_span.hh:401
constexpr const T * end() const
Definition: BLI_span.hh:212
constexpr const T * begin() const
Definition: BLI_span.hh:208
constexpr bool is_empty() const
Definition: BLI_span.hh:248
DEGForeachIDComponentCallback callback
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
__int64 int64_t
Definition: stdint.h:89