helpers.h
Go to the documentation of this file.
1 /* -*-C-*-
2  ********************************************************************************
3  *
4  * File: helpers.h
5  * Description: General utility functions
6  * Author: Daria Antonova
7  * Created: Wed Apr 8 14:37:00 2009
8  * Language: C++
9  * Package: N/A
10  * Status: Reusable Software Component
11  *
14  ** you may not use this file except in compliance with the License.
15  ** You may obtain a copy of the License at
17  ** Unless required by applicable law or agreed to in writing, software
19  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  ** See the License for the specific language governing permissions and
21  ** limitations under the License.
22  *
23  ********************************************************************************/
24
25 #ifndef TESSERACT_CCUTIL_HELPERS_H_
26 #define TESSERACT_CCUTIL_HELPERS_H_
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "host.h"
32
33 // TODO(rays) Put the rest of the helpers in the namespace.
34 namespace tesseract {
35
36 // A simple linear congruential random number generator, using Knuth's
37 // constants from:
38 // http://en.wikipedia.org/wiki/Linear_congruential_generator.
39 class TRand {
40  public:
41  TRand() : seed_(1) {}
42  // Sets the seed to the given value.
43  void set_seed(uinT64 seed) {
44  seed_ = seed;
45  }
46
47  // Returns an integer in the range 0 to MAX_INT32.
49  Iterate();
50  return seed_ >> 33;
51  }
52  // Returns a floating point value in the range [-range, range].
53  double SignedRand(double range) {
54  return range * 2.0 * IntRand() / MAX_INT32 - range;
55  }
56  // Returns a floating point value in the range [0, range].
57  double UnsignedRand(double range) {
58  return range * IntRand() / MAX_INT32;
59  }
60
61  private:
62  // Steps the generator to the next value.
63  void Iterate() {
64  seed_ *= 6364136223846793005;
65  seed_ += 1442695040888963407;
66  }
67
68  // The current value of the seed.
69  uinT64 seed_;
70 };
71
72 } // namespace tesseract
73
74 // Remove newline (if any) at the end of the string.
75 inline void chomp_string(char *str) {
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 }
82
83 // Advance the current pointer of the file if it points to a newline character.
84 inline void SkipNewline(FILE *file) {
85  if (fgetc(file) != '\n') fseek(file, -1, SEEK_CUR);
86 }
87
88 // Swaps the two args pointed to by the pointers.
89 // Operator= and copy constructor must work on T.
90 template<typename T> inline void Swap(T* p1, T* p2) {
91  T tmp(*p2);
92  *p2 = *p1;
93  *p1 = tmp;
94 }
95
96 // qsort function to sort 2 floats.
97 inline int sort_floats(const void *arg1, const void *arg2) {
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 }
107
108 // return the smallest multiple of block_size greater than or equal to n.
109 inline int RoundUp(int n, int block_size) {
110  return block_size * ((n + block_size - 1) / block_size);
111 }
112
113 // Clip a numeric value to the interval [lower_bound, upper_bound].
114 template<typename T>
115 inline T ClipToRange(const T& x, const T& lower_bound, const T& upper_bound) {
116  if (x < lower_bound)
117  return lower_bound;
118  if (x > upper_bound)
119  return upper_bound;
120  return x;
121 }
122
123 // Extend the range [lower_bound, upper_bound] to include x.
124 template<typename T1, typename T2>
125 inline void UpdateRange(const T1& x, T2* lower_bound, T2* upper_bound) {
126  if (x < *lower_bound)
127  *lower_bound = x;
128  if (x > *upper_bound)
129  *upper_bound = x;
130 }
131
132 // Decrease lower_bound to be <= x_lo AND increase upper_bound to be >= x_hi.
133 template<typename T1, typename T2>
134 inline void UpdateRange(const T1& x_lo, const T1& x_hi,
135  T2* lower_bound, T2* upper_bound) {
136  if (x_lo < *lower_bound)
137  *lower_bound = x_lo;
138  if (x_hi > *upper_bound)
139  *upper_bound = x_hi;
140 }
141
142 // Intersect the range [*lower2, *upper2] with the range [lower1, upper1],
143 // putting the result back in [*lower2, *upper2].
144 // If non-intersecting ranges are given, we end up with *lower2 > *upper2.
145 template<typename T>
146 inline void IntersectRange(const T& lower1, const T& upper1,
147  T* lower2, T* upper2) {
148  if (lower1 > *lower2)
149  *lower2 = lower1;
150  if (upper1 < *upper2)
151  *upper2 = upper1;
152 }
153
154 // Proper modulo arithmetic operator. Returns a mod b that works for -ve a.
155 // For any integer a and positive b, returns r : 0<=r<b and a=n*b + r for
156 // some integer n.
157 inline int Modulo(int a, int b) {
158  return (a % b + b) % b;
159 }
160
161 // Integer division operator with rounding that works for negative input.
162 // Returns a divided by b, rounded to the nearest integer, without double
163 // counting at 0. With simple rounding 1/3 = 0, 0/3 = 0 -1/3 = 0, -2/3 = 0,
164 // -3/3 = 0 and -4/3 = -1.
165 // I want 1/3 = 0, 0/3 = 0, -1/3 = 0, -2/3 = -1, -3/3 = -1 and -4/3 = -1.
166 inline int DivRounded(int a, int b) {
167  if (b < 0) return -DivRounded(a, -b);
168  return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;
169 }
170
171 // Return a double cast to int with rounding.
172 inline int IntCastRounded(double x) {
173  return x >= 0.0 ? static_cast<int>(x + 0.5) : -static_cast<int>(-x + 0.5);
174 }
175
176 // Reverse the order of bytes in a n byte quantity for big/little-endian switch.
177 inline void ReverseN(void* ptr, int num_bytes) {
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 }
186
187 // Reverse the order of bytes in a 16 bit quantity for big/little-endian switch.
188 inline void Reverse16(void *ptr) {
189  ReverseN(ptr, 2);
190 }
191
192 // Reverse the order of bytes in a 32 bit quantity for big/little-endian switch.
193 inline void Reverse32(void *ptr) {
194  ReverseN(ptr, 4);
195 }
196
197 // Reverse the order of bytes in a 64 bit quantity for big/little-endian switch.
198 inline void Reverse64(void* ptr) {
199  ReverseN(ptr, 8);
200 }
201
202
203 #endif // TESSERACT_CCUTIL_HELPERS_H_
void Reverse32(void *ptr)
Definition: helpers.h:193
void IntersectRange(const T &lower1, const T &upper1, T *lower2, T *upper2)
Definition: helpers.h:146
void UpdateRange(const T1 &x, T2 *lower_bound, T2 *upper_bound)
Definition: helpers.h:125
int Modulo(int a, int b)
Definition: helpers.h:157
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:115
int sort_floats(const void *arg1, const void *arg2)
Definition: helpers.h:97
double SignedRand(double range)
Definition: helpers.h:53
void set_seed(uinT64 seed)
Definition: helpers.h:43
void chomp_string(char *str)
Definition: helpers.h:75
inT32 IntRand()
Definition: helpers.h:48
void SkipNewline(FILE *file)
Definition: helpers.h:84
#define MAX_INT32
Definition: host.h:120
void Reverse16(void *ptr)
Definition: helpers.h:188
double UnsignedRand(double range)
Definition: helpers.h:57
unsigned long long int uinT64
Definition: host.h:109
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177
int DivRounded(int a, int b)
Definition: helpers.h:166
int IntCastRounded(double x)
Definition: helpers.h:172
int RoundUp(int n, int block_size)
Definition: helpers.h:109
void Reverse64(void *ptr)
Definition: helpers.h:198
void Swap(T *p1, T *p2)
Definition: helpers.h:90
int inT32
Definition: host.h:102