Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
helpers: Modernize and format code
Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil committed Mar 31, 2019
1 parent 3bb8f9c commit 8b663e7
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions src/ccutil/helpers.h
Expand Up @@ -27,8 +27,6 @@
#include <functional>
#include <string>

#include "host.h"

// TODO(rays) Put the rest of the helpers in the namespace.
namespace tesseract {

Expand All @@ -37,7 +35,7 @@ namespace tesseract {
// http://en.wikipedia.org/wiki/Linear_congruential_generator.
class TRand {
public:
TRand() : seed_(1) {}
TRand() = default;
// Sets the seed to the given value.
void set_seed(uint64_t seed) {
seed_ = seed;
Expand Down Expand Up @@ -70,13 +68,13 @@ class TRand {
}

// The current value of the seed.
uint64_t seed_;
uint64_t seed_{1};
};

} // namespace tesseract

// Remove newline (if any) at the end of the string.
inline void chomp_string(char *str) {
inline void chomp_string(char* str) {
int last_index = static_cast<int>(strlen(str)) - 1;
while (last_index >= 0 &&
(str[last_index] == '\n' || str[last_index] == '\r')) {
Expand All @@ -85,13 +83,16 @@ inline void chomp_string(char *str) {
}

// Advance the current pointer of the file if it points to a newline character.
inline void SkipNewline(FILE *file) {
if (fgetc(file) != '\n') fseek(file, -1, SEEK_CUR);
inline void SkipNewline(FILE* file) {
if (fgetc(file) != '\n') {
fseek(file, -1, SEEK_CUR);
}
}

// Swaps the two args pointed to by the pointers.
// Operator= and copy constructor must work on T.
template<typename T> inline void Swap(T* p1, T* p2) {
template <typename T>
inline void Swap(T* p1, T* p2) {
T tmp(*p2);
*p2 = *p1;
*p1 = tmp;
Expand All @@ -103,44 +104,52 @@ inline int RoundUp(int n, int block_size) {
}

// Clip a numeric value to the interval [lower_bound, upper_bound].
template<typename T>
template <typename T>
inline T ClipToRange(const T& x, const T& lower_bound, const T& upper_bound) {
if (x < lower_bound)
if (x < lower_bound) {
return lower_bound;
if (x > upper_bound)
}
if (x > upper_bound) {
return upper_bound;
}
return x;
}

// Extend the range [lower_bound, upper_bound] to include x.
template<typename T1, typename T2>
template <typename T1, typename T2>
inline void UpdateRange(const T1& x, T2* lower_bound, T2* upper_bound) {
if (x < *lower_bound)
if (x < *lower_bound) {
*lower_bound = x;
if (x > *upper_bound)
}
if (x > *upper_bound) {
*upper_bound = x;
}
}

// Decrease lower_bound to be <= x_lo AND increase upper_bound to be >= x_hi.
template<typename T1, typename T2>
inline void UpdateRange(const T1& x_lo, const T1& x_hi,
T2* lower_bound, T2* upper_bound) {
if (x_lo < *lower_bound)
template <typename T1, typename T2>
inline void UpdateRange(const T1& x_lo, const T1& x_hi, T2* lower_bound,
T2* upper_bound) {
if (x_lo < *lower_bound) {
*lower_bound = x_lo;
if (x_hi > *upper_bound)
}
if (x_hi > *upper_bound) {
*upper_bound = x_hi;
}
}

// Intersect the range [*lower2, *upper2] with the range [lower1, upper1],
// putting the result back in [*lower2, *upper2].
// If non-intersecting ranges are given, we end up with *lower2 > *upper2.
template<typename T>
inline void IntersectRange(const T& lower1, const T& upper1,
T* lower2, T* upper2) {
if (lower1 > *lower2)
template <typename T>
inline void IntersectRange(const T& lower1, const T& upper1, T* lower2,
T* upper2) {
if (lower1 > *lower2) {
*lower2 = lower1;
if (upper1 < *upper2)
}
if (upper1 < *upper2) {
*upper2 = upper1;
}
}

// Proper modulo arithmetic operator. Returns a mod b that works for -ve a.
Expand All @@ -156,7 +165,9 @@ inline int Modulo(int a, int b) {
// -3/3 = 0 and -4/3 = -1.
// I want 1/3 = 0, 0/3 = 0, -1/3 = 0, -2/3 = -1, -3/3 = -1 and -4/3 = -1.
inline int DivRounded(int a, int b) {
if (b < 0) return -DivRounded(a, -b);
if (b < 0) {
return -DivRounded(a, -b);
}
return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;
}

Expand All @@ -167,7 +178,7 @@ inline int IntCastRounded(double x) {

// Return a float cast to int with rounding.
inline int IntCastRounded(float x) {
return x >= 0.0f ? static_cast<int>(x + 0.5f) : -static_cast<int>(-x + 0.5f);
return x >= 0.0F ? static_cast<int>(x + 0.5F) : -static_cast<int>(-x + 0.5F);
}

// Reverse the order of bytes in a n byte quantity for big/little-endian switch.
Expand All @@ -183,12 +194,12 @@ inline void ReverseN(void* ptr, int num_bytes) {
}

// Reverse the order of bytes in a 16 bit quantity for big/little-endian switch.
inline void Reverse16(void *ptr) {
inline void Reverse16(void* ptr) {
ReverseN(ptr, 2);
}

// Reverse the order of bytes in a 32 bit quantity for big/little-endian switch.
inline void Reverse32(void *ptr) {
inline void Reverse32(void* ptr) {
ReverseN(ptr, 4);
}

Expand All @@ -197,5 +208,4 @@ inline void Reverse64(void* ptr) {
ReverseN(ptr, 8);
}


#endif // TESSERACT_CCUTIL_HELPERS_H_
#endif // TESSERACT_CCUTIL_HELPERS_H_

0 comments on commit 8b663e7

Please sign in to comment.