25 #ifndef TESSERACT_CCUTIL_HELPERS_H_ 26 #define TESSERACT_CCUTIL_HELPERS_H_ 51 std::hash<std::string> hasher;
52 set_seed(static_cast<uint64_t>(hasher(str)));
62 return range * 2.0 *
IntRand() / INT32_MAX - range;
66 return range *
IntRand() / INT32_MAX;
72 seed_ *= 6364136223846793005ULL;
73 seed_ += 1442695040888963407ULL;
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';
93 if (fgetc(file) !=
'\n') fseek(file, -1, SEEK_CUR);
98 template<
typename T>
inline void Swap(T* p1, T* p2) {
106 return block_size * ((n + block_size - 1) / block_size);
111 inline T
ClipToRange(
const T& x,
const T& lower_bound,
const T& upper_bound) {
120 template<
typename T1,
typename T2>
121 inline void UpdateRange(
const T1& x, T2* lower_bound, T2* upper_bound) {
122 if (x < *lower_bound)
124 if (x > *upper_bound)
129 template<
typename T1,
typename T2>
131 T2* lower_bound, T2* upper_bound) {
132 if (x_lo < *lower_bound)
134 if (x_hi > *upper_bound)
143 T* lower2, T* upper2) {
144 if (lower1 > *lower2)
146 if (upper1 < *upper2)
154 return (a % b + b) % b;
164 return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;
169 return x >= 0.0 ?
static_cast<int>(x + 0.5) : -
static_cast<int>(-x + 0.5);
174 return x >= 0.0f ?
static_cast<int>(x + 0.5f) : -static_cast<int>(-x + 0.5f);
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) {
184 cptr[i] = cptr[num_bytes - 1 - i];
185 cptr[num_bytes - 1 - i] = tmp;
205 #endif // TESSERACT_CCUTIL_HELPERS_H_ void SkipNewline(FILE *file)
void Reverse64(void *ptr)
void Reverse16(void *ptr)
void set_seed(const std::string &str)
void ReverseN(void *ptr, int num_bytes)
void chomp_string(char *str)
int RoundUp(int n, int block_size)
int IntCastRounded(double x)
void Reverse32(void *ptr)
double UnsignedRand(double range)
void IntersectRange(const T &lower1, const T &upper1, T *lower2, T *upper2)
double SignedRand(double range)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
int DivRounded(int a, int b)
void set_seed(uint64_t seed)
void UpdateRange(const T1 &x, T2 *lower_bound, T2 *upper_bound)