tesseract  4.0.0-1-g2a2b
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 <cassert>
29 #include <cstdio>
30 #include <cstring>
31 #include <functional>
32 #include <string>
33 
34 #include "host.h"
35 
36 // TODO(rays) Put the rest of the helpers in the namespace.
37 namespace tesseract {
38 
39 // A simple linear congruential random number generator, using Knuth's
40 // constants from:
41 // http://en.wikipedia.org/wiki/Linear_congruential_generator.
42 class TRand {
43  public:
44  TRand() : seed_(1) {}
45  // Sets the seed to the given value.
46  void set_seed(uint64_t seed) {
47  seed_ = seed;
48  }
49  // Sets the seed using a hash of a string.
50  void set_seed(const std::string& str) {
51  std::hash<std::string> hasher;
52  set_seed(static_cast<uint64_t>(hasher(str)));
53  }
54 
55  // Returns an integer in the range 0 to INT32_MAX.
56  int32_t IntRand() {
57  Iterate();
58  return seed_ >> 33;
59  }
60  // Returns a floating point value in the range [-range, range].
61  double SignedRand(double range) {
62  return range * 2.0 * IntRand() / INT32_MAX - range;
63  }
64  // Returns a floating point value in the range [0, range].
65  double UnsignedRand(double range) {
66  return range * IntRand() / INT32_MAX;
67  }
68 
69  private:
70  // Steps the generator to the next value.
71  void Iterate() {
72  seed_ *= 6364136223846793005ULL;
73  seed_ += 1442695040888963407ULL;
74  }
75 
76  // The current value of the seed.
77  uint64_t seed_;
78 };
79 
80 } // namespace tesseract
81 
82 // Remove newline (if any) at the end of the string.
83 inline void chomp_string(char *str) {
84  int last_index = static_cast<int>(strlen(str)) - 1;
85  while (last_index >= 0 &&
86  (str[last_index] == '\n' || str[last_index] == '\r')) {
87  str[last_index--] = '\0';
88  }
89 }
90 
91 // Advance the current pointer of the file if it points to a newline character.
92 inline void SkipNewline(FILE *file) {
93  if (fgetc(file) != '\n') fseek(file, -1, SEEK_CUR);
94 }
95 
96 // Swaps the two args pointed to by the pointers.
97 // Operator= and copy constructor must work on T.
98 template<typename T> inline void Swap(T* p1, T* p2) {
99  T tmp(*p2);
100  *p2 = *p1;
101  *p1 = tmp;
102 }
103 
104 // return the smallest multiple of block_size greater than or equal to n.
105 inline int RoundUp(int n, int block_size) {
106  return block_size * ((n + block_size - 1) / block_size);
107 }
108 
109 // Clip a numeric value to the interval [lower_bound, upper_bound].
110 template<typename T>
111 inline T ClipToRange(const T& x, const T& lower_bound, const T& upper_bound) {
112  if (x < lower_bound)
113  return lower_bound;
114  if (x > upper_bound)
115  return upper_bound;
116  return x;
117 }
118 
119 // Extend the range [lower_bound, upper_bound] to include x.
120 template<typename T1, typename T2>
121 inline void UpdateRange(const T1& x, T2* lower_bound, T2* upper_bound) {
122  if (x < *lower_bound)
123  *lower_bound = x;
124  if (x > *upper_bound)
125  *upper_bound = x;
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,
131  T2* lower_bound, T2* upper_bound) {
132  if (x_lo < *lower_bound)
133  *lower_bound = x_lo;
134  if (x_hi > *upper_bound)
135  *upper_bound = x_hi;
136 }
137 
138 // Intersect the range [*lower2, *upper2] with the range [lower1, upper1],
139 // putting the result back in [*lower2, *upper2].
140 // If non-intersecting ranges are given, we end up with *lower2 > *upper2.
141 template<typename T>
142 inline void IntersectRange(const T& lower1, const T& upper1,
143  T* lower2, T* upper2) {
144  if (lower1 > *lower2)
145  *lower2 = lower1;
146  if (upper1 < *upper2)
147  *upper2 = upper1;
148 }
149 
150 // Proper modulo arithmetic operator. Returns a mod b that works for -ve a.
151 // For any integer a and positive b, returns r : 0<=r<b and a=n*b + r for
152 // some integer n.
153 inline int Modulo(int a, int b) {
154  return (a % b + b) % b;
155 }
156 
157 // Integer division operator with rounding that works for negative input.
158 // Returns a divided by b, rounded to the nearest integer, without double
159 // counting at 0. With simple rounding 1/3 = 0, 0/3 = 0 -1/3 = 0, -2/3 = 0,
160 // -3/3 = 0 and -4/3 = -1.
161 // I want 1/3 = 0, 0/3 = 0, -1/3 = 0, -2/3 = -1, -3/3 = -1 and -4/3 = -1.
162 inline int DivRounded(int a, int b) {
163  if (b < 0) return -DivRounded(a, -b);
164  return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;
165 }
166 
167 // Return a double cast to int with rounding.
168 inline int IntCastRounded(double x) {
169  return x >= 0.0 ? static_cast<int>(x + 0.5) : -static_cast<int>(-x + 0.5);
170 }
171 
172 // Return a float cast to int with rounding.
173 inline int IntCastRounded(float x) {
174  return x >= 0.0f ? static_cast<int>(x + 0.5f) : -static_cast<int>(-x + 0.5f);
175 }
176 
177 // Reverse the order of bytes in a n byte quantity for big/little-endian switch.
178 inline void ReverseN(void* ptr, int num_bytes) {
179  assert(num_bytes == 1 || num_bytes == 2 || num_bytes == 4 || num_bytes == 8);
180  char* cptr = static_cast<char*>(ptr);
181  int halfsize = num_bytes / 2;
182  for (int i = 0; i < halfsize; ++i) {
183  char tmp = cptr[i];
184  cptr[i] = cptr[num_bytes - 1 - i];
185  cptr[num_bytes - 1 - i] = tmp;
186  }
187 }
188 
189 // Reverse the order of bytes in a 16 bit quantity for big/little-endian switch.
190 inline void Reverse16(void *ptr) {
191  ReverseN(ptr, 2);
192 }
193 
194 // Reverse the order of bytes in a 32 bit quantity for big/little-endian switch.
195 inline void Reverse32(void *ptr) {
196  ReverseN(ptr, 4);
197 }
198 
199 // Reverse the order of bytes in a 64 bit quantity for big/little-endian switch.
200 inline void Reverse64(void* ptr) {
201  ReverseN(ptr, 8);
202 }
203 
204 
205 #endif // TESSERACT_CCUTIL_HELPERS_H_
void SkipNewline(FILE *file)
Definition: helpers.h:92
void Swap(T *p1, T *p2)
Definition: helpers.h:98
void Reverse64(void *ptr)
Definition: helpers.h:200
void Reverse16(void *ptr)
Definition: helpers.h:190
int32_t IntRand()
Definition: helpers.h:56
int Modulo(int a, int b)
Definition: helpers.h:153
void set_seed(const std::string &str)
Definition: helpers.h:50
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:178
void chomp_string(char *str)
Definition: helpers.h:83
int RoundUp(int n, int block_size)
Definition: helpers.h:105
int IntCastRounded(double x)
Definition: helpers.h:168
void Reverse32(void *ptr)
Definition: helpers.h:195
double UnsignedRand(double range)
Definition: helpers.h:65
void IntersectRange(const T &lower1, const T &upper1, T *lower2, T *upper2)
Definition: helpers.h:142
double SignedRand(double range)
Definition: helpers.h:61
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:111
int DivRounded(int a, int b)
Definition: helpers.h:162
void set_seed(uint64_t seed)
Definition: helpers.h:46
void UpdateRange(const T1 &x, T2 *lower_bound, T2 *upper_bound)
Definition: helpers.h:121