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;
126 void set(
const T& t,
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]) {
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();
424 const T* a =
static_cast<const T*
>(t1);
425 const T* b =
static_cast<const T*
>(t2);
441 const T* a = *
static_cast<T* const*
>(t1);
442 const T* b = *
static_cast<T* const*
>(t2);
473 for (
int i = 0; i < other.
size(); ++i) {
480 if (&other !=
this) {
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)) {
699 if (size < kDefaultVectorSize) {
700 size = kDefaultVectorSize;
703 size_reserved_ = size;
706 compare_cb_ =
nullptr;
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;
735 if (size_reserved_ == 0) {
736 reserve(kDefaultVectorSize);
738 reserve(2 * size_reserved_);
747 for (
int i = 0; i < size; ++i) {
755 assert(index >= 0 && index < size_used_);
761 assert(index >= 0 && index < size_used_);
767 assert(size_used_ > 0);
768 return data_[size_used_ - 1];
773 assert(size_used_ > 0);
774 return data_[--size_used_];
780 assert(index >= 0 && index < size_used_);
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];
804 assert(index >= 0 && index < size_used_);
805 for (
int i = index; i < size_used_ - 1; ++i) {
806 data_[i] = data_[i + 1];
814 return index >= 0 && index < size_used_;
820 for (
int i = 0; i < size_used_; ++i) {
821 assert(compare_cb_ !=
nullptr);
822 if (compare_cb_->Run(
object, data_[i])) {
832 return get_index(
object) != -1;
839 if (size_used_ == size_reserved_) {
842 index = size_used_++;
843 data_[index] = object;
849 int index = get_index(
object);
853 return push_back(
object);
859 if (size_used_ == size_reserved_) {
862 for (
int i = size_used_; i > 0; --i) {
863 data_[i] = data_[i - 1];
878 for (
int i = 0; i < other.
size(); ++i) {
886 if (&other !=
this) {
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;
913 for (
int i = 0; i < size_used_; ++i) {
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()) {
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_) {
974 if (fwrite(&size_used_,
sizeof(size_used_), 1, fp) != 1) {
977 if (fwrite(data_,
sizeof(*data_), size_used_, fp) != unsigned_size()) {
984 if (fp->
FWrite(&size_used_,
sizeof(size_used_), 1) != 1) {
987 if (fp->
FWrite(data_,
sizeof(*data_), size_used_) != size_used_) {
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]));
1023template <
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_;
1039template <
typename T>
1042 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
1045 return (uint32_t)fp->
FRead(
nullptr,
sizeof(T), reserved) == reserved;
1051template <
typename T>
1053 if (fwrite(&size_used_,
sizeof(size_used_), 1, fp) != 1) {
1056 for (
int i = 0; i < size_used_; ++i) {
1057 if (!data_[i].Serialize(fp)) {
1063template <
typename T>
1065 if (fp->
FWrite(&size_used_,
sizeof(size_used_), 1) != 1) {
1068 for (
int i = 0; i < size_used_; ++i) {
1069 if (!data_[i].Serialize(fp)) {
1081template <
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) {
1093 if (!data_[i].DeSerialize(swap, fp)) {
1099template <
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) {
1108 if (!data_[i].DeSerialize(fp)) {
1114template <
typename T>
1117 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
1120 for (
int i = 0; i < reserved; ++i) {
1121 if (!T::SkipDeSerialize(fp)) {
1130template <
typename T>
1133 this->data_ = from->
data_;
1138 from->
data_ =
nullptr;
1145template <
typename T>
1147 sort(&tesseract::sort_cmp<T>);
1163template <
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;
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) {
ICOORD & operator+=(ICOORD &op1, const ICOORD &op2)
int32_t choose_nth_item(int32_t index, float *array, int32_t count)
void Reverse32(void *ptr)
void ReverseN(void *ptr, int num_bytes)
_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)
bool cmp_eq(T const &t1, T const &t2)
int sort_cmp(const void *t1, const void *t2)
bool SaveDataToFile(const GenericVector< char > &data, const STRING &filename)
int sort_ptr_cmp(const void *t1, const void *t2)
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
void init_to_size(int size, const T &t)
void compact(TessResultCallback1< bool, int > *delete_cb)
void resize_no_init(int size)
static bool SkipDeSerializeClasses(tesseract::TFile *fp)
static const int kDefaultVectorSize
int size_reserved() const
bool Serialize(FILE *fp) const
void set(const T &t, int index)
GenericVector(const GenericVector &other)
bool DeSerialize(tesseract::TFile *fp)
bool WithinBounds(const T &rangemin, const T &rangemax) const
bool Serialize(tesseract::TFile *fp) const
void sort(int(*comparator)(const void *, const void *))
size_t unsigned_size() const
int get_index(const T &object) const
bool contains(const T &object) const
bool read(tesseract::TFile *f, TessResultCallback2< bool, tesseract::TFile *, T * > *cb)
TessCallback1< T > * clear_cb_
void insert(const T &t, int index)
int choose_nth_item(int target_index)
void set_compare_callback(TessResultCallback2< bool, T const &, T const & > *cb)
bool DeSerializeClasses(bool swap, FILE *fp)
static T * double_the_size_memcpy(int current_size, T *data)
T dot_product(const GenericVector< T > &other) const
bool SerializeClasses(tesseract::TFile *fp) const
bool write(FILE *f, TessResultCallback2< bool, FILE *, T const & > *cb) const
bool bool_binary_search(const T &target) const
static bool SkipDeSerialize(tesseract::TFile *fp)
void delete_data_pointers()
int push_back_new(const T &object)
bool DeSerialize(bool swap, FILE *fp)
void move(GenericVector< T > *from)
void swap(int index1, int index2)
T contains_index(int index) const
bool SerializeClasses(FILE *fp) const
int binary_search(const T &target) const
int push_front(const T &object)
bool DeSerializeClasses(tesseract::TFile *fp)
int choose_nth_item(int target_index, int start, int end, unsigned int *seed)
void operator+=(const T &t)
TessResultCallback2< bool, T const &, T const & > * compare_cb_
GenericVector(int size, const T &init_val)
void set_clear_callback(TessCallback1< T > *cb)
GenericVector< T > & operator=(const GenericVector &other)
GenericVector< T > & operator+=(const GenericVector &other)
T & operator[](int index) const
GenericVectorEqEq(int size)
static bool DeSerializeSkip(TFile *fp)
bool Serialize(FILE *fp) const
void compact(TessResultCallback1< bool, const T * > *delete_cb)
bool DeSerializeElement(TFile *fp)
bool DeSerialize(bool swap, FILE *fp)
PointerVector(const PointerVector &other)
bool Serialize(TFile *fp) const
bool DeSerialize(TFile *fp)
PointerVector< T > & operator+=(const PointerVector &other)
PointerVector< T > & operator=(const PointerVector &other)
static bool DeSerializeSize(TFile *fp, int32_t *size)
int FWrite(const void *buffer, size_t size, int count)
int FReadEndian(void *buffer, size_t size, int count)
int FRead(void *buffer, size_t size, int count)
const char * string() const