C++ API Reference for Intel® Data Analytics Acceleration Library 2020 Update 1

helpers.h
1 /* file: helpers.h */
2 /*******************************************************************************
3 * Copyright 2014-2020 Intel Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *******************************************************************************/
17 
18 #ifndef __DATA_MANAGEMENT_FEATURES_INTERNAL_HELPERS_H__
19 #define __DATA_MANAGEMENT_FEATURES_INTERNAL_HELPERS_H__
20 
21 #include "services/internal/utilities.h"
22 #include "services/internal/collection.h"
23 
24 #include "data_management/features/indices.h"
25 
26 namespace daal
27 {
28 namespace data_management
29 {
30 namespace features
31 {
32 namespace internal
33 {
34 
35 template<typename T>
36 inline services::Status pickElementsRaw(const FeatureIndicesIfacePtr &indices,
37  T *elements, T **pickedElements)
38 {
39  DAAL_ASSERT( indices );
40  DAAL_ASSERT( elements );
41  DAAL_ASSERT( pickedElements );
42 
43  if (indices->isPlainRange())
44  {
45  const size_t first = indices->getFirst();
46  const size_t last = indices->getLast();
47 
48  size_t k = 0;
49  if (first <= last)
50  {
51  for (size_t i = first; i <= last; i++)
52  {
53  pickedElements[k++] = &elements[i];
54  }
55  }
56  else
57  {
58  for (size_t i = first + 1; i > last; i--)
59  {
60  pickedElements[k++] = &elements[i - 1];
61  }
62  }
63  }
64  else if (indices->areRawFeatureIndicesAvailable())
65  {
66  const services::BufferView<FeatureIndex> indicesBuffer = indices->getRawFeatureIndices();
67  const FeatureIndex *rawIndices = indicesBuffer.data();
68  const size_t indicesSize = indicesBuffer.size();
69 
70  for (size_t i = 0; i < indicesSize; i++)
71  {
72  pickedElements[i] = &elements[ rawIndices[i] ];
73  }
74  }
75  else
76  {
77  return services::throwIfPossible(services::ErrorMethodNotImplemented);
78  }
79 
80  return services::Status();
81 }
82 
83 template<typename T>
84 inline services::internal::CollectionPtr<T *> pickElements(const FeatureIndicesIfacePtr &indices, T *elements,
85  services::Status *status = NULL)
86 {
87  DAAL_ASSERT( indices );
88  DAAL_ASSERT( elements );
89 
90  services::internal::CollectionPtr<T *> pickedElements =
91  services::internal::HeapAllocatableCollection<T *>::create(indices->size(), status);
92  if (!pickedElements) { return pickedElements; }
93 
94  services::Status pickElementsStatus = pickElementsRaw<T>(indices, elements, pickedElements->data());
95  if (!pickElementsStatus.ok())
96  {
97  services::internal::tryAssignStatusAndThrow(status, pickElementsStatus);
98  return services::internal::CollectionPtr<T *>();
99  }
100 
101  return pickedElements;
102 }
103 
104 template<typename T>
105 inline services::internal::CollectionPtr<T *> pickElements(const FeatureIndicesIfacePtr &indices,
106  const services::Collection<T> &elements,
107  services::Status *status = NULL)
108 {
109  return pickElements(indices, const_cast<T *>(elements.data()), status);
110 }
111 
112 template<typename T>
113 inline services::internal::CollectionPtr<T *> pickElements(const FeatureIndicesIfacePtr &indices,
114  const services::internal::CollectionPtr<T> &elements,
115  services::Status *status = NULL)
116 {
117  DAAL_ASSERT( elements );
118  return pickElements(indices, const_cast<T *>(elements->data()), status);
119 }
120 
127 template<typename T>
128 class ElementsPicker
129 {
130 public:
131  services::Status pick(const FeatureIndicesIfacePtr &indices)
132  {
133  DAAL_ASSERT( indices );
134  DAAL_ASSERT( _elements );
135 
136  services::Status status;
137  _pickedElements = pickElements(indices, _elements, &status);
138 
139  return status;
140  }
141 
142  void setElements(const services::internal::CollectionPtr<T> &elements)
143  {
144  DAAL_ASSERT( elements );
145  _elements = elements;
146  }
147 
148  const services::internal::CollectionPtr<T> &getElements() const
149  {
150  return _elements;
151  }
152 
153  const services::internal::CollectionPtr<T *> &getPickedElements() const
154  {
155  return _pickedElements;
156  }
157 
158 private:
159  services::internal::CollectionPtr<T> _elements;
160  services::internal::CollectionPtr<T *> _pickedElements;
161 };
162 
167 template<typename T> inline IndexNumType getIndexNumType() { return DAAL_OTHER_T; }
168 template<> inline IndexNumType getIndexNumType<float>() { return DAAL_FLOAT32; }
169 template<> inline IndexNumType getIndexNumType<double>() { return DAAL_FLOAT64; }
170 template<> inline IndexNumType getIndexNumType<int>() { return DAAL_INT32_S; }
171 template<> inline IndexNumType getIndexNumType<unsigned int>() { return DAAL_INT32_U; }
172 template<> inline IndexNumType getIndexNumType<DAAL_INT64>() { return DAAL_INT64_S; }
173 template<> inline IndexNumType getIndexNumType<DAAL_UINT64>() { return DAAL_INT64_U; }
174 template<> inline IndexNumType getIndexNumType<char>() { return DAAL_INT8_S; }
175 template<> inline IndexNumType getIndexNumType<unsigned char>() { return DAAL_INT8_U; }
176 template<> inline IndexNumType getIndexNumType<short>() { return DAAL_INT16_S; }
177 template<> inline IndexNumType getIndexNumType<unsigned short>() { return DAAL_INT16_U; }
178 
179 template<> inline IndexNumType getIndexNumType<long>()
180 { return (IndexNumType)(DAAL_INT32_S + (sizeof(long) / 4 - 1) * 2); }
181 
182 #if (defined(__APPLE__) || defined(__MACH__)) && !defined(__x86_64__)
183 template<> inline IndexNumType getIndexNumType<unsigned long>()
184 { return (IndexNumType)(DAAL_INT32_U + (sizeof(unsigned long) / 4 - 1) * 2); }
185 #endif
186 
187 #if !(defined(_WIN32) || defined(_WIN64)) && defined(__x86_64__)
188 template<> inline IndexNumType getIndexNumType<size_t>()
189 { return (IndexNumType)(DAAL_INT32_U + (sizeof(size_t) / 4 - 1) * 2); }
190 #endif
191 
195 template<typename T>
196 inline PMMLNumType getPMMLNumType() { return DAAL_GEN_UNKNOWN; }
197 template<>
198 inline PMMLNumType getPMMLNumType<int>() { return DAAL_GEN_INTEGER; }
199 template<>
200 inline PMMLNumType getPMMLNumType<double>() { return DAAL_GEN_DOUBLE; }
201 template<>
202 inline PMMLNumType getPMMLNumType<float>() { return DAAL_GEN_FLOAT; }
203 template<>
204 inline PMMLNumType getPMMLNumType<bool>() { return DAAL_GEN_BOOLEAN; }
205 template<>
206 inline PMMLNumType getPMMLNumType<char *>() { return DAAL_GEN_STRING; }
207 template<>
208 inline PMMLNumType getPMMLNumType<std::string>() { return DAAL_GEN_STRING; }
209 
210 } // namespace internal
211 } // namespace features
212 } // namespace data_management
213 } // namespace daal
214 
215 #endif
daal::services::ErrorMethodNotImplemented
Definition: error_indexes.h:419
daal::data_management::features::interface1::FeatureIndex
size_t FeatureIndex
Definition: indices.h:39
daal::services::internal::CollectionPtr
Shared pointer to the Collection object.
Definition: internal/collection.h:138
daal::data_management::features::internal::ElementsPicker
Class that stores collection of elements of specified type and pointers to the elements of that colle...
Definition: helpers.h:128

For more complete information about compiler optimizations, see our Optimization Notice.