tesseract  4.0.0-1-g2a2b
GenericVector< T > Class Template Reference

#include <baseapi.h>

Inheritance diagram for GenericVector< T >:
GenericVectorEqEq< T > tesseract::PointerVector< GenericVector< double > > tesseract::PointerVector< RecodeBeam > tesseract::PointerVector< tesseract::BaselineBlock > tesseract::PointerVector< tesseract::BaselineRow > tesseract::PointerVector< tesseract::DocumentData > tesseract::PointerVector< tesseract::ImageData > tesseract::PointerVector< tesseract::LanguageModelState > tesseract::PointerVector< tesseract::Network > tesseract::PointerVector< tesseract::NetworkIO > tesseract::PointerVector< tesseract::Shape > tesseract::PointerVector< tesseract::TrainingSample > tesseract::PointerVector< tesseract::TransposedArray > tesseract::PointerVector< WERD_RES >

Public Member Functions

 GenericVector ()
 
 GenericVector (int size, const T &init_val)
 
 GenericVector (const GenericVector &other)
 
GenericVector< T > & operator+= (const GenericVector &other)
 
GenericVector< T > & operator= (const GenericVector &other)
 
 ~GenericVector ()
 
void reserve (int size)
 
void double_the_size ()
 
void init_to_size (int size, const T &t)
 
void resize_no_init (int size)
 
int size () const
 
size_t unsigned_size () const
 
int size_reserved () const
 
int length () const
 
bool empty () const
 
T & get (int index) const
 
T & back () const
 
T & operator[] (int index) const
 
pop_back ()
 
int get_index (const T &object) const
 
bool contains (const T &object) const
 
contains_index (int index) const
 
int push_back (T object)
 
void operator+= (const T &t)
 
int push_back_new (const T &object)
 
int push_front (const T &object)
 
void set (const T &t, int index)
 
void insert (const T &t, int index)
 
void remove (int index)
 
void truncate (int size)
 
void set_clear_callback (TessCallback1< T > *cb)
 
void set_compare_callback (TessResultCallback2< bool, T const &, T const &> *cb)
 
void clear ()
 
void delete_data_pointers ()
 
void move (GenericVector< T > *from)
 
bool write (FILE *f, TessResultCallback2< bool, FILE *, T const &> *cb) const
 
bool read (tesseract::TFile *f, TessResultCallback2< bool, tesseract::TFile *, T *> *cb)
 
bool Serialize (FILE *fp) const
 
bool Serialize (tesseract::TFile *fp) const
 
bool DeSerialize (bool swap, FILE *fp)
 
bool DeSerialize (tesseract::TFile *fp)
 
bool SerializeClasses (FILE *fp) const
 
bool SerializeClasses (tesseract::TFile *fp) const
 
bool DeSerializeClasses (bool swap, FILE *fp)
 
bool DeSerializeClasses (tesseract::TFile *fp)
 
void reverse ()
 
void sort ()
 
void sort (int(*comparator)(const void *, const void *))
 
bool bool_binary_search (const T &target) const
 
int binary_search (const T &target) const
 
void compact_sorted ()
 
void compact (TessResultCallback1< bool, int > *delete_cb)
 
dot_product (const GenericVector< T > &other) const
 
int choose_nth_item (int target_index)
 
void swap (int index1, int index2)
 
bool WithinBounds (const T &rangemin, const T &rangemax) const
 

Static Public Member Functions

static bool SkipDeSerialize (tesseract::TFile *fp)
 
static bool SkipDeSerializeClasses (tesseract::TFile *fp)
 
static T * double_the_size_memcpy (int current_size, T *data)
 

Protected Member Functions

int choose_nth_item (int target_index, int start, int end, unsigned int *seed)
 
void init (int size)
 

Protected Attributes

int32_t size_used_
 
int32_t size_reserved_
 
T * data_
 
TessCallback1< T > * clear_cb_
 
TessResultCallback2< bool, T const &, T const & > * compare_cb_
 

Static Protected Attributes

