00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __DATATYPE_H__
00012 #define __DATATYPE_H__
00013
00014 #include <shogun/lib/common.h>
00015
00016 #define PT_NOT_GENERIC PT_SGOBJECT
00017 #define PT_LONGEST floatmax_t
00018
00019 namespace shogun
00020 {
00021
00022 template<class T> class CCache;
00023
00025 typedef int32_t index_t;
00026
00028 template<class T> class SGVector
00029 {
00030 public:
00032 SGVector() : vector(NULL), vlen(0), do_free(false) { }
00033
00035 SGVector(T* v, index_t len, bool free_vec=false)
00036 : vector(v), vlen(len), do_free(free_vec) { }
00037
00039 SGVector(index_t len, bool free_vec=false)
00040 : vlen(len), do_free(free_vec)
00041 {
00042 vector=SG_MALLOC(T, len);
00043 }
00044
00046 SGVector(const SGVector &orig)
00047 : vector(orig.vector), vlen(orig.vlen), do_free(orig.do_free) { }
00048
00053 static SGVector get_vector(SGVector &src, bool own=true)
00054 {
00055 if (!own)
00056 return src;
00057
00058 src.do_free=false;
00059 return SGVector(src.vector, src.vlen);
00060 }
00061
00063 virtual void free_vector()
00064 {
00065 if (do_free)
00066 SG_FREE(vector);
00067
00068 vector=NULL;
00069 do_free=false;
00070 vlen=0;
00071 }
00072
00074 virtual void destroy_vector()
00075 {
00076 do_free=true;
00077 free_vector();
00078 }
00079
00080 public:
00082 T* vector;
00084 index_t vlen;
00086 bool do_free;
00087 };
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00136 template<class T> class SGMatrix
00137 {
00138 public:
00140 SGMatrix() : matrix(NULL), num_rows(0), num_cols(0), do_free(false) { }
00141
00143 SGMatrix(T* m, index_t nrows, index_t ncols, bool free_mat=false)
00144 : matrix(m), num_rows(nrows), num_cols(ncols), do_free(free_mat) { }
00145
00147 SGMatrix(index_t nrows, index_t ncols, bool free_mat=false)
00148 : num_rows(nrows), num_cols(ncols), do_free(free_mat)
00149 {
00150 matrix=SG_MALLOC(T, nrows*ncols);
00151 }
00152
00154 SGMatrix(const SGMatrix &orig)
00155 : matrix(orig.matrix), num_rows(orig.num_rows),
00156 num_cols(orig.num_cols), do_free(orig.do_free) { }
00157
00159 virtual void free_matrix()
00160 {
00161 if (do_free)
00162 SG_FREE(matrix);
00163
00164 matrix=NULL;
00165 do_free=false;
00166 num_rows=0;
00167 num_cols=0;
00168 }
00169
00171 virtual void destroy_matrix()
00172 {
00173 do_free=true;
00174 free_matrix();
00175 }
00176
00177 public:
00179 T* matrix;
00181 index_t num_rows;
00183 index_t num_cols;
00185 bool do_free;
00186 };
00187
00189 template<class T> class SGNDArray
00190 {
00191 public:
00193 SGNDArray() : array(NULL), dims(NULL), num_dims(0) { }
00194
00196 SGNDArray(T* a, index_t* d, index_t nd)
00197 : array(a), dims(d), num_dims(nd) { }
00198
00200 SGNDArray(const SGNDArray &orig)
00201 : array(orig.array), dims(orig.dims), num_dims(orig.num_dims) { }
00202
00203 public:
00205 T* array;
00207 index_t* dims;
00209 index_t num_dims;
00210 };
00211
00213 template<class T> struct SGString
00214 {
00215 public:
00217 SGString() : string(NULL), slen(0), do_free(false) { }
00218
00220 SGString(T* s, index_t l, bool free_s=false)
00221 : string(s), slen(l), do_free(free_s) { }
00222
00224 SGString(SGVector<T> v)
00225 : string(v.vector), slen(v.vlen), do_free(v.do_free) { }
00226
00228 SGString(index_t len, bool free_s=false) :
00229 slen(len), do_free(free_s)
00230 {
00231 string=SG_MALLOC(T, len);
00232 }
00233
00235 SGString(const SGString &orig)
00236 : string(orig.string), slen(orig.slen), do_free(orig.do_free) { }
00237
00239 void free_string()
00240 {
00241 if (do_free)
00242 SG_FREE(string);
00243
00244 string=NULL;
00245 do_free=false;
00246 slen=0;
00247 }
00248
00250 void destroy_string()
00251 {
00252 do_free=true;
00253 free_string();
00254 }
00255
00256 public:
00258 T* string;
00260 index_t slen;
00262 bool do_free;
00263 };
00264
00266 template <class T> struct SGStringList
00267 {
00268 public:
00270 SGStringList() : num_strings(0), max_string_length(0), strings(NULL),
00271 do_free(false) { }
00272
00274 SGStringList(SGString<T>* s, index_t num_s, index_t max_length,
00275 bool free_strings=false) : num_strings(num_s),
00276 max_string_length(max_length), strings(s), do_free(free_strings) { }
00277
00279 SGStringList(index_t num_s, index_t max_length, bool free_strings=false)
00280 : num_strings(num_s), max_string_length(max_length),
00281 do_free(free_strings)
00282 {
00283 strings=SG_MALLOC(SGString<T>, num_strings);
00284 }
00285
00287 SGStringList(const SGStringList &orig) :
00288 num_strings(orig.num_strings),
00289 max_string_length(orig.max_string_length),
00290 strings(orig.strings), do_free(orig.do_free) { }
00291
00293 void free_list()
00294 {
00295 if (do_free)
00296 SG_FREE(strings);
00297
00298 strings=NULL;
00299 do_free=false;
00300 num_strings=0;
00301 max_string_length=0;
00302 }
00303
00305 void destroy_list()
00306 {
00307 do_free=true;
00308 free_list();
00309 }
00310
00311 public:
00313 index_t num_strings;
00314
00316 index_t max_string_length;
00317
00319 SGString<T>* strings;
00320
00322 bool do_free;
00323 };
00324
00326 template <class T> struct SGSparseVectorEntry
00327 {
00329 index_t feat_index;
00331 T entry;
00332 };
00333
00335 template <class T> struct SGSparseVector
00336 {
00337 public:
00339 SGSparseVector() :
00340 vec_index(0), num_feat_entries(0), features(NULL), do_free(false) {}
00341
00343 SGSparseVector(SGSparseVectorEntry<T>* feats, index_t num_entries,
00344 index_t index, bool free_v=false) :
00345 vec_index(index), num_feat_entries(num_entries), features(feats),
00346 do_free(free_v) {}
00347
00349 SGSparseVector(index_t num_entries, index_t index, bool free_v=false) :
00350 vec_index(index), num_feat_entries(num_entries), do_free(free_v)
00351 {
00352 features=SG_MALLOC(SGSparseVectorEntry<T>, num_feat_entries);
00353 }
00354
00356 SGSparseVector(const SGSparseVector& orig) :
00357 vec_index(orig.vec_index), num_feat_entries(orig.num_feat_entries),
00358 features(orig.features), do_free(orig.do_free) {}
00359
00361 void free_vector()
00362 {
00363 if (do_free)
00364 SG_FREE(features);
00365
00366 features=NULL;
00367 do_free=false;
00368 vec_index=0;
00369 num_feat_entries=0;
00370 }
00371
00373 void destroy_vector()
00374 {
00375 do_free=true;
00376 free_vector();
00377 }
00378
00379 public:
00381 index_t vec_index;
00382
00384 index_t num_feat_entries;
00385
00387 SGSparseVectorEntry<T>* features;
00388
00390 bool do_free;
00391 };
00392
00394 template <class T> class SGSparseMatrix
00395 {
00396 public:
00398 SGSparseMatrix() :
00399 num_vectors(0), num_features(0), sparse_matrix(NULL),
00400 do_free(false) { }
00401
00402
00404 SGSparseMatrix(SGSparseVector<T>* vecs, index_t num_feat,
00405 index_t num_vec, bool free_m=false) :
00406 num_vectors(num_vec), num_features(num_feat),
00407 sparse_matrix(vecs), do_free(free_m) { }
00408
00410 SGSparseMatrix(index_t num_vec, index_t num_feat, bool free_m=false) :
00411 num_vectors(num_vec), num_features(num_feat), do_free(free_m)
00412 {
00413 sparse_matrix=SG_MALLOC(SGSparseVector<T>, num_vectors);
00414 }
00415
00417 SGSparseMatrix(const SGSparseMatrix &orig) :
00418 num_vectors(orig.num_vectors), num_features(orig.num_features),
00419 sparse_matrix(orig.sparse_matrix), do_free(orig.do_free) { }
00420
00422 void free_matrix()
00423 {
00424 if (do_free)
00425 SG_FREE(sparse_matrix);
00426
00427 sparse_matrix=NULL;
00428 do_free=false;
00429 num_vectors=0;
00430 num_features=0;
00431 }
00432
00434 void own_matrix()
00435 {
00436 for (int32_t i=0; i<num_vectors; i++)
00437 sparse_matrix[i].do_free=false;
00438
00439 do_free=false;
00440 }
00441
00443 void destroy_matrix()
00444 {
00445 do_free=true;
00446 free_matrix();
00447 }
00448
00449 public:
00451 int32_t num_vectors;
00452
00454 int32_t num_features;
00455
00457 SGSparseVector<T>* sparse_matrix;
00458
00460 bool do_free;
00461 };
00462
00463 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00464 enum EContainerType
00465 {
00466 CT_SCALAR=0,
00467 CT_VECTOR=1,
00468 CT_MATRIX=2,
00469 CT_NDARRAY=3,
00470 CT_SGVECTOR=4,
00471 CT_SGMATRIX=5
00472 };
00473
00474 enum EStructType
00475 {
00476 ST_NONE=0,
00477 ST_STRING=1,
00478 ST_SPARSE=2
00479 };
00480
00481 enum EPrimitiveType
00482 {
00483 PT_BOOL=0,
00484 PT_CHAR=1,
00485 PT_INT8=2,
00486 PT_UINT8=3,
00487 PT_INT16=4,
00488 PT_UINT16=5,
00489 PT_INT32=6,
00490 PT_UINT32=7,
00491 PT_INT64=8,
00492 PT_UINT64=9,
00493 PT_FLOAT32=10,
00494 PT_FLOAT64=11,
00495 PT_FLOATMAX=12,
00496 PT_SGOBJECT=13
00497 };
00498 #endif
00499
00501 struct TSGDataType
00502 {
00504 EContainerType m_ctype;
00506 EStructType m_stype;
00508 EPrimitiveType m_ptype;
00509
00511 index_t *m_length_y;
00513 index_t *m_length_x;
00514
00520 explicit TSGDataType(EContainerType ctype, EStructType stype,
00521 EPrimitiveType ptype);
00528 explicit TSGDataType(EContainerType ctype, EStructType stype,
00529 EPrimitiveType ptype, index_t* length);
00537 explicit TSGDataType(EContainerType ctype, EStructType stype,
00538 EPrimitiveType ptype, index_t* length_y,
00539 index_t* length_x);
00540
00542 bool operator==(const TSGDataType& a);
00546 inline bool operator!=(const TSGDataType& a)
00547 {
00548 return !(*this == a);
00549 }
00550
00555 void to_string(char* dest, size_t n) const;
00556
00558 size_t sizeof_stype(void) const;
00560 size_t sizeof_ptype(void) const;
00561
00565 static size_t sizeof_sparseentry(EPrimitiveType ptype);
00566
00570 static size_t offset_sparseentry(EPrimitiveType ptype);
00571
00578 static void stype_to_string(char* dest, EStructType stype,
00579 EPrimitiveType ptype, size_t n);
00585 static void ptype_to_string(char* dest, EPrimitiveType ptype,
00586 size_t n);
00591 static bool string_to_ptype(EPrimitiveType* ptype,
00592 const char* str);
00593
00597 size_t get_size();
00598
00602 index_t get_num_elements();
00603 };
00604 }
00605 #endif