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);
196 T *data_new =
new T[current_size * 2];
197 memcpy(data_new, data,
sizeof(T) * current_size);
219 void sort(
int (*comparator)(
const void*,
const void*)) {
230 return data_[index] == target;
242 int middle = (bottom + top) / 2;
243 if (
data_[middle] > target)
248 while (top - bottom > 1);
266 size_used_ = last_write + 1;
275 while (old_index < size_used_ && !delete_cb->Run(old_index++))
279 if (!delete_cb->
Run(old_index)) {
283 size_used_ = new_size;
288 T result =
static_cast<T
>(0);
300 if (target_index < 0)
304 unsigned int seed = 1;
309 void swap(
int index1,
int index2) {
310 if (index1 != index2) {
311 T tmp =
data_[index1];
320 if (
data_[i] < rangemin || rangemax <
data_[i])
328 int choose_nth_item(
int target_index,
int start,
int end,
unsigned int* seed);
358 FILE* fp = fopen(filename.
string(),
"rb");
359 if (fp ==
NULL)
return false;
360 fseek(fp, 0, SEEK_END);
361 size_t size = ftell(fp);
362 fseek(fp, 0, SEEK_SET);
365 bool result = fread(&(*data)[0], 1, size, fp) == size;
373 FILE* fp = fopen(filename.
string(),
"wb");
374 if (fp ==
NULL)
return false;
376 static_cast<int>(fwrite(&data[0], 1, data.
size(), fp)) == data.
size();
381 template <
typename T>
382 bool cmp_eq(T
const & t1, T
const & t2) {
390 template <
typename T>
392 const T* a =
static_cast<const T *
> (t1);
393 const T* b =
static_cast<const T *
> (t2);
396 }
else if (*b < *a) {
407 template <
typename T>
409 const T* a = *
reinterpret_cast<T *
const *
>(t1);
410 const T* b = *
reinterpret_cast<T *
const *
>(t2);
413 }
else if (*b < *a) {
441 for (
int i = 0; i < other.
size(); ++i) {
448 if (&other !=
this) {
457 void remove(
int index) {
465 for (
int i = size; i < GenericVector<T*>::size_used_; ++i)
480 for (; old_index < GenericVector<T*>::size_used_; ++old_index) {
508 if (fwrite(&used,
sizeof(used), 1, fp) != 1)
return false;
509 for (
int i = 0; i < used; ++i) {
511 if (fwrite(&non_null,
sizeof(non_null), 1, fp) != 1)
return false;
518 if (fp->
FWrite(&used,
sizeof(used), 1) != 1)
return false;
519 for (
int i = 0; i < used; ++i) {
521 if (fp->
FWrite(&non_null,
sizeof(non_null), 1) != 1)
return false;
535 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
539 for (
int i = 0; i < reserved; ++i) {
541 if (fread(&non_null,
sizeof(non_null), 1, fp) != 1)
return false;
545 if (!item->DeSerialize(swap, fp)) {
559 if (fp->
FRead(&reserved,
sizeof(reserved), 1) != 1)
return false;
563 for (
int i = 0; i < reserved; ++i) {
565 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1)
return false;
569 if (!item->DeSerialize(swap, fp)) {
585 sort(&sort_ptr_cmp<T>);
592 template <
typename T>
605 template <
typename T>
615 template <
typename T>
622 template <
typename T>
624 if (size_reserved_ >= size || size <= 0)
626 T* new_array =
new T[size];
627 for (
int i = 0; i < size_used_; ++i)
628 new_array[i] = data_[i];
629 if (data_ !=
NULL)
delete[] data_;
631 size_reserved_ = size;
634 template <
typename T>
636 if (size_reserved_ == 0) {
637 reserve(kDefaultVectorSize);
640 reserve(2 * size_reserved_);
645 template <
typename T>
649 for (
int i = 0; i < size; ++i)
655 template <
typename T>
661 template <
typename T>
663 assert(index >= 0 && index < size_used_);
667 template <
typename T>
670 return data_[size_used_ - 1];
673 template <
typename T>
676 return data_[--size_used_];
680 template <
typename T>
689 template <
typename T>
692 if (size_reserved_ == size_used_)
694 for (
int i = size_used_; i > index; --i) {
695 data_[i] = data_[i-1];
703 template <
typename T>
706 for (
int i = index; i < size_used_ - 1; ++i) {
707 data_[i] = data_[i+1];
713 template <
typename T>
715 return index >= 0 && index < size_used_;
719 template <
typename T>
721 for (
int i = 0; i < size_used_; ++i) {
723 if (compare_cb_->Run(
object, data_[i]))
730 template <
typename T>
732 return get_index(
object) != -1;
736 template <
typename T>
739 if (size_used_ == size_reserved_)
741 index = size_used_++;
742 data_[index] = object;
746 template <
typename T>
748 int index = get_index(
object);
751 return push_back(
object);
755 template <
typename T>
757 if (size_used_ == size_reserved_)
759 for (
int i = size_used_; i > 0; --i)
760 data_[i] = data_[i-1];
766 template <
typename T>
771 template <
typename T>
774 for (
int i = 0; i < other.
size(); ++i) {
780 template <
typename T>
782 if (&other !=
this) {
791 template <
typename T>
798 template <
typename T>
805 template <
typename T>
807 if (size_reserved_ > 0) {
808 if (clear_cb_ !=
NULL)
809 for (
int i = 0; i < size_used_; ++i)
810 clear_cb_->Run(data_[i]);
816 if (clear_cb_ !=
NULL) {
820 if (compare_cb_ !=
NULL) {
826 template <
typename T>
828 for (
int i = 0; i < size_used_; ++i)
835 template <
typename T>
838 if (fwrite(&size_reserved_,
sizeof(size_reserved_), 1, f) != 1)
return false;
839 if (fwrite(&size_used_,
sizeof(size_used_), 1, f) != 1)
return false;
841 for (
int i = 0; i < size_used_; ++i) {
842 if (!cb->
Run(f, data_[i])) {
849 if (fwrite(data_,
sizeof(T), size_used_, f) != size_used_)
return false;
854 template <
typename T>
859 if (fread(&reserved,
sizeof(reserved), 1, f) != 1)
return false;
862 if (fread(&size_used_,
sizeof(size_used_), 1, f) != 1)
return false;
865 for (
int i = 0; i < size_used_; ++i) {
866 if (!cb->
Run(f, data_ + i, swap)) {
873 if (fread(data_,
sizeof(T), size_used_, f) != size_used_)
return false;
875 for (
int i = 0; i < size_used_; ++i)
884 template <
typename T>
886 if (fwrite(&size_used_,
sizeof(size_used_), 1, fp) != 1)
return false;
887 if (fwrite(data_,
sizeof(*data_), size_used_, fp) != size_used_)
return false;
890 template <
typename T>
892 if (fp->
FWrite(&size_used_,
sizeof(size_used_), 1) != 1)
return false;
893 if (fp->
FWrite(data_,
sizeof(*data_), size_used_) != size_used_)
return false;
901 template <
typename T>
904 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
907 size_used_ = reserved;
908 if (fread(data_,
sizeof(T), size_used_, fp) != size_used_)
return false;
910 for (
int i = 0; i < size_used_; ++i)
911 ReverseN(&data_[i],
sizeof(data_[i]));
915 template <
typename T>
918 if (fp->
FRead(&reserved,
sizeof(reserved), 1) != 1)
return false;
921 size_used_ = reserved;
922 if (fp->
FRead(data_,
sizeof(T), size_used_) != size_used_)
return false;
924 for (
int i = 0; i < size_used_; ++i)
925 ReverseN(&data_[i],
sizeof(data_[i]));
933 template <
typename T>
935 if (fwrite(&size_used_,
sizeof(size_used_), 1, fp) != 1)
return false;
936 for (
int i = 0; i < size_used_; ++i) {
937 if (!data_[i].Serialize(fp))
return false;
941 template <
typename T>
943 if (fp->
FWrite(&size_used_,
sizeof(size_used_), 1) != 1)
return false;
944 for (
int i = 0; i < size_used_; ++i) {
945 if (!data_[i].Serialize(fp))
return false;
955 template <
typename T>
958 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
961 init_to_size(reserved, empty);
962 for (
int i = 0; i < reserved; ++i) {
963 if (!data_[i].DeSerialize(swap, fp))
return false;
967 template <
typename T>
970 if (fp->
FRead(&reserved,
sizeof(reserved), 1) != 1)
return false;
973 init_to_size(reserved, empty);
974 for (
int i = 0; i < reserved; ++i) {
975 if (!data_[i].DeSerialize(swap, fp))
return false;
982 template <
typename T>
985 this->data_ = from->
data_;
997 template <
typename T>
999 sort(&tesseract::sort_cmp<T>);
1015 template <
typename T>
1017 unsigned int* seed) {
1019 int num_elements = end - start;
1021 if (num_elements <= 1)
1023 if (num_elements == 2) {
1024 if (data_[start] < data_[start + 1]) {
1025 return target_index > start ? start + 1 : start;
1027 return target_index > start ? start : start + 1;
1031 #ifndef rand_r // _MSC_VER, ANDROID
1033 #define rand_r(seed) rand()
1035 int pivot =
rand_r(seed) % num_elements + start;
1041 int next_lesser = start;
1042 int prev_greater = end;
1043 for (
int next_sample = start + 1; next_sample < prev_greater;) {
1044 if (data_[next_sample] < data_[next_lesser]) {
1045 swap(next_lesser++, next_sample++);
1046 }
else if (data_[next_sample] == data_[next_lesser]) {
1049 swap(--prev_greater, next_sample);
1054 if (target_index < next_lesser)
1056 else if (target_index < prev_greater)
1063 #endif // TESSERACT_CCUTIL_GENERICVECTOR_H_
bool WithinBounds(const T &rangemin, const T &rangemax) const
int sort_cmp(const void *t1, const void *t2)
GenericVector< T > & operator+=(const GenericVector &other)
T & operator[](int index) const
bool DeSerializeClasses(bool swap, FILE *fp)
static T * double_the_size_memcpy(int current_size, T *data)
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)
GenericVectorEqEq(int size)
int sort_ptr_cmp(const void *t1, const void *t2)
PointerVector< T > & operator=(const PointerVector &other)
T dot_product(const GenericVector< T > &other) const
void Reverse32(void *ptr)
void set_compare_callback(TessResultCallback2< bool, T const &, T const & > *cb)
PointerVector< T > & operator+=(const PointerVector &other)
void resize_no_init(int size)
bool read(FILE *f, TessResultCallback3< bool, FILE *, T *, bool > *cb, bool swap)
int size_reserved() const
bool bool_binary_search(const T &target) const
bool Serialize(FILE *fp) const
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
bool cmp_eq(T const &t1, T const &t2)
GenericVector< T > & operator=(const GenericVector &other)
void sort(int(*comparator)(const void *, const void *))
bool SaveDataToFile(const GenericVector< char > &data, const STRING &filename)
void compact(TessResultCallback1< bool, int > *delete_cb)
inT32 choose_nth_item(inT32 index, float *array, inT32 count)
bool SerializeClasses(FILE *fp) const
void insert(T t, int index)
bool LoadDataFromFile(const STRING &filename, GenericVector< char > *data)
static const int kDefaultVectorSize
void delete_data_pointers()
GenericVector(int size, T init_val)
int choose_nth_item(int target_index)
void init_to_size(int size, T t)
bool DeSerialize(bool swap, FILE *fp)
int binary_search(const T &target) const
T contains_index(int index) const
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
bool Serialize(TFile *fp) const
TessCallback1< T > * clear_cb_
bool contains(T object) const
int push_back_new(T object)
void swap(int index1, int index2)
void compact(TessResultCallback1< bool, const T * > *delete_cb)
void ReverseN(void *ptr, int num_bytes)
int FWrite(const void *buffer, int size, int count)
bool DeSerialize(bool swap, TFile *fp)
bool Serialize(FILE *fp) const
PointerVector(const PointerVector &other)
GenericVector(const GenericVector &other)
void move(GenericVector< T > *from)
bool write(FILE *f, TessResultCallback2< bool, FILE *, T const & > *cb) const
TessResultCallback2< bool, T const &, T const & > * compare_cb_
bool DeSerialize(bool swap, FILE *fp)
const char * string() const
ICOORD & operator+=(ICOORD &op1, const ICOORD &op2)
int get_index(T object) const
void set_clear_callback(TessCallback1< T > *cb)
int FRead(void *buffer, int size, int count)
virtual R Run(A1, A2, A3)=0