Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
remove remaining inline functions in features/
  • Loading branch information
Soeren Sonnenburg committed Sep 15, 2011
1 parent 8b4ce6c commit c834d2b
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 116 deletions.
34 changes: 34 additions & 0 deletions src/shogun/features/StreamingFeatures.cpp
@@ -0,0 +1,34 @@
#include <shogun/features/StreamingFeatures.h>

using namespace shogun;

CStreamingFeatures::CStreamingFeatures() : CFeatures()
{
}

CStreamingFeatures::CStreamingFeatures(CStreamingFile* file,
bool is_labelled, int32_t size) : CFeatures()
{
}

void CStreamingFeatures::set_read_functions()
{
set_vector_reader();
set_vector_and_label_reader();
}

bool CStreamingFeatures::get_has_labels()
{
return has_labels;
}

bool CStreamingFeatures::is_seekable()
{
return seekable;
}

void CStreamingFeatures::reset_stream()
{
SG_NOTIMPLEMENTED;
return;
}
28 changes: 6 additions & 22 deletions src/shogun/features/StreamingFeatures.h
Expand Up @@ -69,8 +69,7 @@ class CStreamingFeatures : public CFeatures
* Default constructor with no args.
* Doesn't do anything yet.
*/
CStreamingFeatures()
: CFeatures() { }
CStreamingFeatures();

/**
* Constructor with input information passed.
Expand All @@ -79,8 +78,7 @@ class CStreamingFeatures : public CFeatures
* @param is_labelled Whether examples are labelled or not.
* @param size Number of examples to be held in the parser's "ring".
*/
CStreamingFeatures(CStreamingFile* file, bool is_labelled, int32_t size)
: CFeatures() { }
CStreamingFeatures(CStreamingFile* file, bool is_labelled, int32_t size);

/**
* Destructor
Expand All @@ -93,11 +91,7 @@ class CStreamingFeatures : public CFeatures
* The functions are implemented specific to the type in the
* derived class.
*/
void set_read_functions()
{
set_vector_reader();
set_vector_and_label_reader();
}
void set_read_functions();

/**
* The derived object must set the function which will be used
Expand Down Expand Up @@ -166,10 +160,7 @@ class CStreamingFeatures : public CFeatures
*
* @return true if labelled, else false
*/
virtual bool get_has_labels()
{
return has_labels;
}
virtual bool get_has_labels();

/**
* Whether the stream is seekable (to check if multiple epochs
Expand All @@ -181,19 +172,12 @@ class CStreamingFeatures : public CFeatures
*
* @return true if seekable, else false.
*/
virtual bool is_seekable()
{
return seekable;
}
virtual bool is_seekable();

/**
* Function to reset the stream (if possible).
*/
virtual void reset_stream()
{
SG_NOTIMPLEMENTED;
return;
}
virtual void reset_stream();

protected:

Expand Down
109 changes: 108 additions & 1 deletion src/shogun/features/StreamingVwFeatures.cpp
Expand Up @@ -17,6 +17,39 @@

using namespace shogun;

CStreamingVwFeatures::CStreamingVwFeatures() : CStreamingDotFeatures()
{
init();
set_read_functions();
}

CStreamingVwFeatures::CStreamingVwFeatures(CStreamingVwFile* file,
bool is_labelled, int32_t size)
: CStreamingDotFeatures()
{
init(file, is_labelled, size);
set_read_functions();
}

CStreamingVwFeatures::CStreamingVwFeatures(CStreamingVwCacheFile* file,
bool is_labelled, int32_t size)
: CStreamingDotFeatures()
{
init(file, is_labelled, size);
set_read_functions();
}

CStreamingVwFeatures::~CStreamingVwFeatures()
{
parser.end_parser();
SG_UNREF(env);
}

CFeatures* CStreamingVwFeatures::duplicate() const
{
return new CStreamingVwFeatures(*this);
}

void CStreamingVwFeatures::set_vector_reader()
{
parser.set_read_vector(&CStreamingFile::get_vector);
Expand All @@ -27,7 +60,81 @@ void CStreamingVwFeatures::set_vector_and_label_reader()
parser.set_read_vector_and_label(&CStreamingFile::get_vector_and_label);
}

inline EFeatureType CStreamingVwFeatures::get_feature_type()
void CStreamingVwFeatures::reset_stream()
{
if (working_file->is_seekable())
{
working_file->reset_stream();
parser.exit_parser();
parser.init(working_file, has_labels, parser.get_ring_size());
parser.set_free_vector_after_release(false);
parser.start_parser();
}
else
SG_ERROR("The input cannot be reset! Please use 1 pass.\n");
}

CVwEnvironment* CStreamingVwFeatures::get_env()
{
SG_REF(env);
return env;
}

void CStreamingVwFeatures::set_env(CVwEnvironment* vw_env)
{
env = vw_env;
SG_REF(env);
}

void CStreamingVwFeatures::expand_if_required(float32_t*& vec, int32_t& len)
{
int32_t dim = 1 << env->num_bits;
if (dim > len)
{
vec = SG_REALLOC(float32_t, vec, dim);
memset(&vec[len], 0, (dim-len) * sizeof(float32_t));
len = dim;
}
}

void CStreamingVwFeatures::expand_if_required(float64_t*& vec, int32_t& len)
{
int32_t dim = 1 << env->num_bits;
if (dim > len)
{
vec = SG_REALLOC(float64_t, vec, dim);
memset(&vec[len], 0, (dim-len) * sizeof(float64_t));
len = dim;
}
}

float32_t CStreamingVwFeatures::real_weight(float32_t w, float32_t gravity)
{
float32_t wprime = 0;
if (gravity < fabsf(w))
wprime = CMath::sign(w)*(fabsf(w) - gravity);
return wprime;
}

int32_t CStreamingVwFeatures::get_nnz_features_for_vector()
{
return current_length;
}

int32_t CStreamingVwFeatures::get_num_vectors() const
{
if (current_example)
return 1;
else
return 0;
}

int32_t CStreamingVwFeatures::get_size()
{
return sizeof(VwExample);
}

EFeatureType CStreamingVwFeatures::get_feature_type()
{
return F_DREAL;
}
Expand Down

0 comments on commit c834d2b

Please sign in to comment.