21#ifndef TESSERACT_CCUTIL_HELPERS_H_
22#define TESSERACT_CCUTIL_HELPERS_H_
45 std::hash<std::string> hasher;
46 set_seed(
static_cast<uint64_t
>(hasher(str)));
56 return range * 2.0 *
IntRand() / INT32_MAX - range;
60 return range *
IntRand() / INT32_MAX;
66 seed_ *= 6364136223846793005ULL;
67 seed_ += 1442695040888963407ULL;
78 int last_index =
static_cast<int>(strlen(str)) - 1;
79 while (last_index >= 0 &&
80 (str[last_index] ==
'\n' || str[last_index] ==
'\r')) {
81 str[last_index--] =
'\0';
87 if (fgetc(file) !=
'\n') {
88 fseek(file, -1, SEEK_CUR);
95inline void Swap(T* p1, T* p2) {
103 return block_size * ((n + block_size - 1) / block_size);
108inline T
ClipToRange(
const T& x,
const T& lower_bound,
const T& upper_bound) {
109 if (x < lower_bound) {
112 if (x > upper_bound) {
119template <
typename T1,
typename T2>
120inline void UpdateRange(
const T1& x, T2* lower_bound, T2* upper_bound) {
121 if (x < *lower_bound) {
124 if (x > *upper_bound) {
130template <
typename T1,
typename T2>
131inline void UpdateRange(
const T1& x_lo,
const T1& x_hi, T2* lower_bound,
133 if (x_lo < *lower_bound) {
136 if (x_hi > *upper_bound) {
147 if (lower1 > *lower2) {
150 if (upper1 < *upper2) {
159 return (a % b + b) % b;
171 return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;
176 return x >= 0.0 ?
static_cast<int>(x + 0.5) : -
static_cast<int>(-x + 0.5);
181 return x >= 0.0F ?
static_cast<int>(x + 0.5F) : -
static_cast<int>(-x + 0.5F);
186 assert(num_bytes == 1 || num_bytes == 2 || num_bytes == 4 || num_bytes == 8);
187 char* cptr =
static_cast<char*
>(ptr);
188 int halfsize = num_bytes / 2;
189 for (
int i = 0; i < halfsize; ++i) {
191 cptr[i] = cptr[num_bytes - 1 - i];
192 cptr[num_bytes - 1 - i] = tmp;
void Reverse64(void *ptr)
int DivRounded(int a, int b)
void SkipNewline(FILE *file)
void chomp_string(char *str)
void Reverse16(void *ptr)
void IntersectRange(const T &lower1, const T &upper1, T *lower2, T *upper2)
void UpdateRange(const T1 &x, T2 *lower_bound, T2 *upper_bound)
void Reverse32(void *ptr)
int IntCastRounded(double x)
void ReverseN(void *ptr, int num_bytes)
int RoundUp(int n, int block_size)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
void set_seed(const std::string &str)
double SignedRand(double range)
double UnsignedRand(double range)
void set_seed(uint64_t seed)