static const int kDefaultVectorSize = 4
 

Detailed Description

template<typename T>
class GenericVector< T >

Definition at line 37 of file baseapi.h.

Constructor & Destructor Documentation

◆ GenericVector() [1/3]

template<typename T>
GenericVector< T >::GenericVector ( )
inline

Definition at line 39 of file genericvector.h.

39  {
41  }
static const int kDefaultVectorSize
void init(int size)

◆ GenericVector() [2/3]

template<typename T>
GenericVector< T >::GenericVector ( int  size,
const T &  init_val 
)
inline

Definition at line 42 of file genericvector.h.

42  {
43  init(size);
44  init_to_size(size, init_val);
45  }
int size() const
Definition: genericvector.h:71
void init_to_size(int size, const T &t)
void init(int size)

◆ GenericVector() [3/3]

template<typename T>
GenericVector< T >::GenericVector ( const GenericVector< T > &  other)
inline

Definition at line 48 of file genericvector.h.

48  {
49  this->init(other.size());
50  this->operator+=(other);
51  }
int size() const
Definition: genericvector.h:71
GenericVector< T > & operator+=(const GenericVector &other)
void init(int size)

◆ ~GenericVector()

template<typename T >
GenericVector< T >::~GenericVector ( )

Definition at line 677 of file genericvector.h.

677  {
678  clear();
679 }

Member Function Documentation

◆ back()

template<typename T >
T & GenericVector< T >::back ( ) const

Definition at line 730 of file genericvector.h.

730  {
731  assert(size_used_ > 0);
732  return data_[size_used_ - 1];
733 }
int32_t size_used_

◆ binary_search()

template<typename T>
int GenericVector< T >::binary_search ( const T &  target) const
inline

Definition at line 250 of file genericvector.h.

250  {
251  int bottom = 0;
252  int top = size_used_;
253  while (top - bottom > 1) {
254  int middle = (bottom + top) / 2;
255  if (data_[middle] > target)
256  top = middle;
257  else
258  bottom = middle;
259  }
260  return bottom;
261  }
int32_t size_used_

◆ bool_binary_search()

template<typename T>
bool GenericVector< T >::bool_binary_search ( const T &  target) const
inline

Definition at line 238 of file genericvector.h.

238  {
239  int index = binary_search(target);
240  if (index >= size_used_)
241  return false;
242  return data_[index] == target;
243  }
int32_t size_used_
int binary_search(const T &target) const

◆ choose_nth_item() [1/2]

template<typename T>
int GenericVector< T >::choose_nth_item ( int  target_index)
inline

Definition at line 309 of file genericvector.h.

309  {
310  // Make sure target_index is legal.
311  if (target_index < 0)
312  target_index = 0; // ensure legal
313  else if (target_index >= size_used_)
314  target_index = size_used_ - 1;
315  unsigned int seed = 1;
316  return choose_nth_item(target_index, 0, size_used_, &seed);
317  }
int32_t size_used_
int choose_nth_item(int target_index)

◆ choose_nth_item() [2/2]

template<typename T >
int GenericVector< T >::choose_nth_item ( int  target_index,
int  start,
int  end,
unsigned int *  seed 
)
protected

Definition at line 1083 of file genericvector.h.

1084  {
1085  // Number of elements to process.
1086  int num_elements = end - start;
1087  // Trivial cases.
1088  if (num_elements <= 1)
1089  return start;
1090  if (num_elements == 2) {
1091  if (data_[start] < data_[start + 1]) {
1092  return target_index > start ? start + 1 : start;
1093  } else {
1094  return target_index > start ? start : start + 1;
1095  }
1096  }
1097  // Place the pivot at start.
1098  #ifndef rand_r // _MSC_VER, ANDROID
1099  srand(*seed);
1100  #define rand_r(seed) rand()
1101  #endif // _MSC_VER
1102  int pivot = rand_r(seed) % num_elements + start;
1103  swap(pivot, start);
1104  // The invariant condition here is that items [start, next_lesser) are less
1105  // than the pivot (which is at index next_lesser) and items
1106  // [prev_greater, end) are greater than the pivot, with items
1107  // [next_lesser, prev_greater) being equal to the pivot.
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]) {
1114  ++next_sample;
1115  } else {
1116  swap(--prev_greater, next_sample);
1117  }
1118  }
1119  // Now the invariant is set up, we recurse on just the section that contains
1120  // the desired index.
1121  if (target_index < next_lesser)
1122  return choose_nth_item(target_index, start, next_lesser, seed);
1123  else if (target_index < prev_greater)
1124  return next_lesser; // In equal bracket.
1125  else
1126  return choose_nth_item(target_index, prev_greater, end, seed);
1127 }
#define rand_r(seed)
int choose_nth_item(int target_index)
void swap(int index1, int index2)

