19 #ifndef TESSERACT_CCUTIL_GENERICVECTOR_H_ 20 #define TESSERACT_CCUTIL_GENERICVECTOR_H_ 75 static_assert(
sizeof(
size_used_) <=
sizeof(
size_t),
76 "Wow! sizeof(size_t) < sizeof(int32_t)!!");
94 T&
get(
int index)
const;
106 bool contains(
const T&
object)
const;
124 void set(
const T& t,
int index);
127 void insert(
const T& t,
int index);
131 void remove(
int index);
212 T* data_new =
new T[current_size * 2];
213 memcpy(data_new, data,
sizeof(T) * current_size);
236 void sort(
int (*comparator)(
const void*,
const void*)) {
248 return data_[index] == target;
259 while (top - bottom > 1) {
260 int middle = (bottom + top) / 2;
261 if (
data_[middle] > target) {
286 size_used_ = last_write + 1;
295 while (old_index < size_used_ && !delete_cb->Run(old_index++)) {
300 if (!delete_cb->
Run(old_index)) {
304 size_used_ = new_size;
309 T result =
static_cast<T
>(0);
322 if (target_index < 0) {
327 unsigned int seed = 1;
332 void swap(
int index1,
int index2) {
333 if (index1 != index2) {
334 T tmp =
data_[index1];
343 if (
data_[i] < rangemin || rangemax <
data_[i]) {
352 int choose_nth_item(
int target_index,
int start,
int end,
unsigned int* seed);
375 FILE* fp = fopen(filename,
"rb");
377 fseek(fp, 0, SEEK_END);
378 long size = ftell(fp);
379 fseek(fp, 0, SEEK_SET);
381 if (size > 0 && size < LONG_MAX) {
385 result =
static_cast<long>(fread(&(*data)[0], 1, size, fp)) == size;
401 FILE* fp = fopen(filename.
string(),
"wb");
406 static_cast<int>(fwrite(&data[0], 1, data.
size(), fp)) == data.
size();
418 lines_str.
split(
'\n', lines);
422 template <
typename T>
431 template <
typename T>
433 const T* a =
static_cast<const T*
>(t1);
434 const T* b =
static_cast<const T*
>(t2);
448 template <
typename T>
450 const T* a = *
static_cast<T* const*
>(t1);
451 const T* b = *
static_cast<T* const*
>(t2);
464 template <
typename T>
482 for (
int i = 0; i < other.
size(); ++i) {
489 if (&other !=
this) {
498 void remove(
int index) {
506 for (
int i = size; i < GenericVector<T*>::size_used_; ++i) {
523 for (; old_index < GenericVector<T*>::size_used_; ++old_index) {
551 if (fwrite(&used,
sizeof(used), 1, fp) != 1) {
554 for (
int i = 0; i < used; ++i) {
556 if (fwrite(&non_null,
sizeof(non_null), 1, fp) != 1) {
567 if (fp->
FWrite(&used,
sizeof(used), 1) != 1) {
570 for (
int i = 0; i < used; ++i) {
572 if (fp->
FWrite(&non_null,
sizeof(non_null), 1) != 1) {
590 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1) {
597 assert(reserved <= UINT16_MAX);
598 if (reserved > UINT16_MAX) {
603 for (uint32_t i = 0; i < reserved; ++i) {
605 if (fread(&non_null,
sizeof(non_null), 1, fp) != 1) {
611 if (!item->DeSerialize(swap, fp)) {
625 if (!DeSerializeSize(fp, &reserved)) {
630 for (
int i = 0; i < reserved; ++i) {
631 if (!DeSerializeElement(fp)) {
642 return fp->
FReadEndian(size,
sizeof(*size), 1) == 1;
647 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1) {
653 if (!item->DeSerialize(fp)) {
667 if (fp->
FRead(&non_null,
sizeof(non_null), 1) != 1) {
671 if (!T::SkipDeSerialize(fp)) {
688 template <
typename T>
701 template <
typename T>
718 template <
typename T>
725 template <
typename T>
733 T* new_array =
new T[
size];
735 new_array[i] =
data_[i];
742 template <
typename T>
752 template <
typename T>
756 for (
int i = 0; i <
size; ++i) {
762 template <
typename T>
768 template <
typename T>
774 template <
typename T>
780 template <
typename T>
787 template <
typename T>
796 template <
typename T>
811 template <
typename T>
814 for (
int i = index; i <
size_used_ - 1; ++i) {
821 template <
typename T>
827 template <
typename T>
839 template <
typename T>
845 template <
typename T>
852 data_[index] = object;
856 template <
typename T>
866 template <
typename T>
879 template <
typename T>
884 template <
typename T>
887 for (
int i = 0; i < other.
size(); ++i) {
893 template <
typename T>
895 if (&other !=
this) {
903 template <
typename T>
920 template <
typename T>
927 template <
typename T>
952 template <
typename T>
956 if (f->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
981 template <
typename T>
991 template <
typename T>
1006 template <
typename T>
1009 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1) {
1016 assert(reserved <= UINT16_MAX);
1017 if (reserved > UINT16_MAX) {
1032 template <
typename T>
1035 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
1039 const uint32_t limit = 50000000;
1040 assert(reserved <= limit);
1041 if (reserved > limit) {
1048 template <
typename T>
1051 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
1054 return (uint32_t)fp->
FRead(
nullptr,
sizeof(T), reserved) == reserved;
1060 template <
typename T>
1072 template <
typename T>
1090 template <
typename T>
1093 if (fread(&reserved,
sizeof(reserved), 1, fp) != 1) {
1101 for (
int i = 0; i < reserved; ++i) {
1108 template <
typename T>
1111 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
1116 for (
int i = 0; i < reserved; ++i) {
1123 template <
typename T>
1126 if (fp->
FReadEndian(&reserved,
sizeof(reserved), 1) != 1) {
1129 for (
int i = 0; i < reserved; ++i) {
1130 if (!T::SkipDeSerialize(fp)) {
1139 template <
typename T>
1147 from->
data_ =
nullptr;
1154 template <
typename T>
1156 sort(&tesseract::sort_cmp<T>);
1172 template <
typename T>
1174 unsigned int* seed) {
1176 int num_elements = end - start;
1178 if (num_elements <= 1) {
1181 if (num_elements == 2) {
1183 return target_index > start ? start + 1 : start;
1185 return target_index > start ? start : start + 1;
1188 #ifndef rand_r // _MSC_VER, ANDROID 1190 # define rand_r(seed) rand() 1192 int pivot =
rand_r(seed) % num_elements + start;
1198 int next_lesser = start;
1199 int prev_greater = end;
1200 for (
int next_sample = start + 1; next_sample < prev_greater;) {
1201 if (
data_[next_sample] <
data_[next_lesser]) {
1202 swap(next_lesser++, next_sample++);
1203 }
else if (
data_[next_sample] ==
data_[next_lesser]) {
1206 swap(--prev_greater, next_sample);
1211 if (target_index < next_lesser) {
1214 if (target_index < prev_greater) {
1220 #endif // TESSERACT_CCUTIL_GENERICVECTOR_H_ TessCallback1< T > * clear_cb_
bool DeSerialize(bool swap, FILE *fp)
void delete_data_pointers()
int FRead(void *buffer, size_t size, int count)
void swap(int index1, int index2)
bool LoadFileLinesToStrings(const STRING &filename, GenericVector< STRING > *lines)
static bool SkipDeSerialize(tesseract::TFile *fp)
void compact(TessResultCallback1< bool, const T * > *delete_cb)
TessResultCallback2< bool, T const &, T const & > * compare_cb_
bool DeSerialize(TFile *fp)
bool Serialize(FILE *fp) const
PointerVector< T > & operator+=(const PointerVector &other)
bool WithinBounds(const T &rangemin, const T &rangemax) const
void init_to_size(int size, const T &t)
void resize_no_init(int size)
bool contains(const T &object) const
int choose_nth_item(int target_index)
T contains_index(int index) const
void set(const T &t, int index)
void set_compare_callback(TessResultCallback2< bool, T const &, T const & > *cb)
bool write(FILE *f, TessResultCallback2< bool, FILE *, T const & > *cb) const
bool Serialize(TFile *fp) const
GenericVector(int size, const T &init_val)
int push_back_new(const T &object)
GenericVector< T > & operator=(const GenericVector &other)
void compact(TessResultCallback1< bool, int > *delete_cb)
bool SerializeClasses(FILE *fp) const
int sort_cmp(const void *t1, const void *t2)
GenericVector(const GenericVector &other)
bool read(tesseract::TFile *f, TessResultCallback2< bool, tesseract::TFile *, T * > *cb)
PointerVector< T > & operator=(const PointerVector &other)
T dot_product(const GenericVector< T > &other) const
bool DeSerializeClasses(bool swap, FILE *fp)
int get_index(const T &object) const
static bool SkipDeSerializeClasses(tesseract::TFile *fp)
int FWrite(const void *buffer, size_t size, int count)
static bool DeSerializeSize(TFile *fp, int32_t *size)
bool SaveDataToFile(const GenericVector< char > &data, const STRING &filename)
bool LoadDataFromFile(const STRING &filename, GenericVector< char > *data)
void set_clear_callback(TessCallback1< T > *cb)
const char * string() const
void insert(const T &t, int index)
bool DeSerialize(bool swap, FILE *fp)
bool bool_binary_search(const T &target) const
int sort_ptr_cmp(const void *t1, const void *t2)
size_t unsigned_size() const
T & operator[](int index) const
int size_reserved() const
static const int kDefaultVectorSize
bool DeSerializeElement(TFile *fp)
void move(GenericVector< T > *from)
_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)
int binary_search(const T &target) const
bool cmp_eq(T const &t1, T const &t2)
static T * double_the_size_memcpy(int current_size, T *data)
void split(char c, GenericVector< STRING > *splited)
void sort(int(*comparator)(const void *, const void *))
int push_front(const T &object)
PointerVector(const PointerVector &other)
GenericVectorEqEq(int size)
bool Serialize(FILE *fp) const
GenericVector< T > & operator+=(const GenericVector &other)
static bool DeSerializeSkip(TFile *fp)
void ReverseN(void *ptr, int num_bytes)
void Reverse32(void *ptr)
int FReadEndian(void *buffer, size_t size, int count)