Skip to content

Commit

Permalink
remove most inline functions in features
Browse files Browse the repository at this point in the history
- exceptions are some single line functions
- templated classes
  • Loading branch information
Soeren Sonnenburg committed Sep 15, 2011
1 parent 5fee0e6 commit 8b4ce6c
Show file tree
Hide file tree
Showing 33 changed files with 997 additions and 746 deletions.
9 changes: 8 additions & 1 deletion src/shogun/features/Alphabet.cpp
Expand Up @@ -591,6 +591,11 @@ void CAlphabet::print_histogram()
}
}

SGVector<int64_t> CAlphabet::get_histogram()
{
return SGVector<int64_t>(&histogram[0], 1 << (sizeof(uint8_t)*8));
}

bool CAlphabet::check_alphabet(bool print_error)
{
bool result = true;
Expand Down Expand Up @@ -632,7 +637,9 @@ bool CAlphabet::check_alphabet_size(bool print_error)

void CAlphabet::copy_histogram(CAlphabet* a)
{
memcpy(histogram, a->get_histogram(), sizeof(histogram));
SGVector<int64_t> h=a->get_histogram();
ASSERT(h.vlen == sizeof(histogram));
memcpy(histogram, h.vector, sizeof(histogram));
}

const char* CAlphabet::get_alphabet_name(EAlphabet alphabet)
Expand Down
19 changes: 2 additions & 17 deletions src/shogun/features/Alphabet.h
Expand Up @@ -198,24 +198,9 @@ class CAlphabet : public CSGObject

/** get histogram
*
* @param h where the histogram will be stored
* @param len length of histogram
* @return histogram
*/
inline void get_hist(int64_t** h, int32_t* len)
{
int32_t hist_size=(1 << (sizeof(uint8_t)*8));
ASSERT(h && len);
*h= SG_MALLOC(int64_t, hist_size);
*len=hist_size;
ASSERT(*len);
memcpy(*h, &histogram[0], sizeof(int64_t)*hist_size);
}

/// get pointer to histogram
inline const int64_t* get_histogram()
{
return &histogram[0];
}
SGVector<int64_t> get_histogram();

/** check whether symbols in histogram are valid in alphabet
* e.g. for DNA if only letters ACGT appear
Expand Down
67 changes: 67 additions & 0 deletions src/shogun/features/AttributeFeatures.cpp
Expand Up @@ -18,6 +18,73 @@ CAttributeFeatures::CAttributeFeatures()
{
}

CFeatures* CAttributeFeatures::get_attribute(char* attr_name)
{
int32_t idx=find_attr_index(attr_name);
if (idx>=0)
{
CFeatures* f=features[idx].attr_obj;
SG_REF(f);
return f;
}

return NULL;
}

void CAttributeFeatures::get_attribute_by_index(int idx, const char* &attr_name, CFeatures* &attr_obj)
{
T_ATTRIBUTE a= features.get_element_safe(idx);
attr_name= a.attr_name;
attr_obj= a.attr_obj;
SG_REF(a.attr_obj);
}

bool CAttributeFeatures::set_attribute(char* attr_name, CFeatures* attr_obj)
{
int32_t idx=find_attr_index(attr_name);
if (idx==-1)
idx=features.get_num_elements();

T_ATTRIBUTE a;
a.attr_name=strdup(attr_name);
a.attr_obj=attr_obj;

SG_REF(attr_obj);

return features.set_element(a, idx);
}

bool CAttributeFeatures::del_attribute(char* attr_name)
{
int32_t idx=find_attr_index(attr_name);

if (idx>=0)
{
T_ATTRIBUTE a= features[idx];
SG_FREE(a.attr_name);
SG_UNREF(a.attr_obj);
return true;
}
return false;
}

int32_t CAttributeFeatures::get_num_attributes()
{
return features.get_num_elements();
}

int32_t CAttributeFeatures::find_attr_index(char* attr_name)
{
int32_t n=features.get_num_elements();
for (int32_t i=0; i<n; i++)
{
if (!strcmp(features[n].attr_name, attr_name))
return i;
}

return -1;
}

CAttributeFeatures::~CAttributeFeatures()
{
int32_t n=features.get_num_elements();
Expand Down
69 changes: 6 additions & 63 deletions src/shogun/features/AttributeFeatures.h
Expand Up @@ -58,82 +58,36 @@ class CAttributeFeatures : public CFeatures
* @param attr_name attribute name
* @return feature object
*/
CFeatures* get_attribute(char* attr_name)
{
int32_t idx=find_attr_index(attr_name);
if (idx>=0)
{
CFeatures* f=features[idx].attr_obj;
SG_REF(f);
return f;
}

return NULL;
}
CFeatures* get_attribute(char* attr_name);

/** return the feature object at index
*
* @param idx index of attribute
* @param attr_name attribute name (returned by reference)
* @param attr_obj attribute object (returned by reference)
*/
inline void get_attribute_by_index(int idx, const char* &attr_name, CFeatures* &attr_obj)
{
T_ATTRIBUTE a= features.get_element_safe(idx);
attr_name= a.attr_name;
attr_obj= a.attr_obj;
SG_REF(a.attr_obj);
}
void get_attribute_by_index(int idx, const char* &attr_name, CFeatures* &attr_obj);

/** set the feature object for attribute name
*
* @param attr_name attribute name
* @param attr_obj feature object to set
* @return true on success
*/
inline bool set_attribute(char* attr_name, CFeatures* attr_obj)
{
int32_t idx=find_attr_index(attr_name);
if (idx==-1)
idx=features.get_num_elements();

T_ATTRIBUTE a;
a.attr_name=strdup(attr_name);
a.attr_obj=attr_obj;

SG_REF(attr_obj);

return features.set_element(a, idx);
}
bool set_attribute(char* attr_name, CFeatures* attr_obj);

/** delete the attribute matching attribute name
*
* @param attr_name attribute name
* @return true on success
*/
inline bool del_attribute(char* attr_name)
{
int32_t idx=find_attr_index(attr_name);

if (idx>=0)
{
T_ATTRIBUTE a= features[idx];
SG_FREE(a.attr_name);
SG_UNREF(a.attr_obj);
return true;
}
return false;
}

bool del_attribute(char* attr_name);

/** get number of attributes
*
* @return number of attributes
*/
inline int32_t get_num_attributes()
{
return features.get_num_elements();
}
int32_t get_num_attributes();

/** @return object name */
inline virtual const char* get_name() const { return "AttributeFeatures"; }
Expand Down Expand Up @@ -184,18 +138,7 @@ class CAttributeFeatures : public CFeatures
* @param attr_name attribute name
* @return index (if found), otherwise -1
*/
inline int32_t find_attr_index(char* attr_name)
{
int32_t n=features.get_num_elements();
for (int32_t i=0; i<n; i++)
{
if (!strcmp(features[n].attr_name, attr_name))
return i;
}

return -1;
}

inline int32_t find_attr_index(char* attr_name);

protected:
///list of attributes (sorted)
Expand Down
125 changes: 114 additions & 11 deletions src/shogun/features/CombinedDotFeatures.cpp
Expand Up @@ -15,17 +15,6 @@

using namespace shogun;

void
CCombinedDotFeatures::init(void)
{
m_parameters->add(&num_dimensions, "num_dimensions",
"Total number of dimensions.");
m_parameters->add(&num_vectors, "num_vectors",
"Total number of vectors.");
m_parameters->add((CSGObject**) &feature_list,
"feature_list", "Feature list.");
}

CCombinedDotFeatures::CCombinedDotFeatures() : CDotFeatures()
{
init();
Expand Down Expand Up @@ -240,6 +229,109 @@ void CCombinedDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, f
}
}