◆ clear()

template<typename T >
void GenericVector< T >::clear ( )

Definition at line 868 of file genericvector.h.

868  {
869  if (size_reserved_ > 0 && clear_cb_ != nullptr) {
870  for (int i = 0; i < size_used_; ++i)
871  clear_cb_->Run(data_[i]);
872  }
873  delete[] data_;
874  data_ = nullptr;
875  size_used_ = 0;
876  size_reserved_ = 0;
877  delete clear_cb_;
878  clear_cb_ = nullptr;
879  delete compare_cb_;
880  compare_cb_ = nullptr;
881 }
int32_t size_used_
int32_t size_reserved_
virtual void Run(A1)=0
TessResultCallback2< bool, T const &, T const & > * compare_cb_
TessCallback1< T > * clear_cb_

◆ compact()

template<typename T>
void GenericVector< T >::compact ( TessResultCallback1< bool, int > *  delete_cb)
inline

Definition at line 282 of file genericvector.h.

282  {
283  int new_size = 0;
284  int old_index = 0;
285  // Until the callback returns true, the elements stay the same.
286  while (old_index < size_used_ && !delete_cb->Run(old_index++))
287  ++new_size;
288  // Now just copy anything else that gets false from delete_cb.
289  for (; old_index < size_used_; ++old_index) {
290  if (!delete_cb->Run(old_index)) {
291  data_[new_size++] = data_[old_index];
292  }
293  }
294  size_used_ = new_size;
295  delete delete_cb;
296  }
int32_t size_used_
virtual R Run(A1)=0

◆ compact_sorted()

template<typename T>
void GenericVector< T >::compact_sorted ( )
inline

Definition at line 265 of file genericvector.h.

265  {
266  if (size_used_ == 0)
267  return;
268 
269  // First element is in no matter what, hence the i = 1.
270  int last_write = 0;
271  for (int i = 1; i < size_used_; ++i) {
272  // Finds next unique item and writes it.
273  if (data_[last_write] != data_[i])
274  data_[++last_write] = data_[i];
275  }
276  // last_write is the index of a valid data cell, so add 1.
277  size_used_ = last_write + 1;
278  }
int32_t size_used_

◆ contains()

template<typename T>
bool GenericVector< T >::contains ( const T &  object) const

Definition at line 793 of file genericvector.h.

793  {
794  return get_index(object) != -1;
795 }
int get_index(const T &object) const

◆ contains_index()

template<typename T >
T GenericVector< T >::contains_index ( int  index) const

Definition at line 776 of file genericvector.h.

776  {
777  return index >= 0 && index < size_used_;
778 }
int32_t size_used_

◆ delete_data_pointers()

template<typename T >
void GenericVector< T >::delete_data_pointers ( )

Definition at line 884 of file genericvector.h.

884  {
885  for (int i = 0; i < size_used_; ++i) {
886  delete data_[i];
887  }
888 }
int32_t size_used_

◆ DeSerialize() [1/2]

template<typename T >
bool GenericVector< T >::DeSerialize ( bool  swap,
FILE *  fp 
)

Definition at line 954 of file genericvector.h.

