VTK-m  2.0
ArrayHandleOffsetsToNumComponents.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_cont_ArrayHandleOffsetsToNumComponents_h
11 #define vtk_m_cont_ArrayHandleOffsetsToNumComponents_h
12 
13 #include <vtkm/cont/ArrayHandle.h>
14 
15 namespace vtkm
16 {
17 namespace internal
18 {
19 
20 // Note that `ArrayPortalOffsetsToNumComponents` requires a source portal with +1 entry
21 // to avoid branching. See `ArrayHandleOffsetsToNumComponents` for details.
22 template <typename OffsetsPortal>
23 class VTKM_ALWAYS_EXPORT ArrayPortalOffsetsToNumComponents
24 {
25  OffsetsPortal Portal;
26 
27 public:
28  ArrayPortalOffsetsToNumComponents() = default;
29 
30  ArrayPortalOffsetsToNumComponents(const OffsetsPortal& portal)
31  : Portal(portal)
32  {
33  }
34 
35  using ValueType = vtkm::IdComponent;
36 
37  VTKM_EXEC_CONT vtkm::Id GetNumberOfValues() const { return this->Portal.GetNumberOfValues() - 1; }
38 
40  {
41  return static_cast<vtkm::IdComponent>(this->Portal.Get(index + 1) - this->Portal.Get(index));
42  }
43 };
44 
45 }
46 } // namespace vtkm::internal
47 
48 namespace vtkm
49 {
50 namespace cont
51 {
52 
53 template <typename OffsetsStorageTag>
55 {
56 };
57 
58 namespace internal
59 {
60 
61 template <typename OffsetsStorageTag>
64 {
65  using OffsetsStorage = vtkm::cont::internal::Storage<vtkm::Id, OffsetsStorageTag>;
66 
67 public:
70 
71  using ReadPortalType =
72  vtkm::internal::ArrayPortalOffsetsToNumComponents<typename OffsetsStorage::ReadPortalType>;
73 
74  VTKM_CONT static std::vector<vtkm::cont::internal::Buffer> CreateBuffers()
75  {
76  return OffsetsStorage::CreateBuffers();
77  }
78 
79  VTKM_CONT static vtkm::Id GetNumberOfValues(
80  const std::vector<vtkm::cont::internal::Buffer>& buffers)
81  {
82  vtkm::Id numOffsets = OffsetsStorage::GetNumberOfValues(buffers);
83  if (numOffsets < 1)
84  {
86  "ArrayHandleOffsetsToNumComponents requires an offsets array with at least one value.");
87  }
88  return numOffsets - 1;
89  }
90 
91  VTKM_CONT static ReadPortalType CreateReadPortal(
92  const std::vector<vtkm::cont::internal::Buffer>& buffers,
94  vtkm::cont::Token& token)
95  {
96  VTKM_ASSERT(OffsetsStorage::GetNumberOfValues(buffers) > 0);
97  return ReadPortalType(OffsetsStorage::CreateReadPortal(buffers, device, token));
98  }
99 };
100 
101 } // namespace internal
102 
128 template <class OffsetsArray>
130  : public vtkm::cont::ArrayHandle<
131  vtkm::IdComponent,
132  vtkm::cont::StorageTagOffsetsToNumComponents<typename OffsetsArray::StorageTag>>
133 {
134  VTKM_IS_ARRAY_HANDLE(OffsetsArray);
135  VTKM_STATIC_ASSERT_MSG((std::is_same<typename OffsetsArray::ValueType, vtkm::Id>::value),
136  "Offsets array must have a value type of vtkm::Id.");
137 
138 public:
145 
146  VTKM_CONT ArrayHandleOffsetsToNumComponents(const OffsetsArray& array)
147  : Superclass(array.GetBuffers())
148  {
149  }
150 };
151 
152 template <typename OffsetsStorageTag>
157 {
158  // Converts to correct type.
159  return array;
160 }
161 
162 }
163 } // namespace vtkm::cont
164 
165 #endif //vtk_m_cont_ArrayHandleOffsetsToNumComponents_h
vtkm::cont::make_ArrayHandleOffsetsToNumComponents
VTKM_CONT vtkm::cont::ArrayHandleOffsetsToNumComponents< vtkm::cont::ArrayHandle< vtkm::Id, OffsetsStorageTag > > make_ArrayHandleOffsetsToNumComponents(const vtkm::cont::ArrayHandle< vtkm::Id, OffsetsStorageTag > &array)
Definition: ArrayHandleOffsetsToNumComponents.h:155
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
ArrayHandle.h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
VTKM_ARRAY_HANDLE_SUBCLASS
#define VTKM_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:226
VTKM_ASSERT
#define VTKM_ASSERT(condition)
Definition: Assert.h:43
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::Get
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC_CONT auto Get(const vtkm::Tuple< Ts... > &tuple) -> decltype(tuple.template Get< Index >())
Retrieve the object from a vtkm::Tuple at the given index.
Definition: Tuple.h:83
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::cont::StorageTagOffsetsToNumComponents
Definition: ArrayHandleOffsetsToNumComponents.h:54
VTKM_STORAGE_NO_RESIZE
#define VTKM_STORAGE_NO_RESIZE
Definition: Storage.h:185
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:35
VTKM_STORAGE_NO_WRITE_PORTAL
#define VTKM_STORAGE_NO_WRITE_PORTAL
Definition: Storage.h:198
vtkm::cont::ArrayHandleOffsetsToNumComponents::ArrayHandleOffsetsToNumComponents
VTKM_CONT ArrayHandleOffsetsToNumComponents(const OffsetsArray &array)
Definition: ArrayHandleOffsetsToNumComponents.h:146
VTKM_IS_ARRAY_HANDLE
#define VTKM_IS_ARRAY_HANDLE(T)
Definition: ArrayHandle.h:132
VTKM_STATIC_ASSERT_MSG
#define VTKM_STATIC_ASSERT_MSG(condition, message)
Definition: StaticAssert.h:18
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::DeviceAdapterId
Definition: DeviceAdapterTag.h:52
vtkm::cont::ErrorBadValue
This class is thrown when a VTKm function or method encounters an invalid value that inhibits progres...
Definition: ErrorBadValue.h:25
vtkm::cont::ArrayHandleOffsetsToNumComponents
An ArrayHandle that converts an array of offsets to an array of Vec sizes.
Definition: ArrayHandleOffsetsToNumComponents.h:129
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92