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

SHOGUN Machine Learning Toolbox - Documentation