Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef __SGSPARSEMATRIX_H__
00014 #define __SGSPARSEMATRIX_H__
00015
00016 #include <shogun/lib/config.h>
00017 #include <shogun/lib/DataType.h>
00018 #include <shogun/lib/SGReferencedData.h>
00019 #include <shogun/lib/SGSparseVector.h>
00020
00021 namespace shogun
00022 {
00024 template <class T> class SGSparseMatrix : public SGReferencedData
00025 {
00026 public:
00028 SGSparseMatrix() : SGReferencedData()
00029 {
00030 init_data();
00031 }
00032
00034 SGSparseMatrix(SGSparseVector<T>* vecs, index_t num_feat,
00035 index_t num_vec, bool ref_counting=true) :
00036 SGReferencedData(ref_counting),
00037 num_vectors(num_vec), num_features(num_feat),
00038 sparse_matrix(vecs)
00039 {
00040 }
00041
00043 SGSparseMatrix(index_t num_vec, index_t num_feat, bool ref_counting=true) :
00044 SGReferencedData(ref_counting),
00045 num_vectors(num_vec), num_features(num_feat)
00046 {
00047 sparse_matrix=SG_MALLOC(SGSparseVector<T>, num_vectors);
00048 for (int32_t i=0; i<num_vectors; i++)
00049 {
00050 new (&sparse_matrix[i]) SGSparseVector<T>();
00051 sparse_matrix[i] = SGSparseVector<T>(num_feat);
00052 }
00053 }
00054
00056 SGSparseMatrix(const SGSparseMatrix &orig) : SGReferencedData(orig)
00057 {
00058 copy_data(orig);
00059 }
00060
00062 virtual ~SGSparseMatrix()
00063 {
00064 unref();
00065 }
00066
00068 inline const SGSparseVector<T>& operator[](index_t index) const
00069 {
00070 return sparse_matrix[index];
00071 }
00072
00074 inline SGSparseVector<T>& operator[](index_t index)
00075 {
00076 return sparse_matrix[index];
00077 }
00078
00079 protected:
00080
00082 virtual void copy_data(const SGReferencedData& orig)
00083 {
00084 sparse_matrix = ((SGSparseMatrix*)(&orig))->sparse_matrix;
00085 num_vectors = ((SGSparseMatrix*)(&orig))->num_vectors;
00086 num_features = ((SGSparseMatrix*)(&orig))->num_features;
00087 }
00088
00090 virtual void init_data()
00091 {
00092 sparse_matrix = NULL;
00093 num_vectors = 0;
00094 num_features = 0;
00095 }
00096
00098 virtual void free_data()
00099 {
00100 for (int32_t i=0; i<num_vectors; i++)
00101 (&sparse_matrix[i])->~SGSparseVector<T>();
00102
00103 SG_FREE(sparse_matrix);
00104 num_vectors = 0;
00105 num_features = 0;
00106 }
00107
00108 public:
00109
00111 index_t num_vectors;
00112
00114 index_t num_features;
00115
00117 SGSparseVector<T>* sparse_matrix;
00118
00119 };
00120 }
00121 #endif // __SGSPARSEMATRIX_H__