tesseract  5.0.0-alpha-619-ge9db
helpers.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * File: helpers.h
4  * Description: General utility functions
5  * Author: Daria Antonova
6  *
7  * (c) Copyright 2009, Google Inc.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  *****************************************************************************/
19 
20 #ifndef TESSERACT_CCUTIL_HELPERS_H_
21 #define TESSERACT_CCUTIL_HELPERS_H_
22 
23 #include <cassert>
24 #include <cstdio>
25 #include <cstring>
26 #include <functional>
27 #include <string>
28 
29 // TODO(rays) Put the rest of the helpers in the namespace.
30 namespace tesseract {
31 
32 // A simple linear congruential random number generator, using Knuth's
33 // constants from:
34 // http://en.wikipedia.org/wiki/Linear_congruential_generator.
35 class TRand {
36  public:
37  TRand() = default;
38  // Sets the seed to the given value.
39  void set_seed(uint64_t seed) {
40  seed_ = seed;
41  }
42  // Sets the seed using a hash of a string.
43  void set_seed(const std::string& str) {
44  std::hash<std::string> hasher;
45  set_seed(static_cast<uint64_t>(hasher(str)));
46  }
47 
48  // Returns an integer in the range 0 to INT32_MAX.
49  int32_t IntRand() {
50  Iterate();
51  return seed_ >> 33;
52  }
53  // Returns a floating point value in the range [-range, range].
54  double SignedRand(double range) {
55  return range * 2.0 * IntRand() / INT32_MAX - range;
56  }
57  // Returns a floating point value in the range [0, range].
58  double UnsignedRand(double range) {
59  return range * IntRand() / INT32_MAX;
60  }
61 
62  private:
63  // Steps the generator to the next value.
64  void Iterate() {
65  seed_ *= 6364136223846793005ULL;
66  seed_ += 1442695040888963407ULL;
67  }
68 
69  // The current value of the seed.
70  uint64_t seed_{1};
71 };
72 
73 } // namespace tesseract
74 
75 // Remove newline (if any) at the end of the string.
76 inline void chomp_string(char* str) {
77  int last_index = static_cast<int>(strlen(str)) - 1;
78  while (last_index >= 0 &&
79  (str[last_index] == '\n' || str[last_index] == '\r')) {
80  str[last_index--] = '\0';
81  }
82 }
83 
84 // Advance the current pointer of the file if it points to a newline character.
85 inline void SkipNewline(FILE* file) {
86  if (fgetc(file) != '\n') {
87  fseek(file, -1, SEEK_CUR);
88  }
89 }
90 
91 // Swaps the two args pointed to by the pointers.
92 // Operator= and copy constructor must work on T.
93 template <typename T>
94 inline void Swap(T* p1, T* p2) {
95  T tmp(*p2);
96  *p2 = *p1;
97  *p1 = tmp;
98 }
99 
100 // return the smallest multiple of block_size greater than or equal to n.
101 inline int RoundUp(int n, int block_size) {
102  return block_size * ((n + block_size - 1) / block_size);
103 }
104 
105 // Clip a numeric value to the interval [lower_bound, upper_bound].
106 template <typename T>
107 inline T ClipToRange(const T& x, const T& lower_bound, const T& upper_bound) {
108  if (x < lower_bound) {
109  return lower_bound;
110  }
111  if (x > upper_bound) {
112  return upper_bound;
113  }
114  return x;
115 }
116 
117 // Extend the range [lower_bound, upper_bound] to include x.
118 template <typename T1, typename T2>
119 inline void UpdateRange(const T1& x, T2* lower_bound, T2* upper_bound) {
120  if (x < *lower_bound) {
121  *lower_bound = x;
122  }
123  if (x > *upper_bound) {
124  *upper_bound = x;
125  }
126 }
127 
128 // Decrease lower_bound to be <= x_lo AND increase upper_bound to be >= x_hi.
129 template <typename T1, typename T2>
130 inline void UpdateRange(const T1& x_lo, const T1& x_hi, T2* lower_bound,
131  T2* upper_bound) {
132  if (x_lo < *lower_bound) {
133  *lower_bound = x_lo;
134  }
135  if (x_hi > *upper_bound) {
136  *upper_bound = x_hi;
137  }
138 }
139 
140 // Intersect the range [*lower2, *upper2] with the range [lower1, upper1],
141 // putting the result back in [*lower2, *upper2].
142 // If non-intersecting ranges are given, we end up with *lower2 > *upper2.
143 template <typename T>
144 inline void IntersectRange(const T& lower1, const T& upper1, T* lower2,
145  T* upper2) {
146  if (lower1 > *lower2) {
147  *lower2 = lower1;
148  }
149  if (upper1 < *upper2) {
150  *upper2 = upper1;
151  }
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) {
168  return -DivRounded(a, -b);
169  }
170  return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;
171 }
172 
173 // Return a double cast to int with rounding.
174 inline int IntCastRounded(double x) {
175  return x >= 0.0 ? static_cast<int>(x + 0.5) : -static_cast<int>(-x + 0.5);
176 }
177 
178 // Return a float cast to int with rounding.
179 inline int IntCastRounded(float x) {
180  return x >= 0.0F ? static_cast<int>(x + 0.5F) : -static_cast<int>(-x + 0.5F);
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  assert(num_bytes == 1 || num_bytes == 2 || num_bytes == 4 || num_bytes == 8);
186  char* cptr = static_cast<char*>(ptr);
187  int halfsize = num_bytes / 2;
188  for (int i = 0; i < halfsize; ++i) {
189  char tmp = cptr[i];
190  cptr[i] = cptr[num_bytes - 1 - i];
191  cptr[num_bytes - 1 - i] = tmp;
192  }
193 }
194 
195 // Reverse the order of bytes in a 16 bit quantity for big/little-endian switch.
196 inline void Reverse16(void* ptr) {
197  ReverseN(ptr, 2);
198 }
199 
200 // Reverse the order of bytes in a 32 bit quantity for big/little-endian switch.
201 inline void Reverse32(void* ptr) {
202  ReverseN(ptr, 4);
203 }
204 
205 // Reverse the order of bytes in a 64 bit quantity for big/little-endian switch.
206 inline void Reverse64(void* ptr) {
207  ReverseN(ptr, 8);
208 }
209 
210 #endif // TESSERACT_CCUTIL_HELPERS_H_
string
std::string string
Definition: equationdetect_test.cc:21
ClipToRange
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:106
T1
Definition: rune.c:28
Reverse32
void Reverse32(void *ptr)
Definition: helpers.h:200
IntersectRange
void IntersectRange(const T &lower1, const T &upper1, T *lower2, T *upper2)
Definition: helpers.h:143
tesseract::TRand::IntRand
int32_t IntRand()
Definition: helpers.h:80
RoundUp
int RoundUp(int n, int block_size)
Definition: helpers.h:100
IntCastRounded
int IntCastRounded(double x)
Definition: helpers.h:173
file
Definition: include_gunit.h:22
chomp_string
void chomp_string(char *str)
Definition: helpers.h:75
SkipNewline
void SkipNewline(FILE *file)
Definition: helpers.h:84
tesseract::TRand::TRand
TRand()=default
tesseract
Definition: baseapi.h:65
DivRounded
int DivRounded(int a, int b)
Definition: helpers.h:165
T2
Definition: rune.c:30
tesseract::TRand::SignedRand
double SignedRand(double range)
Definition: helpers.h:85
tesseract::TRand::set_seed
void set_seed(uint64_t seed)
Definition: helpers.h:70
Reverse16
void Reverse16(void *ptr)
Definition: helpers.h:195
tesseract::TRand::UnsignedRand
double UnsignedRand(double range)
Definition: helpers.h:89
Modulo
int Modulo(int a, int b)
Definition: helpers.h:156
Swap
void Swap(T *p1, T *p2)
Definition: helpers.h:93
ReverseN
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:183
UpdateRange
void UpdateRange(const T1 &x, T2 *lower_bound, T2 *upper_bound)
Definition: helpers.h:118
Reverse64
void Reverse64(void *ptr)
Definition: helpers.h:205