SHOGUN  v3.2.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
DataType.cpp
浏览该文件的文档.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2010 Soeren Sonnenburg
8  * Written (W) 2011-2013 Heiko Strathmann
9  * Copyright (C) 2010 Berlin Institute of Technology
10  */
11 
12 #include <string.h>
13 
14 #include <shogun/base/SGObject.h>
15 #include <shogun/lib/DataType.h>
16 #include <shogun/lib/SGString.h>
18 
19 using namespace shogun;
20 
21 TSGDataType::TSGDataType(EContainerType ctype, EStructType stype,
22  EPrimitiveType ptype)
23 {
24  m_ctype = ctype, m_stype = stype, m_ptype = ptype;
25  m_length_y = m_length_x = NULL;
26 }
27 
28 TSGDataType::TSGDataType(EContainerType ctype, EStructType stype,
29  EPrimitiveType ptype, index_t* length)
30 {
31  m_ctype = ctype, m_stype = stype, m_ptype = ptype;
32  m_length_y = length, m_length_x = NULL;
33 }
34 
35 TSGDataType::TSGDataType(EContainerType ctype, EStructType stype,
36  EPrimitiveType ptype, index_t* length_y,
37  index_t* length_x)
38 {
39  m_ctype = ctype, m_stype = stype, m_ptype = ptype;
40  m_length_y = length_y, m_length_x = length_x;
41 }
42 
43 bool
45 {
46  /* handle CT_SG* and SG_* ambiguity */
47  bool ctype_equal=false;
48  if ((m_ctype==CT_VECTOR && a.m_ctype==CT_SGVECTOR) ||
49  (m_ctype==CT_SGVECTOR && a.m_ctype==CT_VECTOR) ||
50  (m_ctype==CT_MATRIX && a.m_ctype==CT_SGMATRIX) ||
51  (m_ctype==CT_SGMATRIX && a.m_ctype==CT_MATRIX) ||
52  (m_ctype==a.m_ctype))
53  ctype_equal=true;
54 
55  bool result = ctype_equal && m_stype == a.m_stype
56  && m_ptype == a.m_ptype;
57 
58  result &= m_length_y != NULL && a.m_length_y != NULL
59  ? *m_length_y == *a.m_length_y: m_length_y == a.m_length_y;
60  result &= m_length_x != NULL && a.m_length_x != NULL
61  ? *m_length_x == *a.m_length_x: m_length_x == a.m_length_x;
62 
63  return result;
64 }
65 
67 {
68  if (m_ctype!=other.m_ctype)
69  {
70  SG_SDEBUG("leaving TSGDataType::equals_without_length(): container types are "
71  "different\n");
72  return false;
73  }
74 
75  if (m_stype!=other.m_stype)
76  {
77  SG_SDEBUG("leaving TSGDataType::equals_without_length(): struct types are "
78  "different\n");
79  return false;
80  }
81 
82  if (m_ptype!=other.m_ptype)
83  {
84  SG_SDEBUG("leaving TSGDataType::equals_without_length(): primitive types are "
85  "different\n");
86  return false;
87  }
88 
89  SG_SDEBUG("leaving TSGDataType::equals_without_length(): data types "
90  "without lengths are equal\n");
91  return true;
92 }
93 
95 {
96  SG_SDEBUG("entering TSGDataType::equals()\n");
97 
98  if (!equals_without_length(other))
99  {
100  SG_SDEBUG("leaving TSGDataType::equals(): Data types without lengths "
101  "are not equal\n");
102  return false;
103  }
104 
105  if ((!m_length_y && other.m_length_y) || (m_length_y && !other.m_length_y))
106  {
107  SG_SDEBUG("leaving TSGDataType::equals(): length_y is at %p while "
108  "other's length_y is at %p\n", m_length_y, other.m_length_y);
109  return false;
110  }
111 
112  if (m_length_y && other.m_length_y)
113  {
114  if (*m_length_y!=*other.m_length_y)
115  {
116  SG_SDEBUG("leaving TSGDataType::equals(): length_y=%d while "
117  "other's length_y=%d\n", *m_length_y, *other.m_length_y);
118  return false;
119  }
120  }
121 
122  if ((!m_length_x && other.m_length_x) || (m_length_x && !other.m_length_x))
123  {
124  SG_SDEBUG("leaving TSGDataType::equals(): m_length_x is at %p while "
125  "other's m_length_x is at %p\n", m_length_x, other.m_length_x);
126  return false;
127  }
128 
129  if (m_length_x && other.m_length_x)
130  {
131  if (*m_length_x!=*other.m_length_x)
132  {
133  SG_SDEBUG("leaving TSGDataType::equals(): m_length_x=%d while "
134  "other's m_length_x=%d\n", *m_length_x, *other.m_length_x);
135  return false;
136  }
137  }
138 
139  SG_SDEBUG("leaving TSGDataType::equals(): datatypes are equal\n");
140  return true;
141 }
142 
143 void
144 TSGDataType::to_string(char* dest, size_t n) const
145 {
146  char* p = dest;
147 
148  switch (m_ctype) {
149  case CT_SCALAR: strncpy(p, "", n); break;
150  case CT_VECTOR: strncpy(p, "Vector<", n); break;
151  case CT_SGVECTOR: strncpy(p, "SGVector<", n); break;
152  case CT_MATRIX: strncpy(p, "Matrix<", n); break;
153  case CT_SGMATRIX: strncpy(p, "SGMatrix<", n); break;
154  case CT_NDARRAY: strncpy(p, "N-Dimensional Array<", n); break;
155  case CT_UNDEFINED: default: strncpy(p, "Undefined", n); break;
156  }
157 
158  if (m_ctype != CT_UNDEFINED)
159  {
160  size_t np = strlen(p);
161  stype_to_string(p + np, m_stype, m_ptype, n - np - 2);
162  }
163 
164  switch (m_ctype) {
165  case CT_SCALAR: break;
166  case CT_VECTOR:
167  case CT_SGVECTOR:
168  case CT_MATRIX:
169  case CT_SGMATRIX:
170  case CT_NDARRAY: strcat(p, ">"); break;
171  case CT_UNDEFINED: default: break;
172  }
173 }
174 
175 size_t
177 {
178  return sizeof_stype(m_stype, m_ptype);
179 }
180 
181 size_t
183 {
184  return sizeof_ptype(m_ptype);
185 }
186 
187 size_t
188 TSGDataType::sizeof_stype(EStructType stype, EPrimitiveType ptype)
189 {
190  switch (stype) {
191  case ST_NONE: return sizeof_ptype(ptype);
192  case ST_STRING:
193  switch (ptype) {
194  case PT_BOOL: return sizeof (SGString<bool>);
195  case PT_CHAR: return sizeof (SGString<char>);
196  case PT_INT8: return sizeof (SGString<int8_t>);
197  case PT_UINT8: return sizeof (SGString<uint8_t>);
198  case PT_INT16: return sizeof (SGString<int16_t>);
199  case PT_UINT16: return sizeof (SGString<uint16_t>);
200  case PT_INT32: return sizeof (SGString<int32_t>);
201  case PT_UINT32: return sizeof (SGString<uint32_t>);
202  case PT_INT64: return sizeof (SGString<int64_t>);
203  case PT_UINT64: return sizeof (SGString<uint64_t>);
204  case PT_FLOAT32: return sizeof (SGString<float32_t>);
205  case PT_FLOAT64: return sizeof (SGString<float64_t>);
206  case PT_FLOATMAX: return sizeof (SGString<floatmax_t>);
207  case PT_COMPLEX128:
208  SG_SWARNING("TGSDataType::sizeof_stype(): Strings are"
209  " not supported for complex128_t\n");
210  return -1;
211  case PT_SGOBJECT:
212  SG_SWARNING("TGSDataType::sizeof_stype(): Strings are"
213  " not supported for SGObject\n");
214  return -1;
215  case PT_UNDEFINED: default:
216  SG_SERROR("Implementation error: undefined primitive type\n");
217  break;
218  }
219  break;
220  case ST_SPARSE:
221  switch (ptype) {
222  case PT_BOOL: return sizeof (SGSparseVector<bool>);
223  case PT_CHAR: return sizeof (SGSparseVector<char>);
224  case PT_INT8: return sizeof (SGSparseVector<int8_t>);
225  case PT_UINT8: return sizeof (SGSparseVector<uint8_t>);
226  case PT_INT16: return sizeof (SGSparseVector<int16_t>);
227  case PT_UINT16: return sizeof (SGSparseVector<uint16_t>);
228  case PT_INT32: return sizeof (SGSparseVector<int32_t>);
229  case PT_UINT32: return sizeof (SGSparseVector<uint32_t>);
230  case PT_INT64: return sizeof (SGSparseVector<int64_t>);
231  case PT_UINT64: return sizeof (SGSparseVector<uint64_t>);
232  case PT_FLOAT32: return sizeof (SGSparseVector<float32_t>);
233  case PT_FLOAT64: return sizeof (SGSparseVector<float64_t>);
234  case PT_FLOATMAX: return sizeof (SGSparseVector<floatmax_t>);
235  case PT_COMPLEX128: return sizeof (SGSparseVector<complex128_t>);
236  case PT_SGOBJECT: return -1;
237  case PT_UNDEFINED: default:
238  SG_SERROR("Implementation error: undefined primitive type\n");
239  break;
240  }
241  break;
242  case ST_UNDEFINED: default:
243  SG_SERROR("Implementation error: undefined structure type\n");
244  break;
245  }
246 
247  return -1;
248 }
249 
250 size_t
251 TSGDataType::sizeof_ptype(EPrimitiveType ptype)
252 {
253  switch (ptype) {
254  case PT_BOOL: return sizeof (bool);
255  case PT_CHAR: return sizeof (char);
256  case PT_INT8: return sizeof (int8_t);
257  case PT_UINT8: return sizeof (uint8_t);
258  case PT_INT16: return sizeof (int16_t);
259  case PT_UINT16: return sizeof (uint16_t);
260  case PT_INT32: return sizeof (int32_t);
261  case PT_UINT32: return sizeof (uint32_t);
262  case PT_INT64: return sizeof (int64_t);
263  case PT_UINT64: return sizeof (uint64_t);
264  case PT_FLOAT32: return sizeof (float32_t);
265  case PT_FLOAT64: return sizeof (float64_t);
266  case PT_FLOATMAX: return sizeof (floatmax_t);
267  case PT_COMPLEX128: return sizeof (complex128_t);
268  case PT_SGOBJECT: return sizeof (CSGObject*);
269  case PT_UNDEFINED: default:
270  SG_SERROR("Implementation error: undefined primitive type\n");
271  break;
272  }
273 
274  return -1;
275 }
276 
277 size_t
278 TSGDataType::sizeof_sparseentry(EPrimitiveType ptype)
279 {
280  switch (ptype) {
281  case PT_BOOL: return sizeof (SGSparseVectorEntry<bool>);
282  case PT_CHAR: return sizeof (SGSparseVectorEntry<char>);
283  case PT_INT8: return sizeof (SGSparseVectorEntry<int8_t>);
284  case PT_UINT8: return sizeof (SGSparseVectorEntry<uint8_t>);
285  case PT_INT16: return sizeof (SGSparseVectorEntry<int16_t>);
286  case PT_UINT16: return sizeof (SGSparseVectorEntry<uint16_t>);
287  case PT_INT32: return sizeof (SGSparseVectorEntry<int32_t>);
288  case PT_UINT32: return sizeof (SGSparseVectorEntry<uint32_t>);
289  case PT_INT64: return sizeof (SGSparseVectorEntry<int64_t>);
290  case PT_UINT64: return sizeof (SGSparseVectorEntry<uint64_t>);
291  case PT_FLOAT32: return sizeof (SGSparseVectorEntry<float32_t>);
292  case PT_FLOAT64: return sizeof (SGSparseVectorEntry<float64_t>);
293  case PT_FLOATMAX: return sizeof (SGSparseVectorEntry<floatmax_t>);
294  case PT_COMPLEX128: return sizeof (SGSparseVectorEntry<complex128_t>);
295  case PT_SGOBJECT: return -1;
296  case PT_UNDEFINED: default:
297  SG_SERROR("Implementation error: undefined primitive type\n");
298  break;
299  }
300 
301  return -1;
302 }
303 
304 #define ENTRY_OFFSET(k, type) \
305  ((char*) &((SGSparseVectorEntry<type>*) (k))->entry - (char*) (k))
306 size_t
307 TSGDataType::offset_sparseentry(EPrimitiveType ptype)
308 {
309  size_t result = -1; void* x = &result;
310 
311  switch (ptype) {
312  case PT_BOOL: result = ENTRY_OFFSET(x, bool); break;
313  case PT_CHAR: result = ENTRY_OFFSET(x, char); break;
314  case PT_INT8: result = ENTRY_OFFSET(x, int8_t); break;
315  case PT_UINT8: result = ENTRY_OFFSET(x, uint8_t); break;
316  case PT_INT16: result = ENTRY_OFFSET(x, int16_t); break;
317  case PT_UINT16: result = ENTRY_OFFSET(x, uint16_t); break;
318  case PT_INT32: result = ENTRY_OFFSET(x, int32_t); break;
319  case PT_UINT32: result = ENTRY_OFFSET(x, uint32_t); break;
320  case PT_INT64: result = ENTRY_OFFSET(x, int64_t); break;
321  case PT_UINT64: result = ENTRY_OFFSET(x, uint64_t); break;
322  case PT_FLOAT32: result = ENTRY_OFFSET(x, float32_t); break;
323  case PT_FLOAT64: result = ENTRY_OFFSET(x, float64_t); break;
324  case PT_FLOATMAX: result = ENTRY_OFFSET(x, floatmax_t); break;
325  case PT_COMPLEX128: result = ENTRY_OFFSET(x, complex128_t); break;
326  case PT_SGOBJECT: return -1;
327  case PT_UNDEFINED: default:
328  SG_SERROR("Implementation error: undefined primitive type\n");
329  break;
330  }
331 
332  return result;
333 }
334 
335 void
336 TSGDataType::stype_to_string(char* dest, EStructType stype,
337  EPrimitiveType ptype, size_t n)
338 {
339  char* p = dest;
340 
341  switch (stype) {
342  case ST_NONE: strncpy(p, "", n); break;
343  case ST_STRING: strncpy(p, "String<", n); break;
344  case ST_SPARSE: strncpy(p, "Sparse<", n); break;
345  case ST_UNDEFINED: default:
346  SG_SERROR("Implementation error: undefined structure type\n");
347  break;
348  }
349 
350  size_t np = strlen(p);
351  ptype_to_string(p + np, ptype, n - np - 2);
352 
353  switch (stype) {
354  case ST_NONE: break;
355  case ST_STRING: case ST_SPARSE:
356  strcat(p, ">"); break;
357  case ST_UNDEFINED: default:
358  SG_SERROR("Implementation error: undefined structure type\n");
359  break;
360  }
361 }
362 
363 void
364 TSGDataType::ptype_to_string(char* dest, EPrimitiveType ptype,
365  size_t n)
366 {
367  char* p = dest;
368 
369  switch (ptype) {
370  case PT_BOOL: strncpy(p, "bool", n); break;
371  case PT_CHAR: strncpy(p, "char", n); break;
372  case PT_INT8: strncpy(p, "int8", n); break;
373  case PT_UINT8: strncpy(p, "uint8", n); break;
374  case PT_INT16: strncpy(p, "int16", n); break;
375  case PT_UINT16: strncpy(p, "uint16", n); break;
376  case PT_INT32: strncpy(p, "int32", n); break;
377  case PT_UINT32: strncpy(p, "uint32", n); break;
378  case PT_INT64: strncpy(p, "int64", n); break;
379  case PT_UINT64: strncpy(p, "uint64", n); break;
380  case PT_FLOAT32: strncpy(p, "float32", n); break;
381  case PT_FLOAT64: strncpy(p, "float64", n); break;
382  case PT_FLOATMAX: strncpy(p, "floatmax", n); break;
383  case PT_COMPLEX128: strncpy(p, "complex128", n); break;
384  case PT_SGOBJECT: strncpy(p, "SGSerializable*", n); break;
385  case PT_UNDEFINED: default:
386  SG_SERROR("Implementation error: undefined primitive type\n");
387  break;
388  }
389 }
390 
391 bool
392 TSGDataType::string_to_ptype(EPrimitiveType* ptype, const char* str)
393 {
394  if (strcmp(str, "bool") == 0) {
395  *ptype = PT_BOOL; return true; }
396  if (strcmp(str, "char") == 0) {
397  *ptype = PT_CHAR; return true; }
398  if (strcmp(str, "int8") == 0) {
399  *ptype = PT_INT8; return true; }
400  if (strcmp(str, "uint8") == 0) {
401  *ptype = PT_UINT8; return true; }
402  if (strcmp(str, "int16") == 0) {
403  *ptype = PT_INT16; return true; }
404  if (strcmp(str, "uint16") == 0) {
405  *ptype = PT_UINT16; return true; }
406  if (strcmp(str, "int32") == 0) {
407  *ptype = PT_INT32; return true; }
408  if (strcmp(str, "uint32") == 0) {
409  *ptype = PT_UINT32; return true; }
410  if (strcmp(str, "int64") == 0) {
411  *ptype = PT_INT64; return true; }
412  if (strcmp(str, "uint64") == 0) {
413  *ptype = PT_UINT64; return true; }
414  if (strcmp(str, "float32") == 0) {
415  *ptype = PT_FLOAT32; return true; }
416  if (strcmp(str, "float64") == 0) {
417  *ptype = PT_FLOAT64; return true; }
418  if (strcmp(str, "floatmax") == 0) {
419  *ptype = PT_FLOATMAX; return true; }
420  if (strcmp(str, "complex128") == 0) {
421  *ptype = PT_COMPLEX128; return true; }
422  if (strcmp(str, "SGSerializable*") == 0) {
423  *ptype = PT_SGOBJECT; return true; }
424 
425  /* Make sure that the compiler will warn at this position. */
426  switch (*ptype) {
427  case PT_BOOL: case PT_CHAR: case PT_INT8: case PT_UINT8:
428  case PT_INT16: case PT_UINT16: case PT_INT32: case PT_UINT32:
429  case PT_INT64: case PT_UINT64: case PT_FLOAT32: case PT_FLOAT64:
430  case PT_FLOATMAX: case PT_COMPLEX128: case PT_SGOBJECT: break;
431  case PT_UNDEFINED: default:
432  SG_SERROR("Implementation error: undefined primitive type\n");
433  break;
434  }
435 
436  return false;
437 }
438 
440 {
441  switch (m_stype)
442  {
443  case ST_NONE:
444  return get_num_elements()*sizeof_ptype();
445  case ST_STRING:
446  if (m_ptype==PT_SGOBJECT)
447  return 0;
448 
449  return get_num_elements()*sizeof_stype();
450  case ST_SPARSE:
451  if (m_ptype==PT_SGOBJECT)
452  return 0;
453 
455  case ST_UNDEFINED: default:
456  SG_SERROR("Implementation error: undefined structure type\n");
457  break;
458  }
459 
460  return 0;
461 }
462 
464 {
465  switch (m_ctype)
466  {
467  case CT_SCALAR:
468  return 1;
469  case CT_VECTOR: case CT_SGVECTOR:
470  /* length_y contains the length for vectors */
471  return *m_length_y;
472  case CT_MATRIX: case CT_SGMATRIX:
473  return (*m_length_y)*(*m_length_x);
474  case CT_NDARRAY:
476  case CT_UNDEFINED: default:
477  SG_SERROR("Implementation error: undefined container type\n");
478  break;
479  }
480  return 0;
481 }
std::complex< float64_t > complex128_t
Definition: common.h:65
EStructType m_stype
Definition: DataType.h:72
int32_t index_t
Definition: common.h:60
static bool string_to_ptype(EPrimitiveType *ptype, const char *str)
Definition: DataType.cpp:392
static size_t offset_sparseentry(EPrimitiveType ptype)
Definition: DataType.cpp:307
#define SG_SWARNING(...)
Definition: SGIO.h:180
index_t * m_length_x
Definition: DataType.h:79
size_t sizeof_ptype() const
Definition: DataType.cpp:182
int64_t get_num_elements()
Definition: DataType.cpp:463
#define SG_SNOTIMPLEMENTED
Definition: SGIO.h:200
static void ptype_to_string(char *dest, EPrimitiveType ptype, size_t n)
Definition: DataType.cpp:364
TSGDataType(EContainerType ctype, EStructType stype, EPrimitiveType ptype)
Definition: DataType.cpp:21
Datatypes that shogun supports.
Definition: DataType.h:67
template class SGSparseVector The assumtion is that the stored SGSparseVectorEntry* vector is orde...
shogun string
bool equals_without_length(TSGDataType other)
Definition: DataType.cpp:66
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:102
double float64_t
Definition: common.h:48
long double floatmax_t
Definition: common.h:49
static void stype_to_string(char *dest, EStructType stype, EPrimitiveType ptype, size_t n)
Definition: DataType.cpp:336
size_t sizeof_stype() const
Definition: DataType.cpp:176
bool operator==(const TSGDataType &a)
Definition: DataType.cpp:44
index_t * m_length_y
Definition: DataType.h:77
float float32_t
Definition: common.h:47
void to_string(char *dest, size_t n) const
Definition: DataType.cpp:144
EContainerType m_ctype
Definition: DataType.h:70
#define ENTRY_OFFSET(k, type)
Definition: DataType.cpp:304
#define SG_SDEBUG(...)
Definition: SGIO.h:170
template class SGSparseVectorEntry
Definition: File.h:23
#define SG_SERROR(...)
Definition: SGIO.h:181
bool equals(TSGDataType other)
Definition: DataType.cpp:94
EPrimitiveType m_ptype
Definition: DataType.h:74
static size_t sizeof_sparseentry(EPrimitiveType ptype)
Definition: DataType.cpp:278

SHOGUN Machine Learning Toolbox - Documentation