954  {
955  uint32_t reserved;
956  if (fread(&reserved, sizeof(reserved), 1, fp) != 1) return false;
957  if (swap) Reverse32(&reserved);
958  // Arbitrarily limit the number of elements to protect against bad data.
959  assert(reserved <= UINT16_MAX);
960  if (reserved > UINT16_MAX) return false;
961  reserve(reserved);
962  size_used_ = reserved;
963  if (fread(data_, sizeof(T), size_used_, fp) != unsigned_size()) return false;
964  if (swap) {
965  for (int i = 0; i < size_used_; ++i)
966  ReverseN(&data_[i], sizeof(data_[i]));
967  }
968  return true;
969 }
int32_t size_used_
void reserve(int size)
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:178
size_t unsigned_size() const
Definition: genericvector.h:75
void Reverse32(void *ptr)
Definition: helpers.h:195
void swap(int index1, int index2)

◆ DeSerialize() [2/2]

template<typename T >
bool GenericVector< T >::DeSerialize ( tesseract::TFile fp)

Definition at line 971 of file genericvector.h.

971  {
972  uint32_t reserved;
973  if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false;
974  // Arbitrarily limit the number of elements to protect against bad data.
975  const uint32_t limit = 50000000;
976  assert(reserved <= limit);
977  if (reserved > limit) return false;
978  reserve(reserved);
979  size_used_ = reserved;
980  return fp->FReadEndian(data_, sizeof(T), size_used_) == size_used_;
981 }
int32_t size_used_
void reserve(int size)
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:259

◆ DeSerializeClasses() [1/2]

template<typename T >
bool GenericVector< T >::DeSerializeClasses ( bool  swap,
FILE *  fp 
)

Definition at line 1015 of file genericvector.h.

1015  {
1016  int32_t reserved;
1017  if (fread(&reserved, sizeof(reserved), 1, fp) != 1) return false;
1018  if (swap) Reverse32(&reserved);
1019  T empty;
1020  init_to_size(reserved, empty);
1021  for (int i = 0; i < reserved; ++i) {
1022  if (!data_[i].DeSerialize(swap, fp)) return false;
1023  }
1024  return true;
1025 }
bool DeSerialize(bool swap, FILE *fp)
void init_to_size(int size, const T &t)
bool empty() const
Definition: genericvector.h:90
void Reverse32(void *ptr)
Definition: helpers.h:195
void swap(int index1, int index2)

◆ DeSerializeClasses() [2/2]

template<typename T >
bool GenericVector< T >::DeSerializeClasses ( tesseract::TFile fp)

Definition at line 1027 of file genericvector.h.

1027  {
1028  int32_t reserved;
1029  if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false;
1030  T empty;
1031  init_to_size(reserved, empty);
1032  for (int i = 0; i < reserved; ++i) {
1033  if (!data_[i].DeSerialize(fp)) return false;
1034  }
1035  return true;
1036 }
bool DeSerialize(bool swap, FILE *fp)
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:259
void init_to_size(int size, const T &t)
bool empty() const
Definition: genericvector.h:90

◆ dot_product()

template<typename T>
T GenericVector< T >::dot_product ( const GenericVector< T > &  other) const
inline

Definition at line 298 of file genericvector.h.

298  {
299  T result = static_cast<T>(0);
300  for (int i = std::min(size_used_, other.size_used_) - 1; i >= 0; --i)
301  result += data_[i] * other.data_[i];
302  return result;
303  }
int32_t size_used_

◆ double_the_size()

template<typename T >
void GenericVector< T >::double_the_size ( )

Definition at line 697 of file genericvector.h.

697  {
698  if (size_reserved_ == 0) {
700  }
701  else {
702  reserve(2 * size_reserved_);
703  }
704 }
static const int kDefaultVectorSize
void reserve(int size)
int32_t size_reserved_

◆ double_the_size_memcpy()

template<typename T>
static T* GenericVector< T >::double_the_size_memcpy ( int  current_size,
T *  data 
)
inlinestatic

Definition at line 207 of file genericvector.h.

207  {
208  T *data_new = new T[current_size * 2];
209  memcpy(data_new, data, sizeof(T) * current_size);
210  delete[] data;
211  return data_new;
212  }

