20 #ifndef TESSERACT_CCUTIL_GENERICVECTOR_H_ 21 #define TESSERACT_CCUTIL_GENERICVECTOR_H_ 28 #include "tesscallback.h" 76 static_assert(
sizeof(
size_used_) <=
sizeof(
size_t),
77 "Wow! sizeof(size_t) < sizeof(int32_t)!!");
95 T &
get(
int index)
const;
107 bool contains(
const T&
object)
const;
125 void set(
const T& t,
int index);
128 void insert(
const T& t,
int index);
132 void remove(
int index);
208 T *data_new =
new T[current_size * 2];
209 memcpy(data_new, data,
sizeof(T) * current_size);
231 void sort(
int (*comparator)(
const void*,
const void*)) {
242 return data_[index] == target;
253 while (top - bottom > 1) {
254 int middle = (bottom + top) / 2;
255 if (
data_[middle] > target)
277 size_used_ = last_write + 1;
286 while (old_index < size_used_ && !delete_cb->Run(old_index++))
290 if (!delete_cb->
Run(old_index)) {
294 size_used_ = new_size;
299 T result =
static_cast<T
>(0);
311 if (target_index < 0)
315 unsigned int seed = 1;
320 void swap(
int index1,
int index2) {
321 if (index1 != index2) {
322 T tmp =
data_[index1];
331 if (
data_[i] < rangemin || rangemax <
data_[i])
339 int choose_nth_item(
int target_index,
int start,
int end,
unsigned int* seed);
369 FILE* fp = fopen(filename,
"rb");
371 fseek(fp, 0, SEEK_END);
372 long size = ftell(fp);
373 fseek(fp, 0, SEEK_SET);
375 if (size > 0 && size < LONG_MAX) {
379 result =
static_cast<long>(fread(&(*data)[0], 1, size, fp)) == size;
395 FILE* fp = fopen(filename.
string(),
"wb");
396 if (fp ==
nullptr)
return false;
398 static_cast<int>(fwrite(&data[0], 1, data.
size(), fp)) == data.
size();
410 lines_str.
split(
'\n', lines);
414 template <
typename T>
415 bool cmp_eq(T
const & t1, T
const & t2) {
423 template <
typename T>
425 const T* a =
static_cast<const T *
> (t1);
426 const T* b =
static_cast<const T *
> (t2);
429 }
else if (*b < *a) {
440 template <
typename T>
442 const T* a = *
static_cast<T* const*
>(t1);
443 const T* b = *
static_cast<T* const*
>(t2);
446 }
else if (*b < *a) {
474 for (
int i = 0; i < other.
size(); ++i) {
481 if (&other !=
this) {
490 void remove(
int index) {
498 for (
int i = size; i < GenericVector<T*>::size_used_; ++i)
513 for (; old_index < GenericVector<T*>::size_used_; ++old_index) {
541 if (fwrite(&used,
sizeof(used), 1, fp) != 1)
return false;
542 for (
int i = 0; i < used; ++i) {
544 if (fwrite(&non_null,
sizeof(non_null), 1, fp) != 1)
return false;
551 if (fp->
FWrite(&used,
sizeof(used), 1) != 1)
return false;
552 for (
int i = 0; i < used; ++i) {
554 if (fp->
FWrite(&non_null,
sizeof(non_null), 1) != 1)
return false;
568 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
569 if (swap) Reverse32(&reserved);
571 assert(reserved <= UINT16_MAX);
572 if (reserved > UINT16_MAX) {
577 for (uint32_t i = 0; i < reserved; ++i) {
579 if (fread(&non_null,
sizeof(non_null), 1, fp) != 1)
return false;
583 if (!item->DeSerialize(swap, fp)) {
597 if (!DeSerializeSize(fp, &reserved))
return false;
600 for (
int i = 0; i < reserved; ++i) {
601 if (!DeSerializeElement(fp))
return false;
610 return fp->
FReadEndian(size,
sizeof(*size), 1) == 1;
615 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1)
return false;
619 if (!item->DeSerialize(fp)) {
633 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1)
return false;
635 if (!T::SkipDeSerialize(fp))
return false;
648 template <
typename T>
653 NewPermanentTessCallback(tesseract::cmp_eq<T>));
657 NewPermanentTessCallback(tesseract::cmp_eq<T>));
661 template <
typename T>
676 template <
typename T>
683 template <
typename T>
688 T* new_array =
new T[
size];
690 new_array[i] =
data_[i];
696 template <
typename T>
707 template <
typename T>
711 for (
int i = 0; i <
size; ++i)
717 template <
typename T>
723 template <
typename T>
729 template <
typename T>
735 template <
typename T>
742 template <
typename T>
751 template <
typename T>
765 template <
typename T>
768 for (
int i = index; i <
size_used_ - 1; ++i) {
775 template <
typename T>
781 template <
typename T>
792 template <
typename T>
798 template <
typename T>
804 data_[index] = object;
808 template <
typename T>
817 template <
typename T>
828 template <
typename T>
833 template <
typename T>
836 for (
int i = 0; i < other.
size(); ++i) {
842 template <
typename T>
844 if (&other !=
this) {
853 template <
typename T>
860 template <
typename T>
867 template <
typename T>
883 template <
typename T>
891 template <
typename T>
911 template <
typename T>
915 if (f->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
935 template <
typename T>
942 template <
typename T>
953 template <
typename T>
956 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
957 if (swap) Reverse32(&reserved);
959 assert(reserved <= UINT16_MAX);
960 if (reserved > UINT16_MAX)
return false;
970 template <
typename T>
973 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
975 const uint32_t limit = 50000000;
976 assert(reserved <= limit);
977 if (reserved > limit)
return false;
982 template <
typename T>
985 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
986 return (uint32_t)fp->
FRead(
nullptr,
sizeof(T), reserved) == reserved;
992 template <
typename T>
1000 template <
typename T>
1014 template <
typename T>
1017 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
1018 if (swap) Reverse32(&reserved);
1021 for (
int i = 0; i < reserved; ++i) {
1026 template <
typename T>
1029 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
1032 for (
int i = 0; i < reserved; ++i) {
1037 template <
typename T>
1040 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
1041 for (
int i = 0; i < reserved; ++i) {
1042 if (!T::SkipDeSerialize(fp))
return false;
1049 template <
typename T>
1057 from->
data_ =
nullptr;
1064 template <
typename T>
1066 sort(&tesseract::sort_cmp<T>);
1082 template <
typename T>
1084 unsigned int* seed) {
1086 int num_elements = end - start;
1088 if (num_elements <= 1)
1090 if (num_elements == 2) {
1092 return target_index > start ? start + 1 : start;
1094 return target_index > start ? start : start + 1;
1098 #ifndef rand_r // _MSC_VER, ANDROID 1100 #define rand_r(seed) rand() 1102 int pivot = rand_r(seed) % num_elements + start;
1108 int next_lesser = start;
1109 int prev_greater = end;
1110 for (
int next_sample = start + 1; next_sample < prev_greater;) {
1111 if (
data_[next_sample] <
data_[next_lesser]) {
1112 swap(next_lesser++, next_sample++);
1113 }
else if (
data_[next_sample] ==
data_[next_lesser]) {
1116 swap(--prev_greater, next_sample);
1121 if (target_index < next_lesser)
1123 else if (target_index < prev_greater)
1130 #endif // TESSERACT_CCUTIL_GENERICVECTOR_H_ void sort()
Definition: genericvector.h:1065
Definition: resultiterator.h:33
GenericVector< T > & operator=(const GenericVector &other)
Definition: genericvector.h:843
void remove(int index)
Definition: genericvector.h:766
~PointerVector()
Definition: genericvector.h:461
bool LoadFileLinesToStrings(const STRING &filename, GenericVector< STRING > *lines)
Definition: genericvector.h:403
int sort_ptr_cmp(const void *t1, const void *t2)
Definition: genericvector.h:441
GenericVector(int size, const T &init_val)
Definition: genericvector.h:42
static const int kDefaultVectorSize
Definition: genericvector.h:347
GenericVectorEqEq()
Definition: genericvector.h:651
bool Serialize(TFile *fp) const
Definition: genericvector.h:549
int push_back_new(const T &object)
Definition: genericvector.h:809
bool DeSerialize(bool swap, FILE *fp)
Definition: genericvector.h:566
void set_compare_callback(TessResultCallback2< bool, T const &, T const &> *cb)
Definition: genericvector.h:861
void sort(int(*comparator)(const void *, const void *))
Definition: genericvector.h:231
void compact_sorted()
Definition: genericvector.h:265
bool SaveDataToFile(const GenericVector< char > &data, const STRING &filename)
Definition: genericvector.h:393
PointerVector< T > & operator+=(const PointerVector &other)
Definition: genericvector.h:472
static bool SkipDeSerialize(tesseract::TFile *fp)
Definition: genericvector.h:983
int push_back(T object)
Definition: genericvector.h:799
static T * double_the_size_memcpy(int current_size, T *data)
Definition: genericvector.h:207
void init(int size)
Definition: genericvector.h:662
void clear()
Definition: genericvector.h:868
Definition: serialis.h:77
T & get(int index) const
Definition: genericvector.h:718
int32_t size_reserved_
Definition: genericvector.h:349
void truncate(int size)
Definition: genericvector.h:136
int binary_search(const T &target) const
Definition: genericvector.h:250
bool SerializeClasses(FILE *fp) const
Definition: genericvector.h:993
Definition: baseapi.cpp:94
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:270
void swap(int index1, int index2)
Definition: genericvector.h:320
int choose_nth_item(int target_index)
Definition: genericvector.h:309
bool write(FILE *f, TessResultCallback2< bool, FILE *, T const &> *cb) const
Definition: genericvector.h:892
T contains_index(int index) const
Definition: genericvector.h:776
PointerVector< T > & operator=(const PointerVector &other)
Definition: genericvector.h:480
void set(const T &t, int index)
Definition: genericvector.h:743
int push_front(const T &object)
Definition: genericvector.h:818
void move(GenericVector< T > *from)
Definition: genericvector.h:1050
const char * string() const
Definition: strngs.cpp:196
void split(const char c, GenericVector< STRING > *splited)
Definition: strngs.cpp:284
int sort_cmp(const void *t1, const void *t2)
Definition: genericvector.h:424
GenericVector< T > & operator+=(const GenericVector &other)
Definition: genericvector.h:834
void insert(const T &t, int index)
Definition: genericvector.h:752
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:259
static bool SkipDeSerializeClasses(tesseract::TFile *fp)
Definition: genericvector.h:1038
bool bool_binary_search(const T &target) const
Definition: genericvector.h:238
void clear()
Definition: genericvector.h:529
int get_index(const T &object) const
Definition: genericvector.h:782
void delete_data_pointers()
Definition: genericvector.h:884
T & operator[](int index) const
Definition: genericvector.h:724
bool WithinBounds(const T &rangemin, const T &rangemax) const
Definition: genericvector.h:329
bool Serialize(FILE *fp) const
Definition: genericvector.h:539
void sort()
Definition: genericvector.h:642
bool DeSerializeElement(TFile *fp)
Definition: genericvector.h:613
bool read(tesseract::TFile *f, TessResultCallback2< bool, tesseract::TFile *, T *> *cb)
Definition: genericvector.h:912
void compact(TessResultCallback1< bool, int > *delete_cb)
Definition: genericvector.h:282
Definition: tesscallback.h:1673
void compact(TessResultCallback1< bool, const T *> *delete_cb)
Definition: genericvector.h:505
bool DeSerialize(TFile *fp)
Definition: genericvector.h:595
T & back() const
Definition: genericvector.h:730
bool Serialize(FILE *fp) const
Definition: genericvector.h:936
void truncate(int size)
Definition: genericvector.h:497
size_t unsigned_size() const
Definition: genericvector.h:75
T * data_
Definition: genericvector.h:350
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:318
bool DeSerialize(bool swap, FILE *fp)
Definition: genericvector.h:954
int size() const
Definition: genericvector.h:71
Definition: genericvector.h:457
void reverse()
Definition: genericvector.h:215
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
Definition: genericvector.h:367
int32_t size_used_
Definition: genericvector.h:348
void init_to_size(int size, const T &t)
Definition: genericvector.h:708
static bool DeSerializeSize(TFile *fp, int32_t *size)
Definition: genericvector.h:609
static bool DeSerializeSkip(TFile *fp)
Definition: genericvector.h:631
PointerVector()
Definition: genericvector.h:459
PointerVector(const PointerVector &other)
Definition: genericvector.h:468
T dot_product(const GenericVector< T > &other) const
Definition: genericvector.h:298
void reserve(int size)
Definition: genericvector.h:684
int size_reserved() const
Definition: genericvector.h:81
~GenericVector()
Definition: genericvector.h:677
int length() const
Definition: genericvector.h:85
bool cmp_eq(T const &t1, T const &t2)
Definition: genericvector.h:415
GenericVector()
Definition: genericvector.h:39
void set_clear_callback(TessCallback1< T > *cb)
Definition: genericvector.h:854
bool empty() const
Definition: genericvector.h:90
TessResultCallback2< bool, T const &, T const & > * compare_cb_
Definition: genericvector.h:353
GenericVector(const GenericVector &other)
Definition: genericvector.h:48
TessCallback1< T > * clear_cb_
Definition: genericvector.h:351
void resize_no_init(int size)
Definition: genericvector.h:65
T pop_back()
Definition: genericvector.h:736
bool contains(const T &object) const
Definition: genericvector.h:793
PointerVector(int size)
Definition: genericvector.h:460
void double_the_size()
Definition: genericvector.h:697
bool DeSerializeClasses(bool swap, FILE *fp)
Definition: genericvector.h:1015
GenericVectorEqEq(int size)
Definition: genericvector.h:655