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>
626 T* new_array =
new T[
size];
628 new_array[i] =
data_[i];
634 template <
typename T>
645 template <
typename T>
649 for (
int i = 0; i <
size; ++i)
655 template <
typename T>
661 template <
typename T>
667 template <
typename T>
673 template <
typename T>
680 template <
typename T>
689 template <
typename T>
703 template <
typename T>
706 for (
int i = index; i <
size_used_ - 1; ++i) {
713 template <
typename T>
719 template <
typename T>
730 template <
typename T>
736 template <
typename T>
742 data_[index] = object;
746 template <
typename T>
755 template <
typename T>
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>
826 template <
typename T>
835 template <
typename T>
854 template <
typename T>
859 if (fread(&reserved,
sizeof(reserved), 1, f) != 1)
return false;
884 template <
typename T>
890 template <
typename T>
901 template <
typename T>
904 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
915 template <
typename T>
918 if (fp->
FRead(&reserved,
sizeof(reserved), 1) != 1)
return false;
933 template <
typename T>
941 template <
typename T>
955 template <
typename T>
958 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
962 for (
int i = 0; i < reserved; ++i) {
967 template <
typename T>
970 if (fp->
FRead(&reserved,
sizeof(reserved), 1) != 1)
return false;
974 for (
int i = 0; i < reserved; ++i) {
982 template <
typename T>
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) {
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_
int sort_cmp(const void *t1, const void *t2)
bool DeSerializeClasses(bool swap, FILE *fp)
GenericVector< T > & operator+=(const GenericVector &other)
T & operator[](int index) const
bool Serialize(TFile *fp) const
void set_clear_callback(TessCallback1< T > *cb)
int sort_ptr_cmp(const void *t1, const void *t2)
void ReverseN(void *ptr, int num_bytes)
T dot_product(const GenericVector< T > &other) const
void delete_data_pointers()
GenericVectorEqEq(int size)
int choose_nth_item(int target_index)
static T * double_the_size_memcpy(int current_size, T *data)
int size_reserved() const
int binary_search(const T &target) const
bool bool_binary_search(const T &target) const
bool Serialize(FILE *fp) const
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
void set_compare_callback(TessResultCallback2< bool, T const &, T const & > *cb)
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
void compact(TessResultCallback1< bool, int > *delete_cb)
bool DeSerialize(bool swap, TFile *fp)
static const int kDefaultVectorSize
bool contains(T object) const
void compact(TessResultCallback1< bool, const T * > *delete_cb)
void resize_no_init(int size)
void swap(int index1, int index2)
const char * string() const
bool read(FILE *f, TessResultCallback3< bool, FILE *, T *, bool > *cb, bool swap)
bool DeSerialize(bool swap, FILE *fp)
bool SerializeClasses(FILE *fp) const
void insert(T t, int index)
PointerVector(const PointerVector &other)
GenericVector(int size, T init_val)
void init_to_size(int size, T t)
GenericVector(const GenericVector &other)
GenericVector< T > & operator=(const GenericVector &other)
void sort(int(*comparator)(const void *, const void *))
bool write(FILE *f, TessResultCallback2< bool, FILE *, T const & > *cb) const
T contains_index(int index) const
int push_back_new(T object)
void Reverse32(void *ptr)
bool Serialize(FILE *fp) const
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)
PointerVector< T > & operator=(const PointerVector &other)
bool SaveDataToFile(const GenericVector< char > &data, const STRING &filename)
virtual R Run(A1, A2, A3)=0
bool WithinBounds(const T &rangemin, const T &rangemax) const
void move(GenericVector< T > *from)
bool cmp_eq(T const &t1, T const &t2)
bool DeSerialize(bool swap, FILE *fp)
TessCallback1< T > * clear_cb_
int FWrite(const void *buffer, int size, int count)
TessResultCallback2< bool, T const &, T const & > * compare_cb_
PointerVector< T > & operator+=(const PointerVector &other)
bool LoadDataFromFile(const STRING &filename, GenericVector< char > *data)
int get_index(T object) const
int FRead(void *buffer, int size, int count)