Skip to content

Commit

Permalink
apply autoformatter
Browse files Browse the repository at this point in the history
  • Loading branch information
karlnapf committed Jan 8, 2018
1 parent 0e3cf00 commit 4203d62
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 82 deletions.
6 changes: 4 additions & 2 deletions src/shogun/base/Parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3094,8 +3094,10 @@ bool TParameter::copy_ptype(EPrimitiveType ptype, void* source, void* target)
break;
}
default:
SG_SERROR("TParameter::copy_ptype(): Encountered unknown primitive"
"-type: %d\n", ptype);
SG_SERROR(
"TParameter::copy_ptype(): Encountered unknown primitive"
"-type: %d\n",
ptype);
return false;
break;
}
Expand Down
7 changes: 4 additions & 3 deletions src/shogun/base/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ namespace shogun
std::unique_ptr<CSignal> sg_signal(nullptr);
std::unique_ptr<SGLinalg> sg_linalg(nullptr);

// Two global variables to over-ride CMath::fequals for certain serialization
// Two global variables to over-ride CMath::fequals for certain
// serialization
// unit tests to pass. These should be removed if possible and serialization
// formats should be fixed.
float64_t sg_fequals_epsilon=0.0;
bool sg_fequals_tolerant=0.0;
float64_t sg_fequals_epsilon = 0.0;
bool sg_fequals_tolerant = 0.0;

/// function called to print normal messages
std::function<void(FILE*, const char*)> sg_print_message(nullptr);
Expand Down
11 changes: 7 additions & 4 deletions src/shogun/base/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#ifndef __SG_INIT_H__
#define __SG_INIT_H__

#include <shogun/lib/config.h>
#include <shogun/lib/common.h>
#include <shogun/lib/config.h>

#include <functional>
#include <stdio.h>
Expand Down Expand Up @@ -67,12 +67,14 @@ namespace shogun
*/
SGIO* get_global_io();

/** @return the globally over-ridden floating point epsilon for CMath::fequals
/** @return the globally over-ridden floating point epsilon for
* CMath::fequals
*/
float64_t get_global_fequals_epsilon();

/** Globally over-ride the floating point epsilon for CMath::fequals.
* Hack required for CSGObject::equals checks for certain serialization formats.
* Hack required for CSGObject::equals checks for certain serialization
* formats.
* @param fequals_epsilon new epsilon to use
*/
void set_global_fequals_epsilon(float64_t fequals_epsilon);
Expand All @@ -82,7 +84,8 @@ namespace shogun
bool get_global_fequals_tolerant();

/** Globally enable linient check for CMath::fequals.
* Hack required for CSGObject::equals checks for certain serialization formats.
* Hack required for CSGObject::equals checks for certain serialization
* formats.
* @param fequals_tolerant whether or not to use tolerant check
*/
void set_global_fequals_tolerant(bool fequals_tolerant);
Expand Down
45 changes: 23 additions & 22 deletions src/shogun/lib/SGMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,28 +165,29 @@ bool SGMatrix<T>::equals(const SGMatrix<T>& other) const
}

#ifndef REAL_EQUALS
#define REAL_EQUALS(real_t) \
template <> \
bool SGMatrix<real_t>::equals(const SGMatrix<real_t>& other) const \
{ \
if (*this==other) \
return true; \
\
if (!(num_rows || num_cols || other.num_rows || other.num_cols)) \
return true; \
\
if (!matrix || !other.matrix) \
return false; \
\
if (num_rows!=other.num_rows || num_cols!=other.num_cols) \
return false; \
\
return std::equal(matrix, matrix+size(), other.matrix, \
[](const real_t& a, const real_t& b) \
{ \
return CMath::fequals<real_t>(a, b, std::numeric_limits<real_t>::epsilon()); \
}); \
}
#define REAL_EQUALS(real_t) \
template <> \
bool SGMatrix<real_t>::equals(const SGMatrix<real_t>& other) const \
{ \
if (*this == other) \
return true; \
\
if (!(num_rows || num_cols || other.num_rows || other.num_cols)) \
return true; \
\
if (!matrix || !other.matrix) \
return false; \
\
if (num_rows != other.num_rows || num_cols != other.num_cols) \
return false; \
\
return std::equal( \
matrix, matrix + size(), other.matrix, \
[](const real_t& a, const real_t& b) { \
return CMath::fequals<real_t>( \
a, b, std::numeric_limits<real_t>::epsilon()); \
}); \
}

