20 #ifndef TESSERACT_CCUTIL_HELPERS_H_
21 #define TESSERACT_CCUTIL_HELPERS_H_
37 inline bool contains(
const std::vector<T> &data,
const T &value) {
38 return std::find(data.begin(), data.end(), value) != data.end();
41 inline 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';
98 if (fgetc(
file) !=
'\n') {
99 fseek(
file, -1, SEEK_CUR);
105 return block_size * ((n + block_size - 1) / block_size);
109 template <
typename T>
110 inline T
ClipToRange(
const T &x,
const T &lower_bound,
const T &upper_bound) {
111 if (x < lower_bound) {
114 if (x > upper_bound) {
121 template <
typename T1,
typename T2>
123 if (x < *lower_bound) {
126 if (x > *upper_bound) {
132 template <
typename T1,
typename T2>
134 if (x_lo < *lower_bound) {
137 if (x_hi > *upper_bound) {
145 template <
typename T>
146 inline void IntersectRange(
const T &lower1,
const T &upper1, T *lower2, T *upper2) {
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 assert(std::isfinite(x));
179 return x >= 0.0 ?
static_cast<int>(x + 0.5) : -
static_cast<int>(-x + 0.5);
184 assert(std::isfinite(x));
185 return x >= 0.0F ?
static_cast<int>(x + 0.5F) : -
static_cast<int>(-x + 0.5F);
190 assert(num_bytes == 1 || num_bytes == 2 || num_bytes == 4 || num_bytes == 8);
191 char *cptr =
static_cast<char *
>(ptr);
192 int halfsize = num_bytes / 2;
193 for (
int i = 0; i < halfsize; ++i) {
195 cptr[i] = cptr[num_bytes - 1 - i];
196 cptr[num_bytes - 1 - i] = tmp;
219 template <
typename T>
222 if (fread(&size,
sizeof(size), 1, fp) != 1) {
229 assert(size <= UINT16_MAX);
230 if (size > UINT16_MAX) {
236 if (fread(&data[0],
sizeof(T), size, fp) != size) {
240 for (uint32_t i = 0; i < size; ++i) {
250 template <
typename T>
252 uint32_t size = data.size();
253 if (fwrite(&size,
sizeof(size), 1, fp) != 1) {
255 }
else if constexpr (std::is_class<T>::value) {
257 for (
auto &item : data) {
258 if (!item.Serialize(fp)) {
262 }
else if (size > 0) {
263 if (fwrite(&data[0],
sizeof(T), size, fp) != size) {
const std::vector< std::string > split(const std::string &s, char c)
void ReverseN(void *ptr, int num_bytes)
void Reverse64(void *ptr)
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 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)
bool contains(const std::vector< T > &data, const T &value)
void SkipNewline(FILE *file)
void set_seed(const std::string &str)
double SignedRand(double range)
double UnsignedRand(double range)
void set_seed(uint64_t seed)