Skip to content

Commit

Permalink
Template based malloc/new etc
Browse files Browse the repository at this point in the history
- move implementation code from .h to .cpp for SG datatypes
- no longer by default include lib/memory.h (manual include required)
- turn global functions in SGIO into static member functions
- fix a number of clang warnings
  • Loading branch information
Soeren Sonnenburg committed Nov 20, 2012
1 parent 93dba0d commit aa689b0
Show file tree
Hide file tree
Showing 24 changed files with 453 additions and 334 deletions.
12 changes: 6 additions & 6 deletions src/shogun/classifier/vw/VwParser.cpp
Expand Up @@ -174,7 +174,7 @@ int32_t CVwParser::read_svmlight_features(CIOBuffer* buf, VwExample*& ae)
vw_size_t mask = env->mask;
tokenize(' ', example_string, words);

ae->ld->label = float_of_substring(words[0]);
ae->ld->label = SGIO::float_of_substring(words[0]);
ae->ld->weight = 1.;
ae->ld->initial = 0.;
set_minmax(ae->ld->label);
Expand Down Expand Up @@ -216,7 +216,7 @@ int32_t CVwParser::read_dense_features(CIOBuffer* buf, VwExample*& ae)
vw_size_t mask = env->mask;
tokenize(' ', example_string, words);

ae->ld->label = float_of_substring(words[0]);
ae->ld->label = SGIO::float_of_substring(words[0]);
ae->ld->weight = 1.;
ae->ld->initial = 0.;
set_minmax(ae->ld->label);
Expand All @@ -231,7 +231,7 @@ int32_t CVwParser::read_dense_features(CIOBuffer* buf, VwExample*& ae)
int32_t j=0;
for (substring* i = feature_start; i != words.end; i++)
{
float32_t v = float_of_substring(*i);
float32_t v = SGIO::float_of_substring(*i);
vw_size_t word_hash = j & mask;
VwFeature f = {v,word_hash};
ae->sum_feat_sq[index] += v*v;
Expand Down Expand Up @@ -281,14 +281,14 @@ void CVwParser::feature_value(substring &s, v_array<substring>& feat_name, float
v = 1.;
break;
case 2:
v = float_of_substring(feat_name[1]);
v = SGIO::float_of_substring(feat_name[1]);
if (isnan(v))
SG_SERROR("error NaN value for feature %s! Terminating!\n",
c_string_of_substring(feat_name[0]));
SGIO::c_string_of_substring(feat_name[0]));
break;
default:
SG_SERROR("Examples with a weird name, i.e., '%s'\n",
c_string_of_substring(s));
SGIO::c_string_of_substring(s));
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/shogun/classifier/vw/vw_label.cpp
Expand Up @@ -23,16 +23,16 @@ void VwLabel::label_from_substring(v_array<substring>& words)
case 0:
break;
case 1:
label = float_of_substring(words[0]);
label = SGIO::float_of_substring(words[0]);
break;
case 2:
label = float_of_substring(words[0]);
weight = float_of_substring(words[1]);
label = SGIO::float_of_substring(words[0]);
weight = SGIO::float_of_substring(words[1]);
break;
case 3:
label = float_of_substring(words[0]);
weight = float_of_substring(words[1]);
initial = float_of_substring(words[2]);
label = SGIO::float_of_substring(words[0]);
weight = SGIO::float_of_substring(words[1]);
initial = SGIO::float_of_substring(words[2]);
break;
default:
SG_SERROR("malformed example!\n"
Expand Down
54 changes: 54 additions & 0 deletions src/shogun/io/SGIO.cpp
Expand Up @@ -303,3 +303,57 @@ const char* SGIO::get_msg_intro(EMessageType prio) const

return NULL;
}

char* SGIO::c_string_of_substring(substring s)
{
uint32_t len = s.end - s.start+1;
char* ret = SG_CALLOC(char, len);
memcpy(ret,s.start,len-1);
return ret;
}

void SGIO::print_substring(substring s)
{
char* c_string = c_string_of_substring(s);
SG_SPRINT("%s\n", c_string);
SG_FREE(c_string);
}

float32_t SGIO::float_of_substring(substring s)
{
char* endptr = s.end;
float32_t f = strtof(s.start,&endptr);
if (endptr == s.start && s.start != s.end)
SG_SERROR("error: %s is not a float!\n", c_string_of_substring(s));

return f;
}

float64_t SGIO::double_of_substring(substring s)
{
char* endptr = s.end;
float64_t f = strtod(s.start,&endptr);
if (endptr == s.start && s.start != s.end)
SG_SERROR("Error!:%s is not a double!\n", c_string_of_substring(s));

return f;
}

int32_t SGIO::int_of_substring(substring s)
{
char* c_string = c_string_of_substring(s);
int32_t int_val = atoi(c_string);
SG_FREE(c_string);

return int_val;
}

uint32_t SGIO::ulong_of_substring(substring s)
{
return strtoul(s.start,NULL,10);
}

uint32_t SGIO::ss_length(substring s)
{
return (s.end - s.start);
}
169 changes: 63 additions & 106 deletions src/shogun/io/SGIO.h
Expand Up @@ -110,6 +110,20 @@ __FILE__ ":" func ": Unstable method! Please report if it seems to " \
#define ASSERT(x) { if (!(x)) SG_SERROR("assertion %s failed in file %s line %d\n",#x, __FILE__, __LINE__);}
#define REQUIRE(x, ...) { if (!(x)) SG_SERROR(__VA_ARGS__); }