◆ empty()

template<typename T>
bool GenericVector< T >::empty ( ) const
inline

Definition at line 90 of file genericvector.h.

90  {
91  return size_used_ == 0;
92  }
int32_t size_used_

◆ get()

template<typename T >
T & GenericVector< T >::get ( int  index) const

Definition at line 718 of file genericvector.h.

718  {
719  assert(index >= 0 && index < size_used_);
720  return data_[index];
721 }
int32_t size_used_

◆ get_index()

template<typename T>
int GenericVector< T >::get_index ( const T &  object) const

Definition at line 782 of file genericvector.h.

782  {
783  for (int i = 0; i < size_used_; ++i) {
784  assert(compare_cb_ != nullptr);
785  if (compare_cb_->Run(object, data_[i]))
786  return i;
787  }
788  return -1;
789 }
int32_t size_used_
virtual R Run(A1, A2)=0
TessResultCallback2< bool, T const &, T const & > * compare_cb_

◆ init()

template<typename T >
void GenericVector< T >::init ( int  size)
protected

Definition at line 662 of file genericvector.h.

662  {
663  size_used_ = 0;
664  if (size <= 0) {
665  data_ = nullptr;
666  size_reserved_ = 0;
667  } else {
669  data_ = new T[size];
671  }
672  clear_cb_ = nullptr;
673  compare_cb_ = nullptr;
674 }
int size() const
Definition: genericvector.h:71
int32_t size_used_
static const int kDefaultVectorSize
int32_t size_reserved_
TessResultCallback2< bool, T const &, T const & > * compare_cb_
TessCallback1< T > * clear_cb_

◆ init_to_size()

template<typename T>
void GenericVector< T >::init_to_size ( int  size,
const T &  t 
)

Definition at line 708 of file genericvector.h.

708  {
709  reserve(size);
710  size_used_ = size;
711  for (int i = 0; i < size; ++i)
712  data_[i] = t;
713 }
int size() const
Definition: genericvector.h:71
int32_t size_used_
void reserve(int size)

◆ insert()

template<typename T>
void GenericVector< T >::insert ( const T &  t,
int  index 
)

Definition at line 752 of file genericvector.h.

752  {
753  assert(index >= 0 && index <= size_used_);
754  if (size_reserved_ == size_used_)
755  double_the_size();
756  for (int i = size_used_; i > index; --i) {
757  data_[i] = data_[i-1];
758  }
759  data_[index] = t;
760  size_used_++;
761 }
int32_t size_used_
int32_t size_reserved_
void double_the_size()

◆ length()

template<typename T>
int GenericVector< T >::length ( ) const
inline

Definition at line 85 of file genericvector.h.

85  {
86  return size_used_;
87  }
int32_t size_used_

◆ move()

template<typename T>
void GenericVector< T >::move ( GenericVector< T > *  from)

Definition at line 1050 of file genericvector.h.

1050  {
1051  this->clear();
1052  this->data_ = from->data_;
1053  this->size_reserved_ = from->size_reserved_;
1054  this->size_used_ = from->size_used_;
1055  this->compare_cb_ = from->compare_cb_;
1056  this->clear_cb_ = from->clear_cb_;
1057  from->data_ = nullptr;
1058  from->clear_cb_ = nullptr;
1059  from->compare_cb_ = nullptr;
1060  from->size_used_ = 0;
1061  from->size_reserved_ = 0;
1062 }
int32_t size_used_
int32_t size_reserved_
TessResultCallback2< bool, T const &, T const & > * compare_cb_
TessCallback1< T > * clear_cb_

◆ operator+=() [1/2]

template<typename T >
GenericVector< T > & GenericVector< T >::operator+= ( const GenericVector< T > &  other)

Definition at line 834 of file genericvector.h.

