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