20#ifndef TESSERACT_CCUTIL_HELPERS_H_
21#define TESSERACT_CCUTIL_HELPERS_H_
40 return std::find(data.begin(), data.end(),
value) != data.end();
43inline const std::vector<std::string>
split(
const std::string &s,
char c) {
45 std::vector<std::string> v;
49 }
else if (n == c && !buff.empty()) {
69 std::hash<std::string> hasher;
70 set_seed(
static_cast<uint64_t
>(hasher(str)));
79 return range * 2.0 *
IntRand() / INT32_MAX - range;
83 return range *
IntRand() / INT32_MAX;
92 int last_index =
static_cast<int>(strlen(str)) - 1;
93 while (last_index >= 0 && (str[last_index] ==
'\n' || str[last_index] ==
'\r')) {
94 str[last_index--] =
'\0';
99inline int RoundUp(
int n,
int block_size) {
100 return block_size * ((n + block_size - 1) / block_size);
105inline T
ClipToRange(
const T &
x,
const T &lower_bound,
const T &upper_bound) {
106 if (
x < lower_bound) {
109 if (
x > upper_bound) {
116template <
typename T1,
typename T2>
118 if (
x < *lower_bound) {
121 if (
x > *upper_bound) {
127template <
typename T1,
typename T2>
129 if (x_lo < *lower_bound) {
132 if (x_hi > *upper_bound) {
141inline void IntersectRange(
const T &lower1,
const T &upper1, T *lower2, T *upper2) {
142 if (lower1 > *lower2) {
145 if (upper1 < *upper2) {
154 return (a % b + b) % b;
166 return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;
171 assert(std::isfinite(
x));
174 return x >= 0.0 ?
static_cast<int>(
x + 0.5) : -
static_cast<int>(-
x + 0.5);
179 assert(std::isfinite(
x));
180 return x >= 0.0F ?
static_cast<int>(
x + 0.5F) : -
static_cast<int>(-
x + 0.5F);
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) {
190 cptr[
i] = cptr[num_bytes - 1 -
i];
191 cptr[num_bytes - 1 -
i] = tmp;
207 if (fread(&size,
sizeof(size), 1, fp) != 1) {
214 assert(size <= UINT16_MAX);
215 if (size > UINT16_MAX) {
221 if (fread(&data[0],
sizeof(T), size, fp) != size) {
225 for (uint32_t
i = 0;
i < size; ++
i) {
237 uint32_t size = data.size();
238 if (fwrite(&size,
sizeof(size), 1, fp) != 1) {
242 for (
auto &item : data) {
243 if (!item.Serialize(fp)) {
249 for (
auto &item : data) {
250 uint8_t non_null = (item !=
nullptr);
255 if (!item->Serialize(fp)) {
260 }
else if (size > 0) {
261 if (fwrite(&data[0],
sizeof(T), size, fp) != size) {
void ReverseN(void *ptr, int num_bytes)
int IntCastRounded(double x)
int DivRounded(int a, int b)
bool DeSerialize(bool swap, FILE *fp, std::vector< T > &data)
void chomp_string(char *str)
bool Serialize(FILE *fp, const std::vector< T > &data)
int RoundUp(int n, int block_size)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
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)
const std::vector< std::string > split(const std::string &s, char c)
bool contains(const std::vector< T > &data, const T &value)
void set_seed(const std::string &str)
double SignedRand(double range)
double UnsignedRand(double range)
void set_seed(uint64_t seed)