C++ API Reference for Intel® Data Analytics Acceleration Library 2018 Update 3

data_source_dictionary.h
1 /* file: data_source_dictionary.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 /*
17 //++
18 // Implementation of a data source dictionary.
19 //--
20 */
21 
22 #ifndef __DATA_SOURCE_DICTIONARY_H__
23 #define __DATA_SOURCE_DICTIONARY_H__
24 
25 #include <string>
26 #include <map>
27 #include "data_management/data/data_dictionary.h"
28 
29 namespace daal
30 {
31 namespace data_management
32 {
33 
34 namespace interface1
35 {
40 class CategoricalFeatureDictionary : public std::map<std::string, std::pair<int, int> >
41 {
42 public:
43 };
44 
49 class DataSourceFeature : public SerializationIface
50 {
51 public:
52  NumericTableFeature ntFeature;
53  size_t name_length;
54  char *name;
55 
56  CategoricalFeatureDictionary *cat_dict;
57 
58 public:
62  DataSourceFeature() : name(NULL), name_length(0), cat_dict(NULL) {}
63 
67  DataSourceFeature &operator= (const DataSourceFeature &f)
68  {
69  ntFeature = f.ntFeature;
70 
71  name = new char[f.name_length];
72  daal::services::daal_memcpy_s(name, name_length, f.name, f.name_length);
73 
74  name_length = f.name_length;
75  cat_dict = 0;
76 
77  return *this;
78  }
79 
81  virtual ~DataSourceFeature()
82  {
83  if(name)
84  {
85  delete[] name;
86  }
87 
88  if(cat_dict)
89  {
90  delete cat_dict;
91  }
92  }
93 
98  CategoricalFeatureDictionary *getCategoricalDictionary()
99  {
100  if( !cat_dict )
101  {
102  cat_dict = new CategoricalFeatureDictionary;
103  }
104 
105  return cat_dict;
106  }
107 
112  void setFeatureName(const std::string &featureName)
113  {
114  name_length = featureName.length() + 1;
115  if (name)
116  {
117  delete[] name;
118  }
119  name = new char[name_length];
120  daal::services::daal_memcpy_s(name, name_length, featureName.c_str(), name_length);
121  }
122 
127  template<typename T>
128  void setType()
129  {
130  ntFeature.setType<T>();
131  }
132 
134  services::Status serializeImpl(InputDataArchive *arch) DAAL_C11_OVERRIDE
135  {
136  serialImpl<InputDataArchive, false>( arch );
137 
138  return services::Status();
139  }
140 
142  services::Status deserializeImpl(const OutputDataArchive *arch) DAAL_C11_OVERRIDE
143  {
144  serialImpl<const OutputDataArchive, true>( arch );
145 
146  return services::Status();
147  }
148 
150  template<typename Archive, bool onDeserialize>
151  services::Status serialImpl( Archive *arch )
152  {
153  arch->setObj( &ntFeature );
154 
155  arch->set( name_length );
156 
157  if( onDeserialize )
158  {
159  if( name_length > 0 )
160  {
161  if( name ) { delete[] name; }
162  name = NULL;
163  name = new char[name_length];
164  }
165  }
166 
167  arch->set( name, name_length );
168 
169  int categoricalFeatureDictionaryFlag = (cat_dict != 0);
170 
171  arch->set( categoricalFeatureDictionaryFlag );
172 
173  if( categoricalFeatureDictionaryFlag )
174  {
175  if( onDeserialize )
176  {
177  /* Make sure that dictionary is allocated */
178  getCategoricalDictionary();
179  /* Make sure that dictionary is empty */
180  cat_dict->empty();
181  }
182 
183  size_t size = cat_dict->size();
184 
185  arch->set( size );
186 
187  if( onDeserialize )
188  {
189  size_t buffLen = 10;
190  char* buff = new char[buffLen];
191  for(size_t i=0; i<size; i++)
192  {
193  size_t catNameLen = 0;
194  int catV1 = 0;
195  int catV2 = 0;
196 
197  arch->set( catNameLen );
198  if( catNameLen>buffLen )
199  {
200  delete[] buff;
201  buff = new char[catNameLen];
202  buffLen = catNameLen;
203  }
204  arch->set( buff, catNameLen );
205  arch->set( catV1 );
206  arch->set( catV2 );
207 
208  (*cat_dict)[ std::string(buff, catNameLen) ] = std::pair<int,int>(catV1, catV2);
209  }
210  delete[] buff;
211  }
212  else
213  {
214  typedef CategoricalFeatureDictionary::iterator it_type;
215 
216  for(it_type it=cat_dict->begin(); it != cat_dict->end(); it++)
217  {
218  const std::string & catName = it->first;
219  size_t catNameLen = catName.size();
220  int catV1 = it->second.first;
221  int catV2 = it->second.second;
222 
223  arch->set( catNameLen );
224  arch->set( catName.c_str(), catNameLen );
225  arch->set( catV1 );
226  arch->set( catV2 );
227  }
228  }
229  }
230  else
231  {
232  cat_dict = 0;
233  }
234 
235  return services::Status();
236  }
237 
238  virtual int getSerializationTag() const DAAL_C11_OVERRIDE
239  {
240  return SERIALIZATION_DATAFEATURE_NT_ID;
241  }
242 
243  data_feature_utils::IndexNumType getIndexType() const
244  {
245  return ntFeature.indexType;
246  }
247 };
248 
249 typedef Dictionary<DataSourceFeature, SERIALIZATION_DATADICTIONARY_DS_ID> DataSourceDictionary;
250 typedef services::SharedPtr<DataSourceDictionary> DataSourceDictionaryPtr;
253 } // namespace interface1
254 
255 using interface1::CategoricalFeatureDictionary;
256 using interface1::DataSourceFeature;
257 using interface1::DataSourceDictionary;
258 using interface1::DataSourceDictionaryPtr;
259 
260 }
261 } // namespace daal
262 #endif
daal
Definition: algorithm_base_common.h:31
daal::data_management::interface1::InputDataArchive
Provides methods to create an archive data object (serialized) and access this object.
Definition: data_archive.h:689
daal::data_management::interface1::DataSourceFeature::setType
void setType()
Definition: data_source_dictionary.h:128
daal::data_management::interface1::DataSourceFeature
Data structure that describes the Data Source feature.
Definition: data_source_dictionary.h:49
daal::data_management::interface1::CategoricalFeatureDictionary
Definition: data_source_dictionary.h:40
daal::data_management::interface1::DataSourceFeature::getSerializationTag
virtual int getSerializationTag() const DAAL_C11_OVERRIDE
Definition: data_source_dictionary.h:238
daal::data_management::interface1::DataSourceFeature::operator=
DataSourceFeature & operator=(const DataSourceFeature &f)
Definition: data_source_dictionary.h:67
daal::data_management::interface1::DataSourceFeature::setFeatureName
void setFeatureName(const std::string &featureName)
Definition: data_source_dictionary.h:112
daal::services::daal_memcpy_s
DAAL_EXPORT void daal_memcpy_s(void *dest, size_t numberOfElements, const void *src, size_t count)
daal::data_management::interface1::DataSourceFeature::getCategoricalDictionary
CategoricalFeatureDictionary * getCategoricalDictionary()
Definition: data_source_dictionary.h:98
daal::data_management::interface1::NumericTableFeature::setType
void setType()
Definition: data_dictionary.h:91
daal::data_management::interface1::SerializationIface
Abstract interface class that defines the interface for serialization and deserialization.
Definition: data_serialize.h:50
daal::data_management::interface1::NumericTableFeature
Data structure describes the Numeric Table feature.
Definition: data_dictionary.h:48
daal::data_management::interface1::DataSourceFeature::DataSourceFeature
DataSourceFeature()
Definition: data_source_dictionary.h:62

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