834  {
835  this->reserve(size_used_ + other.size_used_);
836  for (int i = 0; i < other.size(); ++i) {
837  this->operator+=(other.data_[i]);
838  }
839  return *this;
840 }
int size() const
Definition: genericvector.h:71
GenericVector< T > & operator+=(const GenericVector &other)
int32_t size_used_
void reserve(int size)

◆ operator+=() [2/2]

template<typename T>
void GenericVector< T >::operator+= ( const T &  t)

Definition at line 829 of file genericvector.h.

829  {
830  push_back(t);
831 }
int push_back(T object)

◆ operator=()

template<typename T >
GenericVector< T > & GenericVector< T >::operator= ( const GenericVector< T > &  other)

Definition at line 843 of file genericvector.h.

843  {
844  if (&other != this) {
845  this->truncate(0);
846  this->operator+=(other);
847  }
848  return *this;
849 }
GenericVector< T > & operator+=(const GenericVector &other)
void truncate(int size)

◆ operator[]()

template<typename T >
T & GenericVector< T >::operator[] ( int  index) const

Definition at line 724 of file genericvector.h.

724  {
725  assert(index >= 0 && index < size_used_);
726  return data_[index];
727 }
int32_t size_used_

◆ pop_back()

template<typename T >
T GenericVector< T >::pop_back ( )

Definition at line 736 of file genericvector.h.

736  {
737  assert(size_used_ > 0);
738  return data_[--size_used_];
739 }
int32_t size_used_

◆ push_back()

template<typename T>
int GenericVector< T >::push_back ( object)

Definition at line 799 of file genericvector.h.

799  {
800  int index = 0;
801  if (size_used_ == size_reserved_)
802  double_the_size();
803  index = size_used_++;
804  data_[index] = object;
805  return index;
806 }
int32_t size_used_
int32_t size_reserved_
void double_the_size()

◆ push_back_new()

template<typename T>
int GenericVector< T >::push_back_new ( const T &  object)

Definition at line 809 of file genericvector.h.

809  {
810  int index = get_index(object);
811  if (index >= 0)
812  return index;
813  return push_back(object);
814 }
int get_index(const T &object) const
int push_back(T object)

◆ push_front()

template<typename T>
int GenericVector< T >::push_front ( const T &  object)

Definition at line 818 of file genericvector.h.

818  {
819  if (size_used_ == size_reserved_)
820  double_the_size();
821  for (int i = size_used_; i > 0; --i)
822  data_[i] = data_[i-1];
823  data_[0] = object;
824  ++size_used_;
825  return 0;
826 }
int32_t size_used_
int32_t size_reserved_
void double_the_size()

◆ read()

template<typename T>
bool GenericVector< T >::read ( tesseract::TFile f,
TessResultCallback2< bool, tesseract::TFile *, T *> *  cb 
)

Definition at line 912 of file genericvector.h.

