tesseract
5.0.0-alpha-619-ge9db
|
Go to the documentation of this file.
19 #ifndef TESSERACT_CCUTIL_GENERICVECTOR_H_
20 #define TESSERACT_CCUTIL_GENERICVECTOR_H_
76 static_assert(
sizeof(
size_used_) <=
sizeof(
size_t),
77 "Wow! sizeof(size_t) < sizeof(int32_t)!!");
91 T&
get(
int index)
const;
103 bool contains(
const T&
object)
const;
121 void set(
const T& t,
int index);
124 void insert(
const T& t,
int index);
171 bool write(FILE* f, std::function<
bool(FILE*,
const T&)> cb)
const;
208 T* data_new =
new T[current_size * 2];
209 memcpy(data_new, data,
sizeof(T) * current_size);
232 void sort(
int (*comparator)(
const void*,
const void*)) {
244 return data_[index] == target;
255 while (top - bottom > 1) {
256 int middle = (bottom + top) / 2;
257 if (
data_[middle] > target) {
291 if (target_index < 0) {
296 unsigned int seed = 1;
301 void swap(
int index1,
int index2) {
302 if (index1 != index2) {
303 T tmp =
data_[index1];
312 if (
data_[i] < rangemin || rangemax <
data_[i]) {
321 int choose_nth_item(
int target_index,
int start,
int end,
unsigned int* seed);
343 FILE* fp = fopen(filename,
"rb");
345 fseek(fp, 0, SEEK_END);
346 auto size = std::ftell(fp);
347 fseek(fp, 0, SEEK_SET);
349 if (size > 0 && size < LONG_MAX) {
353 result = static_cast<long>(fread(&(*data)[0], 1, size, fp)) == size;
363 const char* filename) {
364 FILE* fp = fopen(filename,
"wb");
369 static_cast<int>(fwrite(&data[0], 1, data.
size(), fp)) == data.
size();
374 template <
typename T>
383 template <
typename T>
385 const T* a = static_cast<const T*>(t1);
386 const T* b = static_cast<const T*>(t2);
400 template <
typename T>
402 const T* a = *static_cast<T* const*>(t1);
403 const T* b = *static_cast<T* const*>(t2);
416 template <
typename T>
434 for (
int i = 0; i < other.
size(); ++i) {
441 if (&other !=
this) {
458 for (
int i =
size; i < GenericVector<T*>::size_used_; ++i) {
466 void compact(std::function<
bool(
const T*)> delete_cb) {
475 for (; old_index < GenericVector<T*>::size_used_; ++old_index) {
502 if (fwrite(&used,
sizeof(used), 1, fp) != 1) {
505 for (
int i = 0; i < used; ++i) {
507 if (fwrite(&non_null,
sizeof(non_null), 1, fp) != 1) {
518 if (fp->
FWrite(&used,
sizeof(used), 1) != 1) {
521 for (
int i = 0; i < used; ++i) {
523 if (fp->
FWrite(&non_null,
sizeof(non_null), 1) != 1) {
541 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1) {
548 assert(reserved <= UINT16_MAX);
549 if (reserved > UINT16_MAX) {
554 for (uint32_t i = 0; i < reserved; ++i) {
556 if (fread(&non_null,
sizeof(non_null), 1, fp) != 1) {
562 if (!item->DeSerialize(
swap, fp)) {
581 for (
int i = 0; i < reserved; ++i) {
598 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1) {
604 if (!item->DeSerialize(fp)) {
618 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1) {
622 if (!T::SkipDeSerialize(fp)) {
639 template <
typename T>
643 using namespace std::placeholders;
645 std::bind(tesseract::cmp_eq<T>, _1, _2));
648 using namespace std::placeholders;
650 std::bind(tesseract::cmp_eq<T>, _1, _2));
654 template <
typename T>
671 template <
typename T>
678 template <
typename T>
686 T* new_array =
new T[
size];
688 new_array[i] =
data_[i];
695 template <
typename T>
705 template <
typename T>
709 for (
int i = 0; i <
size; ++i) {
715 template <
typename T>
721 template <
typename T>
727 template <
typename T>
733 template <
typename T>
740 template <
typename T>
749 template <
typename T>
764 template <
typename T>
767 for (
int i = index; i <
size_used_ - 1; ++i) {
774 template <
typename T>
780 template <
typename T>
792 template <
typename T>
798 template <
typename T>
805 data_[index] = object;
809 template <
typename T>
819 template <
typename T>
832 template <
typename T>
837 template <
typename T>
840 for (
int i = 0; i < other.
size(); ++i) {
846 template <
typename T>
848 if (&other !=
this) {
856 template <
typename T>
871 template <
typename T>
878 template <
typename T>
880 std::function<
bool(FILE*,
const T&)> cb)
const {
889 if (!cb(f,
data_[i])) {
901 template <
typename T>
905 if (f->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
914 if (!cb(f,
data_ + i)) {
928 template <
typename T>
938 template <
typename T>
953 template <
typename T>
956 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1) {
963 assert(reserved <= UINT16_MAX);
964 if (reserved > UINT16_MAX) {
979 template <
typename T>
982 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
986 const uint32_t limit = 50000000;
987 assert(reserved <= limit);
988 if (reserved > limit) {
995 template <
typename T>
998 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
1001 return (uint32_t)fp->
FRead(
nullptr,
sizeof(T), reserved) == reserved;
1007 template <
typename T>
1019 template <
typename T>
1037 template <
typename T>
1040 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1) {
1048 for (
int i = 0; i < reserved; ++i) {
1055 template <
typename T>
1058 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
1063 for (
int i = 0; i < reserved; ++i) {
1070 template <
typename T>
1073 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
1076 for (
int i = 0; i < reserved; ++i) {
1077 if (!T::SkipDeSerialize(fp)) {
1086 template <
typename T>
1094 from->
data_ =
nullptr;
1101 template <
typename T>
1103 sort(&tesseract::sort_cmp<T>);
1119 template <
typename T>
1121 unsigned int* seed) {
1123 int num_elements = end - start;
1125 if (num_elements <= 1) {
1128 if (num_elements == 2) {
1130 return target_index > start ? start + 1 : start;
1132 return target_index > start ? start : start + 1;
1135 #ifndef rand_r // _MSC_VER, ANDROID
1137 # define rand_r(seed) rand()
1139 int pivot =
rand_r(seed) % num_elements + start;
1145 int next_lesser = start;
1146 int prev_greater = end;
1147 for (
int next_sample = start + 1; next_sample < prev_greater;) {
1148 if (
data_[next_sample] <
data_[next_lesser]) {
1149 swap(next_lesser++, next_sample++);
1150 }
else if (
data_[next_sample] ==
data_[next_lesser]) {
1153 swap(--prev_greater, next_sample);
1158 if (target_index < next_lesser) {
1161 if (target_index < prev_greater) {
1167 #endif // TESSERACT_CCUTIL_GENERICVECTOR_H_
void delete_data_pointers()
bool WithinBounds(const T &rangemin, const T &rangemax) const
static bool DeSerializeSize(TFile *fp, int32_t *size)
void swap(int index1, int index2)
GenericVector< T > & operator=(const GenericVector &other)
int sort_ptr_cmp(const void *t1, const void *t2)
void Reverse32(void *ptr)
size_t unsigned_size() const
static bool DeSerializeSkip(TFile *fp)
GenericVector(const GenericVector &other)
int FReadEndian(void *buffer, size_t size, int count)
void insert(const T &t, int index)
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
int push_front(const T &object)
GenericVector(int size, const T &init_val)
static T * double_the_size_memcpy(int current_size, T *data)
int choose_nth_item(int target_index)
bool contains(const T &object) const
bool Serialize(FILE *fp) const
PointerVector< T > & operator+=(const PointerVector &other)
bool DeSerialize(bool swap, FILE *fp)
bool SaveDataToFile(const GenericVector< char > &data, const char *filename)
bool cmp_eq(T const &t1, T const &t2)
void set_clear_callback(std::function< void(T)> cb)
PointerVector(const PointerVector &other)
void move(GenericVector< T > *from)
bool DeSerializeElement(TFile *fp)
int FRead(void *buffer, size_t size, int count)
bool DeSerializeClasses(bool swap, FILE *fp)
bool bool_binary_search(const T &target) const
bool write(FILE *f, std::function< bool(FILE *, const T &)> cb) const
bool SerializeClasses(FILE *fp) const
void set(const T &t, int index)
T contains_index(int index) const
void set_compare_callback(std::function< bool(const T &, const T &)> cb)
bool DeSerialize(bool swap, FILE *fp)
void resize_no_init(int size)
bool DeSerialize(TFile *fp)
GenericVector< T > & operator+=(const GenericVector &other)
int size_reserved() const
int get_index(const T &object) const
void compact(std::function< bool(const T *)> delete_cb)
std::function< void(T)> clear_cb_
static const int kDefaultVectorSize
T & operator[](int index) const
void sort(int(*comparator)(const void *, const void *))
static bool SkipDeSerialize(tesseract::TFile *fp)
std::function< bool(const T &, const T &)> compare_cb_
GenericVectorEqEq(int size)
void init_to_size(int size, const T &t)
int binary_search(const T &target) const
void ReverseN(void *ptr, int num_bytes)
int FWrite(const void *buffer, size_t size, int count)
PointerVector< T > & operator=(const PointerVector &other)
bool Serialize(TFile *fp) const
int push_back_new(const T &object)
bool Serialize(FILE *fp) const
int sort_cmp(const void *t1, const void *t2)
static bool SkipDeSerializeClasses(tesseract::TFile *fp)
bool read(tesseract::TFile *f, std::function< bool(tesseract::TFile *, T *)> cb)