Skip to content

Commit

Permalink
apply autoformater
Browse files Browse the repository at this point in the history
  • Loading branch information
karlnapf committed Jan 8, 2018
1 parent 60531cb commit 2fd32e4
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 138 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
37 changes: 19 additions & 18 deletions src/shogun/lib/SGVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
#include <shogun/lib/SGReferencedData.h>
#include <shogun/io/File.h>

#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/linalg/LinalgNamespace.h>
#include <shogun/mathematics/lapack.h>
#include <algorithm>
#include <limits>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/lapack.h>
#include <shogun/mathematics/linalg/LinalgNamespace.h>

#define COMPLEX128_ERROR_NOARG(function) \
template <> \
Expand Down Expand Up @@ -409,21 +409,22 @@ bool SGVector<T>::equals(SGVector<T>& other) const
}

#ifndef REAL_EQUALS
#define REAL_EQUALS(real_t) \
template <> \
bool SGVector<real_t>::equals(SGVector<real_t>& other) const \
{ \
assert_on_cpu(); \
if (other.vlen!=vlen) \
return false; \
\
for (index_t i=0; i<vlen; ++i) \
{ \
if (!CMath::fequals(vector[i], other.vector[i], \
std::numeric_limits<real_t>::epsilon())) \
return false; \
} \
return true; \
#define REAL_EQUALS(real_t) \
template <> \
bool SGVector<real_t>::equals(SGVector<real_t>& other) const \
{ \
assert_on_cpu(); \
if (other.vlen != vlen) \
return false; \
\
for (index_t i = 0; i < vlen; ++i) \
{ \
if (!CMath::fequals( \
vector[i], other.vector[i], \
std::numeric_limits<real_t>::epsilon())) \
return false; \
} \
return true; \
}
REAL_EQUALS(float32_t)
REAL_EQUALS(float64_t)
Expand Down
37 changes: 19 additions & 18 deletions src/shogun/lib/any.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,33 @@
#include <shogun/lib/any.h>
#include <shogun/mathematics/Math.h>


namespace shogun
{

namespace any_detail
{
namespace any_detail
{

#ifndef REAL_COMPARE_IMPL
#define REAL_COMPARE_IMPL(real_t) \
auto compare_impl(more_important, real_t& lhs, real_t& rhs) -> bool \
{ \
SG_SDEBUG("Comparing using fequals<" #real_t ">(lhs, rhs).\n"); \
return CMath::fequals(lhs, rhs, std::numeric_limits<real_t>::epsilon()); \
#define REAL_COMPARE_IMPL(real_t) \
auto compare_impl(more_important, real_t& lhs, real_t& rhs)->bool \
{ \
SG_SDEBUG("Comparing using fequals<" #real_t ">(lhs, rhs).\n"); \
return CMath::fequals( \
lhs, rhs, std::numeric_limits<real_t>::epsilon()); \
}

REAL_COMPARE_IMPL(float32_t)
REAL_COMPARE_IMPL(float64_t)
REAL_COMPARE_IMPL(floatmax_t)
REAL_COMPARE_IMPL(float32_t)
REAL_COMPARE_IMPL(float64_t)
REAL_COMPARE_IMPL(floatmax_t)
#undef REAL_COMPARE_IMPL
#endif // REAL_COMPARE_IMPL

auto compare_impl(more_important, complex128_t& lhs, complex128_t& rhs) -> decltype(lhs == rhs)
{
SG_SDEBUG("Comparing using fequals<complex128_t>(lhs, rhs).\n");
return CMath::fequals(lhs.real(), rhs.real(), LDBL_EPSILON) && CMath::fequals(lhs.imag(), rhs.imag(), LDBL_EPSILON);
}

}
auto compare_impl(more_important, complex128_t& lhs, complex128_t& rhs)
-> decltype(lhs == rhs)
{
SG_SDEBUG("Comparing using fequals<complex128_t>(lhs, rhs).\n");
return CMath::fequals(lhs.real(), rhs.real(), LDBL_EPSILON) &&
CMath::fequals(lhs.imag(), rhs.imag(), LDBL_EPSILON);
}
}
}
16 changes: 10 additions & 6 deletions src/shogun/lib/any.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
#ifndef _ANY_H_
#define _ANY_H_

#include <shogun/io/SGIO.h>
#include <shogun/base/init.h>
#include <shogun/io/SGIO.h>

#include <limits>
#include <stdexcept>
#include <string.h>
#include <string>
#include <typeinfo>
#include <limits>
#ifdef HAVE_CXA_DEMANGLE
#include <cxxabi.h>
#endif
Expand Down Expand Up @@ -147,10 +147,14 @@ namespace shogun
return lhs == rhs;
}

auto compare_impl(more_important, float32_t& lhs, float32_t& rhs) -> bool;
auto compare_impl(more_important, float64_t& lhs, float64_t& rhs) -> bool;
auto compare_impl(more_important, floatmax_t& lhs, floatmax_t& rhs) -> bool;
auto compare_impl(more_important, complex128_t& lhs, complex128_t& rhs) -> bool;
auto compare_impl(more_important, float32_t& lhs, float32_t& rhs)
-> bool;
auto compare_impl(more_important, float64_t& lhs, float64_t& rhs)
-> bool;
auto compare_impl(more_important, floatmax_t& lhs, floatmax_t& rhs)
-> bool;
auto compare_impl(more_important, complex128_t& lhs, complex128_t& rhs)
-> bool;

template <class T>
auto compare_impl(more_important, T& lhs, T& rhs)
Expand Down
23 changes: 12 additions & 11 deletions src/shogun/mathematics/Math.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,16 @@ 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);
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));

Expand All @@ -320,8 +321,8 @@ class CMath : public CSGObject
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
T comp = (std::is_same<float32_t, T>::value) ? CMath::F_MIN_NORM_VAL32 : CMath::F_MIN_NORM_VAL64;
Expand Down

0 comments on commit 2fd32e4

Please sign in to comment.