VTK-m  2.2
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::IdComponent GetNumberOfComponentsFlat(
80  const std::vector<vtkm::cont::internal::Buffer>&)
81  {
82  return 1;
83  }
84 
85  VTKM_CONT static vtkm::Id GetNumberOfValues(
86  const std::vector<vtkm::cont::internal::Buffer>& buffers)
87  {
88  vtkm::Id numOffsets = OffsetsStorage::GetNumberOfValues(buffers);
89  if (numOffsets < 1)
90  {
92  "ArrayHandleOffsetsToNumComponents requires an offsets array with at least one value.");
93  }
94  return numOffsets - 1;
95  }
96 
97  VTKM_CONT static ReadPortalType CreateReadPortal(
98  const std::vector<vtkm::cont::internal::Buffer>& buffers,
100  vtkm::cont::Token& token)
101  {
102  VTKM_ASSERT(OffsetsStorage::GetNumberOfValues(buffers) > 0);
103  return ReadPortalType(OffsetsStorage::CreateReadPortal(buffers, device, token));
104  }
105 };
106 
107 } // namespace internal
108 
134 template <class OffsetsArray>
136  : public vtkm::cont::ArrayHandle<
137  vtkm::IdComponent,
138  vtkm::cont::StorageTagOffsetsToNumComponents<typename OffsetsArray::StorageTag>>
139 {
140  VTKM_IS_ARRAY_HANDLE(OffsetsArray);
141  VTKM_STATIC_ASSERT_MSG((std::is_same<typename OffsetsArray::ValueType, vtkm::Id>::value),
142  "Offsets array must have a value type of vtkm::Id.");
143 
144 public:
151 
152  VTKM_CONT ArrayHandleOffsetsToNumComponents(const OffsetsArray& array)
153  : Superclass(array.GetBuffers())
154  {
155  }
156 };
157 
158 template <typename OffsetsStorageTag>
163 {
164  // Converts to correct type.
165  return array;
166 }
167 
168 }
169 } // namespace vtkm::cont
170 
171 #endif //vtk_m_cont_ArrayHandleOffsetsToNumComponents_h
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:300
ArrayHandle.h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::Get
auto Get(const vtkm::Tuple< Ts... > &tuple)
Retrieve the object from a vtkm::Tuple at the given index.
Definition: Tuple.h:81
VTKM_ARRAY_HANDLE_SUBCLASS
#define VTKM_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:243
VTKM_ASSERT
#define VTKM_ASSERT(condition)
Definition: Assert.h:43
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::cont::StorageTagOffsetsToNumComponents
Definition: ArrayHandleOffsetsToNumComponents.h:54
VTKM_STORAGE_NO_RESIZE
#define VTKM_STORAGE_NO_RESIZE
Definition: Storage.h:185
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
ArrayHandleOffsetsToNumComponents(const OffsetsArray &array)
Definition: ArrayHandleOffsetsToNumComponents.h:152
VTKM_IS_ARRAY_HANDLE
#define VTKM_IS_ARRAY_HANDLE(T)
Checks that the given type is a vtkm::cont::ArrayHandle.
Definition: ArrayHandle.h:137
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::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:58
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:135
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:89
vtkm::cont::make_ArrayHandleOffsetsToNumComponents
vtkm::cont::ArrayHandleOffsetsToNumComponents< vtkm::cont::ArrayHandle< vtkm::Id, OffsetsStorageTag > > make_ArrayHandleOffsetsToNumComponents(const vtkm::cont::ArrayHandle< vtkm::Id, OffsetsStorageTag > &array)
Definition: ArrayHandleOffsetsToNumComponents.h:161
vtkm::cont::ArrayHandleOffsetsToNumComponents::Superclass
typename vtkm::cont::detail::GetTypeInParentheses< void(vtkm::cont::ArrayHandle< vtkm::IdComponent, vtkm::cont::StorageTagOffsetsToNumComponents< typename OffsetsArray::StorageTag > >) >::type Superclass
Definition: ArrayHandleOffsetsToNumComponents.h:150