void* CCombinedDotFeatures::get_feature_iterator(int32_t vector_index)
{
combined_feature_iterator* it=SG_MALLOC(combined_feature_iterator, 1);

it->current=NULL;
it->f=get_first_feature_obj(it->current);
it->iterator=it->f->get_feature_iterator(vector_index);
it->vector_index=vector_index;
return it;
}

bool CCombinedDotFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator)
{
ASSERT(iterator);
combined_feature_iterator* it = (combined_feature_iterator*) iterator;

while (it->f)
{
if (it->f->get_next_feature(index, value, it->iterator))
{
value*=get_combined_feature_weight();
return true;
}

it->f->free_feature_iterator(it->iterator);
it->f=get_next_feature_obj(it->current);
if (it->f)
it->iterator=it->f->get_feature_iterator(it->vector_index);
else
it->iterator=NULL;
}
return false;
}

void CCombinedDotFeatures::free_feature_iterator(void* iterator)
{
if (iterator)
{
combined_feature_iterator* it = (combined_feature_iterator*) iterator;
if (it->iterator && it->f)
it->f->free_feature_iterator(it->iterator);
SG_FREE(it);
}
}

CDotFeatures* CCombinedDotFeatures::get_first_feature_obj()
{
return (CDotFeatures*) feature_list->get_first_element();
}

CDotFeatures* CCombinedDotFeatures::get_first_feature_obj(CListElement*& current)
{
return (CDotFeatures*) feature_list->get_first_element(current);
}

CDotFeatures* CCombinedDotFeatures::get_next_feature_obj()
{
return (CDotFeatures*) feature_list->get_next_element();
}

CDotFeatures* CCombinedDotFeatures::get_next_feature_obj(CListElement*& current)
{
return (CDotFeatures*) feature_list->get_next_element(current);
}

CDotFeatures* CCombinedDotFeatures::get_last_feature_obj()
{
return (CDotFeatures*) feature_list->get_last_element();
}

bool CCombinedDotFeatures::insert_feature_obj(CDotFeatures* obj)
{
ASSERT(obj);
bool result=feature_list->insert_element(obj);
update_dim_feature_space_and_num_vec();
return result;
}

bool CCombinedDotFeatures::append_feature_obj(CDotFeatures* obj)
{
ASSERT(obj);
bool result=feature_list->append_element(obj);
update_dim_feature_space_and_num_vec();
return result;
}

bool CCombinedDotFeatures::delete_feature_obj()
{
CDotFeatures* f=(CDotFeatures*) feature_list->delete_element();
if (f)
{
SG_UNREF(f);
update_dim_feature_space_and_num_vec();
return true;
}
else
return false;
}

int32_t CCombinedDotFeatures::get_num_feature_obj()
{
return feature_list->get_num_elements();
}

int32_t CCombinedDotFeatures::get_nnz_features_for_vector(int32_t num)
{
Expand Down Expand Up @@ -296,3 +388,14 @@ void CCombinedDotFeatures::set_subfeature_weights(
i++;
}
}

void CCombinedDotFeatures::init()
{
m_parameters->add(&num_dimensions, "num_dimensions",
"Total number of dimensions.");
m_parameters->add(&num_vectors, "num_vectors",
"Total number of vectors.");
m_parameters->add((CSGObject**) &feature_list,
"feature_list", "Feature list.");
}

0 comments on commit 8b4ce6c

Please sign in to comment.