tesseract  4.00.00dev
helpers.h File Reference
#include <stdio.h>
#include <string.h>
#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 sort_floats (const void *arg1, const void *arg2)
 
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)
 
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 82 of file helpers.h.

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

◆ ClipToRange()

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

Definition at line 122 of file helpers.h.

122  {
123  if (x < lower_bound)
124  return lower_bound;
125  if (x > upper_bound)
126  return upper_bound;
127  return x;
128 }

◆ DivRounded()

int DivRounded ( int  a,
int  b 
)
inline

Definition at line 173 of file helpers.h.

173  {
174  if (b < 0) return -DivRounded(a, -b);
175  return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;
176 }
int DivRounded(int a, int b)
Definition: helpers.h:173

◆ IntCastRounded()

int IntCastRounded ( double  x)
inline

Definition at line 179 of file helpers.h.

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

◆ IntersectRange()

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

Definition at line 153 of file helpers.h.

154  {
155  if (lower1 > *lower2)
156  *lower2 = lower1;
157  if (upper1 < *upper2)
158  *upper2 = upper1;
159 }

◆ Modulo()

int Modulo ( int  a,
int  b 
)
inline

Definition at line 164 of file helpers.h.

164  {
165  return (a % b + b) % b;
166 }

◆ Reverse16()

void Reverse16 ( void *  ptr)
inline

Definition at line 195 of file helpers.h.

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

◆ Reverse32()

void Reverse32 ( void *  ptr)
inline

Definition at line 200 of file helpers.h.

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

◆ Reverse64()

void Reverse64 ( void *  ptr)
inline

Definition at line 205 of file helpers.h.

205  {
206  ReverseN(ptr, 8);
207 }
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:184

◆ ReverseN()

void ReverseN ( void *  ptr,
int  num_bytes 
)
inline

Definition at line 184 of file helpers.h.

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

◆ RoundUp()

int RoundUp ( int  n,
int  block_size 
)
inline

Definition at line 116 of file helpers.h.

116  {
117  return block_size * ((n + block_size - 1) / block_size);
118 }

◆ SkipNewline()

void SkipNewline ( FILE *  file)
inline

Definition at line 91 of file helpers.h.

91  {
92  if (fgetc(file) != '\n') fseek(file, -1, SEEK_CUR);
93 }
#define SEEK_CUR
Definition: ioapi.c:21

◆ sort_floats()

int sort_floats ( const void *  arg1,
const void *  arg2 
)
inline

Definition at line 104 of file helpers.h.

104  {
105  float diff = *((float *) arg1) - *((float *) arg2);
106  if (diff > 0) {
107  return 1;
108  } else if (diff < 0) {
109  return -1;
110  } else {
111  return 0;
112  }
113 }

◆ Swap()

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

Definition at line 97 of file helpers.h.

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

◆ UpdateRange() [1/2]

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

Definition at line 132 of file helpers.h.

132  {
133  if (x < *lower_bound)
134  *lower_bound = x;
135  if (x > *upper_bound)
136  *upper_bound = x;
137 }

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

142  {
143  if (x_lo < *lower_bound)
144  *lower_bound = x_lo;
145  if (x_hi > *upper_bound)
146  *upper_bound = x_hi;
147 }