Skip to content

Commit

Permalink
split up SG* datatypes into separate files
Browse files Browse the repository at this point in the history
- fix includes accordingly
- drop direct access to do_free component of sgvector
- add some ref counting to sgvector
  • Loading branch information
Soeren Sonnenburg committed May 2, 2012
1 parent 4fbd7c5 commit 52b1bf0
Show file tree
Hide file tree
Showing 34 changed files with 1,028 additions and 890 deletions.
7 changes: 7 additions & 0 deletions src/interfaces/modular/Library.i
Expand Up @@ -27,6 +27,13 @@

/* Templated Datatype Classes */
%include <shogun/lib/DataType.h>
%include <shogun/lib/SGVector.h>
%include <shogun/lib/SGMatrix.h>
%include <shogun/lib/SGSparseVector.h>
%include <shogun/lib/SGSparseMatrix.h>
%include <shogun/lib/SGString.h>
%include <shogun/lib/SGStringList.h>
%include <shogun/lib/SGNDArray.h>
namespace shogun
{
#ifdef USE_BOOL
Expand Down
7 changes: 7 additions & 0 deletions src/interfaces/modular/Library_includes.i
@@ -1,5 +1,12 @@
%{
#include <shogun/lib/DataType.h>
#include <shogun/lib/SGVector.h>
#include <shogun/lib/SGMatrix.h>
#include <shogun/lib/SGSparseVector.h>
#include <shogun/lib/SGSparseMatrix.h>
#include <shogun/lib/SGString.h>
#include <shogun/lib/SGStringList.h>
#include <shogun/lib/SGNDArray.h>
#include <shogun/lib/Cache.h>
#include <shogun/lib/List.h>
#include <shogun/lib/Signal.h>
Expand Down
2 changes: 0 additions & 2 deletions src/interfaces/python_modular/swig_typemaps.i
Expand Up @@ -274,8 +274,6 @@ static bool vector_to_numpy(PyObject* &obj, SGVector<type> sg_vec, int typecode)
((PyArrayObject*) obj)->flags |= NPY_OWNDATA;
}

sg_vec.free_vector();

return descr!=NULL;
}

Expand Down
1 change: 1 addition & 0 deletions src/shogun/base/SGObject.h
Expand Up @@ -15,6 +15,7 @@
#include <shogun/lib/config.h>
#include <shogun/io/SGIO.h>
#include <shogun/lib/DataType.h>
#include <shogun/lib/SGStringList.h>
#include <shogun/lib/ShogunException.h>
#include <shogun/lib/memory.h>
#include <shogun/base/Parallel.h>
Expand Down
1 change: 1 addition & 0 deletions src/shogun/classifier/QDA.h
Expand Up @@ -18,6 +18,7 @@
#include <shogun/features/DotFeatures.h>
#include <shogun/features/SimpleFeatures.h>
#include <shogun/machine/Machine.h>
#include <shogun/lib/SGNDArray.h>

namespace shogun
{
Expand Down
8 changes: 2 additions & 6 deletions src/shogun/features/DotFeatures.cpp
Expand Up @@ -310,17 +310,13 @@ SGMatrix<float64_t> CDotFeatures::get_computed_dot_feature_matrix()

SGVector<float64_t> CDotFeatures::get_computed_dot_feature_vector(int32_t num)
{
SGVector<float64_t> v;

int32_t dim=get_dim_feature_space();
ASSERT(num>=0 && num<=get_num_vectors());
ASSERT(dim>0);

v.do_free=true;
v.vlen=dim;
v.vector=SG_MALLOC(float64_t, dim);
memset(v.vector, 0, dim*sizeof(float64_t));

SGVector<float64_t> v(dim);
v.zero();
add_to_dense_vec(1.0, num, v.vector, dim);
return v;
}
Expand Down
4 changes: 1 addition & 3 deletions src/shogun/features/Labels.cpp
Expand Up @@ -73,14 +73,12 @@ void CLabels::init()
SG_REF(m_subset_stack);
}

void CLabels::set_labels(const SGVector<float64_t>& v)
void CLabels::set_labels(const SGVector<float64_t> v)
{
if (m_subset_stack->has_subsets())
SG_ERROR("A subset is set, cannot set labels\n");

labels.free_vector();
labels=v;
labels.do_free=false;
}

bool CLabels::is_two_class_labeling()
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/Labels.h
Expand Up @@ -165,7 +165,7 @@ class CLabels : public CSGObject
*
* @param v labels
*/
void set_labels(const SGVector<float64_t>& v);
void set_labels(const SGVector<float64_t> v);

