SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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  * Copyright (C) 2010 Berlin Institute of Technology
9  */
10 
11 #include <string.h>
12 
13 #include <shogun/base/SGObject.h>
14 #include <shogun/lib/DataType.h>
15 
16 using namespace shogun;
17 
18 TSGDataType::TSGDataType(EContainerType ctype, EStructType stype,
19  EPrimitiveType ptype)
20 {
21  m_ctype = ctype, m_stype = stype, m_ptype = ptype;
22  m_length_y = m_length_x = NULL;
23 }
24 
25 TSGDataType::TSGDataType(EContainerType ctype, EStructType stype,
26  EPrimitiveType ptype, index_t* length)
27 {
28  m_ctype = ctype, m_stype = stype, m_ptype = ptype;
29  m_length_y = length, m_length_x = NULL;
30 }
31 
32 TSGDataType::TSGDataType(EContainerType ctype, EStructType stype,
33  EPrimitiveType ptype, index_t* length_y,
34  index_t* length_x)
35 {
36  m_ctype = ctype, m_stype = stype, m_ptype = ptype;
37  m_length_y = length_y, m_length_x = length_x;
38 }
39 
40 bool
42 {
43  bool result = m_ctype == a.m_ctype && m_stype == a.m_stype
44  && m_ptype == a.m_ptype;
45 
46  result &= m_length_y != NULL && a.m_length_y != NULL
47  ? *m_length_y == *a.m_length_y: m_length_y == a.m_length_y;
48  result &= m_length_x != NULL && a.m_length_x != NULL
49  ? *m_length_x == *a.m_length_x: m_length_x == a.m_length_x;
50 
51  return result;
52 }
53 
54 void
55 TSGDataType::to_string(char* dest, size_t n) const
56 {
57  char* p = dest;
58 
59  switch (m_ctype) {
60  case CT_SCALAR: strncpy(p, "", n); break;
61  case CT_VECTOR: strncpy(p, "Vector<", n); break;
62  case CT_SGVECTOR: strncpy(p, "SGVector<", n); break;
63  case CT_MATRIX: strncpy(p, "Matrix<", n); break;
64  case CT_SGMATRIX: strncpy(p, "SGMatrix<", n); break;
65  case CT_NDARRAY: strncpy(p, "N-Dimensional Array<", n); break;
66  }
67 
68  size_t np = strlen(p);
69  stype_to_string(p + np, m_stype, m_ptype, n - np - 2);
70 
71  switch (m_ctype) {
72  case CT_SCALAR: break;
73  case CT_VECTOR:
74  case CT_SGVECTOR:
75  case CT_MATRIX:
76  case CT_SGMATRIX:
77  case CT_NDARRAY:
78  strcat(p, ">"); break;
79  }
80 }
81 
82 size_t
84 {
85  switch (m_stype) {
86  case ST_NONE: return sizeof_ptype();
87  case ST_STRING:
88  switch (m_ptype) {
89  case PT_BOOL: return sizeof (SGString<bool>);
90  case PT_CHAR: return sizeof (SGString<char>);
91  case PT_INT8: return sizeof (SGString<int8_t>);
92  case PT_UINT8: return sizeof (SGString<uint8_t>);
93  case PT_INT16: return sizeof (SGString<int16_t>);
94  case PT_UINT16: return sizeof (SGString<uint16_t>);
95  case PT_INT32: return sizeof (SGString<int32_t>);
96  case PT_UINT32: return sizeof (SGString<uint32_t>);
97  case PT_INT64: return sizeof (SGString<int64_t>);
98  case PT_UINT64: return sizeof (SGString<uint64_t>);
99  case PT_FLOAT32: return sizeof (SGString<float32_t>);
100  case PT_FLOAT64: return sizeof (SGString<float64_t>);
101  case PT_FLOATMAX: return sizeof (SGString<floatmax_t>);
102  case PT_SGOBJECT: return -1;
103  }
104  break;
105  case ST_SPARSE:
106  switch (m_ptype) {
107  case PT_BOOL: return sizeof (SGSparseVector<bool>);
108  case PT_CHAR: return sizeof (SGSparseVector<char>);
109  case PT_INT8: return sizeof (SGSparseVector<int8_t>);
110  case PT_UINT8: return sizeof (SGSparseVector<uint8_t>);
111  case PT_INT16: return sizeof (SGSparseVector<int16_t>);
112  case PT_UINT16: return sizeof (SGSparseVector<uint16_t>);
113  case PT_INT32: return sizeof (SGSparseVector<int32_t>);
114  case PT_UINT32: return sizeof (SGSparseVector<uint32_t>);
115  case PT_INT64: return sizeof (SGSparseVector<int64_t>);
116  case PT_UINT64: return sizeof (SGSparseVector<uint64_t>);
117  case PT_FLOAT32: return sizeof (SGSparseVector<float32_t>);
118  case PT_FLOAT64: return sizeof (SGSparseVector<float64_t>);
119  case PT_FLOATMAX: return sizeof (SGSparseVector<floatmax_t>);
120  case PT_SGOBJECT: return -1;
121  }
122  break;
123  }
124 
125  return -1;
126 }
127 
128 size_t
130 {
131  switch (m_ptype) {
132  case PT_BOOL: return sizeof (bool);
133  case PT_CHAR: return sizeof (char);
134  case PT_INT8: return sizeof (int8_t);
135  case PT_UINT8: return sizeof (uint8_t);
136  case PT_INT16: return sizeof (int16_t);
137  case PT_UINT16: return sizeof (uint16_t);
138  case PT_INT32: return sizeof (int32_t);
139  case PT_UINT32: return sizeof (uint32_t);
140  case PT_INT64: return sizeof (int64_t);
141  case PT_UINT64: return sizeof (uint64_t);
142  case PT_FLOAT32: return sizeof (float32_t);
143  case PT_FLOAT64: return sizeof (float64_t);
144  case PT_FLOATMAX: return sizeof (floatmax_t);
145  case PT_SGOBJECT: return sizeof (CSGObject*);
146  }
147 
148  return -1;
149 }
150 
151 size_t
152 TSGDataType::sizeof_sparseentry(EPrimitiveType ptype)
153 {
154  switch (ptype) {
155  case PT_BOOL: return sizeof (SGSparseVectorEntry<bool>);
156  case PT_CHAR: return sizeof (SGSparseVectorEntry<char>);
157  case PT_INT8: return sizeof (SGSparseVectorEntry<int8_t>);
158  case PT_UINT8: return sizeof (SGSparseVectorEntry<uint8_t>);
159  case PT_INT16: return sizeof (SGSparseVectorEntry<int16_t>);
160  case PT_UINT16: return sizeof (SGSparseVectorEntry<uint16_t>);
161  case PT_INT32: return sizeof (SGSparseVectorEntry<int32_t>);
162  case PT_UINT32: return sizeof (SGSparseVectorEntry<uint32_t>);
163  case PT_INT64: return sizeof (SGSparseVectorEntry<int64_t>);
164  case PT_UINT64: return sizeof (SGSparseVectorEntry<uint64_t>);
165  case PT_FLOAT32: return sizeof (SGSparseVectorEntry<float32_t>);
166  case PT_FLOAT64: return sizeof (SGSparseVectorEntry<float64_t>);
167  case PT_FLOATMAX: return sizeof (SGSparseVectorEntry<floatmax_t>);
168  case PT_SGOBJECT: return -1;
169  }
170 
171  return -1;
172 }
173 
174 #define ENTRY_OFFSET(k, type) \
175  ((char*) &((SGSparseVectorEntry<type>*) (k))->entry - (char*) (k))
176 size_t
177 TSGDataType::offset_sparseentry(EPrimitiveType ptype)
178 {
179  size_t result = -1; void* x = &result;
180 
181  switch (ptype) {
182  case PT_BOOL: result = ENTRY_OFFSET(x, bool); break;
183  case PT_CHAR: result = ENTRY_OFFSET(x, char); break;
184  case PT_INT8: result = ENTRY_OFFSET(x, int8_t); break;
185  case PT_UINT8: result = ENTRY_OFFSET(x, uint8_t); break;
186  case PT_INT16: result = ENTRY_OFFSET(x, int16_t); break;
187  case PT_UINT16: result = ENTRY_OFFSET(x, uint16_t); break;
188  case PT_INT32: result = ENTRY_OFFSET(x, int32_t); break;
189  case PT_UINT32: result = ENTRY_OFFSET(x, uint32_t); break;
190  case PT_INT64: result = ENTRY_OFFSET(x, int64_t); break;
191  case PT_UINT64: result = ENTRY_OFFSET(x, uint64_t); break;
192  case PT_FLOAT32: result = ENTRY_OFFSET(x, float32_t); break;
193  case PT_FLOAT64: result = ENTRY_OFFSET(x, float64_t); break;
194  case PT_FLOATMAX: result = ENTRY_OFFSET(x, floatmax_t); break;
195  case PT_SGOBJECT: return -1;
196  }
197 
198  return result;
199 }
200 
201 void
202 TSGDataType::stype_to_string(char* dest, EStructType stype,
203  EPrimitiveType ptype, size_t n)
204 {
205  char* p = dest;
206 
207  switch (stype) {
208  case ST_NONE: strncpy(p, "", n); break;
209  case ST_STRING: strncpy(p, "String<", n); break;
210  case ST_SPARSE: strncpy(p, "Sparse<", n); break;
211  }
212 
213  size_t np = strlen(p);
214  ptype_to_string(p + np, ptype, n - np - 2);
215 
216  switch (stype) {
217  case ST_NONE: break;
218  case ST_STRING: case ST_SPARSE:
219  strcat(p, ">"); break;
220  }
221 }
222 
223 void
224 TSGDataType::ptype_to_string(char* dest, EPrimitiveType ptype,
225  size_t n)
226 {
227  char* p = dest;
228 
229  switch (ptype) {
230  case PT_BOOL: strncpy(p, "bool", n); break;
231  case PT_CHAR: strncpy(p, "char", n); break;
232  case PT_INT8: strncpy(p, "int8", n); break;
233  case PT_UINT8: strncpy(p, "uint8", n); break;
234  case PT_INT16: strncpy(p, "int16", n); break;
235  case PT_UINT16: strncpy(p, "uint16", n); break;
236  case PT_INT32: strncpy(p, "int32", n); break;
237  case PT_UINT32: strncpy(p, "uint32", n); break;
238  case PT_INT64: strncpy(p, "int64", n); break;
239  case PT_UINT64: strncpy(p, "uint64", n); break;
240  case PT_FLOAT32: strncpy(p, "float32", n); break;
241  case PT_FLOAT64: strncpy(p, "float64", n); break;
242  case PT_FLOATMAX: strncpy(p, "floatmax", n); break;
243  case PT_SGOBJECT: strncpy(p, "SGSerializable*", n); break;
244  }
245 }
246 
247 bool
248 TSGDataType::string_to_ptype(EPrimitiveType* ptype, const char* str)
249 {
250  if (strcmp(str, "bool") == 0) {
251  *ptype = PT_BOOL; return true; }
252  if (strcmp(str, "char") == 0) {
253  *ptype = PT_CHAR; return true; }
254  if (strcmp(str, "int8") == 0) {
255  *ptype = PT_INT8; return true; }
256  if (strcmp(str, "uint8") == 0) {
257  *ptype = PT_UINT8; return true; }
258  if (strcmp(str, "int16") == 0) {
259  *ptype = PT_INT16; return true; }
260  if (strcmp(str, "uint16") == 0) {
261  *ptype = PT_UINT16; return true; }
262  if (strcmp(str, "int32") == 0) {
263  *ptype = PT_INT32; return true; }
264  if (strcmp(str, "uint32") == 0) {
265  *ptype = PT_UINT32; return true; }
266  if (strcmp(str, "int64") == 0) {
267  *ptype = PT_INT64; return true; }
268  if (strcmp(str, "uint64") == 0) {
269  *ptype = PT_UINT64; return true; }
270  if (strcmp(str, "float32") == 0) {
271  *ptype = PT_FLOAT32; return true; }
272  if (strcmp(str, "float64") == 0) {
273  *ptype = PT_FLOAT64; return true; }
274  if (strcmp(str, "floatmax") == 0) {
275  *ptype = PT_FLOATMAX; return true; }
276  if (strcmp(str, "SGSerializable*") == 0) {
277  *ptype = PT_SGOBJECT; return true; }
278 
279  /* Make sure that the compiler will warn at this position. */
280  switch (*ptype) {
281  case PT_BOOL: case PT_CHAR: case PT_INT8: case PT_UINT8:
282  case PT_INT16: case PT_UINT16: case PT_INT32: case PT_UINT32:
283  case PT_INT64: case PT_UINT64: case PT_FLOAT32: case PT_FLOAT64:
284  case PT_FLOATMAX: case PT_SGOBJECT: break;
285  }
286 
287  return false;
288 }
289 
291 {
292  switch (m_stype)
293  {
294  case ST_NONE:
295  return get_num_elements()*sizeof_ptype();
296  case ST_STRING:
297  if (m_ptype==PT_SGOBJECT)
298  return 0;
299 
300  return get_num_elements()*sizeof_stype();
301  case ST_SPARSE:
302  if (m_ptype==PT_SGOBJECT)
303  return 0;
304 
306  }
307 
308  return 0;
309 }
310 
312 {
313  switch (m_ctype)
314  {
315  case CT_SCALAR:
316  return 1;
317  case CT_VECTOR: case CT_SGVECTOR:
318  /* length_y contains the length for vectors */
319  return *m_length_y;
320  case CT_MATRIX: case CT_SGMATRIX:
321  return (*m_length_y)*(*m_length_x);
322  case CT_NDARRAY:
324  }
325  return 0;
326 }

SHOGUN Machine Learning Toolbox - Documentation