C++ API Reference for Intel® Data Analytics Acceleration Library 2019

helpers.h
1 /* file: helpers.h */
2 /*******************************************************************************
3 * Copyright 2014-2018 Intel Corporation.
4 *
5 * This software and the related documents are Intel copyrighted materials, and
6 * your use of them is governed by the express license under which they were
7 * provided to you (License). Unless the License provides otherwise, you may not
8 * use, modify, copy, publish, distribute, disclose or transmit this software or
9 * the related documents without Intel's prior written permission.
10 *
11 * This software and the related documents are provided as is, with no express
12 * or implied warranties, other than those that are expressly stated in the
13 * License.
14 *******************************************************************************/
15 
16 #ifndef __DATA_MANAGEMENT_FEATURES_INTERNAL_HELPERS_H__
17 #define __DATA_MANAGEMENT_FEATURES_INTERNAL_HELPERS_H__
18 
19 #include "services/internal/utilities.h"
20 #include "services/internal/collection.h"
21 
22 #include "data_management/features/indices.h"
23 
24 namespace daal
25 {
26 namespace data_management
27 {
28 namespace features
29 {
30 namespace internal
31 {
32 
33 template<typename T>
34 inline services::Status pickElementsRaw(const FeatureIndicesIfacePtr &indices,
35  T *elements, T **pickedElements)
36 {
37  DAAL_ASSERT( indices );
38  DAAL_ASSERT( elements );
39  DAAL_ASSERT( pickedElements );
40 
41  if (indices->isPlainRange())
42  {
43  const size_t first = indices->getFirst();
44  const size_t last = indices->getLast();
45 
46  size_t k = 0;
47  if (first <= last)
48  {
49  for (size_t i = first; i <= last; i++)
50  {
51  pickedElements[k++] = &elements[i];
52  }
53  }
54  else
55  {
56  for (size_t i = first; i >= last; i--)
57  {
58  pickedElements[k++] = &elements[i];
59  }
60  }
61  }
62  else if (indices->areRawFeatureIndicesAvailable())
63  {
64  const services::BufferView<FeatureIndex> indicesBuffer = indices->getRawFeatureIndices();
65  const FeatureIndex *rawIndices = indicesBuffer.data();
66  const size_t indicesSize = indicesBuffer.size();
67 
68  for (size_t i = 0; i < indicesSize; i++)
69  {
70  pickedElements[i] = &elements[ rawIndices[i] ];
71  }
72  }
73  else
74  {
75  return services::throwIfPossible(services::ErrorMethodNotImplemented);
76  }
77 
78  return services::Status();
79 }
80 
81 template<typename T>
82 inline services::internal::CollectionPtr<T *> pickElements(const FeatureIndicesIfacePtr &indices, T *elements,
83  services::Status *status = NULL)
84 {
85  DAAL_ASSERT( indices );
86  DAAL_ASSERT( elements );
87 
88  services::internal::CollectionPtr<T *> pickedElements =
89  services::internal::HeapAllocatableCollection<T *>::create(indices->size(), status);
90  if (!pickedElements) { return pickedElements; }
91 
92  services::Status pickElementsStatus = pickElementsRaw<T>(indices, elements, pickedElements->data());
93  if (!pickElementsStatus.ok())
94  {
95  services::internal::tryAssignStatusAndThrow(status, pickElementsStatus);
96  return services::internal::CollectionPtr<T *>();
97  }
98 
99  return pickedElements;
100 }
101 
102 template<typename T>
103 inline services::internal::CollectionPtr<T *> pickElements(const FeatureIndicesIfacePtr &indices,
104  const services::Collection<T> &elements,
105  services::Status *status = NULL)
106 {
107  return pickElements(indices, const_cast<T *>(elements.data()), status);
108 }
109 
110 template<typename T>
111 inline services::internal::CollectionPtr<T *> pickElements(const FeatureIndicesIfacePtr &indices,
112  const services::internal::CollectionPtr<T> &elements,
113  services::Status *status = NULL)
114 {
115  DAAL_ASSERT( elements );
116  return pickElements(indices, const_cast<T *>(elements->data()), status);
117 }
118 
125 template<typename T>
126 class ElementsPicker
127 {
128 public:
129  services::Status pick(const FeatureIndicesIfacePtr &indices)
130  {
131  DAAL_ASSERT( indices );
132  DAAL_ASSERT( _elements );
133 
134  services::Status status;
135  _pickedElements = pickElements(indices, _elements, &status);
136 
137  return status;
138  }
139 
140  void setElements(const services::internal::CollectionPtr<T> &elements)
141  {
142  DAAL_ASSERT( elements );
143  _elements = elements;
144  }
145 
146  const services::internal::CollectionPtr<T> &getElements() const
147  {
148  return _elements;
149  }
150 
151  const services::internal::CollectionPtr<T *> &getPickedElements() const
152  {
153  return _pickedElements;
154  }
155 
156 private:
157  services::internal::CollectionPtr<T> _elements;
158  services::internal::CollectionPtr<T *> _pickedElements;
159 };
160 
165 template<typename T> inline IndexNumType getIndexNumType() { return DAAL_OTHER_T; }
166 template<> inline IndexNumType getIndexNumType<float>() { return DAAL_FLOAT32; }
167 template<> inline IndexNumType getIndexNumType<double>() { return DAAL_FLOAT64; }
168 template<> inline IndexNumType getIndexNumType<int>() { return DAAL_INT32_S; }
169 template<> inline IndexNumType getIndexNumType<unsigned int>() { return DAAL_INT32_U; }
170 template<> inline IndexNumType getIndexNumType<DAAL_INT64>() { return DAAL_INT64_S; }
171 template<> inline IndexNumType getIndexNumType<DAAL_UINT64>() { return DAAL_INT64_U; }
172 template<> inline IndexNumType getIndexNumType<char>() { return DAAL_INT8_S; }
173 template<> inline IndexNumType getIndexNumType<unsigned char>() { return DAAL_INT8_U; }
174 template<> inline IndexNumType getIndexNumType<short>() { return DAAL_INT16_S; }
175 template<> inline IndexNumType getIndexNumType<unsigned short>() { return DAAL_INT16_U; }
176 
177 template<> inline IndexNumType getIndexNumType<long>()
178 { return (IndexNumType)(DAAL_INT32_S + (sizeof(long) / 4 - 1) * 2); }
179 
180 #if (defined(__APPLE__) || defined(__MACH__)) && !defined(__x86_64__)
181 template<> inline IndexNumType getIndexNumType<unsigned long>()
182 { return (IndexNumType)(DAAL_INT32_U + (sizeof(unsigned long) / 4 - 1) * 2); }
183 #endif
184 
185 #if !(defined(_WIN32) || defined(_WIN64)) && defined(__x86_64__)
186 template<> inline IndexNumType getIndexNumType<size_t>()
187 { return (IndexNumType)(DAAL_INT32_U + (sizeof(size_t) / 4 - 1) * 2); }
188 #endif
189 
193 template<typename T>
194 inline PMMLNumType getPMMLNumType() { return DAAL_GEN_UNKNOWN; }
195 template<>
196 inline PMMLNumType getPMMLNumType<int>() { return DAAL_GEN_INTEGER; }
197 template<>
198 inline PMMLNumType getPMMLNumType<double>() { return DAAL_GEN_DOUBLE; }
199 template<>
200 inline PMMLNumType getPMMLNumType<float>() { return DAAL_GEN_FLOAT; }
201 template<>
202 inline PMMLNumType getPMMLNumType<bool>() { return DAAL_GEN_BOOLEAN; }
203 template<>
204 inline PMMLNumType getPMMLNumType<char *>() { return DAAL_GEN_STRING; }
205 template<>
206 inline PMMLNumType getPMMLNumType<std::string>() { return DAAL_GEN_STRING; }
207 
208 } // namespace internal
209 } // namespace features
210 } // namespace data_management
211 } // namespace daal
212 
213 #endif
daal::data_management::features::interface1::FeatureIndex
size_t FeatureIndex
Definition: indices.h:37
daal
Definition: algorithm_base_common.h:31
daal::services::internal::CollectionPtr
Shared pointer to the Collection object.
Definition: internal/collection.h:136
daal::services::ErrorMethodNotImplemented
Definition: error_indexes.h:405
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:126

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