00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <string.h>
00012
00013 #include "base/SGObject.h"
00014 #include "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_MATRIX: strncpy(p, "Matrix<", n); break;
00063 }
00064
00065 size_t np = strlen(p);
00066 stype_to_string(p + np, m_stype, m_ptype, n - np - 2);
00067
00068 switch (m_ctype) {
00069 case CT_SCALAR: break;
00070 case CT_VECTOR: case CT_MATRIX:
00071 strcat(p, ">"); break;
00072 }
00073 }
00074
00075 size_t
00076 TSGDataType::sizeof_stype(void) const
00077 {
00078 switch (m_stype) {
00079 case ST_NONE: return sizeof_ptype();
00080 case ST_STRING:
00081 switch (m_ptype) {
00082 case PT_BOOL: return sizeof (TString<bool>);
00083 case PT_CHAR: return sizeof (TString<char>);
00084 case PT_INT8: return sizeof (TString<int8_t>);
00085 case PT_UINT8: return sizeof (TString<uint8_t>);
00086 case PT_INT16: return sizeof (TString<int16_t>);
00087 case PT_UINT16: return sizeof (TString<uint16_t>);
00088 case PT_INT32: return sizeof (TString<int32_t>);
00089 case PT_UINT32: return sizeof (TString<uint32_t>);
00090 case PT_INT64: return sizeof (TString<int64_t>);
00091 case PT_UINT64: return sizeof (TString<uint64_t>);
00092 case PT_FLOAT32: return sizeof (TString<float32_t>);
00093 case PT_FLOAT64: return sizeof (TString<float64_t>);
00094 case PT_FLOATMAX: return sizeof (TString<floatmax_t>);
00095 case PT_SGOBJECT: return -1;
00096 }
00097 break;
00098 case ST_SPARSE:
00099 switch (m_ptype) {
00100 case PT_BOOL: return sizeof (TSparse<bool>);
00101 case PT_CHAR: return sizeof (TSparse<char>);
00102 case PT_INT8: return sizeof (TSparse<int8_t>);
00103 case PT_UINT8: return sizeof (TSparse<uint8_t>);
00104 case PT_INT16: return sizeof (TSparse<int16_t>);
00105 case PT_UINT16: return sizeof (TSparse<uint16_t>);
00106 case PT_INT32: return sizeof (TSparse<int32_t>);
00107 case PT_UINT32: return sizeof (TSparse<uint32_t>);
00108 case PT_INT64: return sizeof (TSparse<int64_t>);
00109 case PT_UINT64: return sizeof (TSparse<uint64_t>);
00110 case PT_FLOAT32: return sizeof (TSparse<float32_t>);
00111 case PT_FLOAT64: return sizeof (TSparse<float64_t>);
00112 case PT_FLOATMAX: return sizeof (TSparse<floatmax_t>);
00113 case PT_SGOBJECT: return -1;
00114 }
00115 break;
00116 }
00117
00118 return -1;
00119 }
00120
00121 size_t
00122 TSGDataType::sizeof_ptype(void) const
00123 {
00124 switch (m_ptype) {
00125 case PT_BOOL: return sizeof (bool);
00126 case PT_CHAR: return sizeof (char);
00127 case PT_INT8: return sizeof (int8_t);
00128 case PT_UINT8: return sizeof (uint8_t);
00129 case PT_INT16: return sizeof (int16_t);
00130 case PT_UINT16: return sizeof (uint16_t);
00131 case PT_INT32: return sizeof (int32_t);
00132 case PT_UINT32: return sizeof (uint32_t);
00133 case PT_INT64: return sizeof (int64_t);
00134 case PT_UINT64: return sizeof (uint64_t);
00135 case PT_FLOAT32: return sizeof (float32_t);
00136 case PT_FLOAT64: return sizeof (float64_t);
00137 case PT_FLOATMAX: return sizeof (floatmax_t);
00138 case PT_SGOBJECT: return sizeof (CSGObject*);
00139 }
00140
00141 return -1;
00142 }
00143
00144 size_t
00145 TSGDataType::sizeof_sparseentry(EPrimitiveType ptype)
00146 {
00147 switch (ptype) {
00148 case PT_BOOL: return sizeof (TSparseEntry<bool>);
00149 case PT_CHAR: return sizeof (TSparseEntry<char>);
00150 case PT_INT8: return sizeof (TSparseEntry<int8_t>);
00151 case PT_UINT8: return sizeof (TSparseEntry<uint8_t>);
00152 case PT_INT16: return sizeof (TSparseEntry<int16_t>);
00153 case PT_UINT16: return sizeof (TSparseEntry<uint16_t>);
00154 case PT_INT32: return sizeof (TSparseEntry<int32_t>);
00155 case PT_UINT32: return sizeof (TSparseEntry<uint32_t>);
00156 case PT_INT64: return sizeof (TSparseEntry<int64_t>);
00157 case PT_UINT64: return sizeof (TSparseEntry<uint64_t>);
00158 case PT_FLOAT32: return sizeof (TSparseEntry<float32_t>);
00159 case PT_FLOAT64: return sizeof (TSparseEntry<float64_t>);
00160 case PT_FLOATMAX: return sizeof (TSparseEntry<floatmax_t>);
00161 case PT_SGOBJECT: return -1;
00162 }
00163
00164 return -1;
00165 }
00166
00167 #define ENTRY_OFFSET(k, type) \
00168 ((char*) &((TSparseEntry<type>*) (k))->entry - (char*) (k))
00169 size_t
00170 TSGDataType::offset_sparseentry(EPrimitiveType ptype)
00171 {
00172 size_t result = -1; void* x = &result;
00173
00174 switch (ptype) {
00175 case PT_BOOL: result = ENTRY_OFFSET(x, bool); break;
00176 case PT_CHAR: result = ENTRY_OFFSET(x, char); break;
00177 case PT_INT8: result = ENTRY_OFFSET(x, int8_t); break;
00178 case PT_UINT8: result = ENTRY_OFFSET(x, uint8_t); break;
00179 case PT_INT16: result = ENTRY_OFFSET(x, int16_t); break;
00180 case PT_UINT16: result = ENTRY_OFFSET(x, uint16_t); break;
00181 case PT_INT32: result = ENTRY_OFFSET(x, int32_t); break;
00182 case PT_UINT32: result = ENTRY_OFFSET(x, uint32_t); break;
00183 case PT_INT64: result = ENTRY_OFFSET(x, int64_t); break;
00184 case PT_UINT64: result = ENTRY_OFFSET(x, uint64_t); break;
00185 case PT_FLOAT32: result = ENTRY_OFFSET(x, float32_t); break;
00186 case PT_FLOAT64: result = ENTRY_OFFSET(x, float64_t); break;
00187 case PT_FLOATMAX: result = ENTRY_OFFSET(x, floatmax_t); break;
00188 case PT_SGOBJECT: return -1;
00189 }
00190
00191 return result;
00192 }
00193
00194 void
00195 TSGDataType::stype_to_string(char* dest, EStructType stype,
00196 EPrimitiveType ptype, size_t n)
00197 {
00198 char* p = dest;
00199
00200 switch (stype) {
00201 case ST_NONE: strncpy(p, "", n); break;
00202 case ST_STRING: strncpy(p, "String<", n); break;
00203 case ST_SPARSE: strncpy(p, "Sparse<", n); break;
00204 }
00205
00206 size_t np = strlen(p);
00207 ptype_to_string(p + np, ptype, n - np - 2);
00208
00209 switch (stype) {
00210 case ST_NONE: break;
00211 case ST_STRING: case ST_SPARSE:
00212 strcat(p, ">"); break;
00213 }
00214 }
00215
00216 void
00217 TSGDataType::ptype_to_string(char* dest, EPrimitiveType ptype,
00218 size_t n)
00219 {
00220 char* p = dest;
00221
00222 switch (ptype) {
00223 case PT_BOOL: strncpy(p, "bool", n); break;
00224 case PT_CHAR: strncpy(p, "char", n); break;
00225 case PT_INT8: strncpy(p, "int8", n); break;
00226 case PT_UINT8: strncpy(p, "uint8", n); break;
00227 case PT_INT16: strncpy(p, "int16", n); break;
00228 case PT_UINT16: strncpy(p, "uint16", n); break;
00229 case PT_INT32: strncpy(p, "int32", n); break;
00230 case PT_UINT32: strncpy(p, "uint32", n); break;
00231 case PT_INT64: strncpy(p, "int64", n); break;
00232 case PT_UINT64: strncpy(p, "uint64", n); break;
00233 case PT_FLOAT32: strncpy(p, "float32", n); break;
00234 case PT_FLOAT64: strncpy(p, "float64", n); break;
00235 case PT_FLOATMAX: strncpy(p, "floatmax", n); break;
00236 case PT_SGOBJECT: strncpy(p, "SGSerializable*", n); break;
00237 }
00238 }
00239
00240 bool
00241 TSGDataType::string_to_ptype(EPrimitiveType* result, const char* str)
00242 {
00243 if (strcmp(str, "bool") == 0) {
00244 *result = PT_BOOL; return true; }
00245 if (strcmp(str, "char") == 0) {
00246 *result = PT_CHAR; return true; }
00247 if (strcmp(str, "int8") == 0) {
00248 *result = PT_INT8; return true; }
00249 if (strcmp(str, "uint8") == 0) {
00250 *result = PT_UINT8; return true; }
00251 if (strcmp(str, "int16") == 0) {
00252 *result = PT_INT16; return true; }
00253 if (strcmp(str, "uint16") == 0) {
00254 *result = PT_UINT16; return true; }
00255 if (strcmp(str, "int32") == 0) {
00256 *result = PT_INT32; return true; }
00257 if (strcmp(str, "uint32") == 0) {
00258 *result = PT_UINT32; return true; }
00259 if (strcmp(str, "int64") == 0) {
00260 *result = PT_INT64; return true; }
00261 if (strcmp(str, "uint64") == 0) {
00262 *result = PT_UINT64; return true; }
00263 if (strcmp(str, "float32") == 0) {
00264 *result = PT_FLOAT32; return true; }
00265 if (strcmp(str, "float64") == 0) {
00266 *result = PT_FLOAT64; return true; }
00267 if (strcmp(str, "floatmax") == 0) {
00268 *result = PT_FLOATMAX; return true; }
00269 if (strcmp(str, "SGSerializable*") == 0) {
00270 *result = PT_SGOBJECT; return true; }
00271
00272
00273 switch (*result) {
00274 case PT_BOOL: case PT_CHAR: case PT_INT8: case PT_UINT8:
00275 case PT_INT16: case PT_UINT16: case PT_INT32: case PT_UINT32:
00276 case PT_INT64: case PT_UINT64: case PT_FLOAT32: case PT_FLOAT64:
00277 case PT_FLOATMAX: case PT_SGOBJECT: break;
00278 }
00279
00280 return false;
00281 }