913  {
914  int32_t reserved;
915  if (f->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false;
916  reserve(reserved);
917  if (f->FReadEndian(&size_used_, sizeof(size_used_), 1) != 1) return false;
918  if (cb != nullptr) {
919  for (int i = 0; i < size_used_; ++i) {
920  if (!cb->Run(f, data_ + i)) {
921  delete cb;
922  return false;
923  }
924  }
925  delete cb;
926  } else {
927  if (f->FReadEndian(data_, sizeof(T), size_used_) != size_used_)
928  return false;
929  }
930  return true;
931 }
int32_t size_used_
void reserve(int size)
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:259
virtual R Run(A1, A2)=0

◆ remove()

template<typename T >
void GenericVector< T >::remove ( int  index)

Definition at line 766 of file genericvector.h.

766  {
767  assert(index >= 0 && index < size_used_);
768  for (int i = index; i < size_used_ - 1; ++i) {
769  data_[i] = data_[i+1];
770  }
771  size_used_--;
772 }
int32_t size_used_

◆ reserve()

template<typename T >
void GenericVector< T >::reserve ( int  size)

Definition at line 684 of file genericvector.h.

684  {
685  if (size_reserved_ >= size || size <= 0)
686  return;
688  T* new_array = new T[size];
689  for (int i = 0; i < size_used_; ++i)
690  new_array[i] = data_[i];
691  delete[] data_;
692  data_ = new_array;
694 }
int size() const
Definition: genericvector.h:71
int32_t size_used_
static const int kDefaultVectorSize
int32_t size_reserved_

◆ resize_no_init()

template<typename T>
void GenericVector< T >::resize_no_init ( int  size)
inline

Definition at line 65 of file genericvector.h.

65  {
66  reserve(size);
67  size_used_ = size;
68  }
int size() const
Definition: genericvector.h:71
int32_t size_used_
void reserve(int size)

◆ reverse()

template<typename T>
void GenericVector< T >::reverse ( )
inline

Definition at line 215 of file genericvector.h.

215  {
216  for (int i = 0; i < size_used_ / 2; ++i)
217  Swap(&data_[i], &data_[size_used_ - 1 - i]);
218  }
void Swap(T *p1, T *p2)
Definition: helpers.h:98
int32_t size_used_

◆ Serialize() [1/2]

template<typename T >
bool GenericVector< T >::Serialize ( FILE *  fp) const

Definition at line 936 of file genericvector.h.

936  {
937  if (fwrite(&size_used_, sizeof(size_used_), 1, fp) != 1) return false;
938  if (fwrite(data_, sizeof(*data_), size_used_, fp) != unsigned_size())
939  return false;
940  return true;
941 }
int32_t size_used_
size_t unsigned_size() const
Definition: genericvector.h:75

◆ Serialize() [2/2]

template<typename T >
bool GenericVector< T >::Serialize ( tesseract::TFile fp) const

Definition at line 943 of file genericvector.h.

943  {
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;
946  return true;
947 }
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:318
int32_t size_used_

◆ SerializeClasses() [1/2]

template<typename T >
bool GenericVector< T >::SerializeClasses ( FILE *  fp) const

Definition at line 993 of file genericvector.h.

993  {
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;
997  }
998  return true;
999 }
int32_t size_used_
bool Serialize(FILE *fp) const

◆ SerializeClasses() [2/2]

template<typename T >
bool GenericVector< T >::SerializeClasses ( tesseract::TFile fp) const

Definition at line 1001 of file genericvector.h.

1001  {
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;
1005  }
1006  return true;
1007 }
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:318
int32_t size_used_
bool Serialize(FILE *fp) const

◆ set()

template<typename T>
void GenericVector< T >::set ( const T &  t,
int  index 
)

Definition at line 743 of file genericvector.h.

743  {
744  assert(index >= 0 && index < size_used_);
745  data_[index] = t;
746 }
int32_t size_used_

◆ set_clear_callback()

template<typename T>
void GenericVector< T >::set_clear_callback ( TessCallback1< T > *  cb)

Definition at line 854 of file genericvector.h.

854  {
855  clear_cb_ = cb;
856 }
TessCallback1< T > * clear_cb_

◆ set_compare_callback()

template<typename T>
void GenericVector< T >::set_compare_callback ( TessResultCallback2< bool, T const &, T const &> *  cb)

Definition at line 861 of file genericvector.h.

862  {
863  compare_cb_ = cb;
864 }
TessResultCallback2< bool, T const &, T const & > * compare_cb_

◆ size()

template<typename T>
int GenericVector< T >::size ( ) const
inline

Definition at line 71 of file genericvector.h.

71  {
72  return size_used_;
73  }
int32_t size_used_

◆ size_reserved()

template<typename T>
int GenericVector< T >::size_reserved ( ) const
inline

Definition at line 81 of file genericvector.h.

81  {
82  return size_reserved_;
83  }
int32_t size_reserved_

◆ SkipDeSerialize()

template<typename T >
bool GenericVector< T >::SkipDeSerialize ( tesseract::TFile fp)
static

Definition at line 983 of file genericvector.h.

983  {
984  uint32_t reserved;
985  if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false;
986  return fp->FRead(nullptr, sizeof(T), reserved) == reserved;
987 }
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:270
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:259

◆ SkipDeSerializeClasses()

