00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <string.h>
00012
00013 #include <shogun/base/SGObject.h>
00014 #include <shogun/lib/DataType.h>
00015 #include <shogun/lib/SGString.h>
00016 #include <shogun/lib/SGSparseVector.h>
00017
00018 using namespace shogun;
00019
00020 TSGDataType::TSGDataType(EContainerType ctype, EStructType stype,
00021 EPrimitiveType ptype)
00022 {
00023 m_ctype = ctype, m_stype = stype, m_ptype = ptype;
00024 m_length_y = m_length_x = NULL;
00025 }
00026
00027 TSGDataType::TSGDataType(EContainerType ctype, EStructType stype,
00028 EPrimitiveType ptype, index_t* length)
00029 {
00030 m_ctype = ctype, m_stype = stype, m_ptype = ptype;
00031 m_length_y = length, m_length_x = NULL;
00032 }
00033
00034 TSGDataType::TSGDataType(EContainerType ctype, EStructType stype,
00035 EPrimitiveType ptype, index_t* length_y,
00036 index_t* length_x)
00037 {
00038 m_ctype = ctype, m_stype = stype, m_ptype = ptype;
00039 m_length_y = length_y, m_length_x = length_x;
00040 }
00041
00042 bool
00043 TSGDataType::operator==(const TSGDataType& a)
00044 {
00045 bool result = m_ctype == a.m_ctype && m_stype == a.m_stype
00046 && m_ptype == a.m_ptype;
00047
00048 result &= m_length_y != NULL && a.m_length_y != NULL
00049 ? *m_length_y == *a.m_length_y: m_length_y == a.m_length_y;
00050 result &= m_length_x != NULL && a.m_length_x != NULL
00051 ? *m_length_x == *a.m_length_x: m_length_x == a.m_length_x;
00052
00053 return result;
00054 }
00055
00056 void
00057 TSGDataType::to_string(char* dest, size_t n) const
00058 {
00059 char* p = dest;
00060
00061 switch (m_ctype) {
00062 case CT_SCALAR: strncpy(p, "", n); break;
00063 case CT_VECTOR: strncpy(p, "Vector<", n); break;
00064 case CT_SGVECTOR: strncpy(p, "SGVector<", n); break;
00065 case CT_MATRIX: strncpy(p, "Matrix<", n); break;
00066 case CT_SGMATRIX: strncpy(p, "SGMatrix<", n); break;
00067 case CT_NDARRAY: strncpy(p, "N-Dimensional Array<", n); break;
00068 }
00069
00070 size_t np = strlen(p);
00071 stype_to_string(p + np, m_stype, m_ptype, n - np - 2);
00072
00073 switch (m_ctype) {
00074 case CT_SCALAR: break;
00075 case CT_VECTOR:
00076 case CT_SGVECTOR:
00077 case CT_MATRIX:
00078 case CT_SGMATRIX:
00079 case CT_NDARRAY:
00080 strcat(p, ">"); break;
00081 }
00082 }
00083
00084 size_t
00085 TSGDataType::sizeof_stype() const
00086 {
00087 switch (m_stype) {
00088 case ST_NONE: return sizeof_ptype();
00089 case ST_STRING:
00090 switch (m_ptype) {
00091 case PT_BOOL: return sizeof (SGString<bool>);
00092 case PT_CHAR: return sizeof (SGString<char>);
00093 case PT_INT8: return sizeof (SGString<int8_t>);
00094 case PT_UINT8: return sizeof (SGString<uint8_t>);
00095 case PT_INT16: return sizeof (SGString<int16_t>);
00096 case PT_UINT16: return sizeof (SGString<uint16_t>);
00097 case PT_INT32: return sizeof (SGString<int32_t>);
00098 case PT_UINT32: return sizeof (SGString<uint32_t>);
00099 case PT_INT64: return sizeof (SGString<int64_t>);
00100 case PT_UINT64: return sizeof (SGString<uint64_t>);
00101 case PT_FLOAT32: return sizeof (SGString<float32_t>);
00102 case PT_FLOAT64: return sizeof (SGString<float64_t>);
00103 case PT_FLOATMAX: return sizeof (SGString<floatmax_t>);
00104 case PT_SGOBJECT: return -1;
00105 }
00106 break;
00107 case ST_SPARSE:
00108 switch (m_ptype) {
00109 case PT_BOOL: return sizeof (SGSparseVector<bool>);
00110 case PT_CHAR: return sizeof (SGSparseVector<char>);
00111 case PT_INT8: return sizeof (SGSparseVector<int8_t>);
00112 case PT_UINT8: return sizeof (SGSparseVector<uint8_t>);
00113 case PT_INT16: return sizeof (SGSparseVector<int16_t>);
00114 case PT_UINT16: return sizeof (SGSparseVector<uint16_t>);
00115 case PT_INT32: return sizeof (SGSparseVector<int32_t>);
00116 case PT_UINT32: return sizeof (SGSparseVector<uint32_t>);
00117 case PT_INT64: return sizeof (SGSparseVector<int64_t>);
00118 case PT_UINT64: return sizeof (SGSparseVector<uint64_t>);
00119 case PT_FLOAT32: return sizeof (SGSparseVector<float32_t>);
00120 case PT_FLOAT64: return sizeof (SGSparseVector<float64_t>);
00121 case PT_FLOATMAX: return sizeof (SGSparseVector<floatmax_t>);
00122 case PT_SGOBJECT: return -1;
00123 }
00124 break;
00125 }
00126
00127 return -1;
00128 }
00129
00130 size_t
00131 TSGDataType::sizeof_ptype() const
00132 {
00133 switch (m_ptype) {
00134 case PT_BOOL: return sizeof (bool);
00135 case PT_CHAR: return sizeof (char);
00136 case PT_INT8: return sizeof (int8_t);
00137 case PT_UINT8: return sizeof (uint8_t);
00138 case PT_INT16: return sizeof (int16_t);
00139 case PT_UINT16: return sizeof (uint16_t);
00140 case PT_INT32: return sizeof (int32_t);
00141 case PT_UINT32: return sizeof (uint32_t);
00142 case PT_INT64: return sizeof (int64_t);
00143 case PT_UINT64: return sizeof (uint64_t);
00144 case PT_FLOAT32: return sizeof (float32_t);
00145 case PT_FLOAT64: return sizeof (float64_t);
00146 case PT_FLOATMAX: return sizeof (floatmax_t);
00147 case PT_SGOBJECT: return sizeof (CSGObject*);
00148 }
00149
00150 return -1;
00151 }
00152
00153 size_t
00154 TSGDataType::sizeof_sparseentry(EPrimitiveType ptype)
00155 {
00156 switch (ptype) {
00157 case PT_BOOL: return sizeof (SGSparseVectorEntry<bool>);
00158 case PT_CHAR: return sizeof (SGSparseVectorEntry<char>);
00159 case PT_INT8: return sizeof (SGSparseVectorEntry<int8_t>);
00160 case PT_UINT8: return sizeof (SGSparseVectorEntry<uint8_t>);
00161 case PT_INT16: return sizeof (SGSparseVectorEntry<int16_t>);
00162 case PT_UINT16: return sizeof (SGSparseVectorEntry<uint16_t>);
00163 case PT_INT32: return sizeof (SGSparseVectorEntry<int32_t>);
00164 case PT_UINT32: return sizeof (SGSparseVectorEntry<uint32_t>);
00165 case PT_INT64: return sizeof (SGSparseVectorEntry<int64_t>);
00166 case PT_UINT64: return sizeof (SGSparseVectorEntry<uint64_t>);
00167 case PT_FLOAT32: return sizeof (SGSparseVectorEntry<float32_t>);
00168 case PT_FLOAT64: return sizeof (SGSparseVectorEntry<float64_t>);
00169 case PT_FLOATMAX: return sizeof (SGSparseVectorEntry<floatmax_t>);
00170 case PT_SGOBJECT: return -1;
00171 }
00172
00173 return -1;
00174 }
00175
00176 #define ENTRY_OFFSET(k, type) \
00177 ((char*) &((SGSparseVectorEntry<type>*) (k))->entry - (char*) (k))
00178 size_t
00179 TSGDataType::offset_sparseentry(EPrimitiveType ptype)
00180 {
00181 size_t result = -1; void* x = &result;
00182
00183 switch (ptype) {
00184 case PT_BOOL: result = ENTRY_OFFSET(x, bool); break;
00185 case PT_CHAR: result = ENTRY_OFFSET(x, char); break;
00186 case PT_INT8: result = ENTRY_OFFSET(x, int8_t); break;
00187 case PT_UINT8: result = ENTRY_OFFSET(x, uint8_t); break;
00188 case PT_INT16: result = ENTRY_OFFSET(x, int16_t); break;
00189 case PT_UINT16: result = ENTRY_OFFSET(x, uint16_t); break;
00190 case PT_INT32: result = ENTRY_OFFSET(x, int32_t); break;
00191 case PT_UINT32: result = ENTRY_OFFSET(x, uint32_t); break;
00192 case PT_INT64: result = ENTRY_OFFSET(x, int64_t); break;
00193 case PT_UINT64: result = ENTRY_OFFSET(x, uint64_t); break;
00194 case PT_FLOAT32: result = ENTRY_OFFSET(x, float32_t); break;
00195 case PT_FLOAT64: result = ENTRY_OFFSET(x, float64_t); break;
00196 case PT_FLOATMAX: result = ENTRY_OFFSET(x, floatmax_t); break;
00197 case PT_SGOBJECT: return -1;
00198 }
00199
00200 return result;
00201 }
00202
00203 void
00204 TSGDataType::stype_to_string(char* dest, EStructType stype,
00205 EPrimitiveType ptype, size_t n)
00206 {
00207 char* p = dest;
00208
00209 switch (stype) {
00210 case ST_NONE: strncpy(p, "", n); break;
00211 case ST_STRING: strncpy(p, "String<", n); break;
00212 case ST_SPARSE: strncpy(p, "Sparse<", n); break;
00213 }
00214
00215 size_t np = strlen(p);
00216 ptype_to_string(p + np, ptype, n - np - 2);
00217
00218 switch (stype) {
00219 case ST_NONE: break;
00220 case ST_STRING: case ST_SPARSE:
00221 strcat(p, ">"); break;
00222 }
00223 }
00224
00225 void
00226 TSGDataType::ptype_to_string(char* dest, EPrimitiveType ptype,
00227 size_t n)
00228 {
00229 char* p = dest;
00230
00231 switch (ptype) {
00232 case PT_BOOL: strncpy(p, "bool", n); break;
00233 case PT_CHAR: strncpy(p, "char", n); break;
00234 case PT_INT8: strncpy(p, "int8", n); break;
00235 case PT_UINT8: strncpy(p, "uint8", n); break;
00236 case PT_INT16: strncpy(p, "int16", n); break;
00237 case PT_UINT16: strncpy(p, "uint16", n); break;
00238 case PT_INT32: strncpy(p, "int32", n); break;
00239 case PT_UINT32: strncpy(p, "uint32", n); break;
00240 case PT_INT64: strncpy(p, "int64", n); break;
00241 case PT_UINT64: strncpy(p, "uint64", n); break;
00242 case PT_FLOAT32: strncpy(p, "float32", n); break;
00243 case PT_FLOAT64: strncpy(p, "float64", n); break;
00244 case PT_FLOATMAX: strncpy(p, "floatmax", n); break;
00245 case PT_SGOBJECT: strncpy(p, "SGSerializable*", n); break;
00246 }
00247 }
00248
00249 bool
00250 TSGDataType::string_to_ptype(EPrimitiveType* ptype, const char* str)
00251 {
00252 if (strcmp(str, "bool") == 0) {
00253 *ptype = PT_BOOL; return true; }
00254 if (strcmp(str, "char") == 0) {
00255 *ptype = PT_CHAR; return true; }
00256 if (strcmp(str, "int8") == 0) {
00257 *ptype = PT_INT8; return true; }
00258 if (strcmp(str, "uint8") == 0) {
00259 *ptype = PT_UINT8; return true; }
00260 if (strcmp(str, "int16") == 0) {
00261 *ptype = PT_INT16; return true; }
00262 if (strcmp(str, "uint16") == 0) {
00263 *ptype = PT_UINT16; return true; }
00264 if (strcmp(str, "int32") == 0) {
00265 *ptype = PT_INT32; return true; }
00266 if (strcmp(str, "uint32") == 0) {
00267 *ptype = PT_UINT32; return true; }
00268 if (strcmp(str, "int64") == 0) {
00269 *ptype = PT_INT64; return true; }
00270 if (strcmp(str, "uint64") == 0) {
00271 *ptype = PT_UINT64; return true; }
00272 if (strcmp(str, "float32") == 0) {
00273 *ptype = PT_FLOAT32; return true; }
00274 if (strcmp(str, "float64") == 0) {
00275 *ptype = PT_FLOAT64; return true; }
00276 if (strcmp(str, "floatmax") == 0) {
00277 *ptype = PT_FLOATMAX; return true; }
00278 if (strcmp(str, "SGSerializable*") == 0) {
00279 *ptype = PT_SGOBJECT; return true; }
00280
00281
00282 switch (*ptype) {
00283 case PT_BOOL: case PT_CHAR: case PT_INT8: case PT_UINT8:
00284 case PT_INT16: case PT_UINT16: case PT_INT32: case PT_UINT32:
00285 case PT_INT64: case PT_UINT64: case PT_FLOAT32: case PT_FLOAT64:
00286 case PT_FLOATMAX: case PT_SGOBJECT: break;
00287 }
00288
00289 return false;
00290 }
00291
00292 size_t TSGDataType::get_size()
00293 {
00294 switch (m_stype)
00295 {
00296 case ST_NONE:
00297 return get_num_elements()*sizeof_ptype();
00298 case ST_STRING:
00299 if (m_ptype==PT_SGOBJECT)
00300 return 0;
00301
00302 return get_num_elements()*sizeof_stype();
00303 case ST_SPARSE:
00304 if (m_ptype==PT_SGOBJECT)
00305 return 0;
00306
00307 return get_num_elements()*sizeof_sparseentry(m_ptype);
00308 }
00309
00310 return 0;
00311 }
00312
00313 index_t TSGDataType::get_num_elements()
00314 {
00315 switch (m_ctype)
00316 {
00317 case CT_SCALAR:
00318 return 1;
00319 case CT_VECTOR: case CT_SGVECTOR:
00320
00321 return *m_length_y;
00322 case CT_MATRIX: case CT_SGMATRIX:
00323 return (*m_length_y)*(*m_length_x);
00324 case CT_NDARRAY:
00325 SG_SNOTIMPLEMENTED;
00326 }
00327 return 0;
00328 }