tesseract  4.1.1
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 77 of file helpers.h.

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

◆ ClipToRange()

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

Definition at line 108 of file helpers.h.

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

◆ DivRounded()

int DivRounded ( int  a,
int  b 
)
inline

Definition at line 167 of file helpers.h.

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

◆ IntCastRounded() [1/2]

int IntCastRounded ( double  x)
inline

Definition at line 175 of file helpers.h.

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

◆ IntCastRounded() [2/2]

int IntCastRounded ( float  x)
inline

Definition at line 180 of file helpers.h.

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

◆ IntersectRange()

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

Definition at line 145 of file helpers.h.

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

◆ Modulo()

int Modulo ( int  a,
int  b 
)
inline

Definition at line 158 of file helpers.h.

158  {
159  return (a % b + b) % b;
160 }

◆ Reverse16()

void Reverse16 ( void *  ptr)
inline

Definition at line 197 of file helpers.h.

197  {
198  ReverseN(ptr, 2);
199 }

◆ Reverse32()

void Reverse32 ( void *  ptr)
inline

Definition at line 202 of file helpers.h.

202  {
203  ReverseN(ptr, 4);
204 }

◆ Reverse64()

void Reverse64 ( void *  ptr)
inline

Definition at line 207 of file helpers.h.

207  {
208  ReverseN(ptr, 8);
209 }

◆ ReverseN()

void ReverseN ( void *  ptr,
int  num_bytes 
)
inline

Definition at line 185 of file helpers.h.

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

◆ RoundUp()

int RoundUp ( int  n,
int  block_size 
)
inline

Definition at line 102 of file helpers.h.

102  {
103  return block_size * ((n + block_size - 1) / block_size);
104 }

◆ SkipNewline()

void SkipNewline ( FILE *  file)
inline

Definition at line 86 of file helpers.h.

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

◆ Swap()

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

Definition at line 95 of file helpers.h.

95  {
96  T tmp(*p2);
97  *p2 = *p1;
98  *p1 = tmp;
99 }

◆ UpdateRange() [1/2]

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

Definition at line 120 of file helpers.h.

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

◆ 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 131 of file helpers.h.

132  {
133  if (x_lo < *lower_bound) {
134  *lower_bound = x_lo;
135  }
136  if (x_hi > *upper_bound) {
137  *upper_bound = x_hi;
138  }
139 }
DivRounded
int DivRounded(int a, int b)
Definition: helpers.h:167
ReverseN
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:185