template<typename T >
bool GenericVector< T >::SkipDeSerializeClasses ( tesseract::TFile fp)
static

Definition at line 1038 of file genericvector.h.

1038  {
1039  int32_t reserved;
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;
1043  }
1044  return true;
1045 }
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:259

◆ sort() [1/2]

template<typename T >
void GenericVector< T >::sort ( )

Definition at line 1065 of file genericvector.h.

1065  {
1066  sort(&tesseract::sort_cmp<T>);
1067 }

◆ sort() [2/2]

template<typename T>
void GenericVector< T >::sort ( int(*)(const void *, const void *)  comparator)
inline

Definition at line 231 of file genericvector.h.

231  {
232  qsort(data_, size_used_, sizeof(*data_), comparator);
233  }
int32_t size_used_

◆ swap()

template<typename T>
void GenericVector< T >::swap ( int  index1,
int  index2 
)
inline

Definition at line 320 of file genericvector.h.

320  {
321  if (index1 != index2) {
322  T tmp = data_[index1];
323  data_[index1] = data_[index2];
324  data_[index2] = tmp;
325  }
326  }

◆ truncate()

template<typename T>
void GenericVector< T >::truncate ( int  size)
inline

Definition at line 136 of file genericvector.h.

136  {
137  if (size < size_used_)
138  size_used_ = size;
139  }
int size() const
Definition: genericvector.h:71
int32_t size_used_

◆ unsigned_size()

template<typename T>
size_t GenericVector< T >::unsigned_size ( ) const
inline

Definition at line 75 of file genericvector.h.

75  {
76  static_assert(sizeof(size_used_) <= sizeof(size_t),
77  "Wow! sizeof(size_t) < sizeof(int32_t)!!");
78  assert(0 <= size_used_);
79  return static_cast<size_t>(size_used_);
80  }
int32_t size_used_

◆ WithinBounds()

template<typename T>
bool GenericVector< T >::WithinBounds ( const T &  rangemin,
const T &  rangemax 
) const
inline

Definition at line 329 of file genericvector.h.

329  {
330  for (int i = 0; i < size_used_; ++i) {
331  if (data_[i] < rangemin || rangemax < data_[i])
332  return false;
333  }
334  return true;
335  }
int32_t size_used_

◆ write()

template<typename T>
bool GenericVector< T >::write ( FILE *  f,
TessResultCallback2< bool, FILE *, T const &> *  cb 
) const

Definition at line 892 of file genericvector.h.

893  {
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;
896  if (cb != nullptr) {
897  for (int i = 0; i < size_used_; ++i) {
898  if (!cb->Run(f, data_[i])) {
899  delete cb;
900  return false;
901  }
902  }
903  delete cb;
904  } else {
905  if (fwrite(data_, sizeof(T), size_used_, f) != unsigned_size())
906  return false;
907  }
908  return true;
909 }
int32_t size_used_
int32_t size_reserved_
virtual R Run(A1, A2)=0
size_t unsigned_size() const
Definition: genericvector.h:75

Member Data Documentation

◆ clear_cb_

template<typename T>
TessCallback1<T>* GenericVector< T >::clear_cb_
protected

Definition at line 351 of file genericvector.h.

◆ compare_cb_

template<typename T>
TessResultCallback2<bool, T const &, T const &>* GenericVector< T >::compare_cb_
mutableprotected

Definition at line 353 of file genericvector.h.

◆ data_

template<typename T>
T* GenericVector< T >::data_
protected

Definition at line 350 of file genericvector.h.

◆ kDefaultVectorSize

template<typename T>
const int GenericVector< T >::kDefaultVectorSize = 4
staticprotected

Definition at line 347 of file genericvector.h.

◆ size_reserved_

template<typename T>
int32_t GenericVector< T >::size_reserved_
protected

Definition at line 349 of file genericvector.h.

◆ size_used_

template<typename T>
int32_t GenericVector< T >::size_used_
protected

Definition at line 348 of file genericvector.h.


The documentation for this class was generated from the following files: