20#ifndef TESSERACT_CCUTIL_HELPERS_H_
21#define TESSERACT_CCUTIL_HELPERS_H_
37inline bool contains(
const std::vector<T> &data,
const T &value) {
38 return std::find(data.begin(), data.end(), value) != data.end();
41inline const std::vector<std::string>
split(
const std::string &s,
char c) {
43 std::vector<std::string> v;
47 }
else if (n == c && !buff.empty()) {
67 std::hash<std::string> hasher;
68 set_seed(
static_cast<uint64_t
>(hasher(str)));
77 return range * 2.0 *
IntRand() / INT32_MAX - range;
81 return range *
IntRand() / INT32_MAX;
90 int last_index =
static_cast<int>(strlen(str)) - 1;
91 while (last_index >= 0 && (str[last_index] ==
'\n' || str[last_index] ==
'\r')) {
92 str[last_index--] =
'\0';
97inline int RoundUp(
int n,
int block_size) {
98 return block_size * ((n + block_size - 1) / block_size);
103inline T
ClipToRange(
const T &x,
const T &lower_bound,
const T &upper_bound) {
104 if (x < lower_bound) {
107 if (x > upper_bound) {
114template <
typename T1,
typename T2>
116 if (x < *lower_bound) {
119 if (x > *upper_bound) {
125template <
typename T1,
typename T2>
127 if (x_lo < *lower_bound) {
130 if (x_hi > *upper_bound) {
139inline void IntersectRange(
const T &lower1,
const T &upper1, T *lower2, T *upper2) {
140 if (lower1 > *lower2) {
143 if (upper1 < *upper2) {
152 return (a % b + b) % b;
164 return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;
169 assert(std::isfinite(x));
172 return x >= 0.0 ?
static_cast<int>(x + 0.5) : -
static_cast<int>(-x + 0.5);
177 assert(std::isfinite(x));
178 return x >= 0.0F ?
static_cast<int>(x + 0.5F) : -
static_cast<int>(-x + 0.5F);
183 assert(num_bytes == 1 || num_bytes == 2 || num_bytes == 4 || num_bytes == 8);
184 char *cptr =
static_cast<char *
>(ptr);
185 int halfsize = num_bytes / 2;
186 for (
int i = 0; i < halfsize; ++i) {
188 cptr[i] = cptr[num_bytes - 1 - i];
189 cptr[num_bytes - 1 - i] = tmp;
205 if (fread(&size,
sizeof(size), 1, fp) != 1) {
212 assert(size <= UINT16_MAX);
213 if (size > UINT16_MAX) {
219 if (fread(&data[0],
sizeof(T), size, fp) != size) {
223 for (uint32_t i = 0; i < size; ++i) {
235 uint32_t size = data.size();
236 if (fwrite(&size,
sizeof(size), 1, fp) != 1) {
238 }
else if constexpr (std::is_class<T>::value) {
240 for (
auto &item : data) {
241 if (!item.Serialize(fp)) {
245 }
else if (size > 0) {
246 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)