20 #ifndef TESSERACT_CCUTIL_GENERICVECTOR_H_ 21 #define TESSERACT_CCUTIL_GENERICVECTOR_H_ 77 static_assert(
sizeof(
size_used_) <=
sizeof(
size_t),
78 "Wow! sizeof(size_t) < sizeof(int32_t)!!");
96 T &
get(
int index)
const;
126 void set(T t,
int index);
129 void insert(T t,
int index);
133 void remove(
int index);
209 T *data_new =
new T[current_size * 2];
210 memcpy(data_new, data,
sizeof(T) * current_size);
232 void sort(
int (*comparator)(
const void*,
const void*)) {
243 return data_[index] == target;
254 while (top - bottom > 1) {
255 int middle = (bottom + top) / 2;
256 if (
data_[middle] > target)
278 size_used_ = last_write + 1;
287 while (old_index < size_used_ && !delete_cb->Run(old_index++))
291 if (!delete_cb->
Run(old_index)) {
295 size_used_ = new_size;
300 T result =
static_cast<T
>(0);
312 if (target_index < 0)
316 unsigned int seed = 1;
321 void swap(
int index1,
int index2) {
322 if (index1 != index2) {
323 T tmp =
data_[index1];
332 if (
data_[i] < rangemin || rangemax <
data_[i])
340 int choose_nth_item(
int target_index,
int start,
int end,
unsigned int* seed);
370 FILE* fp = fopen(filename,
"rb");
372 fseek(fp, 0, SEEK_END);
373 long size = ftell(fp);
374 fseek(fp, 0, SEEK_SET);
376 if (size > 0 && size < LONG_MAX) {
378 result = fread(&(*data)[0], 1, size, fp) ==
size;
394 FILE* fp = fopen(filename.
string(),
"wb");
395 if (fp == NULL)
return false;
397 static_cast<int>(fwrite(&data[0], 1, data.
size(), fp)) == data.
size();
409 lines_str.
split(
'\n', lines);
413 template <
typename T>
414 bool cmp_eq(T
const & t1, T
const & t2) {
422 template <
typename T>
424 const T* a =
static_cast<const T *
> (t1);
425 const T* b =
static_cast<const T *
> (t2);
428 }
else if (*b < *a) {
439 template <
typename T>
441 const T* a = *
static_cast<T* const*
>(t1);
442 const T* b = *
static_cast<T* const*
>(t2);
445 }
else if (*b < *a) {
473 for (
int i = 0; i < other.
size(); ++i) {
480 if (&other !=
this) {
489 void remove(
int index) {
497 for (
int i = size; i < GenericVector<T*>::size_used_; ++i)
512 for (; old_index < GenericVector<T*>::size_used_; ++old_index) {
540 if (fwrite(&used,
sizeof(used), 1, fp) != 1)
return false;
541 for (
int i = 0; i < used; ++i) {
543 if (fwrite(&non_null,
sizeof(non_null), 1, fp) != 1)
return false;
550 if (fp->
FWrite(&used,
sizeof(used), 1) != 1)
return false;
551 for (
int i = 0; i < used; ++i) {
553 if (fp->
FWrite(&non_null,
sizeof(non_null), 1) != 1)
return false;
567 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
571 for (
int i = 0; i < reserved; ++i) {
573 if (fread(&non_null,
sizeof(non_null), 1, fp) != 1)
return false;
577 if (!item->DeSerialize(swap, fp)) {
591 if (!DeSerializeSize(fp, &reserved))
return false;
594 for (
int i = 0; i < reserved; ++i) {
595 if (!DeSerializeElement(fp))
return false;
604 return fp->
FReadEndian(size,
sizeof(*size), 1) == 1;
609 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1)
return false;
613 if (!item->DeSerialize(fp)) {
627 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1)
return false;
629 if (!T::SkipDeSerialize(fp))
return false;
642 template <
typename T>
655 template <
typename T>
665 template <
typename T>
672 template <
typename T>
677 T* new_array =
new T[
size];
679 new_array[i] =
data_[i];
685 template <
typename T>
696 template <
typename T>
700 for (
int i = 0; i <
size; ++i)
706 template <
typename T>
712 template <
typename T>
718 template <
typename T>
724 template <
typename T>
731 template <
typename T>
740 template <
typename T>
754 template <
typename T>
757 for (
int i = index; i <
size_used_ - 1; ++i) {
764 template <
typename T>
770 template <
typename T>
781 template <
typename T>
787 template <
typename T>
793 data_[index] = object;
797 template <
typename T>
806 template <
typename T>
817 template <
typename T>
822 template <
typename T>
825 for (
int i = 0; i < other.
size(); ++i) {
831 template <
typename T>
833 if (&other !=
this) {
842 template <
typename T>
849 template <
typename T>
856 template <
typename T>
877 template <
typename T>
886 template <
typename T>
906 template <
typename T>
910 if (f->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
930 template <
typename T>
937 template <
typename T>
948 template <
typename T>
951 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
962 template <
typename T>
965 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
970 template <
typename T>
973 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
974 return fp->
FRead(NULL,
sizeof(T), reserved) == reserved;
980 template <
typename T>
988 template <
typename T>
1002 template <
typename T>
1005 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
1009 for (
int i = 0; i < reserved; ++i) {
1014 template <
typename T>
1017 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
1020 for (
int i = 0; i < reserved; ++i) {
1025 template <
typename T>
1028 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
1029 for (
int i = 0; i < reserved; ++i) {
1030 if (!T::SkipDeSerialize(fp))
return false;
1037 template <
typename T>
1052 template <
typename T>
1054 sort(&tesseract::sort_cmp<T>);
1070 template <
typename T>
1072 unsigned int* seed) {
1074 int num_elements = end - start;
1076 if (num_elements <= 1)
1078 if (num_elements == 2) {
1080 return target_index > start ? start + 1 : start;
1082 return target_index > start ? start : start + 1;
1086 #ifndef rand_r // _MSC_VER, ANDROID 1088 #define rand_r(seed) rand() 1090 int pivot =
rand_r(seed) % num_elements + start;
1096 int next_lesser = start;
1097 int prev_greater = end;
1098 for (
int next_sample = start + 1; next_sample < prev_greater;) {
1099 if (
data_[next_sample] <
data_[next_lesser]) {
1100 swap(next_lesser++, next_sample++);
1101 }
else if (
data_[next_sample] ==
data_[next_lesser]) {
1104 swap(--prev_greater, next_sample);
1109 if (target_index < next_lesser)
1111 else if (target_index < prev_greater)
1118 #endif // TESSERACT_CCUTIL_GENERICVECTOR_H_
void set_compare_callback(TessResultCallback2< bool, T const &, T const &> *cb)
int sort_ptr_cmp(const void *t1, const void *t2)
static bool DeSerializeSize(TFile *fp, inT32 *size)
PointerVector(const PointerVector &other)
int FWrite(const void *buffer, int size, int count)
bool cmp_eq(T const &t1, T const &t2)
bool DeSerializeElement(TFile *fp)
void compact(TessResultCallback1< bool, int > *delete_cb)
static bool SkipDeSerializeClasses(tesseract::TFile *fp)
int sort_cmp(const void *t1, const void *t2)
void Reverse32(void *ptr)
static bool SkipDeSerialize(tesseract::TFile *fp)
bool DeSerialize(bool swap, FILE *fp)
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
void resize_no_init(int size)
TessCallback1< T > * clear_cb_
bool LoadDataFromFile(const STRING &filename, GenericVector< char > *data)
static const int kDefaultVectorSize
bool read(tesseract::TFile *f, TessResultCallback2< bool, tesseract::TFile *, T *> *cb)
GenericVector< T > & operator=(const GenericVector &other)
void delete_data_pointers()
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)
TessResultCallback2< bool, T const &, T const & > * compare_cb_
int size_reserved() const
int get_index(T object) const
bool Serialize(FILE *fp) const
int choose_nth_item(int target_index)
const char * string() const
bool DeSerialize(TFile *fp)
bool WithinBounds(const T &rangemin, const T &rangemax) const
void move(GenericVector< T > *from)
PointerVector< T > & operator+=(const PointerVector &other)
bool DeSerializeClasses(bool swap, FILE *fp)
bool Serialize(FILE *fp) const
static bool DeSerializeSkip(TFile *fp)
void compact(TessResultCallback1< bool, const T *> *delete_cb)
int binary_search(const T &target) const
bool Serialize(TFile *fp) const
size_t unsigned_size() const
bool bool_binary_search(const T &target) const
bool SaveDataToFile(const GenericVector< char > &data, const STRING &filename)
GenericVectorEqEq(int size)
bool DeSerialize(bool swap, FILE *fp)
T dot_product(const GenericVector< T > &other) const
T contains_index(int index) const
GenericVector(int size, T init_val)
void sort(int(*comparator)(const void *, const void *))
int FReadEndian(void *buffer, int size, int count)
int push_back_new(T object)
void ReverseN(void *ptr, int num_bytes)
void set_clear_callback(TessCallback1< T > *cb)
bool write(FILE *f, TessResultCallback2< bool, FILE *, T const &> *cb) const
PointerVector< T > & operator=(const PointerVector &other)
void insert(T t, int index)
T & operator[](int index) const
void swap(int index1, int index2)
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
GenericVector< T > & operator+=(const GenericVector &other)
bool SerializeClasses(FILE *fp) const
void init_to_size(int size, T t)
GenericVector(const GenericVector &other)
static T * double_the_size_memcpy(int current_size, T *data)
bool LoadFileLinesToStrings(const STRING &filename, GenericVector< STRING > *lines)
void split(const char c, GenericVector< STRING > *splited)
int FRead(void *buffer, int size, int count)
bool contains(T object) const