20 #ifndef TESSERACT_CCUTIL_GENERICVECTOR_H_ 21 #define TESSERACT_CCUTIL_GENERICVECTOR_H_ 77 static_assert(
sizeof(
size_used_) <=
sizeof(
unsigned int),
"");
95 T &
get(
int index)
const;
125 void set(T t,
int index);
128 void insert(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);
370 FILE* fp = fopen(filename,
"rb");
373 size_t size = ftell(fp);
377 result = fread(&(*data)[0], 1, size, fp) ==
size;
393 FILE* fp = fopen(filename.
string(),
"wb");
394 if (fp == NULL)
return false;
396 static_cast<int>(fwrite(&data[0], 1, data.
size(), fp)) == data.
size();
408 lines_str.
split(
'\n', lines);
412 template <
typename T>
413 bool cmp_eq(T
const & t1, T
const & t2) {
421 template <
typename T>
423 const T* a =
static_cast<const T *
> (t1);
424 const T* b =
static_cast<const T *
> (t2);
427 }
else if (*b < *a) {
438 template <
typename T>
440 const T* a = *
static_cast<T *
const *
>(t1);
441 const T* b = *
static_cast<T *
const *
>(t2);
444 }
else if (*b < *a) {
472 for (
int i = 0; i < other.
size(); ++i) {
479 if (&other !=
this) {
488 void remove(
int index) {
496 for (
int i = size; i < GenericVector<T*>::size_used_; ++i)
511 for (; old_index < GenericVector<T*>::size_used_; ++old_index) {
539 if (fwrite(&used,
sizeof(used), 1, fp) != 1)
return false;
540 for (
int i = 0; i < used; ++i) {
542 if (fwrite(&non_null,
sizeof(non_null), 1, fp) != 1)
return false;
549 if (fp->
FWrite(&used,
sizeof(used), 1) != 1)
return false;
550 for (
int i = 0; i < used; ++i) {
552 if (fp->
FWrite(&non_null,
sizeof(non_null), 1) != 1)
return false;
566 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
570 for (
int i = 0; i < reserved; ++i) {
572 if (fread(&non_null,
sizeof(non_null), 1, fp) != 1)
return false;
576 if (!item->DeSerialize(swap, fp)) {
590 if (!DeSerializeSize(fp, &reserved))
return false;
593 for (
int i = 0; i < reserved; ++i) {
594 if (!DeSerializeElement(fp))
return false;
603 return fp->
FReadEndian(size,
sizeof(*size), 1) == 1;
608 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1)
return false;
612 if (!item->DeSerialize(fp)) {
626 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1)
return false;
628 if (!T::SkipDeSerialize(fp))
return false;
641 template <
typename T>
654 template <
typename T>
664 template <
typename T>
671 template <
typename T>
676 T* new_array =
new T[
size];
678 new_array[i] =
data_[i];
684 template <
typename T>
695 template <
typename T>
699 for (
int i = 0; i <
size; ++i)
705 template <
typename T>
711 template <
typename T>
717 template <
typename T>
723 template <
typename T>
730 template <
typename T>
739 template <
typename T>
753 template <
typename T>
756 for (
int i = index; i <
size_used_ - 1; ++i) {
763 template <
typename T>
769 template <
typename T>
780 template <
typename T>
786 template <
typename T>
792 data_[index] = object;
796 template <
typename T>
805 template <
typename T>
816 template <
typename T>
821 template <
typename T>
824 for (
int i = 0; i < other.
size(); ++i) {
830 template <
typename T>
832 if (&other !=
this) {
841 template <
typename T>
848 template <
typename T>
855 template <
typename T>
876 template <
typename T>
885 template <
typename T>
904 template <
typename T>
908 if (f->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
928 template <
typename T>
934 template <
typename T>
945 template <
typename T>
948 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
959 template <
typename T>
962 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
967 template <
typename T>
970 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
971 return fp->
FRead(NULL,
sizeof(T), reserved) == reserved;
977 template <
typename T>
985 template <
typename T>
999 template <
typename T>
1002 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
1006 for (
int i = 0; i < reserved; ++i) {
1011 template <
typename T>
1014 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
1017 for (
int i = 0; i < reserved; ++i) {
1022 template <
typename T>
1025 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
1026 for (
int i = 0; i < reserved; ++i) {
1027 if (!T::SkipDeSerialize(fp))
return false;
1034 template <
typename T>
1049 template <
typename T>
1051 sort(&tesseract::sort_cmp<T>);
1067 template <
typename T>
1069 unsigned int* seed) {
1071 int num_elements = end - start;
1073 if (num_elements <= 1)
1075 if (num_elements == 2) {
1077 return target_index > start ? start + 1 : start;
1079 return target_index > start ? start : start + 1;
1083 #ifndef rand_r // _MSC_VER, ANDROID 1085 #define rand_r(seed) rand() 1087 int pivot =
rand_r(seed) % num_elements + start;
1093 int next_lesser = start;
1094 int prev_greater = end;
1095 for (
int next_sample = start + 1; next_sample < prev_greater;) {
1096 if (
data_[next_sample] <
data_[next_lesser]) {
1097 swap(next_lesser++, next_sample++);
1098 }
else if (
data_[next_sample] ==
data_[next_lesser]) {
1101 swap(--prev_greater, next_sample);
1106 if (target_index < next_lesser)
1108 else if (target_index < prev_greater)
1115 #endif // TESSERACT_CCUTIL_GENERICVECTOR_H_ int sort_cmp(const void *t1, const void *t2)
bool DeSerialize(bool swap, FILE *fp)
int get_index(T object) const
unsigned int unsigned_size() const
bool DeSerializeElement(TFile *fp)
bool DeSerialize(TFile *fp)
T contains_index(int index) const
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
bool SaveDataToFile(const GenericVector< char > &data, const STRING &filename)
void init_to_size(int size, T t)
GenericVector(int size, T init_val)
GenericVector< T > & operator+=(const GenericVector &other)
GenericVector(const GenericVector &other)
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
void sort(int(*comparator)(const void *, const void *))
void swap(int index1, int index2)
T & operator[](int index) const
bool cmp_eq(T const &t1, T const &t2)
bool bool_binary_search(const T &target) const
const char * string() const
void resize_no_init(int size)
void move(GenericVector< T > *from)
int FReadEndian(void *buffer, int size, int count)
static bool SkipDeSerializeClasses(tesseract::TFile *fp)
GenericVectorEqEq(int size)
static bool DeSerializeSize(TFile *fp, inT32 *size)
static T * double_the_size_memcpy(int current_size, T *data)
static bool DeSerializeSkip(TFile *fp)
PointerVector< T > & operator=(const PointerVector &other)
T dot_product(const GenericVector< T > &other) const
void insert(T t, int index)
void compact(TessResultCallback1< bool, const T *> *delete_cb)
bool WithinBounds(const T &rangemin, const T &rangemax) const
int sort_ptr_cmp(const void *t1, const void *t2)
bool contains(T object) const
bool LoadFileLinesToStrings(const STRING &filename, GenericVector< STRING > *lines)
PointerVector< T > & operator+=(const PointerVector &other)
bool write(FILE *f, TessResultCallback2< bool, FILE *, T const &> *cb) const
int binary_search(const T &target) const
int FWrite(const void *buffer, int size, int count)
static bool SkipDeSerialize(tesseract::TFile *fp)
PointerVector(const PointerVector &other)
bool LoadDataFromFile(const STRING &filename, GenericVector< char > *data)
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)
void compact(TessResultCallback1< bool, int > *delete_cb)
bool DeSerialize(bool swap, FILE *fp)
bool Serialize(FILE *fp) const
GenericVector< T > & operator=(const GenericVector &other)
int size_reserved() const
bool read(tesseract::TFile *f, TessResultCallback2< bool, tesseract::TFile *, T *> *cb)
bool Serialize(FILE *fp) const
void Reverse32(void *ptr)
TessCallback1< T > * clear_cb_
void delete_data_pointers()
int choose_nth_item(int target_index)
TessResultCallback2< bool, T const &, T const & > * compare_cb_
int push_back_new(T object)
bool DeSerializeClasses(bool swap, FILE *fp)
bool SerializeClasses(FILE *fp) const
static const int kDefaultVectorSize
void split(const char c, GenericVector< STRING > *splited)
bool Serialize(TFile *fp) const
void set_clear_callback(TessCallback1< T > *cb)
void ReverseN(void *ptr, int num_bytes)
void set_compare_callback(TessResultCallback2< bool, T const &, T const &> *cb)
int FRead(void *buffer, int size, int count)