Blender  V2.93
BLI_index_mask.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
40 #include "BLI_index_range.hh"
41 #include "BLI_span.hh"
42 
43 namespace blender {
44 
45 class IndexMask {
46  private:
47  /* The underlying reference to sorted integers. */
48  Span<int64_t> indices_;
49 
50  public:
51  /* Creates an IndexMask that contains no indices. */
52  IndexMask() = default;
53 
60  {
61 #ifdef DEBUG
62  for (int64_t i = 1; i < indices.size(); i++) {
63  BLI_assert(indices[i - 1] < indices[i]);
64  }
65 #endif
66  }
67 
72  IndexMask(IndexRange range) : indices_(range.as_span())
73  {
74  }
75 
86  IndexMask(const std::initializer_list<int64_t> &indices) : IndexMask(Span<int64_t>(indices))
87  {
88  }
89 
94  {
95  }
96 
97  operator Span<int64_t>() const
98  {
99  return indices_;
100  }
101 
102  const int64_t *begin() const
103  {
104  return indices_.begin();
105  }
106 
107  const int64_t *end() const
108  {
109  return indices_.end();
110  }
111 
117  {
118  return indices_[n];
119  }
120 
126  {
127  if (indices_.size() == 0) {
128  return 0;
129  }
130  else {
131  return indices_.last() + 1;
132  }
133  }
134 
136  {
137  return indices_;
138  }
139 
143  bool is_range() const
144  {
145  return indices_.size() > 0 && indices_.last() - indices_.first() == indices_.size() - 1;
146  }
147 
153  {
154  BLI_assert(this->is_range());
155  return IndexRange{indices_.first(), indices_.size()};
156  }
157 
165  template<typename CallbackT> void foreach_index(const CallbackT &callback) const
166  {
167  if (this->is_range()) {
168  IndexRange range = this->as_range();
169  for (int64_t i : range) {
170  callback(i);
171  }
172  }
173  else {
174  for (int64_t i : indices_) {
175  callback(i);
176  }
177  }
178  }
179 
188  {
189  return indices_.index_range();
190  }
191 
195  int64_t last() const
196  {
197  return indices_.last();
198  }
199 
203  int64_t size() const
204  {
205  return indices_.size();
206  }
207 };
208 
209 } // namespace blender
#define BLI_assert(a)
Definition: BLI_assert.h:58
const int64_t * end() const
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_range() const
IndexMask(Span< int64_t > indices)
Span< int64_t > indices() const
int64_t min_array_size() const
IndexRange index_range() const
void foreach_index(const CallbackT &callback) const
const int64_t * begin() const
IndexRange as_range() const
constexpr const T & last() const
Definition: BLI_span.hh:327
constexpr const T & first() const
Definition: BLI_span.hh:317
constexpr int64_t size() const
Definition: BLI_span.hh:254
constexpr IndexRange index_range() const
Definition: BLI_span.hh:414
constexpr const T * end() const
Definition: BLI_span.hh:226
constexpr const T * begin() const
Definition: BLI_span.hh:222
DEGForeachIDComponentCallback callback
__int64 int64_t
Definition: stdint.h:92