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

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

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