C++ API Reference for Intel® Data Analytics Acceleration Library 2019

string_data_source.h
1 /* file: string_data_source.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 the string data source class.
19 //--
20 */
21 
22 #ifndef __STRING_DATA_SOURCE_H__
23 #define __STRING_DATA_SOURCE_H__
24 
25 #include "services/daal_memory.h"
26 #include "data_management/data_source/data_source.h"
27 #include "data_management/data_source/csv_data_source.h"
28 #include "data_management/data/data_dictionary.h"
29 #include "data_management/data/numeric_table.h"
30 #include "data_management/data/homogen_numeric_table.h"
31 
32 namespace daal
33 {
34 namespace data_management
35 {
36 
37 namespace interface1
38 {
48 template<typename FeatureManager, typename SummaryStatisticsType = DAAL_SUMMARY_STATISTICS_TYPE>
49 class StringDataSource : public CsvDataSource<FeatureManager, SummaryStatisticsType>
50 {
51 private:
52  typedef CsvDataSource<FeatureManager, SummaryStatisticsType> super;
53  typedef data_management::HomogenNumericTable<DAAL_DATA_TYPE> DefaultNumericTableType;
54 
55 protected:
56  using super::_rawLineBuffer;
57  using super::_rawLineBufferLen;
58  using super::_rawLineLength;
59  using super::_status;
60 
61 public:
71  StringDataSource(const byte *data,
72  DataSourceIface::NumericTableAllocationFlag doAllocateNumericTable = DataSource::notAllocateNumericTable,
73  DataSourceIface::DictionaryCreationFlag doCreateDictionaryFromContext = DataSource::notDictionaryFromContext,
74  size_t initialMaxRows = 10):
75  super(doAllocateNumericTable, doCreateDictionaryFromContext, initialMaxRows),
76  _contextDictFlag(false)
77  {
78  setData(data);
79  }
80 
85  void setData( const byte *data )
86  {
87  if( !data )
88  {
89  _status.add(services::throwIfPossible(services::Status(services::ErrorNullPtr)));
90  return;
91  }
92  _stringBufferPos = 0;
93  _stringBuffer = (char *)data;
94  }
95 
100  const byte *getData()
101  {
102  return (const byte *)(_stringBuffer);
103  }
104 
108  void resetData()
109  {
110  _stringBufferPos = 0;
111  }
112 
113 public:
114  services::Status createDictionaryFromContext() DAAL_C11_OVERRIDE
115  {
116  services::Status s = super::createDictionaryFromContext();
117  _stringBufferPos = 0;
118  return s;
119  }
120 
121  DataSourceIface::DataSourceStatus getStatus() DAAL_C11_OVERRIDE
122  {
123  return (iseof() ? DataSourceIface::endOfData : DataSourceIface::readyForLoad);
124  }
125 
126 protected:
127  bool iseof() const DAAL_C11_OVERRIDE
128  {
129  return (_stringBuffer[_stringBufferPos] == '\0');
130  }
131 
132  int readLine(char *buffer, int count)
133  {
134  int pos = 0;
135  for(;pos<count-1;pos++)
136  {
137  buffer[pos] = _stringBuffer[_stringBufferPos+pos];
138 
139  if( buffer[pos]=='\0' || buffer[pos]=='\n' )
140  {
141  break;
142  }
143  }
144  if(buffer[pos]=='\n')
145  {
146  pos++;
147  }
148  _stringBufferPos += pos;
149  buffer[pos] = '\0';
150  return pos;
151  }
152 
153  services::Status readLine() DAAL_C11_OVERRIDE
154  {
155  _rawLineLength = 0;
156  while(!iseof())
157  {
158  const int readLen = readLine (_rawLineBuffer + _rawLineLength, (int)(_rawLineBufferLen - _rawLineLength));
159  if (readLen <= 0)
160  {
161  _rawLineLength = 0;
162  return services::Status();
163  }
164  _rawLineLength += readLen;
165  if (_rawLineBuffer[_rawLineLength - 1] == '\n' || _rawLineBuffer[_rawLineLength - 1] == '\r')
166  {
167  while (_rawLineLength > 0 && (_rawLineBuffer[_rawLineLength - 1] == '\n' || _rawLineBuffer[_rawLineLength - 1] == '\r'))
168  {
169  _rawLineLength--;
170  }
171  _rawLineBuffer[_rawLineLength] = '\0';
172  return services::Status();
173  }
174  if(!super::enlargeBuffer())
175  return services::Status(services::ErrorMemoryAllocationFailed);
176  }
177  return services::Status();
178  }
179 
180 private:
181  char *_stringBuffer;
182  size_t _stringBufferPos;
183 
184  bool _contextDictFlag;
185 };
187 } // namespace interface1
188 using interface1::StringDataSource;
189 
190 }
191 }
192 #endif
daal::data_management::interface1::StringDataSource::resetData
void resetData()
Definition: string_data_source.h:108
daal
Definition: algorithm_base_common.h:31
daal::data_management::interface1::StringDataSource::StringDataSource
StringDataSource(const byte *data, DataSourceIface::NumericTableAllocationFlag doAllocateNumericTable=DataSource::notAllocateNumericTable, DataSourceIface::DictionaryCreationFlag doCreateDictionaryFromContext=DataSource::notDictionaryFromContext, size_t initialMaxRows=10)
Definition: string_data_source.h:71
daal::data_management::interface1::StringDataSource
Specifies methods to access data stored in byte arrays in the C-string format.
Definition: string_data_source.h:49
daal::data_management::interface1::CsvDataSource::createDictionaryFromContext
services::Status createDictionaryFromContext() DAAL_C11_OVERRIDE
Definition: csv_data_source.h:305
daal::data_management::interface1::DataSourceIface::DictionaryCreationFlag
DictionaryCreationFlag
Specifies whether a Data Dictionary is created from the context of a Data Source. ...
Definition: data_source.h:69
daal::data_management::interface1::StringDataSource::setData
void setData(const byte *data)
Definition: string_data_source.h:85
daal::data_management::interface1::StringDataSource::getStatus
DataSourceIface::DataSourceStatus getStatus() DAAL_C11_OVERRIDE
Definition: string_data_source.h:121
daal::services::ErrorMemoryAllocationFailed
Definition: error_indexes.h:146
daal::data_management::interface1::DataSourceIface::notAllocateNumericTable
Definition: data_source.h:81
daal::services::ErrorNullPtr
Definition: error_indexes.h:139
daal::data_management::interface1::DataSourceIface::doAllocateNumericTable
Definition: data_source.h:82
daal::data_management::interface1::DataSourceTemplate
Implements the abstract DataSourceIface interface.
Definition: data_source.h:463
daal::data_management::interface1::StringDataSource::createDictionaryFromContext
services::Status createDictionaryFromContext() DAAL_C11_OVERRIDE
Definition: string_data_source.h:114
daal::data_management::interface1::CsvDataSource
Specifies methods to access data stored in files.
Definition: csv_data_source.h:97
daal::algorithms::association_rules::data
Definition: apriori_types.h:81
daal::data_management::interface1::DataSourceIface::DataSourceStatus
DataSourceStatus
Specifies the status of the Data Source.
Definition: data_source.h:57
daal::data_management::interface1::DataSourceIface::readyForLoad
Definition: data_source.h:59
daal::data_management::interface1::StringDataSource::getData
const byte * getData()
Definition: string_data_source.h:100
daal::data_management::interface1::DataSourceIface::notDictionaryFromContext
Definition: data_source.h:71
daal::data_management::interface1::DataSourceIface::endOfData
Definition: data_source.h:61
daal::data_management::interface1::DataSourceIface::NumericTableAllocationFlag
NumericTableAllocationFlag
Specifies whether a Numeric Table is allocated inside of the Data Source object.
Definition: data_source.h:79

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