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;
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';
92 inline void SkipNewline(FILE *file) {
93 if (fgetc(file) !=
'\n') fseek(file, -1, SEEK_CUR);
98 template<
typename T>
inline void Swap(T* p1, T* p2) {
105 inline int RoundUp(
int n,
int block_size) {
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>
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)
134 if (x_hi > *upper_bound)
142 inline void IntersectRange(
const T& lower1,
const T& upper1,
143 T* lower2, T* upper2) {
144 if (lower1 > *lower2)
146 if (upper1 < *upper2)
153 inline int Modulo(
int a,
int b) {
154 return (a % b + b) % b;
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;
168 inline int IntCastRounded(
double x) {
169 return x >= 0.0 ?
static_cast<int>(x + 0.5) : -
static_cast<int>(-x + 0.5);
173 inline int IntCastRounded(
float x) {
174 return x >= 0.0f ?
static_cast<int>(x + 0.5f) : -static_cast<int>(-x + 0.5f);
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) {
184 cptr[i] = cptr[num_bytes - 1 - i];
185 cptr[num_bytes - 1 - i] = tmp;
190 inline void Reverse16(
void *ptr) {
195 inline void Reverse32(
void *ptr) {
200 inline void Reverse64(
void* ptr) {
205 #endif // TESSERACT_CCUTIL_HELPERS_H_
void set_seed(uint64_t seed)
Definition: helpers.h:46
Definition: baseapi.cpp:94
void Iterate()
Definition: helpers.h:71
int32_t IntRand()
Definition: helpers.h:56
void set_seed(const std::string &str)
Definition: helpers.h:50
uint64_t seed_
Definition: helpers.h:77
double SignedRand(double range)
Definition: helpers.h:61
double UnsignedRand(double range)
Definition: helpers.h:65
TRand()
Definition: helpers.h:44