All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
helpers.h File Reference
#include <stdio.h>
#include <string.h>
#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

void chomp_string ( char *  str)
inline

Definition at line 75 of file helpers.h.

75  {
76  int last_index = strlen(str) - 1;
77  while (last_index >= 0 &&
78  (str[last_index] == '\n' || str[last_index] == '\r')) {
79  str[last_index--] = '\0';
80  }
81 }
template<typename T >
T ClipToRange ( const T &  x,
const T &  lower_bound,
const T &  upper_bound 
)
inline

Definition at line 115 of file helpers.h.

115  {
116  if (x < lower_bound)
117  return lower_bound;
118  if (x > upper_bound)
119  return upper_bound;
120  return x;
121 }
int DivRounded ( int  a,
int  b 
)
inline

Definition at line 166 of file helpers.h.

166  {
167  if (b < 0) return -DivRounded(a, -b);
168  return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;
169 }
int DivRounded(int a, int b)
Definition: helpers.h:166
int IntCastRounded ( double  x)
inline

Definition at line 172 of file helpers.h.

172  {
173  return x >= 0.0 ? static_cast<int>(x + 0.5) : -static_cast<int>(-x + 0.5);
174 }
template<typename T >
void IntersectRange ( const T &  lower1,
const T &  upper1,
T *  lower2,
T *  upper2 
)
inline

Definition at line 146 of file helpers.h.

147  {
148  if (lower1 > *lower2)
149  *lower2 = lower1;
150  if (upper1 < *upper2)
151  *upper2 = upper1;
152 }
int Modulo ( int  a,
int  b 
)
inline

Definition at line 157 of file helpers.h.

157  {
158  return (a % b + b) % b;
159 }
void Reverse16 ( void *  ptr)
inline

Definition at line 188 of file helpers.h.

188  {
189  ReverseN(ptr, 2);
190 }
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177
void Reverse32 ( void *  ptr)
inline

Definition at line 193 of file helpers.h.

193  {
194  ReverseN(ptr, 4);
195 }
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177
void Reverse64 ( void *  ptr)
inline

Definition at line 198 of file helpers.h.

198  {
199  ReverseN(ptr, 8);
200 }
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177
void ReverseN ( void *  ptr,
int  num_bytes 
)
inline

Definition at line 177 of file helpers.h.

177  {
178  char *cptr = reinterpret_cast<char *>(ptr);
179  int halfsize = num_bytes / 2;
180  for (int i = 0; i < halfsize; ++i) {
181  char tmp = cptr[i];
182  cptr[i] = cptr[num_bytes - 1 - i];
183  cptr[num_bytes - 1 - i] = tmp;
184  }
185 }
int RoundUp ( int  n,
int  block_size 
)
inline

Definition at line 109 of file helpers.h.

109  {
110  return block_size * ((n + block_size - 1) / block_size);
111 }
void SkipNewline ( FILE *  file)
inline

Definition at line 84 of file helpers.h.

84  {
85  if (fgetc(file) != '\n') fseek(file, -1, SEEK_CUR);
86 }
int sort_floats ( const void *  arg1,
const void *  arg2 
)
inline

Definition at line 97 of file helpers.h.

97  {
98  float diff = *((float *) arg1) - *((float *) arg2);
99  if (diff > 0) {
100  return 1;
101  } else if (diff < 0) {
102  return -1;
103  } else {
104  return 0;
105  }
106 }
template<typename T >
void Swap ( T *  p1,
T *  p2 
)
inline

Definition at line 90 of file helpers.h.

90  {
91  T tmp(*p2);
92  *p2 = *p1;
93  *p1 = tmp;
94 }
template<typename T1 , typename T2 >
void UpdateRange ( const T1 &  x,
T2 *  lower_bound,
T2 *  upper_bound 
)
inline

Definition at line 125 of file helpers.h.

125  {
126  if (x < *lower_bound)
127  *lower_bound = x;
128  if (x > *upper_bound)
129  *upper_bound = x;
130 }
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 134 of file helpers.h.

135  {
136  if (x_lo < *lower_bound)
137  *lower_bound = x_lo;
138  if (x_hi > *upper_bound)
139  *upper_bound = x_hi;
140 }