SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SGSparseMatrix.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) 2012 Fernando José Iglesias García
8  * Written (W) 2010,2012 Soeren Sonnenburg
9  * Copyright (C) 2010 Berlin Institute of Technology
10  * Copyright (C) 2012 Soeren Sonnenburg
11  */
12 
13 #ifndef __SGSPARSEMATRIX_H__
14 #define __SGSPARSEMATRIX_H__
15 
16 #include <shogun/lib/config.h>
17 #include <shogun/lib/DataType.h>
20 
21 namespace shogun
22 {
24 template <class T> class SGSparseMatrix : public SGReferencedData
25 {
26  public:
29  {
30  init_data();
31  }
32 
35  index_t num_vec, bool ref_counting=true) :
36  SGReferencedData(ref_counting),
37  num_vectors(num_vec), num_features(num_feat),
38  sparse_matrix(vecs)
39  {
40  }
41 
43  SGSparseMatrix(index_t num_vec, index_t num_feat, bool ref_counting=true) :
44  SGReferencedData(ref_counting),
45  num_vectors(num_vec), num_features(num_feat)
46  {
48  for (int32_t i=0; i<num_vectors; i++)
49  {
50  new (&sparse_matrix[i]) SGSparseVector<T>();
51  sparse_matrix[i] = SGSparseVector<T>(num_feat);
52  }
53  }
54 
57  {
58  copy_data(orig);
59  }
60 
62  virtual ~SGSparseMatrix()
63  {
64  unref();
65  }
66 
68  inline const SGSparseVector<T>& operator[](index_t index) const
69  {
70  return sparse_matrix[index];
71  }
72 
75  {
76  return sparse_matrix[index];
77  }
78 
79 protected:
80 
82  virtual void copy_data(const SGReferencedData& orig)
83  {
85  num_vectors = ((SGSparseMatrix*)(&orig))->num_vectors;
87  }
88 
90  virtual void init_data()
91  {
92  sparse_matrix = NULL;
93  num_vectors = 0;
94  num_features = 0;
95  }
96 
98  virtual void free_data()
99  {
100  for (int32_t i=0; i<num_vectors; i++)
101  (&sparse_matrix[i])->~SGSparseVector<T>();
102 
104  num_vectors = 0;
105  num_features = 0;
106  }
107 
108 public:
109 
112 
115 
118 
119 };
120 }
121 #endif // __SGSPARSEMATRIX_H__

SHOGUN Machine Learning Toolbox - Documentation