VTK-m  2.0
ArrayHandleCounting.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_ArrayHandleCounting_h
11 #define vtk_m_cont_ArrayHandleCounting_h
12 
14 
15 #include <vtkm/TypeTraits.h>
16 #include <vtkm/VecTraits.h>
17 
18 namespace vtkm
19 {
20 namespace cont
21 {
22 
24 {
25 };
26 
27 namespace internal
28 {
29 
31 template <class CountingValueType>
32 class VTKM_ALWAYS_EXPORT ArrayPortalCounting
33 {
34  using ComponentType = typename vtkm::VecTraits<CountingValueType>::ComponentType;
35 
36 public:
37  using ValueType = CountingValueType;
38 
40  ArrayPortalCounting()
41  : Start(0)
42  , Step(1)
43  , NumberOfValues(0)
44  {
45  }
46 
48  ArrayPortalCounting(ValueType start, ValueType step, vtkm::Id numValues)
49  : Start(start)
50  , Step(step)
51  , NumberOfValues(numValues)
52  {
53  }
54 
56  ValueType GetStart() const { return this->Start; }
57 
59  ValueType GetStep() const { return this->Step; }
60 
62  vtkm::Id GetNumberOfValues() const { return this->NumberOfValues; }
63 
65  ValueType Get(vtkm::Id index) const
66  {
67  return ValueType(this->Start + this->Step * ValueType(static_cast<ComponentType>(index)));
68  }
69 
70 private:
71  ValueType Start;
72  ValueType Step;
73  vtkm::Id NumberOfValues;
74 };
75 
76 namespace detail
77 {
78 
79 template <typename T, typename UseVecTraits = vtkm::HasVecTraits<T>>
80 struct CanCountImpl;
81 
82 template <typename T>
83 struct CanCountImpl<T, std::false_type>
84 {
85  using TTraits = vtkm::TypeTraits<T>;
86  static constexpr bool IsNumeric =
87  !std::is_same<typename TTraits::NumericTag, vtkm::TypeTraitsUnknownTag>::value;
88 
89  static constexpr bool value = IsNumeric;
90 };
91 
92 template <typename T>
93 struct CanCountImpl<T, std::true_type>
94 {
95  using VTraits = vtkm::VecTraits<T>;
96  using BaseType = typename VTraits::BaseComponentType;
97  static constexpr bool IsBool = std::is_same<BaseType, bool>::value;
98 
99  static constexpr bool value = CanCountImpl<BaseType, std::false_type>::value && !IsBool;
100 };
101 
102 } // namespace detail
103 
104 // Not all types can be counted.
105 template <typename T>
106 struct CanCount
107 {
108  static constexpr bool value = detail::CanCountImpl<T>::value;
109 };
110 
111 template <typename T>
112 using StorageTagCountingSuperclass =
114 
115 template <typename T>
116 struct Storage<T, typename std::enable_if<CanCount<T>::value, vtkm::cont::StorageTagCounting>::type>
117  : Storage<T, StorageTagCountingSuperclass<T>>
118 {
119 };
120 
121 } // namespace internal
122 
126 template <typename CountingValueType>
128  : public vtkm::cont::ArrayHandle<CountingValueType, vtkm::cont::StorageTagCounting>
129 {
130 public:
134 
135  VTKM_CONT
136  ArrayHandleCounting(CountingValueType start, CountingValueType step, vtkm::Id length)
137  : Superclass(internal::PortalToArrayHandleImplicitBuffers(
138  internal::ArrayPortalCounting<CountingValueType>(start, step, length)))
139  {
140  }
141 
142  VTKM_CONT CountingValueType GetStart() const { return this->ReadPortal().GetStart(); }
143 
144  VTKM_CONT CountingValueType GetStep() const { return this->ReadPortal().GetStep(); }
145 };
146 
149 template <typename CountingValueType>
151 make_ArrayHandleCounting(CountingValueType start, CountingValueType step, vtkm::Id length)
152 {
153  return vtkm::cont::ArrayHandleCounting<CountingValueType>(start, step, length);
154 }
155 }
156 } // namespace vtkm::cont
157 
158 //=============================================================================
159 // Specializations of serialization related classes
161 namespace vtkm
162 {
163 namespace cont
164 {
165 
166 template <typename T>
167 struct SerializableTypeString<vtkm::cont::ArrayHandleCounting<T>>
168 {
169  static VTKM_CONT const std::string& Get()
170  {
171  static std::string name = "AH_Counting<" + SerializableTypeString<T>::Get() + ">";
172  return name;
173  }
174 };
175 
176 template <typename T>
177 struct SerializableTypeString<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagCounting>>
178  : SerializableTypeString<vtkm::cont::ArrayHandleCounting<T>>
179 {
180 };
181 }
182 } // vtkm::cont
183 
184 namespace mangled_diy_namespace
185 {
186 
187 template <typename T>
188 struct Serialization<vtkm::cont::ArrayHandleCounting<T>>
189 {
190 private:
193 
194 public:
195  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
196  {
197  auto portal = obj.ReadPortal();
198  vtkmdiy::save(bb, portal.GetStart());
199  vtkmdiy::save(bb, portal.GetStep());
200  vtkmdiy::save(bb, portal.GetNumberOfValues());
201  }
202 
203  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
204  {
205  T start{}, step{};
206  vtkm::Id count = 0;
207 
208  vtkmdiy::load(bb, start);
209  vtkmdiy::load(bb, step);
210  vtkmdiy::load(bb, count);
211 
212  obj = vtkm::cont::make_ArrayHandleCounting(start, step, count);
213  }
214 };
215 
216 template <typename T>
217 struct Serialization<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagCounting>>
218  : Serialization<vtkm::cont::ArrayHandleCounting<T>>
219 {
220 };
221 } // diy
223 
224 #endif //vtk_m_cont_ArrayHandleCounting_h
vtkm::cont::ArrayHandleCounting::GetStart
VTKM_CONT CountingValueType GetStart() const
Definition: ArrayHandleCounting.h:142
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::TypeTraits
The TypeTraits class provides helpful compile-time information about the basic types used in VTKm (an...
Definition: TypeTraits.h:61
vtkm::cont::ArrayHandleCounting::ArrayHandleCounting
VTKM_CONT ArrayHandleCounting(CountingValueType start, CountingValueType step, vtkm::Id length)
Definition: ArrayHandleCounting.h:136
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::cont::StorageTagImplicit
An implementation for read-only implicit arrays.
Definition: ArrayHandleImplicit.h:86
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::cont::make_ArrayHandleCounting
VTKM_CONT vtkm::cont::ArrayHandleCounting< CountingValueType > make_ArrayHandleCounting(CountingValueType start, CountingValueType step, vtkm::Id length)
A convenience function for creating an ArrayHandleCounting.
Definition: ArrayHandleCounting.h:151
vtkm::cont::ArrayHandleCounting::VTKM_ARRAY_HANDLE_SUBCLASS
VTKM_ARRAY_HANDLE_SUBCLASS(ArrayHandleCounting,(ArrayHandleCounting< CountingValueType >),(vtkm::cont::ArrayHandle< CountingValueType, StorageTagCounting >))
mangled_diy_namespace
Definition: Particle.h:331
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::exec::arg::load
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC T load(const U &u, vtkm::Id v)
Definition: FetchTagArrayDirectIn.h:36
TypeTraits.h
vtkm::cont::StorageTagCounting
Definition: ArrayHandleCounting.h:23
vtkm::cont::ArrayHandleCounting
ArrayHandleCounting is a specialization of ArrayHandle.
Definition: ArrayHandleCounting.h:127
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::cont::ArrayHandle< CountingValueType, vtkm::cont::StorageTagCounting >::ReadPortal
VTKM_CONT ReadPortalType ReadPortal() const
Get an array portal that can be used in the control environment.
Definition: ArrayHandle.h:414
vtkm::VecTraits::ComponentType
typename VecType::ComponentType ComponentType
Type of the components in the vector.
Definition: VecTraits.h:73
vtkm::VecTraits< T >
vtkm::cont::ArrayHandleCounting::GetStep
VTKM_CONT CountingValueType GetStep() const
Definition: ArrayHandleCounting.h:144
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
ArrayHandleImplicit.h
VecTraits.h