SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DataType.h
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 #ifndef __DATATYPE_H__
12 #define __DATATYPE_H__
13 
14 #include <shogun/lib/common.h>
15 //#include <shogun/mathematics/Math.h>
16 #include <shogun/io/SGIO.h>
17 
18 #define PT_NOT_GENERIC PT_SGOBJECT
19 #define PT_LONGEST floatmax_t
20 
21 namespace shogun
22 {
23 
24 //class CMath;
25 template<class T> class CCache;
26 
28 typedef int32_t index_t;
29 
31 template<class T> class SGVector
32 {
33  public:
35  SGVector() : vector(NULL), vlen(0), do_free(false) { }
36 
38  SGVector(T* v, index_t len, bool free_vec=false)
39  : vector(v), vlen(len), do_free(free_vec) { }
40 
42  SGVector(index_t len, bool free_vec=false)
43  : vlen(len), do_free(free_vec)
44  {
45  vector=SG_MALLOC(T, len);
46  }
47 
49  SGVector(const SGVector &orig)
50  : vector(orig.vector), vlen(orig.vlen), do_free(orig.do_free) { }
51 
53  virtual ~SGVector()
54  {
55  }
56 
61  static SGVector get_vector(SGVector &src, bool own=true)
62  {
63  if (!own)
64  return src;
65 
66  src.do_free=false;
67  return SGVector(src.vector, src.vlen);
68  }
69 
71  void zero()
72  {
73  if (vector && vlen)
74  set_const(0);
75  }
76 
78  void set_const(T const_elem)
79  {
80  for (index_t i=0; i<vlen; i++)
81  vector[i]=const_elem ;
82  }
83 
85  void range_fill(T start=0)
86  {
87  range_fill_vector(vector, vlen, start);
88  }
89 
91  void random(T min_value, T max_value)
92  {
93  random_vector(vector, vlen, min_value, max_value);
94  }
95 
97  void randperm()
98  {
100  }
101 
103  template <class VT>
104  static VT* clone_vector(const VT* vec, int32_t len)
105  {
106  VT* result = SG_MALLOC(VT, len);
107  for (int32_t i=0; i<len; i++)
108  result[i]=vec[i];
109 
110  return result;
111  }
112 
114  template <class VT>
115  static void fill_vector(VT* vec, int32_t len, VT value)
116  {
117  for (int32_t i=0; i<len; i++)
118  vec[i]=value;
119  }
120 
122  template <class VT>
123  static void range_fill_vector(VT* vec, int32_t len, VT start=0)
124  {
125  for (int32_t i=0; i<len; i++)
126  vec[i]=i+start;
127  }
128 
130  template <class VT>
131  static void random_vector(VT* vec, int32_t len, VT min_value, VT max_value)
132  {
133  //FIXME for (int32_t i=0; i<len; i++)
134  //FIXME vec[i]=CMath::random(min_value, max_value);
135  }
136 
138  template <class VT>
139  static void randperm(VT* perm, int32_t n)
140  {
141  for (int32_t i = 0; i < n; i++)
142  perm[i] = i;
143  permute(perm,n);
144  }
145 
147  template <class VT>
148  static void permute(VT* perm, int32_t n)
149  {
150  //FIXME for (int32_t i = 0; i < n; i++)
151  //FIXME CMath::swap(perm[random(0, n - 1)], perm[i]);
152  }
153 
159  const T& get_element(index_t index)
160  {
161  ASSERT(vector && (index>=0) && (index<vlen));
162  return vector[index];
163  }
164 
171  void set_element(const T& p_element, index_t index)
172  {
173  ASSERT(vector && (index>=0) && (index<vlen));
174  vector[index]=p_element;
175  }
176 
182  void resize_vector(int32_t n)
183  {
184  vector=SG_REALLOC(T, vector, n);
185 
186  if (n > vlen)
187  memset(&vector[vlen], 0, (n-vlen)*sizeof(T));
188  vlen=n;
189  }
190 
196  inline const T& operator[](index_t index) const
197  {
198  return vector[index];
199  }
200 
206  inline T& operator[](index_t index)
207  {
208  return vector[index];
209  }
210 
212  virtual void free_vector()
213  {
214  if (do_free)
215  SG_FREE(vector);
216 
217  vector=NULL;
218  do_free=false;
219  vlen=0;
220  }
221 
223  virtual void destroy_vector()
224  {
225  do_free=true;
226  free_vector();
227  }
228 
230  void display_size() const
231  {
232  SG_SPRINT("SGVector '%p' of size: %d\n", vector, vlen);
233  }
234 
236  void display_vector() const
237  {
238  display_size();
239  for (int32_t i=0; i<vlen; i++)
240  SG_SPRINT("%10.10g,", (float64_t) vector[i]);
241  SG_SPRINT("\n");
242  }
243 
244  public:
246  T* vector;
250  bool do_free;
251 };
252 
253 //template<class T> class SGCachedVector : public SGVector<T>
254 //{
255 // public:
256 // /** default constructor */
257 // SGCachedVector(CCache<T>* c, index_t i)
258 // : SGVector<T>(), cache(c), idx(i)
259 // {
260 // }
261 //
262 // /** constructor for setting params */
263 // SGCachedVector(CCache<T>* c, index_t i,
264 // T* v, index_t len, bool free_vec=false)
265 // : SGVector<T>(v, len, free_vec), cache(c), idx(i)
266 // {
267 // }
268 //
269 // /** constructor to create new vector in memory */
270 // SGCachedVector(CCache<T>* c, index_t i, index_t len, bool free_vec=false) :
271 // SGVector<T>(len, free_vec), cache(c), idx(i)
272 // {
273 // }
274 //
275 // /** free vector */
276 // virtual void free_vector()
277 // {
278 // //clean up cache fixme
279 // SGVector<T>::free_vector();
280 // }
281 //
282 // /** destroy vector */
283 // virtual void destroy_vector()
284 // {
285 // //clean up cache fixme
286 // SGVector<T>::destroy_vector();
287 // if (cache)
288 // cache->unlock_entry(idx);
289 // }
290 //
291 // public:
292 // /** idx */
293 // index_t idx;
294 //
295 // /** cache */
296 // CCache<T>* cache;
297 //};
298 
300 template<class T> class SGMatrix
301 {
302  public:
304  SGMatrix() : matrix(NULL), num_rows(0), num_cols(0), do_free(false) { }
305 
307  SGMatrix(T* m, index_t nrows, index_t ncols, bool free_mat=false)
308  : matrix(m), num_rows(nrows), num_cols(ncols), do_free(free_mat) { }
309 
311  SGMatrix(index_t nrows, index_t ncols, bool free_mat=false)
312  : num_rows(nrows), num_cols(ncols), do_free(free_mat)
313  {
314  matrix=SG_MALLOC(T, nrows*ncols);
315  }
316 
318  SGMatrix(const SGMatrix &orig)
319  : matrix(orig.matrix), num_rows(orig.num_rows),
320  num_cols(orig.num_cols), do_free(orig.do_free) { }
321 
323  virtual ~SGMatrix()
324  {
325  }
326 
328  virtual void free_matrix()
329  {
330  if (do_free)
331  SG_FREE(matrix);
332 
333  matrix=NULL;
334  do_free=false;
335  num_rows=0;
336  num_cols=0;
337  }
338 
340  virtual void destroy_matrix()
341  {
342  do_free=true;
343  free_matrix();
344  }
345 
349  inline const T& operator[](index_t index) const
350  {
351  return matrix[index];
352  }
353 
357  inline T& operator[](index_t index)
358  {
359  return matrix[index];
360  }
361 
362  public:
364  T* matrix;
370  bool do_free;
371 };
372 
374 template<class T> class SGNDArray
375 {
376  public:
378  SGNDArray() : array(NULL), dims(NULL), num_dims(0) { }
379 
381  SGNDArray(T* a, index_t* d, index_t nd)
382  : array(a), dims(d), num_dims(nd) { }
383 
385  SGNDArray(const SGNDArray &orig)
386  : array(orig.array), dims(orig.dims), num_dims(orig.num_dims) { }
387 
388  public:
390  T* array;
395 };
396 
398 template<class T> class SGString
399 {
400 public:
402  SGString() : string(NULL), slen(0), do_free(false) { }
403 
405  SGString(T* s, index_t l, bool free_s=false)
406  : string(s), slen(l), do_free(free_s) { }
407 
410  : string(v.vector), slen(v.vlen), do_free(v.do_free) { }
411 
413  SGString(index_t len, bool free_s=false) :
414  slen(len), do_free(free_s)
415  {
416  string=SG_MALLOC(T, len);
417  }
418 
420  SGString(const SGString &orig)
421  : string(orig.string), slen(orig.slen), do_free(orig.do_free) { }
422 
424  void free_string()
425  {
426  if (do_free)
427  SG_FREE(string);
428 
429  string=NULL;
430  do_free=false;
431  slen=0;
432  }
433 
436  {
437  do_free=true;
438  free_string();
439  }
440 
441 public:
443  T* string;
447  bool do_free;
448 };
449 
451 template <class T> struct SGStringList
452 {
453 public:
456  do_free(false) { }
457 
459  SGStringList(SGString<T>* s, index_t num_s, index_t max_length,
460  bool free_strings=false) : num_strings(num_s),
461  max_string_length(max_length), strings(s), do_free(free_strings) { }
462 
464  SGStringList(index_t num_s, index_t max_length, bool free_strings=false)
465  : num_strings(num_s), max_string_length(max_length),
466  do_free(free_strings)
467  {
469  }
470 
472  SGStringList(const SGStringList &orig) :
473  num_strings(orig.num_strings),
475  strings(orig.strings), do_free(orig.do_free) { }
476 
478  void free_list()
479  {
480  if (do_free)
481  SG_FREE(strings);
482 
483  strings=NULL;
484  do_free=false;
485  num_strings=0;
487  }
488 
491  {
492  do_free=true;
493  free_list();
494  }
495 
496 public:
499 
502 
505 
507  bool do_free;
508 };
509 
511 template <class T> struct SGSparseVectorEntry
512 {
516  T entry;
517 };
518 
520 template <class T> class SGSparseVector
521 {
522 public:
525  vec_index(0), num_feat_entries(0), features(NULL), do_free(false) {}
526 
529  index_t index, bool free_v=false) :
530  vec_index(index), num_feat_entries(num_entries), features(feats),
531  do_free(free_v) {}
532 
534  SGSparseVector(index_t num_entries, index_t index, bool free_v=false) :
535  vec_index(index), num_feat_entries(num_entries), do_free(free_v)
536  {
538  }
539 
543  features(orig.features), do_free(orig.do_free) {}
544 
546  void free_vector()
547  {
548  if (do_free)
549  SG_FREE(features);
550 
551  features=NULL;
552  do_free=false;
553  vec_index=0;
555  }
556 
559  {
560  do_free=true;
561  free_vector();
562  }
563 
564 public:
567 
570 
573 
575  bool do_free;
576 };
577 
579 template <class T> class SGSparseMatrix
580 {
581  public:
584  num_vectors(0), num_features(0), sparse_matrix(NULL),
585  do_free(false) { }
586 
587 
590  index_t num_vec, bool free_m=false) :
591  num_vectors(num_vec), num_features(num_feat),
592  sparse_matrix(vecs), do_free(free_m) { }
593 
595  SGSparseMatrix(index_t num_vec, index_t num_feat, bool free_m=false) :
596  num_vectors(num_vec), num_features(num_feat), do_free(free_m)
597  {
599  }
600 
604  sparse_matrix(orig.sparse_matrix), do_free(orig.do_free) { }
605 
607  void free_matrix()
608  {
609  if (do_free)
611 
612  sparse_matrix=NULL;
613  do_free=false;
614  num_vectors=0;
615  num_features=0;
616  }
617 
619  void own_matrix()
620  {
621  for (index_t i=0; i<num_vectors; i++)
622  sparse_matrix[i].do_free=false;
623 
624  do_free=false;
625  }
626 
629  {
630  do_free=true;
631  free_matrix();
632  }
633 
634  public:
637 
640 
643 
645  bool do_free;
646 };
647 
648 #ifndef DOXYGEN_SHOULD_SKIP_THIS
649 enum EContainerType
650 {
651  CT_SCALAR=0,
652  CT_VECTOR=1,
653  CT_MATRIX=2,
654  CT_NDARRAY=3,
655  CT_SGVECTOR=4,
656  CT_SGMATRIX=5
657 };
658 
659 enum EStructType
660 {
661  ST_NONE=0,
662  ST_STRING=1,
663  ST_SPARSE=2
664 };
665 
666 enum EPrimitiveType
667 {
668  PT_BOOL=0,
669  PT_CHAR=1,
670  PT_INT8=2,
671  PT_UINT8=3,
672  PT_INT16=4,
673  PT_UINT16=5,
674  PT_INT32=6,
675  PT_UINT32=7,
676  PT_INT64=8,
677  PT_UINT64=9,
678  PT_FLOAT32=10,
679  PT_FLOAT64=11,
680  PT_FLOATMAX=12,
681  PT_SGOBJECT=13
682 };
683 #endif
684 
687 {
689  EContainerType m_ctype;
691  EStructType m_stype;
693  EPrimitiveType m_ptype;
694 
699 
705  explicit TSGDataType(EContainerType ctype, EStructType stype,
706  EPrimitiveType ptype);
713  explicit TSGDataType(EContainerType ctype, EStructType stype,
714  EPrimitiveType ptype, index_t* length);
722  explicit TSGDataType(EContainerType ctype, EStructType stype,
723  EPrimitiveType ptype, index_t* length_y,
724  index_t* length_x);
725 
727  bool operator==(const TSGDataType& a);
731  inline bool operator!=(const TSGDataType& a)
732  {
733  return !(*this == a);
734  }
735 
740  void to_string(char* dest, size_t n) const;
741 
743  size_t sizeof_stype() const;
745  size_t sizeof_ptype() const;
746 
750  static size_t sizeof_sparseentry(EPrimitiveType ptype);
751 
755  static size_t offset_sparseentry(EPrimitiveType ptype);
756 
763  static void stype_to_string(char* dest, EStructType stype,
764  EPrimitiveType ptype, size_t n);
770  static void ptype_to_string(char* dest, EPrimitiveType ptype,
771  size_t n);
776  static bool string_to_ptype(EPrimitiveType* ptype,
777  const char* str);
778 
782  size_t get_size();
783 
788 };
789 }
790 #endif /* __DATATYPE_H__ */

SHOGUN Machine Learning Toolbox - Documentation