/**
* @brief struct Substring, specified by
* start position and end position.
*
* Used to mark strings in a buffer, where they
* need not be delimited by NUL characters.
*/
struct substring
{
/** start */
char *start;
/** end */
char *end;
};

/** @brief Class SGIO, used to do input output operations throughout shogun.
*
Expand Down Expand Up @@ -366,6 +380,55 @@ class SGIO
return 0;
}

/**
* Return a C string from the substring
* @param s substring
* @return new C string representation
*/
static char* c_string_of_substring(substring s);

/**
* Print the substring
* @param s substring
*/
static void print_substring(substring s);

/**
* Get value of substring as float
* (if possible)
* @param s substring
* @return float32_t value of substring
*/
static float32_t float_of_substring(substring s);

/**
* Return value of substring as double
* @param s substring
* @return substring as double
*/
static float64_t double_of_substring(substring s);

/**
* Integer value of substring
* @param s substring
* @return int value of substring
*/
static int32_t int_of_substring(substring s);

/**
* Unsigned long value of substring
* @param s substring
* @return unsigned long value of substring
*/
static uint32_t ulong_of_substring(substring s);

/**
* Length of substring
* @param s substring
* @return length of substring
*/
static uint32_t ss_length(substring s);

/** increase reference counter
*
* @return reference count
Expand Down Expand Up @@ -447,111 +510,5 @@ class SGIO
private:
int32_t refcount;
};

/**
* @brief struct Substring, specified by
* start position and end position.
*
* Used to mark strings in a buffer, where they
* need not be delimited by NUL characters.
*/
struct substring
{
/** start */
char *start;
/** end */
char *end;
};


/**
* Return a C string from the substring
* @param s substring
* @return new C string representation
*/
inline char* c_string_of_substring(substring s)
{
uint32_t len = s.end - s.start+1;
char* ret = SG_CALLOC(char, len);
memcpy(ret,s.start,len-1);
return ret;
}

/**
* Print the substring
* @param s substring
*/
inline void print_substring(substring s)
{
char* c_string = c_string_of_substring(s);
SG_SPRINT("%s\n", c_string);
SG_FREE(c_string);
}

/**
* Get value of substring as float
* (if possible)
* @param s substring
* @return float32_t value of substring
*/
inline float32_t float_of_substring(substring s)
{
char* endptr = s.end;
float32_t f = strtof(s.start,&endptr);
if (endptr == s.start && s.start != s.end)
SG_SERROR("error: %s is not a float!\n", c_string_of_substring(s));

return f;
}

/**
* Return value of substring as double
* @param s substring
* @return substring as double
*/
inline float64_t double_of_substring(substring s)
{
char* endptr = s.end;
float64_t f = strtod(s.start,&endptr);
if (endptr == s.start && s.start != s.end)
SG_SERROR("Error!:%s is not a double!\n", c_string_of_substring(s));

return f;
}

/**
* Integer value of substring
* @param s substring
* @return int value of substring
*/
inline int32_t int_of_substring(substring s)
{
char* c_string = c_string_of_substring(s);
int32_t int_val = atoi(c_string);
SG_FREE(c_string);

return int_val;
}

/**
* Unsigned long value of substring
* @param s substring
* @return unsigned long value of substring
*/
inline uint32_t ulong_of_substring(substring s)
{
return strtoul(s.start,NULL,10);
}

/**
* Length of substring
* @param s substring
* @return length of substring
*/
inline uint32_t ss_length(substring s)
{
return (s.end - s.start);
}

}
#endif // __SGIO_H__
6 changes: 3 additions & 3 deletions src/shogun/io/streaming/StreamingAsciiFile.cpp
Expand Up @@ -139,7 +139,7 @@ GET_VECTOR(get_longreal_vector, atoi, floatmax_t)
int32_t j=0; \
for (substring* i = feature_start; i != words.end; i++) \
{ \
vector[j++] = float_of_substring(*i); \
vector[j++] = SGIO::float_of_substring(*i); \
} \
}

Expand Down Expand Up @@ -249,7 +249,7 @@ GET_VECTOR_AND_LABEL(get_longreal_vector_and_label, atoi, floatmax_t)
\
CAsciiFile::tokenize(' ', example_string, words); \
\
label = float_of_substring(words[0]); \
label = SGIO::float_of_substring(words[0]); \
\
len = words.index() - 1; \
substring* feature_start = &words[1]; \
Expand All @@ -260,7 +260,7 @@ GET_VECTOR_AND_LABEL(get_longreal_vector_and_label, atoi, floatmax_t)
int32_t j=0; \
for (substring* i = feature_start; i != words.end; i++) \
{ \
vector[j++] = float_of_substring(*i); \
vector[j++] = SGIO::float_of_substring(*i); \
} \
}

Expand Down
1 change: 1 addition & 0 deletions src/shogun/lib/SGMatrix.h
Expand Up @@ -13,6 +13,7 @@
#define __SGMATRIX_H__

#include <shogun/lib/config.h>
#include <shogun/lib/memory.h>
#include <shogun/lib/DataType.h>
#include <shogun/lib/SGReferencedData.h>

Expand Down
1 change: 1 addition & 0 deletions src/shogun/lib/SGNDArray.cpp
Expand Up @@ -10,6 +10,7 @@
* Copyright (C) 2012 Soeren Sonnenburg
*/

#include <shogun/lib/memory.h>
#include <shogun/lib/SGNDArray.h>
#include <shogun/lib/SGReferencedData.h>

Expand Down

0 comments on commit aa689b0

Please sign in to comment.