19 #ifndef TESSERACT_CCUTIL_GENERICVECTOR_H_ 20 #define TESSERACT_CCUTIL_GENERICVECTOR_H_ 77 static_assert(
sizeof(
size_used_) <=
sizeof(
size_t),
78 "Wow! sizeof(size_t) < sizeof(int32_t)!!");
96 T&
get(
int index)
const;
108 bool contains(
const T&
object)
const;
126 void set(
const T& t,
int index);
129 void insert(
const T& t,
int index);
133 void remove(
int index);
214 T* data_new =
new T[current_size * 2];
215 memcpy(data_new, data,
sizeof(T) * current_size);
238 void sort(
int (*comparator)(
const void*,
const void*)) {
250 return data_[index] == target;
261 while (top - bottom > 1) {
262 int middle = (bottom + top) / 2;
263 if (
data_[middle] > target) {
297 while (old_index < size_used_ && !delete_cb->Run(old_index++)) {
302 if (!delete_cb->
Run(old_index)) {
311 T result =
static_cast<T
>(0);
324 if (target_index < 0) {
329 unsigned int seed = 1;
334 void swap(
int index1,
int index2) {
335 if (index1 != index2) {
336 T tmp =
data_[index1];
345 if (
data_[i] < rangemin || rangemax <
data_[i]) {
354 int choose_nth_item(
int target_index,
int start,
int end,
unsigned int* seed);
377 FILE* fp = fopen(filename,
"rb");
379 fseek(fp, 0, SEEK_END);
380 auto size = std::ftell(fp);
381 fseek(fp, 0, SEEK_SET);
383 if (size > 0 && size < LONG_MAX) {
387 result =
static_cast<long>(fread(&(*data)[0], 1, size, fp)) == size;
403 FILE* fp = fopen(filename.
string(),
"wb");
408 static_cast<int>(fwrite(&data[0], 1, data.
size(), fp)) == data.
size();
413 template <
typename T>
422 template <
typename T>
424 const T* a =
static_cast<const T*
>(t1);
425 const T* b =
static_cast<const T*
>(t2);
439 template <
typename T>
441 const T* a = *
static_cast<T* const*
>(t1);
442 const T* b = *
static_cast<T* const*
>(t2);
455 template <
typename T>
473 for (
int i = 0; i < other.
size(); ++i) {
480 if (&other !=
this) {
489 void remove(
int index) {
497 for (
int i =
size; i < GenericVector<T*>::size_used_; ++i) {
514 for (; old_index < GenericVector<T*>::size_used_; ++old_index) {
542 if (fwrite(&used,
sizeof(used), 1, fp) != 1) {
545 for (
int i = 0; i < used; ++i) {
547 if (fwrite(&non_null,
sizeof(non_null), 1, fp) != 1) {
558 if (fp->
FWrite(&used,
sizeof(used), 1) != 1) {
561 for (
int i = 0; i < used; ++i) {
563 if (fp->
FWrite(&non_null,
sizeof(non_null), 1) != 1) {
581 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1) {
588 assert(reserved <= UINT16_MAX);
589 if (reserved > UINT16_MAX) {
594 for (uint32_t i = 0; i < reserved; ++i) {
596 if (fread(&non_null,
sizeof(non_null), 1, fp) != 1) {
602 if (!item->DeSerialize(
swap, fp)) {
621 for (
int i = 0; i < reserved; ++i) {
638 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1) {
644 if (!item->DeSerialize(fp)) {
658 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1) {
662 if (!T::SkipDeSerialize(fp)) {
679 template <
typename T>
692 template <
typename T>
699 if (size < kDefaultVectorSize) {
700 size = kDefaultVectorSize;
703 size_reserved_ = size;
706 compare_cb_ =
nullptr;
709 template <
typename T>
716 template <
typename T>
718 if (size_reserved_ >= size || size <= 0) {
721 if (size < kDefaultVectorSize) {
722 size = kDefaultVectorSize;
724 T* new_array =
new T[size];
725 for (
int i = 0; i < size_used_; ++i) {
726 new_array[i] = data_[i];
730 size_reserved_ = size;
733 template <
typename T>
735 if (size_reserved_ == 0) {
736 reserve(kDefaultVectorSize);
738 reserve(2 * size_reserved_);
743 template <
typename T>
747 for (
int i = 0; i < size; ++i) {
753 template <
typename T>
755 assert(index >= 0 && index < size_used_);
759 template <
typename T>
761 assert(index >= 0 && index < size_used_);
765 template <
typename T>
767 assert(size_used_ > 0);
768 return data_[size_used_ - 1];
771 template <
typename T>
773 assert(size_used_ > 0);
774 return data_[--size_used_];
778 template <
typename T>
780 assert(index >= 0 && index < size_used_);
787 template <
typename T>
789 assert(index >= 0 && index <= size_used_);
790 if (size_reserved_ == size_used_) {
793 for (
int i = size_used_; i > index; --i) {
794 data_[i] = data_[i - 1];
802 template <
typename T>
804 assert(index >= 0 && index < size_used_);
805 for (
int i = index; i < size_used_ - 1; ++i) {
806 data_[i] = data_[i + 1];
812 template <
typename T>
814 return index >= 0 && index < size_used_;
818 template <
typename T>
820 for (
int i = 0; i < size_used_; ++i) {
821 assert(compare_cb_ !=
nullptr);
822 if (compare_cb_->Run(
object, data_[i])) {
830 template <
typename T>
832 return get_index(
object) != -1;
836 template <
typename T>
839 if (size_used_ == size_reserved_) {
842 index = size_used_++;
843 data_[index] = object;
847 template <
typename T>
849 int index = get_index(
object);
853 return push_back(
object);
857 template <
typename T>
859 if (size_used_ == size_reserved_) {
862 for (
int i = size_used_; i > 0; --i) {
863 data_[i] = data_[i - 1];
870 template <
typename T>
875 template <
typename T>
878 for (
int i = 0; i < other.
size(); ++i) {
884 template <
typename T>
886 if (&other !=
this) {
894 template <
typename T>
896 if (size_reserved_ > 0 && clear_cb_ !=
nullptr) {
897 for (
int i = 0; i < size_used_; ++i) {
898 clear_cb_->Run(data_[i]);
908 compare_cb_ =
nullptr;
911 template <
typename T>
913 for (
int i = 0; i < size_used_; ++i) {
918 template <
typename T>
921 if (fwrite(&size_reserved_,
sizeof(size_reserved_), 1, f) != 1) {
924 if (fwrite(&size_used_,
sizeof(size_used_), 1, f) != 1) {
928 for (
int i = 0; i < size_used_; ++i) {
929 if (!cb->
Run(f, data_[i])) {
936 if (fwrite(data_,
sizeof(T), size_used_, f) != unsigned_size()) {
943 template <
typename T>
947 if (f->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
951 if (f->
FReadEndian(&size_used_,
sizeof(size_used_), 1) != 1) {
955 for (
int i = 0; i < size_used_; ++i) {
956 if (!cb->
Run(f, data_ + i)) {
963 if (f->
FReadEndian(data_,
sizeof(T), size_used_) != size_used_) {
972 template <
typename T>
974 if (fwrite(&size_used_,
sizeof(size_used_), 1, fp) != 1) {
977 if (fwrite(data_,
sizeof(*data_), size_used_, fp) != unsigned_size()) {
982 template <
typename T>
984 if (fp->
FWrite(&size_used_,
sizeof(size_used_), 1) != 1) {
987 if (fp->
FWrite(data_,
sizeof(*data_), size_used_) != size_used_) {
997 template <
typename T>
1000 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1) {
1007 assert(reserved <= UINT16_MAX);
1008 if (reserved > UINT16_MAX) {
1012 size_used_ = reserved;
1013 if (fread(data_,
sizeof(T), size_used_, fp) != unsigned_size()) {
1017 for (
int i = 0; i < size_used_; ++i) {
1018 ReverseN(&data_[i],
sizeof(data_[i]));
1023 template <
typename T>
1026 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
1030 const uint32_t limit = 50000000;
1031 assert(reserved <= limit);
1032 if (reserved > limit) {
1036 size_used_ = reserved;
1037 return fp->
FReadEndian(data_,
sizeof(T), size_used_) == size_used_;
1039 template <
typename T>
1042 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
1045 return (uint32_t)fp->
FRead(
nullptr,
sizeof(T), reserved) == reserved;
1051 template <
typename T>
1053 if (fwrite(&size_used_,
sizeof(size_used_), 1, fp) != 1) {
1056 for (
int i = 0; i < size_used_; ++i) {
1063 template <
typename T>
1065 if (fp->
FWrite(&size_used_,
sizeof(size_used_), 1) != 1) {
1068 for (
int i = 0; i < size_used_; ++i) {
1081 template <
typename T>
1084 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1) {
1091 init_to_size(reserved, empty);
1092 for (
int i = 0; i < reserved; ++i) {
1099 template <
typename T>
1102 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
1106 init_to_size(reserved, empty);
1107 for (
int i = 0; i < reserved; ++i) {
1114 template <
typename T>
1117 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
1120 for (
int i = 0; i < reserved; ++i) {
1121 if (!T::SkipDeSerialize(fp)) {
1130 template <
typename T>
1133 this->data_ = from->
data_;
1138 from->
data_ =
nullptr;
1145 template <
typename T>
1147 sort(&tesseract::sort_cmp<T>);
1163 template <
typename T>
1165 unsigned int* seed) {
1167 int num_elements = end - start;
1169 if (num_elements <= 1) {
1172 if (num_elements == 2) {
1173 if (data_[start] < data_[start + 1]) {
1174 return target_index > start ? start + 1 : start;
1176 return target_index > start ? start : start + 1;
1179 #ifndef rand_r // _MSC_VER, ANDROID 1181 # define rand_r(seed) rand() 1183 int pivot =
rand_r(seed) % num_elements + start;
1189 int next_lesser = start;
1190 int prev_greater = end;
1191 for (
int next_sample = start + 1; next_sample < prev_greater;) {
1192 if (data_[next_sample] < data_[next_lesser]) {
1193 swap(next_lesser++, next_sample++);
1194 }
else if (data_[next_sample] == data_[next_lesser]) {
1197 swap(--prev_greater, next_sample);
1202 if (target_index < next_lesser) {
1205 if (target_index < prev_greater) {
1211 #endif // TESSERACT_CCUTIL_GENERICVECTOR_H_
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
int choose_nth_item(int target_index)
bool WithinBounds(const T &rangemin, const T &rangemax) const
T dot_product(const GenericVector< T > &other) const
GenericVector< T > & operator+=(const GenericVector &other)
void Reverse32(void *ptr)
int FReadEndian(void *buffer, size_t size, int count)
bool bool_binary_search(const T &target) const
void init_to_size(int size, const T &t)
void resize_no_init(int size)
bool write(FILE *f, TessResultCallback2< bool, FILE *, T const &> *cb) const
ICOORD & operator+=(ICOORD &op1, const ICOORD &op2)
int sort_ptr_cmp(const void *t1, const void *t2)
int32_t choose_nth_item(int32_t index, float *array, int32_t count)
int binary_search(const T &target) const
void delete_data_pointers()
size_t unsigned_size() const
bool Serialize(FILE *fp, const char *data, size_t n)
int get_index(const T &object) const
PointerVector< T > & operator=(const PointerVector &other)
bool Serialize(TFile *fp) const
bool SerializeClasses(FILE *fp) const
bool SaveDataToFile(const GenericVector< char > &data, const STRING &filename)
void move(GenericVector< T > *from)
const char * string() const
T & operator[](int index) const
static T * double_the_size_memcpy(int current_size, T *data)
void set(const T &t, int index)
bool DeSerialize(bool swap, FILE *fp)
void swap(int index1, int index2)
PointerVector(const PointerVector &other)
void set_clear_callback(TessCallback1< T > *cb)
int push_back_new(const T &object)
void sort(int(*comparator)(const void *, const void *))
int push_front(const T &object)
bool DeSerialize(bool swap, FILE *fp)
bool DeSerializeClasses(bool swap, FILE *fp)
int sort_cmp(const void *t1, const void *t2)
static bool SkipDeSerialize(tesseract::TFile *fp)
int size_reserved() const
TessCallback1< T > * clear_cb_
bool read(tesseract::TFile *f, TessResultCallback2< bool, tesseract::TFile *, T *> *cb)
static bool SkipDeSerializeClasses(tesseract::TFile *fp)
void ReverseN(void *ptr, int num_bytes)
bool DeSerialize(TFile *fp)
GenericVector(const GenericVector &other)
bool DeSerialize(FILE *fp, char *data, size_t n)
bool DeSerializeElement(TFile *fp)
void insert(const T &t, int index)
static bool DeSerializeSize(TFile *fp, int32_t *size)
PointerVector< T > & operator+=(const PointerVector &other)
static const int kDefaultVectorSize
_ConstTessMemberResultCallback_5_0< false, R, T1, P1, P2, P3, P4, P5 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)(P1, P2, P3, P4, P5) const, typename Identity< P1 >::type p1, typename Identity< P2 >::type p2, typename Identity< P3 >::type p3, typename Identity< P4 >::type p4, typename Identity< P5 >::type p5)
T contains_index(int index) const
GenericVector(int size, const T &init_val)
void compact(TessResultCallback1< bool, int > *delete_cb)
bool Serialize(FILE *fp) const
bool cmp_eq(T const &t1, T const &t2)
TessResultCallback2< bool, T const &, T const & > * compare_cb_
void compact(TessResultCallback1< bool, const T *> *delete_cb)
bool Serialize(FILE *fp) const
int FRead(void *buffer, size_t size, int count)
bool contains(const T &object) const
int FWrite(const void *buffer, size_t size, int count)
void set_compare_callback(TessResultCallback2< bool, T const &, T const &> *cb)
GenericVectorEqEq(int size)
GenericVector< T > & operator=(const GenericVector &other)
static bool DeSerializeSkip(TFile *fp)