Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
helpers.h File Reference
#include <stdio.h>
#include <string.h>

Go to the source code of this file.

Functions

void chomp_string (char *str)
 
void SkipNewline (FILE *file)
 
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 32 of file helpers.h.

32  {
33  int last_index = strlen(str) - 1;
34  while (last_index >= 0 &&
35  (str[last_index] == '\n' || str[last_index] == '\r')) {
36  str[last_index--] = '\0';
37  }
38 }
template<typename T >
T ClipToRange ( const T &  x,
const T &  lower_bound,
const T &  upper_bound 
)
inline

Definition at line 64 of file helpers.h.

64  {
65  if (x < lower_bound)
66  return lower_bound;
67  if (x > upper_bound)
68  return upper_bound;
69  return x;
70 }
int DivRounded ( int  a,
int  b 
)
inline

Definition at line 115 of file helpers.h.

115  {
116  if (b < 0) return -DivRounded(a, -b);
117  return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;
118 }
int DivRounded(int a, int b)
Definition: helpers.h:115
int IntCastRounded ( double  x)
inline

Definition at line 121 of file helpers.h.

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

Definition at line 95 of file helpers.h.

96  {
97  if (lower1 > *lower2)
98  *lower2 = lower1;
99  if (upper1 < *upper2)
100  *upper2 = upper1;
101 }
int Modulo ( int  a,
int  b 
)
inline

Definition at line 106 of file helpers.h.

106  {
107  return (a % b + b) % b;
108 }
void Reverse16 ( void *  ptr)
inline

Definition at line 137 of file helpers.h.

137  {
138  ReverseN(ptr, 2);
139 }
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:126
void Reverse32 ( void *  ptr)
inline

Definition at line 142 of file helpers.h.

142  {
143  ReverseN(ptr, 4);
144 }
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:126
void Reverse64 ( void *  ptr)
inline

Definition at line 147 of file helpers.h.

147  {
148  ReverseN(ptr, 8);
149 }
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:126
void ReverseN ( void *  ptr,
int  num_bytes 
)
inline

Definition at line 126 of file helpers.h.

126  {
127  char *cptr = reinterpret_cast<char *>(ptr);
128  int halfsize = num_bytes / 2;
129  for (int i = 0; i < halfsize; ++i) {
130  char tmp = cptr[i];
131  cptr[i] = cptr[num_bytes - 1 - i];
132  cptr[num_bytes - 1 - i] = tmp;
133  }
134 }
int RoundUp ( int  n,
int  block_size 
)
inline

Definition at line 58 of file helpers.h.

58  {
59  return block_size * ((n + block_size - 1) / block_size);
60 }
void SkipNewline ( FILE *  file)
inline

Definition at line 41 of file helpers.h.

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

Definition at line 46 of file helpers.h.

46  {
47  float diff = *((float *) arg1) - *((float *) arg2);
48  if (diff > 0) {
49  return 1;
50  } else if (diff < 0) {
51  return -1;
52  } else {
53  return 0;
54  }
55 }
template<typename T1 , typename T2 >
void UpdateRange ( const T1 &  x,
T2 *  lower_bound,
T2 *  upper_bound 
)
inline

Definition at line 74 of file helpers.h.

74  {
75  if (x < *lower_bound)
76  *lower_bound = x;
77  if (x > *upper_bound)
78  *upper_bound = x;
79 }
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 83 of file helpers.h.

84  {
85  if (x_lo < *lower_bound)
86  *lower_bound = x_lo;
87  if (x_hi > *upper_bound)
88  *upper_bound = x_hi;
89 }