/**
* set all labels to +1
Expand Down
12 changes: 7 additions & 5 deletions src/shogun/features/SimpleFeatures.cpp
Expand Up @@ -164,9 +164,10 @@ template<class ST> SGVector<ST> CSimpleFeatures<ST>::get_feature_vector(int32_t
"requested %d)\n", get_num_vectors(), real_num);
}

SGVector<ST> vec;
vec.vector = get_feature_vector(num, vec.vlen, vec.do_free);
return vec;
int32_t vlen;
bool do_free;
ST* vector= get_feature_vector(num, vlen, do_free);
return SGVector<ST>(vector, vlen, do_free);
}

template<class ST> void CSimpleFeatures<ST>::free_feature_vector(ST* feat_vec, int32_t num, bool dofree)
Expand All @@ -178,9 +179,10 @@ template<class ST> void CSimpleFeatures<ST>::free_feature_vector(ST* feat_vec, i
SG_FREE(feat_vec);
}

template<class ST> void CSimpleFeatures<ST>::free_feature_vector(const SGVector<ST>& vec, int32_t num)
template<class ST> void CSimpleFeatures<ST>::free_feature_vector(SGVector<ST> vec, int32_t num)
{
free_feature_vector(vec.vector, num, vec.do_free);
free_feature_vector(vec.vector, num, false);
SG_VUNREF(vec);
}

template<class ST> void CSimpleFeatures<ST>::vector_subset(int32_t* idx, int32_t idx_len)
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/SimpleFeatures.h
Expand Up @@ -162,7 +162,7 @@ template<class ST> class CSimpleFeatures: public CDotFeatures
* @param vec feature vector to free
* @param num index in feature cache
*/
void free_feature_vector(const SGVector<ST>& vec, int32_t num);
void free_feature_vector(SGVector<ST> vec, int32_t num);

/**
* Extracts the feature vectors mentioned in idx and replaces them in
Expand Down
8 changes: 2 additions & 6 deletions src/shogun/features/SparseFeatures.cpp
Expand Up @@ -172,15 +172,11 @@ template<class ST> SGVector<ST> CSparseFeatures<ST>::get_full_feature_vector(int

if (sv.features)
{
dense.do_free=true;
dense.vlen=num_features;
dense.vector=SG_MALLOC(ST, num_features);
memset(dense.vector, 0, sizeof(ST)*num_features);
dense=SGVector<ST>(num_features);
dense.zero();

for (int32_t i=0; i<sv.num_feat_entries; i++)
{
dense.vector[sv.features[i].feat_index]= sv.features[i].entry;
}
}

free_sparse_feature_vector(sv, num);
Expand Down
3 changes: 3 additions & 0 deletions src/shogun/features/SparseFeatures.h
Expand Up @@ -15,6 +15,8 @@
#define _SPARSEFEATURES__H__

#include <shogun/lib/common.h>
#include <shogun/lib/DataType.h>
#include <shogun/lib/SGSparseMatrix.h>
#include <shogun/lib/Cache.h>
#include <shogun/io/File.h>

Expand All @@ -31,6 +33,7 @@ class CLabels;
class CFeatures;
class CDotFeatures;
template <class ST> class CSimpleFeatures;
template <class ST> class SGSparseMatrix;

/** @brief Template class SparseFeatures implements sparse matrices.
*
Expand Down
5 changes: 1 addition & 4 deletions src/shogun/features/StringFeatures.cpp
Expand Up @@ -359,7 +359,7 @@ template<class ST> void CStringFeatures<ST>::free_feature_vector(ST* feat_vec, i
SG_FREE(feat_vec);
}

template<class ST> void CStringFeatures<ST>::free_feature_vector(SGVector<ST>& feat_vec, int32_t num)
template<class ST> void CStringFeatures<ST>::free_feature_vector(SGVector<ST> feat_vec, int32_t num)
{
if (num>=get_num_vectors())
{
Expand All @@ -372,9 +372,6 @@ template<class ST> void CStringFeatures<ST>::free_feature_vector(SGVector<ST>& f

if (feature_cache)
feature_cache->unlock_entry(real_num);

if (feat_vec.do_free)
SG_FREE(feat_vec.vector);
}

template<class ST> ST CStringFeatures<ST>::get_feature(int32_t vec_num, int32_t feat_num)
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/features/StringFeatures.h
Expand Up @@ -239,7 +239,7 @@ template <class ST> class CStringFeatures : public CFeatures
* @param feat_vec feature vector to free
* @param num index in feature cache, possibly from subset
*/
void free_feature_vector(SGVector<ST>& feat_vec, int32_t num);
void free_feature_vector(SGVector<ST> feat_vec, int32_t num);

/** get feature
*
Expand Down
1 change: 1 addition & 0 deletions src/shogun/io/SerializableFile.h
Expand Up @@ -15,6 +15,7 @@
#include <stdio.h>
#include <shogun/base/SGObject.h>
#include <shogun/lib/DataType.h>
#include <shogun/lib/SGSparseVector.h>

namespace shogun
{
Expand Down
1 change: 1 addition & 0 deletions src/shogun/io/StreamingFile.h
Expand Up @@ -16,6 +16,7 @@
#include <shogun/io/File.h>
#include <shogun/io/SGIO.h>
#include <shogun/lib/DataType.h>
#include <shogun/lib/SGSparseVector.h>
#include <shogun/io/IOBuffer.h>
#include <shogun/classifier/vw/vw_common.h>

Expand Down
2 changes: 2 additions & 0 deletions src/shogun/lib/DataType.cpp
Expand Up @@ -12,6 +12,8 @@

#include <shogun/base/SGObject.h>
#include <shogun/lib/DataType.h>
#include <shogun/lib/SGString.h>
#include <shogun/lib/SGSparseVector.h>

using namespace shogun;

Expand Down

0 comments on commit 52b1bf0

Please sign in to comment.