REAL_EQUALS(float32_t)
REAL_EQUALS(float64_t)
Expand Down
40 changes: 22 additions & 18 deletions src/shogun/lib/SGSparseVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,7 @@ SGSparseVector<T> SGSparseVector<T>::clone() const
}

template <class T>
inline bool
SGSparseVector<T>::operator==(const SGSparseVector<T>& other) const
inline bool SGSparseVector<T>::operator==(const SGSparseVector<T>& other) const
{
if (num_feat_entries != other.num_feat_entries)
return false;
Expand All @@ -375,7 +374,7 @@ template <class T>
bool SGSparseVector<T>::equals(const SGSparseVector<T>& other) const
{
/* same instance */
if (*this==other)
if (*this == other)
return true;

// both empty
Expand All @@ -391,7 +390,7 @@ bool SGSparseVector<T>::equals(const SGSparseVector<T>& other) const
return false;

// content
return std::equal(features, features+num_feat_entries, other.features);
return std::equal(features, features + num_feat_entries, other.features);
}

template<class T> void SGSparseVector<T>::load(CFile * loader)
Expand Down Expand Up @@ -651,9 +650,9 @@ void SGSparseVector<complex128_t>::display_vector(const char * name, const char
SG_SPRINT("%s]\n", prefix);
}


template <class T>
bool SGSparseVectorEntry<T>::operator==(const SGSparseVectorEntry<T>& other) const
bool SGSparseVectorEntry<T>::
operator==(const SGSparseVectorEntry<T>& other) const
{
if (feat_index != other.feat_index)
return false;
Expand All @@ -662,15 +661,17 @@ bool SGSparseVectorEntry<T>::operator==(const SGSparseVectorEntry<T>& other) con
}

#ifndef REAL_SPARSE_EQUALS
#define REAL_SPARSE_EQUALS(real_t) \
template <> \
bool SGSparseVectorEntry<real_t>::operator==(const SGSparseVectorEntry<real_t>& other) const \
{ \
if (feat_index != other.feat_index) \
return false; \
\
return CMath::fequals<real_t>(entry, other.entry, std::numeric_limits<real_t>::epsilon()); \
}
#define REAL_SPARSE_EQUALS(real_t) \
template <> \
bool SGSparseVectorEntry<real_t>::operator==( \
const SGSparseVectorEntry<real_t>& other) const \
{ \
if (feat_index != other.feat_index) \
return false; \
\
return CMath::fequals<real_t>( \
entry, other.entry, std::numeric_limits<real_t>::epsilon()); \
}

REAL_SPARSE_EQUALS(float32_t)
REAL_SPARSE_EQUALS(float64_t)
Expand All @@ -679,13 +680,16 @@ REAL_SPARSE_EQUALS(floatmax_t)
#endif // REAL_SPARSE_EQUALS

template <>
bool SGSparseVectorEntry<complex128_t>::operator==(const SGSparseVectorEntry<complex128_t>& other) const
bool SGSparseVectorEntry<complex128_t>::
operator==(const SGSparseVectorEntry<complex128_t>& other) const
{
if (feat_index != other.feat_index)
return false;

return CMath::fequals<float64_t>(entry.real(), other.entry.real(), LDBL_EPSILON) &&
CMath::fequals<float64_t>(entry.imag(), other.entry.imag(), LDBL_EPSILON);
return CMath::fequals<float64_t>(
entry.real(), other.entry.real(), LDBL_EPSILON) &&
CMath::fequals<float64_t>(
entry.imag(), other.entry.imag(), LDBL_EPSILON);
}

template class SGSparseVector<bool>;
Expand Down
4 changes: 1 addition & 3 deletions src/shogun/lib/SGSparseVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,10 @@ template <class T> class SGSparseVector : public SGReferencedData
void display_vector(const char* name="vector",
const char* prefix="");


/** Pointer identify comparison.
* @return true iff length and pointer are equal
*/
inline bool
operator==(const SGSparseVector<T>& other) const;
inline bool operator==(const SGSparseVector<T>& other) const;

bool equals(const SGSparseVector<T>& other) const;

Expand Down
27 changes: 14 additions & 13 deletions src/shogun/mathematics/Math.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,27 +303,28 @@ class CMath : public CSGObject
* @param eps threshold for values to be equal/different
* @return true if values are equal within eps accuracy, false if not.
*/
template <class T, class = typename std::enable_if<std::is_floating_point<T>::value>::type>
static inline bool fequals(const T& a, const T& b,
const float64_t eps_)
{
// global fequals epsilon might override passed one
// hack for lossy serialization formats
float64_t eps = std::max(eps_, get_global_fequals_epsilon());

const T absA = CMath::abs<T>(a);
const T absB = CMath::abs<T>(b);
template <class T, class = typename std::enable_if<
std::is_floating_point<T>::value>::type>
static inline bool
fequals(const T& a, const T& b, const float64_t eps_)
{
// global fequals epsilon might override passed one
// hack for lossy serialization formats
float64_t eps = std::max(eps_, get_global_fequals_epsilon());

const T absA = CMath::abs<T>(a);
const T absB = CMath::abs<T>(b);
const T diff = CMath::abs<T>((a-b));

// Handle this separately since NAN is unordered
if (CMath::is_nan((float64_t)a) && CMath::is_nan((float64_t)b))
return true;

// Required for JSON Serialization Tests
if (get_global_fequals_tolerant())
return CMath::fequals_abs<T>(a, b, eps);
if (get_global_fequals_tolerant())
return CMath::fequals_abs<T>(a, b, eps);

// handles float32_t and float64_t separately
// handles float32_t and float64_t separately
T comp = (std::is_same<float32_t, T>::value) ? CMath::F_MIN_NORM_VAL32 : CMath::F_MIN_NORM_VAL64;

if (a == b)
Expand Down
6 changes: 3 additions & 3 deletions tests/meta/tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ int main(int argc, const char *argv[])
bool equal = a->equals(a_ref);

// print comparison output only if different as it it slow
if (!equal)
if (!equal)
{
a->get_global_io()->set_loglevel(MSG_DEBUG);
a->equals(a_ref);
}
SG_UNREF(f);

SG_UNREF(f);
SG_UNREF(f_ref);
SG_UNREF(a);
SG_UNREF(a_ref);
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/base/Parameter_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,9 @@ TEST(TParameter,copy_SGMATRIX_SPARSE_same_size)
TParameter* param1=new TParameter(&type, &mat1.sparse_matrix, "", "");
TParameter* param2=new TParameter(&type, &mat2.sparse_matrix, "", "");

// EXPECT_FALSE(param1->equals(param2, accuracy));
// EXPECT_FALSE(param1->equals(param2, accuracy));
EXPECT_TRUE(param1->copy(param2));
// EXPECT_TRUE(param1->equals(param2, accuracy));
// EXPECT_TRUE(param1->equals(param2, accuracy));

EXPECT_EQ(mat1[0].num_feat_entries, mat2[0].num_feat_entries);
EXPECT_EQ(mat1[0].features[0].feat_index, mat2[0].features[0].feat_index);
Expand Down Expand Up @@ -787,9 +787,9 @@ TEST(TParameter,copy_SGMATRIX_SPARSE_different_size)
TParameter* param1=new TParameter(&type, &mat1.sparse_matrix, "", "");
TParameter* param2=new TParameter(&type, &mat2.sparse_matrix, "", "");

// EXPECT_FALSE(param1->equals(param2, accuracy));
// EXPECT_FALSE(param1->equals(param2, accuracy));
EXPECT_TRUE(param1->copy(param2));
// EXPECT_TRUE(param1->equals(param2, accuracy));
// EXPECT_TRUE(param1->equals(param2, accuracy));

EXPECT_EQ(mat1[0].num_feat_entries, mat2[0].num_feat_entries);
EXPECT_EQ(mat1[0].features[0].feat_index, mat2[0].features[0].feat_index);
Expand Down Expand Up @@ -825,9 +825,9 @@ TEST(TParameter,copy_SGMATRIX_SPARSE_target_empty)
TParameter* param1=new TParameter(&type, &mat1.sparse_matrix, "", "");
TParameter* param2=new TParameter(&type, &mat2.sparse_matrix, "", "");

// EXPECT_FALSE(param1->equals(param2, accuracy));
// EXPECT_FALSE(param1->equals(param2, accuracy));
EXPECT_TRUE(param1->copy(param2));
// EXPECT_TRUE(param1->equals(param2, accuracy));
// EXPECT_TRUE(param1->equals(param2, accuracy));

delete param1;
delete param2;
Expand All @@ -846,7 +846,7 @@ TEST(TParameter,copy_SGMATRIX_SPARSE_source_and_target_empty)
TParameter* param2=new TParameter(&type, &mat2.sparse_matrix, "", "");

EXPECT_TRUE(param1->copy(param2));
// EXPECT_TRUE(param1->equals(param2, accuracy));
// EXPECT_TRUE(param1->equals(param2, accuracy));

delete param1;
delete param2;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/lib/SGMatrix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ TEST(SGMatrixTest,equals_different_size)
EXPECT_FALSE(a.equals(b));
}

TEST(SGMatrixTest,equals_both_empty)
TEST(SGMatrixTest, equals_both_empty)
{
SGMatrix<float64_t> a;
SGMatrix<float64_t> b;
Expand Down
13 changes: 7 additions & 6 deletions tests/unit/lib/SGSparseVector_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -515,13 +515,15 @@ class SGSparseVectorEquals : public ::testing::Test
SGSparseVector<T> v1_ = SGSparseVector<T>(1);
SGSparseVector<T> v2_ = SGSparseVector<T>(1);
};
typedef ::testing::Types<int16_t, int32_t, int64_t, float32_t, float64_t, floatmax_t, complex128_t> SGSparseVectorEqualsTypes;
typedef ::testing::Types<int16_t, int32_t, int64_t, float32_t, float64_t,
floatmax_t, complex128_t>
SGSparseVectorEqualsTypes;
TYPED_TEST_CASE(SGSparseVectorEquals, SGSparseVectorEqualsTypes);

TYPED_TEST(SGSparseVectorEquals, equals_same_dim)
{
auto& v1=this->v1_;
auto& v2=this->v2_;
auto& v1 = this->v1_;
auto& v2 = this->v2_;

v1.features[0].feat_index = 1;
v1.features[0].entry = 1;
Expand All @@ -548,10 +550,9 @@ TYPED_TEST(SGSparseVectorEquals, equals_same_dim)

TYPED_TEST(SGSparseVectorEquals, equals_different_dim)
{
auto& v1=this->v1_;
auto& v2=this->v2_;
auto& v1 = this->v1_;
auto& v2 = this->v2_;

EXPECT_FALSE(v1.equals(v2));
EXPECT_FALSE(v2.equals(v1));
}

0 comments on commit 4203d62

Please sign in to comment.