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

FN_array_spans.hh « functions « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3f71b36c49c64706fa76976c66400733d486ff1b (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
197
198
199
200
201
202
203
204
205
206
207
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef __FN_ARRAY_SPANS_HH__
#define __FN_ARRAY_SPANS_HH__

/** \file
 * \ingroup fn
 *
 * An ArraySpan is a span where every element contains an array (instead of a single element as is
 * the case in a normal span). It's main use case is to reference many small arrays.
 */

#include "FN_spans.hh"

namespace blender::fn {

/**
 * Depending on the use case, the referenced data might have a different structure. More
 * categories can be added when necessary.
 */
enum class VArraySpanCategory {
  SingleArray,
  StartsAndSizes,
};

template<typename T> class VArraySpanBase {
 protected:
  uint virtual_size_;
  VArraySpanCategory category_;

  union {
    struct {
      const T *start;
      uint size;
    } single_array;
    struct {
      const T *const *starts;
      const uint *sizes;
    } starts_and_sizes;
  } data_;

 public:
  bool is_single_array() const
  {
    switch (category_) {
      case VArraySpanCategory::SingleArray:
        return true;
      case VArraySpanCategory::StartsAndSizes:
        return virtual_size_ == 1;
    }
    BLI_assert(false);
    return false;
  }

  bool is_empty() const
  {
    return this->virtual_size_ == 0;
  }

  uint size() const
  {
    return this->virtual_size_;
  }
};

/**
 * A virtual array span. Every element of this span contains a virtual span. So it behaves like
 * a blender::Span, but might not be backed up by an actual array.
 */
template<typename T> class VArraySpan : public VArraySpanBase<T> {
 private:
  friend class GVArraySpan;

  VArraySpan(const VArraySpanBase<void> &other)
  {
    memcpy(this, &other, sizeof(VArraySpanBase<void>));
  }

 public:
  VArraySpan()
  {
    this->virtual_size_ = 0;
    this->category_ = VArraySpanCategory::StartsAndSizes;
    this->data_.starts_and_sizes.starts = nullptr;
    this->data_.starts_and_sizes.sizes = nullptr;
  }

  VArraySpan(Span<T> span, uint virtual_size)
  {
    this->virtual_size_ = virtual_size;
    this->category_ = VArraySpanCategory::SingleArray;
    this->data_.single_array.start = span.data();
    this->data_.single_array.size = span.size();
  }

  VArraySpan(Span<const T *> starts, Span<uint> sizes)
  {
    BLI_assert(starts.size() == sizes.size());
    this->virtual_size_ = starts.size();
    this->category_ = VArraySpanCategory::StartsAndSizes;
    this->data_.starts_and_sizes.starts = starts.begin();
    this->data_.starts_and_sizes.sizes = sizes.begin();
  }

  VSpan<T> operator[](uint index) const
  {
    BLI_assert(index < this->virtual_size_);
    switch (this->category_) {
      case VArraySpanCategory::SingleArray:
        return VSpan<T>(Span<T>(this->data_.single_array.start, this->data_.single_array.size));
      case VArraySpanCategory::StartsAndSizes:
        return VSpan<T>(Span<T>(this->data_.starts_and_sizes.starts[index],
                                this->data_.starts_and_sizes.sizes[index]));
    }
    BLI_assert(false);
    return {};
  }
};

/**
 * A generic virtual array span. It's just like a VArraySpan, but the type is only known at
 * run-time.
 */
class GVArraySpan : public VArraySpanBase<void> {
 private:
  const CPPType *type_;

  GVArraySpan() = default;

 public:
  GVArraySpan(const CPPType &type)
  {
    this->type_ = &type;
    this->virtual_size_ = 0;
    this->category_ = VArraySpanCategory::StartsAndSizes;
    this->data_.starts_and_sizes.starts = nullptr;
    this->data_.starts_and_sizes.sizes = nullptr;
  }

  GVArraySpan(GSpan array, uint virtual_size)
  {
    this->type_ = &array.type();
    this->virtual_size_ = virtual_size;
    this->category_ = VArraySpanCategory::SingleArray;
    this->data_.single_array.start = array.buffer();
    this->data_.single_array.size = array.size();
  }

  GVArraySpan(const CPPType &type, Span<const void *> starts, Span<uint> sizes)
  {
    BLI_assert(starts.size() == sizes.size());
    this->type_ = &type;
    this->virtual_size_ = starts.size();
    this->category_ = VArraySpanCategory::StartsAndSizes;
    this->data_.starts_and_sizes.starts = (void **)starts.begin();
    this->data_.starts_and_sizes.sizes = sizes.begin();
  }

  template<typename T> GVArraySpan(VArraySpan<T> other)
  {
    this->type_ = &CPPType::get<T>();
    memcpy(this, &other, sizeof(VArraySpanBase<void>));
  }

  const CPPType &type() const
  {
    return *this->type_;
  }

  template<typename T> VArraySpan<T> typed() const
  {
    BLI_assert(type_->is<T>());
    return VArraySpan<T>(*this);
  }

  GVSpan operator[](uint index) const
  {
    BLI_assert(index < virtual_size_);
    switch (category_) {
      case VArraySpanCategory::SingleArray:
        return GVSpan(GSpan(*type_, data_.single_array.start, data_.single_array.size));
      case VArraySpanCategory::StartsAndSizes:
        return GVSpan(GSpan(
            *type_, data_.starts_and_sizes.starts[index], data_.starts_and_sizes.sizes[index]));
    }
    BLI_assert(false);
    return GVSpan(*type_);
  }
};

}  // namespace blender::fn

#endif /* __FN_ARRAY_SPANS_HH__ */