16 #ifndef __DATA_MANAGEMENT_FEATURES_INTERNAL_IDENTIFIERS_IMPL_H__
17 #define __DATA_MANAGEMENT_FEATURES_INTERNAL_IDENTIFIERS_IMPL_H__
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"
30 namespace data_management
41 class FeatureIdDefaultMapping :
public FeatureIdMapping
44 typedef std::map<std::string, FeatureIndex> KeyToIndexMap;
47 static services::SharedPtr<FeatureIdDefaultMapping> create(
size_t numberOfFeatures,
48 services::Status *status = NULL)
50 return services::internal::wrapSharedAndTryThrow<FeatureIdDefaultMapping>(
51 new FeatureIdDefaultMapping(numberOfFeatures), status);
54 virtual size_t getNumberOfFeatures()
const DAAL_C11_OVERRIDE
56 return _numberOfFeatures;
59 virtual FeatureIndex getIndexByKey(
const services::String &key)
const DAAL_C11_OVERRIDE
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())
66 return FeatureIndexTraits::invalid();
71 virtual bool areKeysAvailable()
const DAAL_C11_OVERRIDE
73 return _keyToIndexMap.size() > 0;
76 void setFeatureKey(FeatureIndex featureIndex,
const services::String &key)
78 const std::string stdKey(key.c_str(), key.c_str() + key.length());
79 _keyToIndexMap[stdKey] = featureIndex;
82 void setNumberOfFeatures(
size_t numberOfFeatures)
84 _numberOfFeatures = numberOfFeatures;
88 explicit FeatureIdDefaultMapping(
size_t numberOfFeatures) :
89 _numberOfFeatures(numberOfFeatures) { }
91 size_t _numberOfFeatures;
92 KeyToIndexMap _keyToIndexMap;
94 typedef services::SharedPtr<FeatureIdDefaultMapping> FeatureIdDefaultMappingPtr;
100 class FeatureIdList :
public FeatureIdCollection
103 static services::SharedPtr<FeatureIdList> create(services::Status *status = NULL)
105 return services::internal::wrapSharedAndTryThrow<FeatureIdList>(
new FeatureIdList(), status);
108 virtual FeatureIndicesIfacePtr mapToFeatureIndices(
const FeatureIdMappingIface &mapping,
109 services::Status *status = NULL) DAAL_C11_OVERRIDE
111 const size_t numberOfFeatures = _featureIds.size();
113 services::Status localStatus;
115 FeatureIndicesListPtr featureFeatureIndices = FeatureIndicesList::create(&localStatus);
116 services::internal::tryAssignStatusAndThrow(status, localStatus);
117 if (!localStatus.ok()) {
return FeatureIndicesPtr(); }
119 for (
size_t i = 0; i < numberOfFeatures; i++)
121 const FeatureIdIfacePtr &
id = _featureIds[i];
123 const FeatureIndex mappedIndex =
id->mapToIndex(mapping, &localStatus);
124 services::internal::tryAssignStatusAndThrow(status, localStatus);
125 if (!localStatus.ok()) {
return FeatureIndicesPtr(); }
127 featureFeatureIndices->add(mappedIndex);
130 return featureFeatureIndices;
133 services::Status add(
const FeatureIdIfacePtr &
id)
136 {
return services::throwIfPossible(services::ErrorNullPtr); }
138 if ( !_featureIds.safe_push_back(
id) )
139 {
return services::throwIfPossible(services::ErrorMemoryAllocationFailed); }
141 return services::Status();
146 return _featureIds.size();
152 services::Collection<FeatureIdIfacePtr> _featureIds;
154 typedef services::SharedPtr<FeatureIdList> FeatureIdListPtr;
160 class FeatureIdRange :
public FeatureIdCollection
163 static services::SharedPtr<FeatureIdRange> create(
const FeatureIdIfacePtr &begin,
164 const FeatureIdIfacePtr &end,
165 services::Status *status = NULL)
169 services::internal::tryAssignStatusAndThrow(status, services::ErrorNullPtr);
170 return services::SharedPtr<FeatureIdRange>();
173 return services::internal::wrapSharedAndTryThrow<FeatureIdRange>(
174 new FeatureIdRange(begin, end), status);
177 virtual FeatureIndicesIfacePtr mapToFeatureIndices(
const FeatureIdMappingIface &mapping,
178 services::Status *status = NULL) DAAL_C11_OVERRIDE
180 services::Status localStatus;
182 const FeatureIndex beginIndex = _begin->mapToIndex(mapping, &localStatus);
183 services::internal::tryAssignStatusAndThrow(status, localStatus);
185 const FeatureIndex endIndex = _end->mapToIndex(mapping, &localStatus);
186 services::internal::tryAssignStatusAndThrow(status, localStatus);
188 if (!localStatus.ok()) {
return FeatureIndicesPtr(); }
189 return FeatureIndicesRange::create(beginIndex, endIndex, status);
193 explicit FeatureIdRange(
const FeatureIdIfacePtr &begin,
const FeatureIdIfacePtr &end) :
197 FeatureIdIfacePtr _begin;
198 FeatureIdIfacePtr _end;
200 typedef services::SharedPtr<FeatureIdRange> FeatureIdRangePtr;
206 class NumericFeatureId :
public FeatureId
209 typedef long long InternalIndex;
211 static services::SharedPtr<NumericFeatureId> create(NumericFeatureId::InternalIndex index,
212 services::Status *status = NULL)
214 return services::internal::wrapSharedAndTryThrow<NumericFeatureId>(
new NumericFeatureId(index), status);
217 virtual FeatureIndex mapToIndex(
const FeatureIdMappingIface &mapping,
218 services::Status *status = NULL) DAAL_C11_OVERRIDE
220 const size_t numberOfFeatures = mapping.getNumberOfFeatures();
224 if ((InternalIndex)numberOfFeatures + _index > (std::numeric_limits<InternalIndex>::max)())
226 services::internal::tryAssignStatusAndThrow(status, services::ErrorIncorrectIndex);
227 return FeatureIndexTraits::invalid();
232 const InternalIndex nf = (InternalIndex)numberOfFeatures;
233 if (_index >= nf || _index < -nf)
235 services::internal::tryAssignStatusAndThrow(status, services::ErrorIncorrectIndex);
236 return FeatureIndexTraits::invalid();
239 return (FeatureIndex)( (nf + _index) % nf );
243 explicit NumericFeatureId(InternalIndex index) :
246 InternalIndex _index;
248 typedef services::SharedPtr<NumericFeatureId> NumericFeatureIdPtr;
254 class StringFeatureId :
public FeatureId
257 static services::SharedPtr<StringFeatureId> create(
const services::String &name,
258 services::Status *status = NULL)
260 return services::internal::wrapSharedAndTryThrow<StringFeatureId>(
new StringFeatureId(name), status);
263 virtual FeatureIndex mapToIndex(
const FeatureIdMappingIface &mapping,
264 services::Status *status = NULL) DAAL_C11_OVERRIDE
266 if (!mapping.areKeysAvailable())
268 services::internal::tryAssignStatusAndThrow(status, services::ErrorFeatureNamesNotAvailable);
269 return FeatureIndexTraits::invalid();
272 const FeatureIndex index = mapping.getIndexByKey(_name);
273 if (index == FeatureIndexTraits::invalid())
275 services::internal::tryAssignStatusAndThrow(status, services::ErrorFeatureNamesNotAvailable);
276 return FeatureIndexTraits::invalid();
283 explicit StringFeatureId(
const services::String &name) :
286 services::String _name;
288 typedef services::SharedPtr<StringFeatureId> StringFeatureIdPtr;
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