18 #ifndef __DATA_MANAGEMENT_FEATURES_INTERNAL_IDENTIFIERS_IMPL_H__
19 #define __DATA_MANAGEMENT_FEATURES_INTERNAL_IDENTIFIERS_IMPL_H__
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"
32 namespace data_management
43 class FeatureIdDefaultMapping :
public FeatureIdMapping
46 typedef std::map<std::string, FeatureIndex> KeyToIndexMap;
49 static services::SharedPtr<FeatureIdDefaultMapping> create(
size_t numberOfFeatures,
50 services::Status *status = NULL)
52 return services::internal::wrapSharedAndTryThrow<FeatureIdDefaultMapping>(
53 new FeatureIdDefaultMapping(numberOfFeatures), status);
56 virtual size_t getNumberOfFeatures()
const DAAL_C11_OVERRIDE
58 return _numberOfFeatures;
61 virtual FeatureIndex getIndexByKey(
const services::String &key)
const DAAL_C11_OVERRIDE
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())
68 return FeatureIndexTraits::invalid();
73 virtual bool areKeysAvailable()
const DAAL_C11_OVERRIDE
75 return _keyToIndexMap.size() > 0;
78 void setFeatureKey(FeatureIndex featureIndex,
const services::String &key)
80 const std::string stdKey(key.c_str(), key.c_str() + key.length());
81 _keyToIndexMap[stdKey] = featureIndex;
84 void setNumberOfFeatures(
size_t numberOfFeatures)
86 _numberOfFeatures = numberOfFeatures;
90 explicit FeatureIdDefaultMapping(
size_t numberOfFeatures) :
91 _numberOfFeatures(numberOfFeatures) { }
93 size_t _numberOfFeatures;
94 KeyToIndexMap _keyToIndexMap;
96 typedef services::SharedPtr<FeatureIdDefaultMapping> FeatureIdDefaultMappingPtr;
102 class FeatureIdList :
public FeatureIdCollection
105 static services::SharedPtr<FeatureIdList> create(services::Status *status = NULL)
107 return services::internal::wrapSharedAndTryThrow<FeatureIdList>(
new FeatureIdList(), status);
110 virtual FeatureIndicesIfacePtr mapToFeatureIndices(
const FeatureIdMappingIface &mapping,
111 services::Status *status = NULL) DAAL_C11_OVERRIDE
113 const size_t numberOfFeatures = _featureIds.size();
115 services::Status localStatus;
117 FeatureIndicesListPtr featureFeatureIndices = FeatureIndicesList::create(&localStatus);
118 services::internal::tryAssignStatusAndThrow(status, localStatus);
119 if (!localStatus.ok()) {
return FeatureIndicesPtr(); }
121 for (
size_t i = 0; i < numberOfFeatures; i++)
123 const FeatureIdIfacePtr &
id = _featureIds[i];
125 const FeatureIndex mappedIndex =
id->mapToIndex(mapping, &localStatus);
126 services::internal::tryAssignStatusAndThrow(status, localStatus);
127 if (!localStatus.ok()) {
return FeatureIndicesPtr(); }
129 featureFeatureIndices->add(mappedIndex);
132 return featureFeatureIndices;
135 services::Status add(
const FeatureIdIfacePtr &
id)
138 {
return services::throwIfPossible(services::ErrorNullPtr); }
140 if ( !_featureIds.safe_push_back(
id) )
141 {
return services::throwIfPossible(services::ErrorMemoryAllocationFailed); }
143 return services::Status();
148 return _featureIds.size();
154 services::Collection<FeatureIdIfacePtr> _featureIds;
156 typedef services::SharedPtr<FeatureIdList> FeatureIdListPtr;
162 class FeatureIdRange :
public FeatureIdCollection
165 static services::SharedPtr<FeatureIdRange> create(
const FeatureIdIfacePtr &begin,
166 const FeatureIdIfacePtr &end,
167 services::Status *status = NULL)
171 services::internal::tryAssignStatusAndThrow(status, services::ErrorNullPtr);
172 return services::SharedPtr<FeatureIdRange>();
175 return services::internal::wrapSharedAndTryThrow<FeatureIdRange>(
176 new FeatureIdRange(begin, end), status);
179 virtual FeatureIndicesIfacePtr mapToFeatureIndices(
const FeatureIdMappingIface &mapping,
180 services::Status *status = NULL) DAAL_C11_OVERRIDE
182 services::Status localStatus;
184 const FeatureIndex beginIndex = _begin->mapToIndex(mapping, &localStatus);
185 services::internal::tryAssignStatusAndThrow(status, localStatus);
187 const FeatureIndex endIndex = _end->mapToIndex(mapping, &localStatus);
188 services::internal::tryAssignStatusAndThrow(status, localStatus);
190 if (!localStatus.ok()) {
return FeatureIndicesPtr(); }
191 return FeatureIndicesRange::create(beginIndex, endIndex, status);
195 explicit FeatureIdRange(
const FeatureIdIfacePtr &begin,
const FeatureIdIfacePtr &end) :
199 FeatureIdIfacePtr _begin;
200 FeatureIdIfacePtr _end;
202 typedef services::SharedPtr<FeatureIdRange> FeatureIdRangePtr;
208 class NumericFeatureId :
public FeatureId
211 typedef long long InternalIndex;
213 static services::SharedPtr<NumericFeatureId> create(NumericFeatureId::InternalIndex index,
214 services::Status *status = NULL)
216 return services::internal::wrapSharedAndTryThrow<NumericFeatureId>(
new NumericFeatureId(index), status);
219 virtual FeatureIndex mapToIndex(
const FeatureIdMappingIface &mapping,
220 services::Status *status = NULL) DAAL_C11_OVERRIDE
222 const size_t numberOfFeatures = mapping.getNumberOfFeatures();
226 if ((InternalIndex)numberOfFeatures + _index > (std::numeric_limits<InternalIndex>::max)())
228 services::internal::tryAssignStatusAndThrow(status, services::ErrorIncorrectIndex);
229 return FeatureIndexTraits::invalid();
234 const InternalIndex nf = (InternalIndex)numberOfFeatures;
235 if (_index >= nf || _index < -nf)
237 services::internal::tryAssignStatusAndThrow(status, services::ErrorIncorrectIndex);
238 return FeatureIndexTraits::invalid();
241 return (FeatureIndex)( (nf + _index) % nf );
245 explicit NumericFeatureId(InternalIndex index) :
248 InternalIndex _index;
250 typedef services::SharedPtr<NumericFeatureId> NumericFeatureIdPtr;
256 class StringFeatureId :
public FeatureId
259 static services::SharedPtr<StringFeatureId> create(
const services::String &name,
260 services::Status *status = NULL)
262 return services::internal::wrapSharedAndTryThrow<StringFeatureId>(
new StringFeatureId(name), status);
265 virtual FeatureIndex mapToIndex(
const FeatureIdMappingIface &mapping,
266 services::Status *status = NULL) DAAL_C11_OVERRIDE
268 if (!mapping.areKeysAvailable())
270 services::internal::tryAssignStatusAndThrow(status, services::ErrorFeatureNamesNotAvailable);
271 return FeatureIndexTraits::invalid();
274 const FeatureIndex index = mapping.getIndexByKey(_name);
275 if (index == FeatureIndexTraits::invalid())
277 services::internal::tryAssignStatusAndThrow(status, services::ErrorFeatureNamesNotAvailable);
278 return FeatureIndexTraits::invalid();
285 explicit StringFeatureId(
const services::String &name) :
288 services::String _name;
290 typedef services::SharedPtr<StringFeatureId> StringFeatureIdPtr;
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