20 #ifndef TESSERACT_CCUTIL_GENERICVECTOR_H_ 21 #define TESSERACT_CCUTIL_GENERICVECTOR_H_ 89 T &
get(
int index)
const;
119 void set(T t,
int index);
122 void insert(T t,
int index);
126 void remove(
int index);
200 T *data_new =
new T[current_size * 2];
201 memcpy(data_new, data,
sizeof(T) * current_size);
223 void sort(
int (*comparator)(
const void*,
const void*)) {
234 return data_[index] == target;
245 while (top - bottom > 1) {
246 int middle = (bottom + top) / 2;
247 if (
data_[middle] > target)
278 while (old_index < size_used_ && !delete_cb->Run(old_index++))
282 if (!delete_cb->
Run(old_index)) {
291 T result =
static_cast<T
>(0);
303 if (target_index < 0)
307 unsigned int seed = 1;
312 void swap(
int index1,
int index2) {
313 if (index1 != index2) {
314 T tmp =
data_[index1];
323 if (
data_[i] < rangemin || rangemax <
data_[i])
331 int choose_nth_item(
int target_index,
int start,
int end,
unsigned int* seed);
362 FILE* fp = fopen(
filename.string(),
"rb");
364 fseek(fp, 0, SEEK_END);
365 size_t size = ftell(fp);
366 fseek(fp, 0, SEEK_SET);
371 result = fread(&(*data)[0], 1, size, fp) == size;
381 FILE* fp = fopen(
filename.string(),
"wb");
382 if (fp == NULL)
return false;
384 static_cast<int>(fwrite(&data[0], 1, data.
size(), fp)) == data.
size();
389 template <
typename T>
390 bool cmp_eq(T
const & t1, T
const & t2) {
398 template <
typename T>
400 const T* a =
static_cast<const T *
> (t1);
401 const T* b =
static_cast<const T *
> (t2);
404 }
else if (*b < *a) {
415 template <
typename T>
417 const T* a = *
reinterpret_cast<T *
const *
>(t1);
418 const T* b = *
reinterpret_cast<T *
const *
>(t2);
421 }
else if (*b < *a) {
449 for (
int i = 0; i < other.
size(); ++i) {
456 if (&other !=
this) {
465 void remove(
int index) {
473 for (
int i =
size; i < GenericVector<T*>::size_used_; ++i)
488 for (; old_index < GenericVector<T*>::size_used_; ++old_index) {
516 if (fwrite(&used,
sizeof(used), 1, fp) != 1)
return false;
517 for (
int i = 0; i < used; ++i) {
519 if (fwrite(&non_null,
sizeof(non_null), 1, fp) != 1)
return false;
526 if (fp->
FWrite(&used,
sizeof(used), 1) != 1)
return false;
527 for (
int i = 0; i < used; ++i) {
529 if (fp->
FWrite(&non_null,
sizeof(non_null), 1) != 1)
return false;
543 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
547 for (
int i = 0; i < reserved; ++i) {
549 if (fread(&non_null,
sizeof(non_null), 1, fp) != 1)
return false;
553 if (!item->DeSerialize(
swap, fp)) {
570 for (
int i = 0; i < reserved; ++i) {
587 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1)
return false;
591 if (!item->DeSerialize(
swap, fp)) {
605 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1)
return false;
607 if (!T::SkipDeSerialize(
swap, fp))
return false;
620 template <
typename T>
633 template <
typename T>
643 template <
typename T>
650 template <
typename T>
652 if (size_reserved_ >= size || size <= 0)
654 if (size < kDefaultVectorSize) size = kDefaultVectorSize;
655 T* new_array =
new T[size];
656 for (
int i = 0; i < size_used_; ++i)
657 new_array[i] = data_[i];
660 size_reserved_ = size;
663 template <
typename T>
665 if (size_reserved_ == 0) {
666 reserve(kDefaultVectorSize);
669 reserve(2 * size_reserved_);
674 template <
typename T>
678 for (
int i = 0; i < size; ++i)
684 template <
typename T>
690 template <
typename T>
692 assert(index >= 0 && index < size_used_);
696 template <
typename T>
699 return data_[size_used_ - 1];
702 template <
typename T>
705 return data_[--size_used_];
709 template <
typename T>
718 template <
typename T>
721 if (size_reserved_ == size_used_)
723 for (
int i = size_used_; i > index; --i) {
724 data_[i] = data_[i-1];
732 template <
typename T>
735 for (
int i = index; i < size_used_ - 1; ++i) {
736 data_[i] = data_[i+1];
742 template <
typename T>
744 return index >= 0 && index < size_used_;
748 template <
typename T>
750 for (
int i = 0; i < size_used_; ++i) {
752 if (compare_cb_->Run(
object, data_[i]))
759 template <
typename T>
761 return get_index(
object) != -1;
765 template <
typename T>
768 if (size_used_ == size_reserved_)
770 index = size_used_++;
771 data_[index] = object;
775 template <
typename T>
777 int index = get_index(
object);
780 return push_back(
object);
784 template <
typename T>
786 if (size_used_ == size_reserved_)
788 for (
int i = size_used_; i > 0; --i)
789 data_[i] = data_[i-1];
795 template <
typename T>
800 template <
typename T>
803 for (
int i = 0; i < other.
size(); ++i) {
809 template <
typename T>
811 if (&other !=
this) {
820 template <
typename T>
827 template <
typename T>
834 template <
typename T>
836 if (size_reserved_ > 0) {
837 if (clear_cb_ != NULL)
838 for (
int i = 0; i < size_used_; ++i)
839 clear_cb_->Run(data_[i]);
845 if (clear_cb_ != NULL) {
849 if (compare_cb_ != NULL) {
855 template <
typename T>
857 for (
int i = 0; i < size_used_; ++i)
864 template <
typename T>
867 if (fwrite(&size_reserved_,
sizeof(size_reserved_), 1, f) != 1)
return false;
868 if (fwrite(&size_used_,
sizeof(size_used_), 1, f) != 1)
return false;
870 for (
int i = 0; i < size_used_; ++i) {
871 if (!cb->
Run(f, data_[i])) {
878 if (fwrite(data_,
sizeof(T), size_used_, f) != size_used_)
return false;
883 template <
typename T>
888 if (fread(&reserved,
sizeof(reserved), 1, f) != 1)
return false;
891 if (fread(&size_used_,
sizeof(size_used_), 1, f) != 1)
return false;
894 for (
int i = 0; i < size_used_; ++i) {
895 if (!cb->
Run(f, data_ + i, swap)) {
902 if (fread(data_,
sizeof(T), size_used_, f) != size_used_)
return false;
904 for (
int i = 0; i < size_used_; ++i)
913 template <
typename T>
915 if (fwrite(&size_used_,
sizeof(size_used_), 1, fp) != 1)
return false;
916 if (fwrite(data_,
sizeof(*data_), size_used_, fp) != size_used_)
return false;
919 template <
typename T>
921 if (fp->
FWrite(&size_used_,
sizeof(size_used_), 1) != 1)
return false;
922 if (fp->
FWrite(data_,
sizeof(*data_), size_used_) != size_used_)
return false;
930 template <
typename T>
933 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
936 size_used_ = reserved;
937 if (fread(data_,
sizeof(T), size_used_, fp) != size_used_)
return false;
939 for (
int i = 0; i < size_used_; ++i)
940 ReverseN(&data_[i],
sizeof(data_[i]));
944 template <
typename T>
947 if (fp->
FRead(&reserved,
sizeof(reserved), 1) != 1)
return false;
950 size_used_ = reserved;
951 if (fp->
FRead(data_,
sizeof(T), size_used_) != size_used_)
return false;
953 for (
int i = 0; i < size_used_; ++i)
954 ReverseN(&data_[i],
sizeof(data_[i]));
958 template <
typename T>
961 if (fp->
FRead(&reserved,
sizeof(reserved), 1) != 1)
return false;
963 return fp->
FRead(NULL,
sizeof(T), reserved) == reserved;
969 template <
typename T>
971 if (fwrite(&size_used_,
sizeof(size_used_), 1, fp) != 1)
return false;
972 for (
int i = 0; i < size_used_; ++i) {
973 if (!data_[i].Serialize(fp))
return false;
977 template <
typename T>
979 if (fp->
FWrite(&size_used_,
sizeof(size_used_), 1) != 1)
return false;
980 for (
int i = 0; i < size_used_; ++i) {
981 if (!data_[i].Serialize(fp))
return false;
991 template <
typename T>
994 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
997 init_to_size(reserved, empty);
998 for (
int i = 0; i < reserved; ++i) {
999 if (!data_[i].DeSerialize(swap, fp))
return false;
1003 template <
typename T>
1006 if (fp->
FRead(&reserved,
sizeof(reserved), 1) != 1)
return false;
1009 init_to_size(reserved, empty);
1010 for (
int i = 0; i < reserved; ++i) {
1011 if (!data_[i].DeSerialize(swap, fp))
return false;
1015 template <
typename T>
1018 if (fp->
FRead(&reserved,
sizeof(reserved), 1) != 1)
return false;
1020 for (
int i = 0; i < reserved; ++i) {
1021 if (!T::SkipDeSerialize(swap, fp))
return false;
1028 template <
typename T>
1031 this->data_ = from->
data_;
1043 template <
typename T>
1045 sort(&tesseract::sort_cmp<T>);
1061 template <
typename T>
1063 unsigned int* seed) {
1065 int num_elements = end - start;
1067 if (num_elements <= 1)
1069 if (num_elements == 2) {
1070 if (data_[start] < data_[start + 1]) {
1071 return target_index > start ? start + 1 : start;
1073 return target_index > start ? start : start + 1;
1077 #ifndef rand_r // _MSC_VER, ANDROID 1079 #define rand_r(seed) rand() 1081 int pivot =
rand_r(seed) % num_elements + start;
1087 int next_lesser = start;
1088 int prev_greater = end;
1089 for (
int next_sample = start + 1; next_sample < prev_greater;) {
1090 if (data_[next_sample] < data_[next_lesser]) {
1091 swap(next_lesser++, next_sample++);
1092 }
else if (data_[next_sample] == data_[next_lesser]) {
1095 swap(--prev_greater, next_sample);
1100 if (target_index < next_lesser)
1102 else if (target_index < prev_greater)
1109 #endif // TESSERACT_CCUTIL_GENERICVECTOR_H_ int sort_ptr_cmp(const void *t1, const void *t2)
TessResultCallback2< bool, T const &, T const & > * compare_cb_
bool DeSerializeElement(bool swap, TFile *fp)
PointerVector< T > & operator+=(const PointerVector &other)
int size_reserved() const
bool DeSerialize(bool swap, TFile *fp)
void compact(TessResultCallback1< bool, const T *> *delete_cb)
GenericVectorEqEq(int size)
void resize_no_init(int size)
static const int kDefaultVectorSize
static bool DeSerializeSkip(bool swap, TFile *fp)
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
int FWrite(const void *buffer, int size, int count)
void Reverse32(void *ptr)
void insert(T t, int index)
GenericVector< T > & operator+=(const GenericVector &other)
static T * double_the_size_memcpy(int current_size, T *data)
void swap(int index1, int index2)
ICOORD & operator+=(ICOORD &op1, const ICOORD &op2)
PointerVector< T > & operator=(const PointerVector &other)
bool contains(T object) const
bool Serialize(TFile *fp) const
GenericVector(int size, T init_val)
void move(GenericVector< T > *from)
GenericVector(const GenericVector &other)
int get_index(T object) const
int push_back_new(T object)
bool DeSerializeClasses(bool swap, FILE *fp)
static bool SkipDeSerialize(bool swap, tesseract::TFile *fp)
bool Serialize(FILE *fp) const
void set_compare_callback(TessResultCallback2< bool, T const &, T const &> *cb)
T dot_product(const GenericVector< T > &other) const
inT32 choose_nth_item(inT32 index, float *array, inT32 count)
virtual R Run(A1, A2, A3)=0
bool LoadDataFromFile(const STRING &filename, GenericVector< char > *data)
T & operator[](int index) const
void set_clear_callback(TessCallback1< T > *cb)
PointerVector(const PointerVector &other)
int FRead(void *buffer, int size, int count)
TessCallback1< T > * clear_cb_
bool read(FILE *f, TessResultCallback3< bool, FILE *, T *, bool > *cb, bool swap)
void delete_data_pointers()
bool write(FILE *f, TessResultCallback2< bool, FILE *, T const &> *cb) const
static bool SkipDeSerializeClasses(bool swap, tesseract::TFile *fp)
bool SaveDataToFile(const GenericVector< char > &data, const STRING &filename)
bool WithinBounds(const T &rangemin, const T &rangemax) const
int binary_search(const T &target) const
static bool DeSerializeSize(bool swap, TFile *fp, inT32 *size)
bool SerializeClasses(FILE *fp) const
int choose_nth_item(int target_index)
T contains_index(int index) const
GenericVector< T > & operator=(const GenericVector &other)
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
bool cmp_eq(T const &t1, T const &t2)
void sort(int(*comparator)(const void *, const void *))
int sort_cmp(const void *t1, const void *t2)
bool DeSerialize(bool swap, FILE *fp)
void ReverseN(void *ptr, int num_bytes)
void init_to_size(int size, T t)
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)
void compact(TessResultCallback1< bool, int > *delete_cb)
bool bool_binary_search(const T &target) const
bool Serialize(FILE *fp) const
bool DeSerialize(bool swap, FILE *fp)