tesseract  4.00.00dev
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  *
12  * (c) Copyright 2009, Google Inc.
13  ** Licensed under the Apache License, Version 2.0 (the "License");
14  ** you may not use this file except in compliance with the License.
15  ** You may obtain a copy of the License at
16  ** http://www.apache.org/licenses/LICENSE-2.0
17  ** Unless required by applicable law or agreed to in writing, software
18  ** distributed under the License is distributed on an "AS IS" BASIS,
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 #include <functional>
31 #include <string>
32 
33 #include "host.h"
34 
35 // TODO(rays) Put the rest of the helpers in the namespace.
36 namespace tesseract {
37 
38 // A simple linear congruential random number generator, using Knuth's
39 // constants from:
40 // http://en.wikipedia.org/wiki/Linear_congruential_generator.
41 class TRand {
42  public:
43  TRand() : seed_(1) {}
44  // Sets the seed to the given value.
45  void set_seed(uinT64 seed) {
46  seed_ = seed;
47  }
48  // Sets the seed using a hash of a string.
49  void set_seed(const std::string& str) {
50  std::hash<std::string> hasher;
51  set_seed(static_cast<uinT64>(hasher(str)));
52  }
53 
54  // Returns an integer in the range 0 to MAX_INT32.
56  Iterate();
57  return seed_ >> 33;
58  }
59  // Returns a floating point value in the range [-range, range].
60  double SignedRand(double range) {
61  return range * 2.0 * IntRand() / MAX_INT32 - range;
62  }
63  // Returns a floating point value in the range [0, range].
64  double UnsignedRand(double range) {
65  return range * IntRand() / MAX_INT32;
66  }
67 
68  private:
69  // Steps the generator to the next value.
70  void Iterate() {
71  seed_ *= 6364136223846793005ULL;
72  seed_ += 1442695040888963407ULL;
73  }
74 
75  // The current value of the seed.
76  uinT64 seed_;
77 };
78 
79 } // namespace tesseract
80 
81 // Remove newline (if any) at the end of the string.
82 inline void chomp_string(char *str) {
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 }
89 
90 // Advance the current pointer of the file if it points to a newline character.
91 inline void SkipNewline(FILE *file) {
92  if (fgetc(file) != '\n') fseek(file, -1, SEEK_CUR);
93 }
94 
95 // Swaps the two args pointed to by the pointers.
96 // Operator= and copy constructor must work on T.
97 template<typename T> inline void Swap(T* p1, T* p2) {
98  T tmp(*p2);
99  *p2 = *p1;
100  *p1 = tmp;
101 }
102 
103 // qsort function to sort 2 floats.
104 inline int sort_floats(const void *arg1, const void *arg2) {
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 }
114 
115 // return the smallest multiple of block_size greater than or equal to n.
116 inline int RoundUp(int n, int block_size) {
117  return block_size * ((n + block_size - 1) / block_size);
118 }
119 
120 // Clip a numeric value to the interval [lower_bound, upper_bound].
121 template<typename T>
122 inline T ClipToRange(const T& x, const T& lower_bound, const T& upper_bound) {
123  if (x < lower_bound)
124  return lower_bound;
125  if (x > upper_bound)
126  return upper_bound;
127  return x;
128 }
129 
130 // Extend the range [lower_bound, upper_bound] to include x.
131 template<typename T1, typename T2>
132 inline void UpdateRange(const T1& x, T2* lower_bound, T2* upper_bound) {
133  if (x < *lower_bound)
134  *lower_bound = x;
135  if (x > *upper_bound)
136  *upper_bound = x;
137 }
138 
139 // Decrease lower_bound to be <= x_lo AND increase upper_bound to be >= x_hi.
140 template<typename T1, typename T2>
141 inline void UpdateRange(const T1& x_lo, const T1& x_hi,
142  T2* lower_bound, T2* upper_bound) {
143  if (x_lo < *lower_bound)
144  *lower_bound = x_lo;
145  if (x_hi > *upper_bound)
146  *upper_bound = x_hi;
147 }
148 
149 // Intersect the range [*lower2, *upper2] with the range [lower1, upper1],
150 // putting the result back in [*lower2, *upper2].
151 // If non-intersecting ranges are given, we end up with *lower2 > *upper2.
152 template<typename T>
153 inline void IntersectRange(const T& lower1, const T& upper1,
154  T* lower2, T* upper2) {
155  if (lower1 > *lower2)
156  *lower2 = lower1;
157  if (upper1 < *upper2)
158  *upper2 = upper1;
159 }
160 
161 // Proper modulo arithmetic operator. Returns a mod b that works for -ve a.
162 // For any integer a and positive b, returns r : 0<=r<b and a=n*b + r for
163 // some integer n.
164 inline int Modulo(int a, int b) {
165  return (a % b + b) % b;
166 }
167 
168 // Integer division operator with rounding that works for negative input.
169 // Returns a divided by b, rounded to the nearest integer, without double
170 // counting at 0. With simple rounding 1/3 = 0, 0/3 = 0 -1/3 = 0, -2/3 = 0,
171 // -3/3 = 0 and -4/3 = -1.
172 // I want 1/3 = 0, 0/3 = 0, -1/3 = 0, -2/3 = -1, -3/3 = -1 and -4/3 = -1.
173 inline int DivRounded(int a, int b) {
174  if (b < 0) return -DivRounded(a, -b);
175  return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;
176 }
177 
178 // Return a double cast to int with rounding.
179 inline int IntCastRounded(double x) {
180  return x >= 0.0 ? static_cast<int>(x + 0.5) : -static_cast<int>(-x + 0.5);
181 }
182 
183 // Reverse the order of bytes in a n byte quantity for big/little-endian switch.
184 inline void ReverseN(void* ptr, int num_bytes) {
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 }
193 
194 // Reverse the order of bytes in a 16 bit quantity for big/little-endian switch.
195 inline void Reverse16(void *ptr) {
196  ReverseN(ptr, 2);
197 }
198 
199 // Reverse the order of bytes in a 32 bit quantity for big/little-endian switch.
200 inline void Reverse32(void *ptr) {
201  ReverseN(ptr, 4);
202 }
203 
204 // Reverse the order of bytes in a 64 bit quantity for big/little-endian switch.
205 inline void Reverse64(void* ptr) {
206  ReverseN(ptr, 8);
207 }
208 
209 
210 #endif // TESSERACT_CCUTIL_HELPERS_H_
void Swap(T *p1, T *p2)
Definition: helpers.h:97
int32_t inT32
Definition: host.h:38
inT32 IntRand()
Definition: helpers.h:55
uint64_t uinT64
Definition: host.h:41
#define MAX_INT32
Definition: host.h:62
void Reverse64(void *ptr)
Definition: helpers.h:205
void set_seed(const std::string &str)
Definition: helpers.h:49
int IntCastRounded(double x)
Definition: helpers.h:179
#define SEEK_CUR
Definition: ioapi.c:21
void Reverse16(void *ptr)
Definition: helpers.h:195
double UnsignedRand(double range)
Definition: helpers.h:64
int sort_floats(const void *arg1, const void *arg2)
Definition: helpers.h:104
void chomp_string(char *str)
Definition: helpers.h:82
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:122
void IntersectRange(const T &lower1, const T &upper1, T *lower2, T *upper2)
Definition: helpers.h:153
void SkipNewline(FILE *file)
Definition: helpers.h:91
void set_seed(uinT64 seed)
Definition: helpers.h:45
void Reverse32(void *ptr)
Definition: helpers.h:200
double SignedRand(double range)
Definition: helpers.h:60
void UpdateRange(const T1 &x, T2 *lower_bound, T2 *upper_bound)
Definition: helpers.h:132
int DivRounded(int a, int b)
Definition: helpers.h:173
int Modulo(int a, int b)
Definition: helpers.h:164
int RoundUp(int n, int block_size)
Definition: helpers.h:116
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:184