SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataType.cpp
Go to the documentation of this file.
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  bool result = m_ctype == a.m_ctype && m_stype == a.m_stype
47  && m_ptype == a.m_ptype;
48 
49  result &= m_length_y != NULL && a.m_length_y != NULL
50  ? *m_length_y == *a.m_length_y: m_length_y == a.m_length_y;
51  result &= m_length_x != NULL && a.m_length_x != NULL
52  ? *m_length_x == *a.m_length_x: m_length_x == a.m_length_x;
53 
54  return result;
55 }
56 
58 {
59  if (m_ctype!=other.m_ctype)
60  {
61  SG_SDEBUG("leaving TSGDataType::equals_wihtout_length(): container types are "
62  "different\n");
63  return false;
64  }
65 
66  if (m_stype!=other.m_stype)
67  {
68  SG_SDEBUG("leaving TSGDataType::equals_wihtout_length(): struct types are "
69  "different\n");
70  return false;
71  }
72 
73  if (m_ptype!=other.m_ptype)
74  {
75  SG_SDEBUG("leaving TSGDataType::equals_wihtout_length(): primitive types are "
76  "different\n");
77  return false;
78  }
79 
80  SG_SDEBUG("leaving TSGDataType::equals_wihtout_length(): data types "
81  "without lengths are equal\n");
82  return true;
83 }
84 
86 {
87  SG_SDEBUG("entering TSGDataType::equals()\n");
88 
89  if (!equals_without_length(other))
90  {
91  SG_SDEBUG("leaving TSGDataType::equals(): Data types without lengths "
92  "are not equal\n");
93  return false;
94  }
95 
96  if ((!m_length_y && other.m_length_y) || (m_length_y && !other.m_length_y))
97  {
98  SG_SDEBUG("leaving TSGDataType::equals(): length_y is at %p while "
99  "other's length_y is at %p\n", m_length_y, other.m_length_y);
100  return false;
101  }
102 
103  if (m_length_y && other.m_length_y)
104  {
105  if (*m_length_y!=*other.m_length_y)
106  {
107  SG_SDEBUG("leaving TSGDataType::equals(): length_y=%d while "
108  "other's length_y=%d\n", *m_length_y, *other.m_length_y);
109  return false;
110  }
111  }
112 
113  if ((!m_length_x && other.m_length_x) || (m_length_x && !other.m_length_x))
114  {
115  SG_SDEBUG("leaving TSGDataType::equals(): m_length_x is at %p while "
116  "other's m_length_x is at %p\n", m_length_x, other.m_length_x);
117  return false;
118  }
119 
120  if (m_length_x && other.m_length_x)
121  {
122  if (*m_length_x!=*other.m_length_x)
123  {
124  SG_SDEBUG("leaving TSGDataType::equals(): m_length_x=%d while "
125  "other's m_length_x=%d\n", *m_length_x, *other.m_length_x);
126  return false;
127  }
128  }
129 
130  SG_SDEBUG("leaving TSGDataType::equals(): datatypes are equal\n");
131  return true;
132 }
133 
134 void
135 TSGDataType::to_string(char* dest, size_t n) const
136 {
137  char* p = dest;
138 
139  switch (m_ctype) {
140  case CT_SCALAR: strncpy(p, "", n); break;
141  case CT_VECTOR: strncpy(p, "Vector<", n); break;
142  case CT_SGVECTOR: strncpy(p, "SGVector<", n); break;
143  case CT_MATRIX: strncpy(p, "Matrix<", n); break;
144  case CT_SGMATRIX: strncpy(p, "SGMatrix<", n); break;
145  case CT_NDARRAY: strncpy(p, "N-Dimensional Array<", n); break;
146  case CT_UNDEFINED: default: strncpy(p, "Undefined", n); break;
147  }
148 
149  if (m_ctype != CT_UNDEFINED)
150  {
151  size_t np = strlen(p);
152  stype_to_string(p + np, m_stype, m_ptype, n - np - 2);
153  }
154 
155  switch (m_ctype) {
156  case CT_SCALAR: break;
157  case CT_VECTOR:
158  case CT_SGVECTOR:
159  case CT_MATRIX:
160  case CT_SGMATRIX:
161  case CT_NDARRAY: strcat(p, ">"); break;
162  case CT_UNDEFINED: default: break;
163  }
164 }
165 
166 size_t
168 {
169  return sizeof_stype(m_stype, m_ptype);
170 }
171 
172 size_t
174 {
175  return sizeof_ptype(m_ptype);
176 }
177 
178 size_t
179 TSGDataType::sizeof_stype(EStructType stype, EPrimitiveType ptype)
180 {
181  switch (stype) {
182  case ST_NONE: return sizeof_ptype(ptype);
183  case ST_STRING:
184  switch (ptype) {
185  case PT_BOOL: return sizeof (SGString<bool>);
186  case PT_CHAR: return sizeof (SGString<char>);
187  case PT_INT8: return sizeof (SGString<int8_t>);
188  case PT_UINT8: return sizeof (SGString<uint8_t>);
189  case PT_INT16: return sizeof (SGString<int16_t>);
190  case PT_UINT16: return sizeof (SGString<uint16_t>);
191  case PT_INT32: return sizeof (SGString<int32_t>);
192  case PT_UINT32: return sizeof (SGString<uint32_t>);
193  case PT_INT64: return sizeof (SGString<int64_t>);
194  case PT_UINT64: return sizeof (SGString<uint64_t>);
195  case PT_FLOAT32: return sizeof (SGString<float32_t>);
196  case PT_FLOAT64: return sizeof (SGString<float64_t>);
197  case PT_FLOATMAX: return sizeof (SGString<floatmax_t>);
198  case PT_COMPLEX128:
199  SG_SWARNING("TGSDataType::sizeof_stype(): Strings are"
200  " not supported for complex128_t\n");
201  return -1;
202  case PT_SGOBJECT:
203  SG_SWARNING("TGSDataType::sizeof_stype(): Strings are"
204  " not supported for SGObject\n");
205  return -1;
206  case PT_UNDEFINED: default:
207  SG_SERROR("Implementation error: undefined primitive type\n");
208  break;
209  }
210  break;
211  case ST_SPARSE:
212  switch (ptype) {
213  case PT_BOOL: return sizeof (SGSparseVector<bool>);
214  case PT_CHAR: return sizeof (SGSparseVector<char>);
215  case PT_INT8: return sizeof (SGSparseVector<int8_t>);
216  case PT_UINT8: return sizeof (SGSparseVector<uint8_t>);
217  case PT_INT16: return sizeof (SGSparseVector<int16_t>);
218  case PT_UINT16: return sizeof (SGSparseVector<uint16_t>);
219  case PT_INT32: return sizeof (SGSparseVector<int32_t>);
220  case PT_UINT32: return sizeof (SGSparseVector<uint32_t>);
221  case PT_INT64: return sizeof (SGSparseVector<int64_t>);
222  case PT_UINT64: return sizeof (SGSparseVector<uint64_t>);
223  case PT_FLOAT32: return sizeof (SGSparseVector<float32_t>);
224  case PT_FLOAT64: return sizeof (SGSparseVector<float64_t>);
225  case PT_FLOATMAX: return sizeof (SGSparseVector<floatmax_t>);
226  case PT_COMPLEX128: return sizeof (SGSparseVector<complex128_t>);
227  case PT_SGOBJECT: return -1;
228  case PT_UNDEFINED: default:
229  SG_SERROR("Implementation error: undefined primitive type\n");
230  break;
231  }
232  break;
233  case ST_UNDEFINED: default:
234  SG_SERROR("Implementation error: undefined structure type\n");
235  break;
236  }
237 
238  return -1;
239 }
240 
241 size_t
242 TSGDataType::sizeof_ptype(EPrimitiveType ptype)
243 {
244  switch (ptype) {
245  case PT_BOOL: return sizeof (bool);
246  case PT_CHAR: return sizeof (char);
247  case PT_INT8: return sizeof (int8_t);
248  case PT_UINT8: return sizeof (uint8_t);
249  case PT_INT16: return sizeof (int16_t);
250  case PT_UINT16: return sizeof (uint16_t);
251  case PT_INT32: return sizeof (int32_t);
252  case PT_UINT32: return sizeof (uint32_t);
253  case PT_INT64: return sizeof (int64_t);
254  case PT_UINT64: return sizeof (uint64_t);
255  case PT_FLOAT32: return sizeof (float32_t);
256  case PT_FLOAT64: return sizeof (float64_t);
257  case PT_FLOATMAX: return sizeof (floatmax_t);
258  case PT_COMPLEX128: return sizeof (complex128_t);
259  case PT_SGOBJECT: return sizeof (CSGObject*);
260  case PT_UNDEFINED: default:
261  SG_SERROR("Implementation error: undefined primitive type\n");
262  break;
263  }
264 
265  return -1;
266 }
267 
268 size_t
269 TSGDataType::sizeof_sparseentry(EPrimitiveType ptype)
270 {
271  switch (ptype) {
272  case PT_BOOL: return sizeof (SGSparseVectorEntry<bool>);
273  case PT_CHAR: return sizeof (SGSparseVectorEntry<char>);
274  case PT_INT8: return sizeof (SGSparseVectorEntry<int8_t>);
275  case PT_UINT8: return sizeof (SGSparseVectorEntry<uint8_t>);
276  case PT_INT16: return sizeof (SGSparseVectorEntry<int16_t>);
277  case PT_UINT16: return sizeof (SGSparseVectorEntry<uint16_t>);
278  case PT_INT32: return sizeof (SGSparseVectorEntry<int32_t>);
279  case PT_UINT32: return sizeof (SGSparseVectorEntry<uint32_t>);
280  case PT_INT64: return sizeof (SGSparseVectorEntry<int64_t>);
281  case PT_UINT64: return sizeof (SGSparseVectorEntry<uint64_t>);
282  case PT_FLOAT32: return sizeof (SGSparseVectorEntry<float32_t>);
283  case PT_FLOAT64: return sizeof (SGSparseVectorEntry<float64_t>);
284  case PT_FLOATMAX: return sizeof (SGSparseVectorEntry<floatmax_t>);
285  case PT_COMPLEX128: return sizeof (SGSparseVectorEntry<complex128_t>);
286  case PT_SGOBJECT: return -1;
287  case PT_UNDEFINED: default:
288  SG_SERROR("Implementation error: undefined primitive type\n");
289  break;
290  }
291 
292  return -1;
293 }
294 
295 #define ENTRY_OFFSET(k, type) \
296  ((char*) &((SGSparseVectorEntry<type>*) (k))->entry - (char*) (k))
297 size_t
298 TSGDataType::offset_sparseentry(EPrimitiveType ptype)
299 {
300  size_t result = -1; void* x = &result;
301 
302  switch (ptype) {
303  case PT_BOOL: result = ENTRY_OFFSET(x, bool); break;
304  case PT_CHAR: result = ENTRY_OFFSET(x, char); break;
305  case PT_INT8: result = ENTRY_OFFSET(x, int8_t); break;
306  case PT_UINT8: result = ENTRY_OFFSET(x, uint8_t); break;
307  case PT_INT16: result = ENTRY_OFFSET(x, int16_t); break;
308  case PT_UINT16: result = ENTRY_OFFSET(x, uint16_t); break;
309  case PT_INT32: result = ENTRY_OFFSET(x, int32_t); break;
310  case PT_UINT32: result = ENTRY_OFFSET(x, uint32_t); break;
311  case PT_INT64: result = ENTRY_OFFSET(x, int64_t); break;
312  case PT_UINT64: result = ENTRY_OFFSET(x, uint64_t); break;
313  case PT_FLOAT32: result = ENTRY_OFFSET(x, float32_t); break;
314  case PT_FLOAT64: result = ENTRY_OFFSET(x, float64_t); break;
315  case PT_FLOATMAX: result = ENTRY_OFFSET(x, floatmax_t); break;
316  case PT_COMPLEX128: result = ENTRY_OFFSET(x, complex128_t); break;
317  case PT_SGOBJECT: return -1;
318  case PT_UNDEFINED: default:
319  SG_SERROR("Implementation error: undefined primitive type\n");
320  break;
321  }
322 
323  return result;
324 }
325 
326 void
327 TSGDataType::stype_to_string(char* dest, EStructType stype,
328  EPrimitiveType ptype, size_t n)
329 {
330  char* p = dest;
331 
332  switch (stype) {
333  case ST_NONE: strncpy(p, "", n); break;
334  case ST_STRING: strncpy(p, "String<", n); break;
335  case ST_SPARSE: strncpy(p, "Sparse<", n); break;
336  case ST_UNDEFINED: default:
337  SG_SERROR("Implementation error: undefined structure type\n");
338  break;
339  }
340 
341  size_t np = strlen(p);
342  ptype_to_string(p + np, ptype, n - np - 2);
343 
344  switch (stype) {
345  case ST_NONE: break;
346  case ST_STRING: case ST_SPARSE:
347  strcat(p, ">"); break;
348  case ST_UNDEFINED: default:
349  SG_SERROR("Implementation error: undefined structure type\n");
350  break;
351  }
352 }
353 
354 void
355 TSGDataType::ptype_to_string(char* dest, EPrimitiveType ptype,
356  size_t n)
357 {
358  char* p = dest;
359 
360  switch (ptype) {
361  case PT_BOOL: strncpy(p, "bool", n); break;
362  case PT_CHAR: strncpy(p, "char", n); break;
363  case PT_INT8: strncpy(p, "int8", n); break;
364  case PT_UINT8: strncpy(p, "uint8", n); break;
365  case PT_INT16: strncpy(p, "int16", n); break;
366  case PT_UINT16: strncpy(p, "uint16", n); break;
367  case PT_INT32: strncpy(p, "int32", n); break;
368  case PT_UINT32: strncpy(p, "uint32", n); break;
369  case PT_INT64: strncpy(p, "int64", n); break;
370  case PT_UINT64: strncpy(p, "uint64", n); break;
371  case PT_FLOAT32: strncpy(p, "float32", n); break;
372  case PT_FLOAT64: strncpy(p, "float64", n); break;
373  case PT_FLOATMAX: strncpy(p, "floatmax", n); break;
374  case PT_COMPLEX128: strncpy(p, "complex128", n); break;
375  case PT_SGOBJECT: strncpy(p, "SGSerializable*", n); break;
376  case PT_UNDEFINED: default:
377  SG_SERROR("Implementation error: undefined primitive type\n");
378  break;
379  }
380 }
381 
382 bool
383 TSGDataType::string_to_ptype(EPrimitiveType* ptype, const char* str)
384 {
385  if (strcmp(str, "bool") == 0) {
386  *ptype = PT_BOOL; return true; }
387  if (strcmp(str, "char") == 0) {
388  *ptype = PT_CHAR; return true; }
389  if (strcmp(str, "int8") == 0) {
390  *ptype = PT_INT8; return true; }
391  if (strcmp(str, "uint8") == 0) {
392  *ptype = PT_UINT8; return true; }
393  if (strcmp(str, "int16") == 0) {
394  *ptype = PT_INT16; return true; }
395  if (strcmp(str, "uint16") == 0) {
396  *ptype = PT_UINT16; return true; }
397  if (strcmp(str, "int32") == 0) {
398  *ptype = PT_INT32; return true; }
399  if (strcmp(str, "uint32") == 0) {
400  *ptype = PT_UINT32; return true; }
401  if (strcmp(str, "int64") == 0) {
402  *ptype = PT_INT64; return true; }
403  if (strcmp(str, "uint64") == 0) {
404  *ptype = PT_UINT64; return true; }
405  if (strcmp(str, "float32") == 0) {
406  *ptype = PT_FLOAT32; return true; }
407  if (strcmp(str, "float64") == 0) {
408  *ptype = PT_FLOAT64; return true; }
409  if (strcmp(str, "floatmax") == 0) {
410  *ptype = PT_FLOATMAX; return true; }
411  if (strcmp(str, "complex128") == 0) {
412  *ptype = PT_COMPLEX128; return true; }
413  if (strcmp(str, "SGSerializable*") == 0) {
414  *ptype = PT_SGOBJECT; return true; }
415 
416  /* Make sure that the compiler will warn at this position. */
417  switch (*ptype) {
418  case PT_BOOL: case PT_CHAR: case PT_INT8: case PT_UINT8:
419  case PT_INT16: case PT_UINT16: case PT_INT32: case PT_UINT32:
420  case PT_INT64: case PT_UINT64: case PT_FLOAT32: case PT_FLOAT64:
421  case PT_FLOATMAX: case PT_COMPLEX128: case PT_SGOBJECT: break;
422  case PT_UNDEFINED: default:
423  SG_SERROR("Implementation error: undefined primitive type\n");
424  break;
425  }
426 
427  return false;
428 }
429 
431 {
432  switch (m_stype)
433  {
434  case ST_NONE:
435  return get_num_elements()*sizeof_ptype();
436  case ST_STRING:
437  if (m_ptype==PT_SGOBJECT)
438  return 0;
439 
440  return get_num_elements()*sizeof_stype();
441  case ST_SPARSE:
442  if (m_ptype==PT_SGOBJECT)
443  return 0;
444 
446  case ST_UNDEFINED: default:
447  SG_SERROR("Implementation error: undefined structure type\n");
448  break;
449  }
450 
451  return 0;
452 }
453 
455 {
456  switch (m_ctype)
457  {
458  case CT_SCALAR:
459  return 1;
460  case CT_VECTOR: case CT_SGVECTOR:
461  /* length_y contains the length for vectors */
462  return *m_length_y;
463  case CT_MATRIX: case CT_SGMATRIX:
464  return (*m_length_y)*(*m_length_x);
465  case CT_NDARRAY:
467  case CT_UNDEFINED: default:
468  SG_SERROR("Implementation error: undefined container type\n");
469  break;
470  }
471  return 0;
472 }

SHOGUN Machine Learning Toolbox - Documentation