VTK  9.3.20240229
vtkDataArrayValueRange_AOS.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
7 #ifndef vtkDataArrayValueRange_AOS_h
8 #define vtkDataArrayValueRange_AOS_h
9 
11 #include "vtkDataArrayMeta.h"
13 #include "vtkDebugRangeIterators.h"
14 
15 #include <algorithm>
16 #include <cassert>
17 #include <iterator>
18 #include <type_traits>
19 
20 // Disable this specialization when iterator debugging is requested:
21 #ifndef VTK_DEBUG_RANGE_ITERATORS
22 
24 
25 namespace vtk
26 {
27 namespace detail
28 {
29 VTK_ABI_NAMESPACE_BEGIN
30 
31 //------------------------------------------------------------------------------
32 // ValueRange
33 // For vtkAOSDataArrayTemplate, the `ForceValueTypeForVtkDataArray` template parameter is not used
34 // at all.
35 template <typename ValueTypeT, ComponentIdType TupleSize, typename ForceValueTypeForVtkDataArray>
36 struct ValueRange<vtkAOSDataArrayTemplate<ValueTypeT>, TupleSize, ForceValueTypeForVtkDataArray>
37 {
38 private:
39  static_assert(IsValidTupleSize<TupleSize>::value, "Invalid tuple size.");
40 
43 
44 public:
46  using ValueType = ValueTypeT;
47 
49  using ConstIteratorType = ValueType const*;
51  using ConstReferenceType = ValueType const&;
52 
53  // May be DynamicTupleSize, or the actual tuple size.
54  constexpr static ComponentIdType TupleSizeTag = TupleSize;
55 
56  // STL-compat
63 
65  ValueRange() noexcept = default;
66 
68  ValueRange(ArrayType* arr, ValueIdType beginValue, ValueIdType endValue) noexcept
69  : Array(arr)
70  , NumComps(arr)
71  , BeginValue(beginValue)
72  , EndValue(endValue)
73  {
74  assert(this->Array);
75  assert(beginValue >= 0 && beginValue <= endValue);
76  assert(endValue >= 0 && endValue <= this->Array->GetNumberOfValues());
77  }
78 
80  ValueRange GetSubRange(ValueIdType beginValue = 0, ValueIdType endValue = -1) const noexcept
81  {
82  const ValueIdType realBegin =
83  std::distance(this->Array->GetPointer(0), this->Array->GetPointer(this->BeginValue)) +
84  beginValue;
85  const ValueIdType realEnd = endValue >= 0
86  ? std::distance(this->Array->GetPointer(0), this->Array->GetPointer(this->BeginValue)) +
87  endValue
88  : std::distance(this->Array->GetPointer(0), this->Array->GetPointer(this->EndValue));
89 
91  realEnd };
92  }
93 
95  ArrayType* GetArray() const noexcept { return this->Array; }
96 
98  ComponentIdType GetTupleSize() const noexcept { return this->NumComps.value; }
99 
101  ValueIdType GetBeginValueId() const noexcept
102  {
103  return static_cast<ValueIdType>(
104  this->Array->GetPointer(this->BeginValue) - this->Array->GetPointer(0));
105  }
106 
108  ValueIdType GetEndValueId() const noexcept
109  {
110  return static_cast<ValueIdType>(
111  this->Array->GetPointer(this->EndValue) - this->Array->GetPointer(0));
112  }
113 
115  size_type size() const noexcept
116  {
117  return static_cast<size_type>(
118  this->Array->GetPointer(this->EndValue) - this->Array->GetPointer(this->BeginValue));
119  }
120 
122  iterator begin() noexcept { return this->Array->GetPointer(this->BeginValue); }
124  iterator end() noexcept { return this->Array->GetPointer(this->EndValue); }
125 
127  const_iterator begin() const noexcept { return this->Array->GetPointer(this->BeginValue); }
129  const_iterator end() const noexcept { return this->Array->GetPointer(this->EndValue); }
130 
132  const_iterator cbegin() const noexcept { return this->Array->GetPointer(this->BeginValue); }
134  const_iterator cend() const noexcept { return this->Array->GetPointer(this->EndValue); }
135 
138  {
139  return this->Array->Buffer->GetBuffer()[this->BeginValue + i];
140  }
143  {
144  return this->Array->Buffer->GetBuffer()[this->BeginValue + i];
145  }
146 
147  // Danger! pointer is non-const!
148  value_type* data() noexcept { return this->Array->Buffer->GetBuffer(); }
149 
150  value_type* data() const noexcept { return this->Array->Buffer->GetBuffer(); }
151 
152 private:
153  mutable ArrayType* Array{ nullptr };
154  NumCompsType NumComps{};
155  ValueIdType BeginValue{ 0 };
156  ValueIdType EndValue{ 0 };
157 };
158 
159 // Unimplemented, only used inside decltype in SelectValueRange:
160 template <typename ArrayType, ComponentIdType TupleSize, typename ForceValueTypeForVtkDataArray,
161  // Convenience:
162  typename ValueType = typename ArrayType::ValueType,
163  typename AOSArrayType = vtkAOSDataArrayTemplate<ValueType>,
164  // SFINAE to select AOS arrays:
167  ArrayType*);
168 
169 VTK_ABI_NAMESPACE_END
170 }
171 } // end namespace vtk::detail
172 
174 
175 #endif // VTK_DEBUG_RANGE_ITERATORS
176 #endif // vtkDataArrayValueRange_AOS_h
177 
178 // VTK-HeaderTest-Exclude: vtkDataArrayValueRange_AOS.h
Array-Of-Structs implementation of vtkGenericDataArray.
@ value
Definition: vtkX3D.h:220
@ type
Definition: vtkX3D.h:516
ValueRange< AOSArrayType, TupleSize, ForceValueTypeForVtkDataArray > DeclareValueRangeSpecialization(ArrayType *)
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
vtkIdType ValueIdType
int ComponentIdType
VTK_ITER_INLINE ValueRange GetSubRange(ValueIdType beginValue=0, ValueIdType endValue=-1) const noexcept
GetAPIType< ArrayType, ForceValueTypeForVtkDataArray > ValueType
ConstValueReference< ArrayType, TupleSize, ForceValueTypeForVtkDataArray > ConstReferenceType
constexpr static ComponentIdType TupleSizeTag
ValueReference< ArrayType, TupleSize, ForceValueTypeForVtkDataArray > ReferenceType
ConstValueIterator< ArrayType, TupleSize, ForceValueTypeForVtkDataArray > ConstIteratorType
ValueIterator< ArrayType, TupleSize, ForceValueTypeForVtkDataArray > IteratorType
This file contains a variety of metaprogramming constructs for working with vtkDataArrays.
#define VTK_ITER_OPTIMIZE_START
#define VTK_ITER_INLINE
#define VTK_ITER_OPTIMIZE_END