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

identifiers_impl.h
1 /* file: identifiers_impl.h */
2 /*******************************************************************************
3 * Copyright 2014-2019 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_IDENTIFIERS_IMPL_H__
17 #define __DATA_MANAGEMENT_FEATURES_INTERNAL_IDENTIFIERS_IMPL_H__
18 
19 #include <map>
20 #include <limits>
21 #include <string>
22 
23 #include "services/collection.h"
24 #include "services/internal/utilities.h"
25 #include "data_management/features/identifiers.h"
26 #include "data_management/features/internal/indices_impl.h"
27 
28 namespace daal
29 {
30 namespace data_management
31 {
32 namespace features
33 {
34 namespace internal
35 {
36 
41 class FeatureIdDefaultMapping : public FeatureIdMapping
42 {
43 private:
44  typedef std::map<std::string, FeatureIndex> KeyToIndexMap;
45 
46 public:
47  static services::SharedPtr<FeatureIdDefaultMapping> create(size_t numberOfFeatures,
48  services::Status *status = NULL)
49  {
50  return services::internal::wrapSharedAndTryThrow<FeatureIdDefaultMapping>(
51  new FeatureIdDefaultMapping(numberOfFeatures), status);
52  }
53 
54  virtual size_t getNumberOfFeatures() const DAAL_C11_OVERRIDE
55  {
56  return _numberOfFeatures;
57  }
58 
59  virtual FeatureIndex getIndexByKey(const services::String &key) const DAAL_C11_OVERRIDE
60  {
61  const std::string stdKey(key.c_str(), key.c_str() + key.length());
62  KeyToIndexMap &keyToIndexMap = const_cast<KeyToIndexMap &>(_keyToIndexMap);
63  KeyToIndexMap::const_iterator it = keyToIndexMap.find(stdKey);
64  if (it == keyToIndexMap.end())
65  {
66  return FeatureIndexTraits::invalid();
67  }
68  return it->second;
69  }
70 
71  virtual bool areKeysAvailable() const DAAL_C11_OVERRIDE
72  {
73  return _keyToIndexMap.size() > 0;
74  }
75 
76  void setFeatureKey(FeatureIndex featureIndex, const services::String &key)
77  {
78  const std::string stdKey(key.c_str(), key.c_str() + key.length());
79  _keyToIndexMap[stdKey] = featureIndex;
80  }
81 
82  void setNumberOfFeatures(size_t numberOfFeatures)
83  {
84  _numberOfFeatures = numberOfFeatures;
85  }
86 
87 private:
88  explicit FeatureIdDefaultMapping(size_t numberOfFeatures) :
89  _numberOfFeatures(numberOfFeatures) { }
90 
91  size_t _numberOfFeatures;
92  KeyToIndexMap _keyToIndexMap;
93 };
94 typedef services::SharedPtr<FeatureIdDefaultMapping> FeatureIdDefaultMappingPtr;
95 
100 class FeatureIdList : public FeatureIdCollection
101 {
102 public:
103  static services::SharedPtr<FeatureIdList> create(services::Status *status = NULL)
104  {
105  return services::internal::wrapSharedAndTryThrow<FeatureIdList>(new FeatureIdList(), status);
106  }
107 
108  virtual FeatureIndicesIfacePtr mapToFeatureIndices(const FeatureIdMappingIface &mapping,
109  services::Status *status = NULL) DAAL_C11_OVERRIDE
110  {
111  const size_t numberOfFeatures = _featureIds.size();
112 
113  services::Status localStatus;
114 
115  FeatureIndicesListPtr featureFeatureIndices = FeatureIndicesList::create(&localStatus);
116  services::internal::tryAssignStatusAndThrow(status, localStatus);
117  if (!localStatus.ok()) { return FeatureIndicesPtr(); }
118 
119  for (size_t i = 0; i < numberOfFeatures; i++)
120  {
121  const FeatureIdIfacePtr &id = _featureIds[i];
122 
123  const FeatureIndex mappedIndex = id->mapToIndex(mapping, &localStatus);
124  services::internal::tryAssignStatusAndThrow(status, localStatus);
125  if (!localStatus.ok()) { return FeatureIndicesPtr(); }
126 
127  featureFeatureIndices->add(mappedIndex);
128  }
129 
130  return featureFeatureIndices;
131  }
132 
133  services::Status add(const FeatureIdIfacePtr &id)
134  {
135  if (!id)
136  { return services::throwIfPossible(services::ErrorNullPtr); }
137 
138  if ( !_featureIds.safe_push_back(id) )
139  { return services::throwIfPossible(services::ErrorMemoryAllocationFailed); }
140 
141  return services::Status();
142  }
143 
144  size_t size() const
145  {
146  return _featureIds.size();
147  }
148 
149 private:
150  FeatureIdList() { }
151 
152  services::Collection<FeatureIdIfacePtr> _featureIds;
153 };
154 typedef services::SharedPtr<FeatureIdList> FeatureIdListPtr;
155 
160 class FeatureIdRange : public FeatureIdCollection
161 {
162 public:
163  static services::SharedPtr<FeatureIdRange> create(const FeatureIdIfacePtr &begin,
164  const FeatureIdIfacePtr &end,
165  services::Status *status = NULL)
166  {
167  if (!begin || !end)
168  {
169  services::internal::tryAssignStatusAndThrow(status, services::ErrorNullPtr);
170  return services::SharedPtr<FeatureIdRange>();
171  }
172 
173  return services::internal::wrapSharedAndTryThrow<FeatureIdRange>(
174  new FeatureIdRange(begin, end), status);
175  }
176 
177  virtual FeatureIndicesIfacePtr mapToFeatureIndices(const FeatureIdMappingIface &mapping,
178  services::Status *status = NULL) DAAL_C11_OVERRIDE
179  {
180  services::Status localStatus;
181 
182  const FeatureIndex beginIndex = _begin->mapToIndex(mapping, &localStatus);
183  services::internal::tryAssignStatusAndThrow(status, localStatus);
184 
185  const FeatureIndex endIndex = _end->mapToIndex(mapping, &localStatus);
186  services::internal::tryAssignStatusAndThrow(status, localStatus);
187 
188  if (!localStatus.ok()) { return FeatureIndicesPtr(); }
189  return FeatureIndicesRange::create(beginIndex, endIndex, status);
190  }
191 
192 private:
193  explicit FeatureIdRange(const FeatureIdIfacePtr &begin, const FeatureIdIfacePtr &end) :
194  _begin(begin),
195  _end(end) { }
196 
197  FeatureIdIfacePtr _begin;
198  FeatureIdIfacePtr _end;
199 };
200 typedef services::SharedPtr<FeatureIdRange> FeatureIdRangePtr;
201 
206 class NumericFeatureId : public FeatureId
207 {
208 public:
209  typedef long long InternalIndex;
210 
211  static services::SharedPtr<NumericFeatureId> create(NumericFeatureId::InternalIndex index,
212  services::Status *status = NULL)
213  {
214  return services::internal::wrapSharedAndTryThrow<NumericFeatureId>(new NumericFeatureId(index), status);
215  }
216 
217  virtual FeatureIndex mapToIndex(const FeatureIdMappingIface &mapping,
218  services::Status *status = NULL) DAAL_C11_OVERRIDE
219  {
220  const size_t numberOfFeatures = mapping.getNumberOfFeatures();
221 
222  /* Check that size of 'InternalIndex' type is enough to store number of
223  * features and is able to store intermediate result of computations */
224  if ((InternalIndex)numberOfFeatures + _index > (std::numeric_limits<InternalIndex>::max)())
225  {
226  services::internal::tryAssignStatusAndThrow(status, services::ErrorIncorrectIndex);
227  return FeatureIndexTraits::invalid();
228  }
229 
230  /* Positive indices in the range [0, nf - 1] and negative indices in the range [ -nf, -1 ] are allowed.
231  * Negative indices shoud be interpreted as a result of subtraction nf - abs(index) */
232  const InternalIndex nf = (InternalIndex)numberOfFeatures;
233  if (_index >= nf || _index < -nf)
234  {
235  services::internal::tryAssignStatusAndThrow(status, services::ErrorIncorrectIndex);
236  return FeatureIndexTraits::invalid();
237  }
238 
239  return (FeatureIndex)( (nf + _index) % nf );
240  }
241 
242 private:
243  explicit NumericFeatureId(InternalIndex index) :
244  _index(index) { }
245 
246  InternalIndex _index;
247 };
248 typedef services::SharedPtr<NumericFeatureId> NumericFeatureIdPtr;
249 
254 class StringFeatureId : public FeatureId
255 {
256 public:
257  static services::SharedPtr<StringFeatureId> create(const services::String &name,
258  services::Status *status = NULL)
259  {
260  return services::internal::wrapSharedAndTryThrow<StringFeatureId>(new StringFeatureId(name), status);
261  }
262 
263  virtual FeatureIndex mapToIndex(const FeatureIdMappingIface &mapping,
264  services::Status *status = NULL) DAAL_C11_OVERRIDE
265  {
266  if (!mapping.areKeysAvailable())
267  {
268  services::internal::tryAssignStatusAndThrow(status, services::ErrorFeatureNamesNotAvailable);
269  return FeatureIndexTraits::invalid();
270  }
271 
272  const FeatureIndex index = mapping.getIndexByKey(_name);
273  if (index == FeatureIndexTraits::invalid())
274  {
275  services::internal::tryAssignStatusAndThrow(status, services::ErrorFeatureNamesNotAvailable);
276  return FeatureIndexTraits::invalid();
277  }
278 
279  return index;
280  }
281 
282 private:
283  explicit StringFeatureId(const services::String &name) :
284  _name(name) { }
285 
286  services::String _name;
287 };
288 typedef services::SharedPtr<StringFeatureId> StringFeatureIdPtr;
289 
290 } // namespace internal
291 } // namespace features
292 } // namespace data_management
293 } // namespace daal
294 
295 #endif
daal
Definition: algorithm_base_common.h:31
daal::services::ErrorMemoryAllocationFailed
Definition: error_indexes.h:146
daal::services::ErrorIncorrectIndex
Definition: error_indexes.h:100
daal::data_management::features::internal::NumericFeatureId
Implementation of FeatureId that uses number as a reference to particular feature.
Definition: identifiers_impl.h:206
daal::services::ErrorNullPtr
Definition: error_indexes.h:139
daal::services::ErrorFeatureNamesNotAvailable
Definition: error_indexes.h:170
daal::data_management::features::internal::FeatureIdDefaultMapping
Default implementation of feature mapping.
Definition: identifiers_impl.h:41
daal::data_management::features::internal::FeatureIdList
Implementation of FeatureIdCollection to store a list of feature identifiers.
Definition: identifiers_impl.h:100
daal::data_management::features::internal::FeatureIdRange
Implementation of FeatureIdCollection to store a range of feature identifiers.
Definition: identifiers_impl.h:160
daal::data_management::features::internal::StringFeatureId
Implementation of FeatureId that uses string as a reference to particular feature.
Definition: identifiers_impl.h:254

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