20 #ifndef TESSERACT_CCUTIL_GENERICVECTOR_H_ 21 #define TESSERACT_CCUTIL_GENERICVECTOR_H_ 76 static_assert(
sizeof(
size_used_) <=
sizeof(
size_t),
77 "Wow! sizeof(size_t) < sizeof(int32_t)!!");
95 T &
get(
int index)
const;
107 bool contains(
const T&
object)
const;
125 void set(
const T& t,
int index);
128 void insert(
const 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)
286 while (old_index < size_used_ && !delete_cb->Run(old_index++))
290 if (!delete_cb->
Run(old_index)) {
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);
369 FILE* fp = fopen(filename,
"rb");
371 fseek(fp, 0, SEEK_END);
372 long size = ftell(fp);
373 fseek(fp, 0, SEEK_SET);
375 if (size > 0 && size < LONG_MAX) {
379 result =
static_cast<long>(fread(&(*data)[0], 1, size, fp)) == size;
395 FILE* fp = fopen(filename.
string(),
"wb");
396 if (fp ==
nullptr)
return false;
398 static_cast<int>(fwrite(&data[0], 1, data.
size(), fp)) == data.
size();
410 lines_str.
split(
'\n', lines);
414 template <
typename T>
415 bool cmp_eq(T
const & t1, T
const & t2) {
423 template <
typename T>
425 const T* a =
static_cast<const T *
> (t1);
426 const T* b =
static_cast<const T *
> (t2);
429 }
else if (*b < *a) {
440 template <
typename T>
442 const T* a = *
static_cast<T* const*
>(t1);
443 const T* b = *
static_cast<T* const*
>(t2);
446 }
else if (*b < *a) {
474 for (
int i = 0; i < other.
size(); ++i) {
481 if (&other !=
this) {
490 void remove(
int index) {
498 for (
int i =
size; i < GenericVector<T*>::size_used_; ++i)
513 for (; old_index < GenericVector<T*>::size_used_; ++old_index) {
541 if (fwrite(&used,
sizeof(used), 1, fp) != 1)
return false;
542 for (
int i = 0; i < used; ++i) {
544 if (fwrite(&non_null,
sizeof(non_null), 1, fp) != 1)
return false;
551 if (fp->
FWrite(&used,
sizeof(used), 1) != 1)
return false;
552 for (
int i = 0; i < used; ++i) {
554 if (fp->
FWrite(&non_null,
sizeof(non_null), 1) != 1)
return false;
568 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
571 assert(reserved <= UINT16_MAX);
572 if (reserved > UINT16_MAX) {
577 for (uint32_t i = 0; i < reserved; ++i) {
579 if (fread(&non_null,
sizeof(non_null), 1, fp) != 1)
return false;
583 if (!item->DeSerialize(
swap, fp)) {
600 for (
int i = 0; i < reserved; ++i) {
615 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1)
return false;
619 if (!item->DeSerialize(fp)) {
633 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1)
return false;
635 if (!T::SkipDeSerialize(fp))
return false;
648 template <
typename T>
661 template <
typename T>
668 if (size < kDefaultVectorSize) size = kDefaultVectorSize;
670 size_reserved_ = size;
673 compare_cb_ =
nullptr;
676 template <
typename T>
683 template <
typename T>
685 if (size_reserved_ >= size || size <= 0)
687 if (size < kDefaultVectorSize) size = kDefaultVectorSize;
688 T* new_array =
new T[size];
689 for (
int i = 0; i < size_used_; ++i)
690 new_array[i] = data_[i];
693 size_reserved_ = size;
696 template <
typename T>
698 if (size_reserved_ == 0) {
699 reserve(kDefaultVectorSize);
702 reserve(2 * size_reserved_);
707 template <
typename T>
711 for (
int i = 0; i < size; ++i)
717 template <
typename T>
719 assert(index >= 0 && index < size_used_);
723 template <
typename T>
725 assert(index >= 0 && index < size_used_);
729 template <
typename T>
731 assert(size_used_ > 0);
732 return data_[size_used_ - 1];
735 template <
typename T>
737 assert(size_used_ > 0);
738 return data_[--size_used_];
742 template <
typename T>
744 assert(index >= 0 && index < size_used_);
751 template <
typename T>
753 assert(index >= 0 && index <= size_used_);
754 if (size_reserved_ == size_used_)
756 for (
int i = size_used_; i > index; --i) {
757 data_[i] = data_[i-1];
765 template <
typename T>
767 assert(index >= 0 && index < size_used_);
768 for (
int i = index; i < size_used_ - 1; ++i) {
769 data_[i] = data_[i+1];
775 template <
typename T>
777 return index >= 0 && index < size_used_;
781 template <
typename T>
783 for (
int i = 0; i < size_used_; ++i) {
784 assert(compare_cb_ !=
nullptr);
785 if (compare_cb_->Run(
object, data_[i]))
792 template <
typename T>
794 return get_index(
object) != -1;
798 template <
typename T>
801 if (size_used_ == size_reserved_)
803 index = size_used_++;
804 data_[index] = object;
808 template <
typename T>
810 int index = get_index(
object);
813 return push_back(
object);
817 template <
typename T>
819 if (size_used_ == size_reserved_)
821 for (
int i = size_used_; i > 0; --i)
822 data_[i] = data_[i-1];
828 template <
typename T>
833 template <
typename T>
836 for (
int i = 0; i < other.
size(); ++i) {
842 template <
typename T>
844 if (&other !=
this) {
853 template <
typename T>
860 template <
typename T>
867 template <
typename T>
869 if (size_reserved_ > 0 && clear_cb_ !=
nullptr) {
870 for (
int i = 0; i < size_used_; ++i)
871 clear_cb_->Run(data_[i]);
880 compare_cb_ =
nullptr;
883 template <
typename T>
885 for (
int i = 0; i < size_used_; ++i) {
891 template <
typename T>
894 if (fwrite(&size_reserved_,
sizeof(size_reserved_), 1, f) != 1)
return false;
895 if (fwrite(&size_used_,
sizeof(size_used_), 1, f) != 1)
return false;
897 for (
int i = 0; i < size_used_; ++i) {
898 if (!cb->
Run(f, data_[i])) {
905 if (fwrite(data_,
sizeof(T), size_used_, f) != unsigned_size())
911 template <
typename T>
915 if (f->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
917 if (f->
FReadEndian(&size_used_,
sizeof(size_used_), 1) != 1)
return false;
919 for (
int i = 0; i < size_used_; ++i) {
920 if (!cb->
Run(f, data_ + i)) {
927 if (f->
FReadEndian(data_,
sizeof(T), size_used_) != size_used_)
935 template <
typename T>
937 if (fwrite(&size_used_,
sizeof(size_used_), 1, fp) != 1)
return false;
938 if (fwrite(data_,
sizeof(*data_), size_used_, fp) != unsigned_size())
942 template <
typename T>
944 if (fp->
FWrite(&size_used_,
sizeof(size_used_), 1) != 1)
return false;
945 if (fp->
FWrite(data_,
sizeof(*data_), size_used_) != size_used_)
return false;
953 template <
typename T>
956 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
959 assert(reserved <= UINT16_MAX);
960 if (reserved > UINT16_MAX)
return false;
962 size_used_ = reserved;
963 if (fread(data_,
sizeof(T), size_used_, fp) != unsigned_size())
return false;
965 for (
int i = 0; i < size_used_; ++i)
966 ReverseN(&data_[i],
sizeof(data_[i]));
970 template <
typename T>
973 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
975 const uint32_t limit = 50000000;
976 assert(reserved <= limit);
977 if (reserved > limit)
return false;
979 size_used_ = reserved;
980 return fp->
FReadEndian(data_,
sizeof(T), size_used_) == size_used_;
982 template <
typename T>
985 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
986 return fp->
FRead(
nullptr,
sizeof(T), reserved) == reserved;
992 template <
typename T>
994 if (fwrite(&size_used_,
sizeof(size_used_), 1, fp) != 1)
return false;
995 for (
int i = 0; i < size_used_; ++i) {
996 if (!data_[i].
Serialize(fp))
return false;
1000 template <
typename T>
1002 if (fp->
FWrite(&size_used_,
sizeof(size_used_), 1) != 1)
return false;
1003 for (
int i = 0; i < size_used_; ++i) {
1004 if (!data_[i].
Serialize(fp))
return false;
1014 template <
typename T>
1017 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1)
return false;
1020 init_to_size(reserved, empty);
1021 for (
int i = 0; i < reserved; ++i) {
1022 if (!data_[i].
DeSerialize(swap, fp))
return false;
1026 template <
typename T>
1029 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
1031 init_to_size(reserved, empty);
1032 for (
int i = 0; i < reserved; ++i) {
1037 template <
typename T>
1040 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1)
return false;
1041 for (
int i = 0; i < reserved; ++i) {
1042 if (!T::SkipDeSerialize(fp))
return false;
1049 template <
typename T>
1052 this->data_ = from->
data_;
1057 from->
data_ =
nullptr;
1064 template <
typename T>
1066 sort(&tesseract::sort_cmp<T>);
1082 template <
typename T>
1084 unsigned int* seed) {
1086 int num_elements = end - start;
1088 if (num_elements <= 1)
1090 if (num_elements == 2) {
1091 if (data_[start] < data_[start + 1]) {
1092 return target_index > start ? start + 1 : start;
1094 return target_index > start ? start : start + 1;
1098 #ifndef rand_r // _MSC_VER, ANDROID 1100 #define rand_r(seed) rand() 1102 int pivot =
rand_r(seed) % num_elements + start;
1108 int next_lesser = start;
1109 int prev_greater = end;
1110 for (
int next_sample = start + 1; next_sample < prev_greater;) {
1111 if (data_[next_sample] < data_[next_lesser]) {
1112 swap(next_lesser++, next_sample++);
1113 }
else if (data_[next_sample] == data_[next_lesser]) {
1116 swap(--prev_greater, next_sample);
1121 if (target_index < next_lesser)
1123 else if (target_index < prev_greater)
1130 #endif // TESSERACT_CCUTIL_GENERICVECTOR_H_ int FWrite(const void *buffer, size_t size, int count)
ICOORD & operator+=(ICOORD &op1, const ICOORD &op2)
static bool DeSerializeSkip(TFile *fp)
GenericVector(const GenericVector &other)
bool WithinBounds(const T &rangemin, const T &rangemax) const
void compact(TessResultCallback1< bool, const T *> *delete_cb)
void resize_no_init(int size)
int FRead(void *buffer, size_t size, int count)
GenericVector< T > & operator+=(const GenericVector &other)
static bool DeSerializeSize(TFile *fp, int32_t *size)
static const int kDefaultVectorSize
bool SaveDataToFile(const GenericVector< char > &data, const STRING &filename)
void move(GenericVector< T > *from)
bool write(FILE *f, TessResultCallback2< bool, FILE *, T const &> *cb) const
GenericVector(int size, const T &init_val)
const char * string() const
static T * double_the_size_memcpy(int current_size, T *data)
void set_clear_callback(TessCallback1< T > *cb)
bool SerializeClasses(FILE *fp) const
PointerVector(const PointerVector &other)
int32_t choose_nth_item(int32_t index, float *array, int32_t count)
bool DeSerialize(bool swap, FILE *fp)
bool DeSerialize(bool swap, FILE *fp)
bool Serialize(FILE *fp, const char *data, size_t n)
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
T & operator[](int index) const
void set(const T &t, int index)
bool contains(const T &object) const
bool Serialize(FILE *fp) const
T contains_index(int index) const
int size_reserved() const
void set_compare_callback(TessResultCallback2< bool, T const &, T const &> *cb)
bool DeSerialize(TFile *fp)
void ReverseN(void *ptr, int num_bytes)
int get_index(const T &object) const
static bool SkipDeSerialize(tesseract::TFile *fp)
GenericVectorEqEq(int size)
int FReadEndian(void *buffer, size_t size, int count)
static bool SkipDeSerializeClasses(tesseract::TFile *fp)
void split(const char c, GenericVector< STRING > *splited)
void insert(const T &t, int index)
int push_front(const T &object)
bool cmp_eq(T const &t1, T const &t2)
bool DeSerializeElement(TFile *fp)
bool bool_binary_search(const T &target) const
bool read(tesseract::TFile *f, TessResultCallback2< bool, tesseract::TFile *, T *> *cb)
void init_to_size(int size, const T &t)
int push_back_new(const T &object)
void compact(TessResultCallback1< bool, int > *delete_cb)
size_t unsigned_size() const
bool Serialize(FILE *fp) const
int choose_nth_item(int target_index)
TessResultCallback2< bool, T const &, T const & > * compare_cb_
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
T dot_product(const GenericVector< T > &other) const
void Reverse32(void *ptr)
void swap(int index1, int index2)
PointerVector< T > & operator+=(const PointerVector &other)
bool LoadFileLinesToStrings(const STRING &filename, GenericVector< STRING > *lines)
bool DeSerializeClasses(bool swap, FILE *fp)
void delete_data_pointers()
int binary_search(const T &target) const
bool Serialize(TFile *fp) const
PointerVector< T > & operator=(const PointerVector &other)
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
int sort_cmp(const void *t1, const void *t2)
bool DeSerialize(FILE *fp, char *data, size_t n)
void sort(int(*comparator)(const void *, const void *))
int sort_ptr_cmp(const void *t1, const void *t2)
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)
TessCallback1< T > * clear_cb_
GenericVector< T > & operator=(const GenericVector &other)