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

data_source_dictionary.h
1 /* file: data_source_dictionary.h */
2 /*******************************************************************************
3 * Copyright 2014-2017 Intel Corporation
4 * All Rights Reserved.
5 *
6 * If this software was obtained under the Intel Simplified Software License,
7 * the following terms apply:
8 *
9 * The source code, information and material ("Material") contained herein is
10 * owned by Intel Corporation or its suppliers or licensors, and title to such
11 * Material remains with Intel Corporation or its suppliers or licensors. The
12 * Material contains proprietary information of Intel or its suppliers and
13 * licensors. The Material is protected by worldwide copyright laws and treaty
14 * provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed or disclosed
16 * in any way without Intel's prior express written permission. No license under
17 * any patent, copyright or other intellectual property rights in the Material
18 * is granted to or conferred upon you, either expressly, by implication,
19 * inducement, estoppel or otherwise. Any license under such intellectual
20 * property rights must be express and approved by Intel in writing.
21 *
22 * Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 * notice or any other notice embedded in Materials by Intel or Intel's
24 * suppliers or licensors in any way.
25 *
26 *
27 * If this software was obtained under the Apache License, Version 2.0 (the
28 * "License"), the following terms apply:
29 *
30 * You may not use this file except in compliance with the License. You may
31 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 *
33 *
34 * Unless required by applicable law or agreed to in writing, software
35 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 *
38 * See the License for the specific language governing permissions and
39 * limitations under the License.
40 *******************************************************************************/
41 
42 /*
43 //++
44 // Implementation of a data source dictionary.
45 //--
46 */
47 
48 #ifndef __DATA_SOURCE_DICTIONARY_H__
49 #define __DATA_SOURCE_DICTIONARY_H__
50 
51 #include <string>
52 #include <map>
53 #include "data_management/data/data_dictionary.h"
54 
55 namespace daal
56 {
57 namespace data_management
58 {
59 
60 namespace interface1
61 {
66 class CategoricalFeatureDictionary : public std::map<std::string, std::pair<int, int> >
67 {
68 public:
69 };
70 
75 class DataSourceFeature : public SerializationIface
76 {
77 public:
78  NumericTableFeature ntFeature;
79  size_t name_length;
80  char *name;
81 
82  CategoricalFeatureDictionary *cat_dict;
83 
84 public:
88  DataSourceFeature() : name(NULL), name_length(0), cat_dict(NULL) {}
89 
93  DataSourceFeature &operator= (const DataSourceFeature &f)
94  {
95  ntFeature = f.ntFeature;
96 
97  name = new char[f.name_length];
98  daal::services::daal_memcpy_s(name, name_length, f.name, f.name_length);
99 
100  name_length = f.name_length;
101  cat_dict = 0;
102 
103  return *this;
104  }
105 
107  virtual ~DataSourceFeature()
108  {
109  if(name)
110  {
111  delete[] name;
112  }
113 
114  if(cat_dict)
115  {
116  delete cat_dict;
117  }
118  }
119 
124  CategoricalFeatureDictionary *getCategoricalDictionary()
125  {
126  if( !cat_dict )
127  {
128  cat_dict = new CategoricalFeatureDictionary;
129  }
130 
131  return cat_dict;
132  }
133 
138  void setFeatureName(const std::string &featureName)
139  {
140  name_length = featureName.length() + 1;
141  if (name)
142  {
143  delete[] name;
144  }
145  name = new char[name_length];
146  daal::services::daal_memcpy_s(name, name_length, featureName.c_str(), name_length);
147  }
148 
153  template<typename T>
154  void setType()
155  {
156  ntFeature.setType<T>();
157  }
158 
160  services::Status serializeImpl(InputDataArchive *arch) DAAL_C11_OVERRIDE
161  {
162  serialImpl<InputDataArchive, false>( arch );
163 
164  return services::Status();
165  }
166 
168  services::Status deserializeImpl(const OutputDataArchive *arch) DAAL_C11_OVERRIDE
169  {
170  serialImpl<const OutputDataArchive, true>( arch );
171 
172  return services::Status();
173  }
174 
176  template<typename Archive, bool onDeserialize>
177  services::Status serialImpl( Archive *arch )
178  {
179  arch->setObj( &ntFeature );
180 
181  arch->set( name_length );
182 
183  if( onDeserialize )
184  {
185  if( name_length > 0 )
186  {
187  if( name ) { delete[] name; }
188  name = NULL;
189  name = new char[name_length];
190  }
191  }
192 
193  arch->set( name, name_length );
194 
195  int categoricalFeatureDictionaryFlag = (cat_dict != 0);
196 
197  arch->set( categoricalFeatureDictionaryFlag );
198 
199  if( categoricalFeatureDictionaryFlag )
200  {
201  if( onDeserialize )
202  {
203  /* Make sure that dictionary is allocated */
204  getCategoricalDictionary();
205  /* Make sure that dictionary is empty */
206  cat_dict->empty();
207  }
208 
209  size_t size = cat_dict->size();
210 
211  arch->set( size );
212 
213  if( onDeserialize )
214  {
215  size_t buffLen = 10;
216  char* buff = new char[buffLen];
217  for(size_t i=0; i<size; i++)
218  {
219  size_t catNameLen = 0;
220  int catV1 = 0;
221  int catV2 = 0;
222 
223  arch->set( catNameLen );
224  if( catNameLen>buffLen )
225  {
226  delete[] buff;
227  buff = new char[catNameLen];
228  buffLen = catNameLen;
229  }
230  arch->set( buff, catNameLen );
231  arch->set( catV1 );
232  arch->set( catV2 );
233 
234  (*cat_dict)[ std::string(buff, catNameLen) ] = std::pair<int,int>(catV1, catV2);
235  }
236  delete[] buff;
237  }
238  else
239  {
240  typedef CategoricalFeatureDictionary::iterator it_type;
241 
242  for(it_type it=cat_dict->begin(); it != cat_dict->end(); it++)
243  {
244  const std::string & catName = it->first;
245  size_t catNameLen = catName.size();
246  int catV1 = it->second.first;
247  int catV2 = it->second.second;
248 
249  arch->set( catNameLen );
250  arch->set( catName.c_str(), catNameLen );
251  arch->set( catV1 );
252  arch->set( catV2 );
253  }
254  }
255  }
256  else
257  {
258  cat_dict = 0;
259  }
260 
261  return services::Status();
262  }
263 
264  virtual int getSerializationTag() const DAAL_C11_OVERRIDE
265  {
266  return SERIALIZATION_DATAFEATURE_NT_ID;
267  }
268 
269  data_feature_utils::IndexNumType getIndexType() const
270  {
271  return ntFeature.indexType;
272  }
273 };
274 
275 typedef Dictionary<DataSourceFeature, SERIALIZATION_DATADICTIONARY_DS_ID> DataSourceDictionary;
276 typedef services::SharedPtr<DataSourceDictionary> DataSourceDictionaryPtr;
279 } // namespace interface1
280 
281 using interface1::CategoricalFeatureDictionary;
282 using interface1::DataSourceFeature;
283 using interface1::DataSourceDictionary;
284 using interface1::DataSourceDictionaryPtr;
285 
286 }
287 } // namespace daal
288 #endif
daal
Definition: algorithm_base_common.h:57
daal::data_management::interface1::InputDataArchive
Provides methods to create an archive data object (serialized) and access this object.
Definition: data_archive.h:715
daal::data_management::interface1::DataSourceFeature::setType
void setType()
Definition: data_source_dictionary.h:154
daal::data_management::interface1::DataSourceFeature
Data structure that describes the Data Source feature.
Definition: data_source_dictionary.h:75
daal::data_management::interface1::CategoricalFeatureDictionary
Definition: data_source_dictionary.h:66
daal::data_management::interface1::DataSourceFeature::getSerializationTag
virtual int getSerializationTag() const DAAL_C11_OVERRIDE
Definition: data_source_dictionary.h:264
daal::data_management::interface1::DataSourceFeature::operator=
DataSourceFeature & operator=(const DataSourceFeature &f)
Definition: data_source_dictionary.h:93
daal::data_management::interface1::DataSourceFeature::setFeatureName
void setFeatureName(const std::string &featureName)
Definition: data_source_dictionary.h:138
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:124
daal::data_management::interface1::NumericTableFeature::setType
void setType()
Definition: data_dictionary.h:117
daal::data_management::interface1::SerializationIface
Abstract interface class that defines the interface for serialization and deserialization.
Definition: data_serialize.h:76
daal::data_management::interface1::NumericTableFeature
Data structure describes the Numeric Table feature.
Definition: data_dictionary.h:74
daal::data_management::interface1::DataSourceFeature::DataSourceFeature
DataSourceFeature()
Definition: data_source_dictionary.h:88

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