Viskores  1.0
ArrayHandleOffsetsToNumComponents.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_cont_ArrayHandleOffsetsToNumComponents_h
19 #define viskores_cont_ArrayHandleOffsetsToNumComponents_h
20 
22 
23 namespace viskores
24 {
25 namespace internal
26 {
27 
28 // Note that `ArrayPortalOffsetsToNumComponents` requires a source portal with +1 entry
29 // to avoid branching. See `ArrayHandleOffsetsToNumComponents` for details.
30 template <typename OffsetsPortal>
31 class VISKORES_ALWAYS_EXPORT ArrayPortalOffsetsToNumComponents
32 {
33  OffsetsPortal Portal;
34 
35 public:
36  ArrayPortalOffsetsToNumComponents() = default;
37 
38  ArrayPortalOffsetsToNumComponents(const OffsetsPortal& portal)
39  : Portal(portal)
40  {
41  }
42 
43  using ValueType = viskores::IdComponent;
44 
45  VISKORES_EXEC_CONT viskores::Id GetNumberOfValues() const
46  {
47  return this->Portal.GetNumberOfValues() - 1;
48  }
49 
51  {
52  return static_cast<viskores::IdComponent>(this->Portal.Get(index + 1) -
53  this->Portal.Get(index));
54  }
55 };
56 
57 }
58 } // namespace viskores::internal
59 
60 namespace viskores
61 {
62 namespace cont
63 {
64 
65 template <typename OffsetsStorageTag>
66 struct VISKORES_ALWAYS_EXPORT StorageTagOffsetsToNumComponents
67 {
68 };
69 
70 namespace internal
71 {
72 
73 template <typename OffsetsStorageTag>
74 class VISKORES_ALWAYS_EXPORT
75  Storage<viskores::IdComponent,
77 {
78  using OffsetsStorage = viskores::cont::internal::Storage<viskores::Id, OffsetsStorageTag>;
79 
80 public:
83 
84  using ReadPortalType =
85  viskores::internal::ArrayPortalOffsetsToNumComponents<typename OffsetsStorage::ReadPortalType>;
86 
87  VISKORES_CONT static std::vector<viskores::cont::internal::Buffer> CreateBuffers()
88  {
89  return OffsetsStorage::CreateBuffers();
90  }
91 
92  VISKORES_CONT static viskores::IdComponent GetNumberOfComponentsFlat(
93  const std::vector<viskores::cont::internal::Buffer>&)
94  {
95  return 1;
96  }
97 
98  VISKORES_CONT static viskores::Id GetNumberOfValues(
99  const std::vector<viskores::cont::internal::Buffer>& buffers)
100  {
101  viskores::Id numOffsets = OffsetsStorage::GetNumberOfValues(buffers);
102  if (numOffsets < 1)
103  {
105  "ArrayHandleOffsetsToNumComponents requires an offsets array with at least one value.");
106  }
107  return numOffsets - 1;
108  }
109 
110  VISKORES_CONT static ReadPortalType CreateReadPortal(
111  const std::vector<viskores::cont::internal::Buffer>& buffers,
113  viskores::cont::Token& token)
114  {
115  VISKORES_ASSERT(OffsetsStorage::GetNumberOfValues(buffers) > 0);
116  return ReadPortalType(OffsetsStorage::CreateReadPortal(buffers, device, token));
117  }
118 };
119 
120 } // namespace internal
121 
147 template <class OffsetsArray>
148 class VISKORES_ALWAYS_EXPORT ArrayHandleOffsetsToNumComponents
150  viskores::IdComponent,
151  viskores::cont::StorageTagOffsetsToNumComponents<typename OffsetsArray::StorageTag>>
152 {
153  VISKORES_IS_ARRAY_HANDLE(OffsetsArray);
154  VISKORES_STATIC_ASSERT_MSG((std::is_same<typename OffsetsArray::ValueType, viskores::Id>::value),
155  "Offsets array must have a value type of viskores::Id.");
156 
157 public:
164 
166  : Superclass(array.GetBuffers())
167  {
168  }
169 };
170 
171 template <typename OffsetsStorageTag>
176 {
177  // Converts to correct type.
178  return array;
179 }
180 
181 }
182 } // namespace viskores::cont
183 
184 #endif //viskores_cont_ArrayHandleOffsetsToNumComponents_h
ArrayHandle.h
VISKORES_IS_ARRAY_HANDLE
#define VISKORES_IS_ARRAY_HANDLE(T)
Checks that the given type is a viskores::cont::ArrayHandle.
Definition: ArrayHandle.h:145
viskores::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:313
viskores::IdComponent
viskores::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:202
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
viskores::cont::ArrayHandleOffsetsToNumComponents::Superclass
typename viskores::cont::detail::GetTypeInParentheses< void(viskores::cont::ArrayHandle< viskores::IdComponent, viskores::cont::StorageTagOffsetsToNumComponents< typename OffsetsArray::StorageTag > >) >::type Superclass
Definition: ArrayHandleOffsetsToNumComponents.h:163
viskores::cont::make_ArrayHandleOffsetsToNumComponents
viskores::cont::ArrayHandleOffsetsToNumComponents< viskores::cont::ArrayHandle< viskores::Id, OffsetsStorageTag > > make_ArrayHandleOffsetsToNumComponents(const viskores::cont::ArrayHandle< viskores::Id, OffsetsStorageTag > &array)
Definition: ArrayHandleOffsetsToNumComponents.h:174
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::cont::StorageTagOffsetsToNumComponents
Definition: ArrayHandleOffsetsToNumComponents.h:66
VISKORES_STORAGE_NO_RESIZE
#define VISKORES_STORAGE_NO_RESIZE
Definition: Storage.h:194
VISKORES_STORAGE_NO_WRITE_PORTAL
#define VISKORES_STORAGE_NO_WRITE_PORTAL
Definition: Storage.h:209
viskores::cont::ArrayHandleOffsetsToNumComponents
An ArrayHandle that converts an array of offsets to an array of Vec sizes.
Definition: ArrayHandleOffsetsToNumComponents.h:148
VISKORES_ASSERT
#define VISKORES_ASSERT(condition)
Definition: Assert.h:51
VISKORES_STATIC_ASSERT_MSG
#define VISKORES_STATIC_ASSERT_MSG(condition, message)
Definition: StaticAssert.h:26
viskores::cont::ArrayHandleOffsetsToNumComponents::ArrayHandleOffsetsToNumComponents
ArrayHandleOffsetsToNumComponents(const OffsetsArray &array)
Definition: ArrayHandleOffsetsToNumComponents.h:165
viskores::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:66
viskores::cont::ErrorBadValue
This class is thrown when a Viskores function or method encounters an invalid value that inhibits pro...
Definition: ErrorBadValue.h:33
VISKORES_ARRAY_HANDLE_SUBCLASS
#define VISKORES_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:256
viskores::Get
auto Get(const viskores::Tuple< Ts... > &tuple)
Retrieve the object from a viskores::Tuple at the given index.
Definition: Tuple.h:89
viskores::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:43