SHOGUN
v2.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
lib
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
>
18
#include <
shogun/lib/SGReferencedData.h
>
19
#include <
shogun/lib/SGSparseVector.h
>
20
21
namespace
shogun
22
{
24
template
<
class
T>
class
SGSparseMatrix
:
public
SGReferencedData
25
{
26
public
:
28
SGSparseMatrix
() :
SGReferencedData
()
29
{
30
init_data
();
31
}
32
34
SGSparseMatrix
(
SGSparseVector<T>
* vecs,
index_t
num_feat,
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
{
47
sparse_matrix
=
SG_MALLOC
(
SGSparseVector<T>
,
num_vectors
);
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
56
SGSparseMatrix
(
const
SGSparseMatrix
&orig) :
SGReferencedData
(orig)
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
74
inline
SGSparseVector<T>
&
operator[]
(
index_t
index)
75
{
76
return
sparse_matrix
[index];
77
}
78
79
protected
:
80
82
virtual
void
copy_data
(
const
SGReferencedData
& orig)
83
{
84
sparse_matrix
= ((
SGSparseMatrix
*)(&orig))->
sparse_matrix
;
85
num_vectors
= ((
SGSparseMatrix
*)(&orig))->
num_vectors
;
86
num_features
= ((
SGSparseMatrix
*)(&orig))->
num_features
;
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
103
SG_FREE
(
sparse_matrix
);
104
num_vectors = 0;
105
num_features
= 0;
106
}
107
108
public
:
109
111
index_t
num_vectors
;
112
114
index_t
num_features
;
115
117
SGSparseVector<T>
*
sparse_matrix
;
118
119
};
120
}
121
#endif // __SGSPARSEMATRIX_H__
SHOGUN
Machine Learning Toolbox - Documentation