tesseract  4.0.0-1-g2a2b
helpers.h File Reference
#include <cassert>
#include <cstdio>
#include <cstring>
#include <functional>
#include <string>
#include "host.h"

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

83  {
84  int last_index = static_cast<int>(strlen(str)) - 1;
85  while (last_index >= 0 &&
86  (str[last_index] == '\n' || str[last_index] == '\r')) {
87  str[last_index--] = '\0';
88  }
89 }

◆ ClipToRange()

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

Definition at line 111 of file helpers.h.

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

◆ DivRounded()

int DivRounded ( int  a,
int  b 
)
inline

Definition at line 162 of file helpers.h.

162  {
163  if (b < 0) return -DivRounded(a, -b);
164  return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;
165 }
int DivRounded(int a, int b)
Definition: helpers.h:162

◆ IntCastRounded() [1/2]

int IntCastRounded ( double  x)
inline

Definition at line 168 of file helpers.h.

168  {
169  return x >= 0.0 ? static_cast<int>(x + 0.5) : -static_cast<int>(-x + 0.5);
170 }

◆ IntCastRounded() [2/2]

int IntCastRounded ( float  x)
inline

Definition at line 173 of file helpers.h.

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

◆ IntersectRange()

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

Definition at line 142 of file helpers.h.

143  {
144  if (lower1 > *lower2)
145  *lower2 = lower1;
146  if (upper1 < *upper2)
147  *upper2 = upper1;
148 }

◆ Modulo()

int Modulo ( int  a,
int  b 
)
inline

Definition at line 153 of file helpers.h.

153  {
154  return (a % b + b) % b;
155 }

◆ Reverse16()

void Reverse16 ( void *  ptr)
inline

Definition at line 190 of file helpers.h.

190  {
191  ReverseN(ptr, 2);
192 }
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:178

◆ Reverse32()

void Reverse32 ( void *  ptr)
inline

Definition at line 195 of file helpers.h.

195  {
196  ReverseN(ptr, 4);
197 }
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:178

◆ Reverse64()

void Reverse64 ( void *  ptr)
inline

Definition at line 200 of file helpers.h.

200  {
201  ReverseN(ptr, 8);
202 }
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:178

◆ ReverseN()

void ReverseN ( void *  ptr,
int  num_bytes 
)
inline

Definition at line 178 of file helpers.h.

178  {
179  assert(num_bytes == 1 || num_bytes == 2 || num_bytes == 4 || num_bytes == 8);
180  char* cptr = static_cast<char*>(ptr);
181  int halfsize = num_bytes / 2;
182  for (int i = 0; i < halfsize; ++i) {
183  char tmp = cptr[i];
184  cptr[i] = cptr[num_bytes - 1 - i];
185  cptr[num_bytes - 1 - i] = tmp;
186  }
187 }

◆ RoundUp()

int RoundUp ( int  n,
int  block_size 
)
inline

Definition at line 105 of file helpers.h.

105  {
106  return block_size * ((n + block_size - 1) / block_size);
107 }

◆ SkipNewline()

void SkipNewline ( FILE *  file)
inline

Definition at line 92 of file helpers.h.

92  {
93  if (fgetc(file) != '\n') fseek(file, -1, SEEK_CUR);
94 }

◆ Swap()

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

Definition at line 98 of file helpers.h.

98  {
99  T tmp(*p2);
100  *p2 = *p1;
101  *p1 = tmp;
102 }

◆ UpdateRange() [1/2]

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

Definition at line 121 of file helpers.h.

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

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

131  {
132  if (x_lo < *lower_bound)
133  *lower_bound = x_lo;
134  if (x_hi > *upper_bound)
135  *upper_bound = x_hi;
136 }