tesseract  5.0.0-alpha-619-ge9db
helpers.h File Reference
#include <cassert>
#include <cstdio>
#include <cstring>
#include <functional>
#include <string>

Go to the source code of this file.

Classes

class  tesseract::TRand
 

Namespaces

 tesseract
 

Functions

void chomp_string (char *str)
 
void SkipNewline (FILE *file)
 
template<typename T >
void Swap (T *p1, T *p2)
 
int RoundUp (int n, int block_size)
 
template<typename T >
ClipToRange (const T &x, const T &lower_bound, const T &upper_bound)
 
template<typename T1 , typename T2 >
void UpdateRange (const T1 &x, T2 *lower_bound, T2 *upper_bound)
 
template<typename T1 , typename T2 >
void UpdateRange (const T1 &x_lo, const T1 &x_hi, T2 *lower_bound, T2 *upper_bound)
 
template<typename T >
void IntersectRange (const T &lower1, const T &upper1, T *lower2, T *upper2)
 
int Modulo (int a, int b)
 
int DivRounded (int a, int b)
 
int IntCastRounded (double x)
 
int IntCastRounded (float x)
 
void ReverseN (void *ptr, int num_bytes)
 
void Reverse16 (void *ptr)
 
void Reverse32 (void *ptr)
 
void Reverse64 (void *ptr)
 

Function Documentation

◆ chomp_string()

void chomp_string ( char *  str)
inline

Definition at line 75 of file helpers.h.

76  {
77  int last_index = static_cast<int>(strlen(str)) - 1;
78  while (last_index >= 0 &&
79  (str[last_index] == '\n' || str[last_index] == '\r')) {
80  str[last_index--] = '\0';
81  }

◆ ClipToRange()

template<typename T >
T ClipToRange ( const T &  x,
const T &  lower_bound,
const T &  upper_bound 
)
inline

Definition at line 106 of file helpers.h.

107  {
108  if (x < lower_bound) {
109  return lower_bound;
110  }
111  if (x > upper_bound) {
112  return upper_bound;
113  }
114  return x;

◆ DivRounded()

int DivRounded ( int  a,
int  b 
)
inline

Definition at line 165 of file helpers.h.

166  {
167  if (b < 0) {
168  return -DivRounded(a, -b);
169  }
170  return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;

◆ IntCastRounded() [1/2]

int IntCastRounded ( double  x)
inline

Definition at line 173 of file helpers.h.

174  {
175  return x >= 0.0 ? static_cast<int>(x + 0.5) : -static_cast<int>(-x + 0.5);

◆ IntCastRounded() [2/2]

int IntCastRounded ( float  x)
inline

Definition at line 178 of file helpers.h.

179  {
180  return x >= 0.0F ? static_cast<int>(x + 0.5F) : -static_cast<int>(-x + 0.5F);

◆ IntersectRange()

template<typename T >
void IntersectRange ( const T &  lower1,
const T &  upper1,
T *  lower2,
T *  upper2 
)
inline

Definition at line 143 of file helpers.h.

145  {
146  if (lower1 > *lower2) {
147  *lower2 = lower1;
148  }
149  if (upper1 < *upper2) {
150  *upper2 = upper1;
151  }

◆ Modulo()

int Modulo ( int  a,
int  b 
)
inline

Definition at line 156 of file helpers.h.

157  {
158  return (a % b + b) % b;

◆ Reverse16()

void Reverse16 ( void *  ptr)
inline

Definition at line 195 of file helpers.h.

196  {
197  ReverseN(ptr, 2);

◆ Reverse32()

void Reverse32 ( void *  ptr)
inline

Definition at line 200 of file helpers.h.

201  {
202  ReverseN(ptr, 4);

◆ Reverse64()

void Reverse64 ( void *  ptr)
inline

Definition at line 205 of file helpers.h.

206  {
207  ReverseN(ptr, 8);

◆ ReverseN()

void ReverseN ( void *  ptr,
int  num_bytes 
)
inline

Definition at line 183 of file helpers.h.

184  {
185  assert(num_bytes == 1 || num_bytes == 2 || num_bytes == 4 || num_bytes == 8);
186  char* cptr = static_cast<char*>(ptr);
187  int halfsize = num_bytes / 2;
188  for (int i = 0; i < halfsize; ++i) {
189  char tmp = cptr[i];
190  cptr[i] = cptr[num_bytes - 1 - i];
191  cptr[num_bytes - 1 - i] = tmp;
192  }

◆ RoundUp()

int RoundUp ( int  n,
int  block_size 
)
inline

Definition at line 100 of file helpers.h.

101  {
102  return block_size * ((n + block_size - 1) / block_size);

◆ SkipNewline()

void SkipNewline ( FILE *  file)
inline

Definition at line 84 of file helpers.h.

85  {
86  if (fgetc(file) != '\n') {
87  fseek(file, -1, SEEK_CUR);
88  }

◆ Swap()

template<typename T >
void Swap ( T *  p1,
T *  p2 
)
inline

Definition at line 93 of file helpers.h.

94  {
95  T tmp(*p2);
96  *p2 = *p1;
97  *p1 = tmp;

◆ UpdateRange() [1/2]

template<typename T1 , typename T2 >
void UpdateRange ( const T1 x,
T2 lower_bound,
T2 upper_bound 
)
inline

Definition at line 118 of file helpers.h.

119  {
120  if (x < *lower_bound) {
121  *lower_bound = x;
122  }
123  if (x > *upper_bound) {
124  *upper_bound = x;
125  }

◆ UpdateRange() [2/2]

template<typename T1 , typename T2 >
void UpdateRange ( const T1 x_lo,
const T1 x_hi,
T2 lower_bound,
T2 upper_bound 
)
inline

Definition at line 129 of file helpers.h.

131  {
132  if (x_lo < *lower_bound) {
133  *lower_bound = x_lo;
134  }
135  if (x_hi > *upper_bound) {
136  *upper_bound = x_hi;
137  }
file
Definition: include_gunit.h:22
tesstrain_utils.int
int
Definition: tesstrain_utils.py:154
DivRounded
int DivRounded(int a, int b)
Definition: helpers.h:165
ReverseN
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:183