tesseract  5.0.0-alpha-619-ge9db
GenericVector< T > Class Template Reference

#include <baseapi.h>

Inheritance diagram for GenericVector< T >:
GenericVectorEqEq< T > GenericVectorEqEq< tesseract::LineHypothesis > 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
 
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 (std::function< void(T)> cb)
 
void set_compare_callback (std::function< bool(const T &, const T &)> cb)
 
void clear ()
 
void delete_data_pointers ()
 
void move (GenericVector< T > *from)
 
bool write (FILE *f, std::function< bool(FILE *, const T &)> cb) const
 
bool read (tesseract::TFile *f, std::function< 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 ()
 
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_
 
std::function< void(T)> clear_cb_
 
std::function< bool(const T &, const T &)> compare_cb_
 

Static Protected Attributes

static const int kDefaultVectorSize = 4
 

Detailed Description

template<typename T>
class GenericVector< T >

Definition at line 40 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  }

◆ 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  }

◆ 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  }

◆ ~GenericVector()

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

Definition at line 672 of file genericvector.h.

672  {
673  clear();
674 }

Member Function Documentation

◆ back()

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

Definition at line 728 of file genericvector.h.

728  {
729  assert(size_used_ > 0);
730  return data_[size_used_ - 1];
731 }

◆ binary_search()

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

Definition at line 252 of file genericvector.h.

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

◆ bool_binary_search()

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

Definition at line 239 of file genericvector.h.

239  {
240  int index = binary_search(target);
241  if (index >= size_used_) {
242  return false;
243  }
244  return data_[index] == target;
245  }

◆ choose_nth_item() [1/2]

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

Definition at line 289 of file genericvector.h.

289  {
290  // Make sure target_index is legal.
291  if (target_index < 0) {
292  target_index = 0; // ensure legal
293  } else if (target_index >= size_used_) {
294  target_index = size_used_ - 1;
295  }
296  unsigned int seed = 1;
297  return choose_nth_item(target_index, 0, size_used_, &seed);
298  }

◆ 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 1120 of file genericvector.h.

1121  {
1122  // Number of elements to process.
1123  int num_elements = end - start;
1124  // Trivial cases.
1125  if (num_elements <= 1) {
1126  return start;
1127  }
1128  if (num_elements == 2) {
1129  if (data_[start] < data_[start + 1]) {
1130  return target_index > start ? start + 1 : start;
1131  }
1132  return target_index > start ? start : start + 1;
1133  }
1134 // Place the pivot at start.
1135 #ifndef rand_r // _MSC_VER, ANDROID
1136  srand(*seed);
1137 # define rand_r(seed) rand()
1138 #endif // _MSC_VER
1139  int pivot = rand_r(seed) % num_elements + start;
1140  swap(pivot, start);
1141  // The invariant condition here is that items [start, next_lesser) are less
1142  // than the pivot (which is at index next_lesser) and items
1143  // [prev_greater, end) are greater than the pivot, with items
1144  // [next_lesser, prev_greater) being equal to the pivot.
1145  int next_lesser = start;
1146  int prev_greater = end;
1147  for (int next_sample = start + 1; next_sample < prev_greater;) {
1148  if (data_[next_sample] < data_[next_lesser]) {
1149  swap(next_lesser++, next_sample++);
1150  } else if (data_[next_sample] == data_[next_lesser]) {
1151  ++next_sample;
1152  } else {
1153  swap(--prev_greater, next_sample);
1154  }
1155  }
1156  // Now the invariant is set up, we recurse on just the section that contains
1157  // the desired index.
1158  if (target_index < next_lesser) {
1159  return choose_nth_item(target_index, start, next_lesser, seed);
1160  }
1161  if (target_index < prev_greater) {
1162  return next_lesser; // In equal bracket.
1163  }
1164  return choose_nth_item(target_index, prev_greater, end, seed);
1165 }

◆ clear()

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

Definition at line 857 of file genericvector.h.

857  {
858  if (size_reserved_ > 0 && clear_cb_ != nullptr) {
859  for (int i = 0; i < size_used_; ++i) {
860  clear_cb_(data_[i]);
861  }
862  }
863  delete[] data_;
864  data_ = nullptr;
865  size_used_ = 0;
866  size_reserved_ = 0;
867  clear_cb_ = nullptr;
868  compare_cb_ = nullptr;
869 }

◆ compact_sorted()

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

Definition at line 268 of file genericvector.h.

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

◆ 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 }

◆ contains_index()

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

Definition at line 775 of file genericvector.h.

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

◆ delete_data_pointers()

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

Definition at line 872 of file genericvector.h.

872  {
873  for (int i = 0; i < size_used_; ++i) {
874  delete data_[i];
875  }
876 }

◆ 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) {
957  return false;
958  }
959  if (swap) {
960  Reverse32(&reserved);
961  }
962  // Arbitrarily limit the number of elements to protect against bad data.
963  assert(reserved <= UINT16_MAX);
964  if (reserved > UINT16_MAX) {
965  return false;
966  }
967  reserve(reserved);
968  size_used_ = reserved;
969  if (fread(data_, sizeof(T), size_used_, fp) != unsigned_size()) {
970  return false;
971  }
972  if (swap) {
973  for (int i = 0; i < size_used_; ++i) {
974  ReverseN(&data_[i], sizeof(data_[i]));
975  }
976  }
977  return true;
978 }

◆ DeSerialize() [2/2]

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

Definition at line 980 of file genericvector.h.

980  {
981  uint32_t reserved;
982  if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) {
983  return false;
984  }
985  // Arbitrarily limit the number of elements to protect against bad data.
986  const uint32_t limit = 50000000;
987  assert(reserved <= limit);
988  if (reserved > limit) {
989  return false;
990  }
991  reserve(reserved);
992  size_used_ = reserved;
993  return fp->FReadEndian(data_, sizeof(T), size_used_) == size_used_;
994 }

◆ DeSerializeClasses() [1/2]

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

Definition at line 1038 of file genericvector.h.

1038  {
1039  int32_t reserved;
1040  if (fread(&reserved, sizeof(reserved), 1, fp) != 1) {
1041  return false;
1042  }
1043  if (swap) {
1044  Reverse32(&reserved);
1045  }
1046  T empty;
1047  init_to_size(reserved, empty);
1048  for (int i = 0; i < reserved; ++i) {
1049  if (!data_[i].DeSerialize(swap, fp)) {
1050  return false;
1051  }
1052  }
1053  return true;
1054 }

◆ DeSerializeClasses() [2/2]

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

Definition at line 1056 of file genericvector.h.

1056  {
1057  int32_t reserved;
1058  if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) {
1059  return false;
1060  }
1061  T empty;
1062  init_to_size(reserved, empty);
1063  for (int i = 0; i < reserved; ++i) {
1064  if (!data_[i].DeSerialize(fp)) {
1065  return false;
1066  }
1067  }
1068  return true;
1069 }

◆ double_the_size()

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

Definition at line 696 of file genericvector.h.

696  {
697  if (size_reserved_ == 0) {
699  } else {
700  reserve(2 * size_reserved_);
701  }
702 }

◆ 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 86 of file genericvector.h.

86  {
87  return size_used_ == 0;
88  }

◆ get()

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

Definition at line 716 of file genericvector.h.

716  {
717  assert(index >= 0 && index < size_used_);
718  return data_[index];
719 }

◆ get_index()

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

Definition at line 781 of file genericvector.h.

781  {
782  for (int i = 0; i < size_used_; ++i) {
783  assert(compare_cb_ != nullptr);
784  if (compare_cb_(object, data_[i])) {
785  return i;
786  }
787  }
788  return -1;
789 }

◆ init()

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

Definition at line 655 of file genericvector.h.

655  {
656  size_used_ = 0;
657  if (size <= 0) {
658  data_ = nullptr;
659  size_reserved_ = 0;
660  } else {
661  if (size < kDefaultVectorSize) {
663  }
664  data_ = new T[size];
666  }
667  clear_cb_ = nullptr;
668  compare_cb_ = nullptr;
669 }

◆ init_to_size()

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

Definition at line 706 of file genericvector.h.

706  {
707  reserve(size);
708  size_used_ = size;
709  for (int i = 0; i < size; ++i) {
710  data_[i] = t;
711  }
712 }

◆ insert()

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

Definition at line 750 of file genericvector.h.

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

◆ move()

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

Definition at line 1087 of file genericvector.h.

1087  {
1088  this->clear();
1089  this->data_ = from->data_;
1090  this->size_reserved_ = from->size_reserved_;
1091  this->size_used_ = from->size_used_;
1092  this->compare_cb_ = from->compare_cb_;
1093  this->clear_cb_ = from->clear_cb_;
1094  from->data_ = nullptr;
1095  from->clear_cb_ = nullptr;
1096  from->compare_cb_ = nullptr;
1097  from->size_used_ = 0;
1098  from->size_reserved_ = 0;
1099 }

◆ operator+=() [1/2]

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

Definition at line 838 of file genericvector.h.

838  {
839  this->reserve(size_used_ + other.size_used_);
840  for (int i = 0; i < other.size(); ++i) {
841  this->operator+=(other.data_[i]);
842  }
843  return *this;
844 }

◆ operator+=() [2/2]

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

Definition at line 833 of file genericvector.h.

833  {
834  push_back(t);
835 }

◆ operator=()

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

Definition at line 847 of file genericvector.h.

847  {
848  if (&other != this) {
849  this->truncate(0);
850  this->operator+=(other);
851  }
852  return *this;
853 }

◆ operator[]()

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

Definition at line 722 of file genericvector.h.

722  {
723  assert(index >= 0 && index < size_used_);
724  return data_[index];
725 }

◆ pop_back()

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

Definition at line 734 of file genericvector.h.

734  {
735  assert(size_used_ > 0);
736  return data_[--size_used_];
737 }

◆ 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  }
804  index = size_used_++;
805  data_[index] = object;
806  return index;
807 }

◆ push_back_new()

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

Definition at line 810 of file genericvector.h.

810  {
811  int index = get_index(object);
812  if (index >= 0) {
813  return index;
814  }
815  return push_back(object);
816 }

◆ push_front()

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

Definition at line 820 of file genericvector.h.

820  {
821  if (size_used_ == size_reserved_) {
822  double_the_size();
823  }
824  for (int i = size_used_; i > 0; --i) {
825  data_[i] = data_[i - 1];
826  }
827  data_[0] = object;
828  ++size_used_;
829  return 0;
830 }

◆ read()

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

Definition at line 902 of file genericvector.h.

903  {
904  int32_t reserved;
905  if (f->FReadEndian(&reserved, sizeof(reserved), 1) != 1) {
906  return false;
907  }
908  reserve(reserved);
909  if (f->FReadEndian(&size_used_, sizeof(size_used_), 1) != 1) {
910  return false;
911  }
912  if (cb != nullptr) {
913  for (int i = 0; i < size_used_; ++i) {
914  if (!cb(f, data_ + i)) {
915  return false;
916  }
917  }
918  } else {
919  if (f->FReadEndian(data_, sizeof(T), size_used_) != size_used_) {
920  return false;
921  }
922  }
923  return true;
924 }

◆ remove()

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

Definition at line 765 of file genericvector.h.

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

◆ reserve()

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

Definition at line 679 of file genericvector.h.

679  {
680  if (size_reserved_ >= size || size <= 0) {
681  return;
682  }
683  if (size < kDefaultVectorSize) {
685  }
686  T* new_array = new T[size];
687  for (int i = 0; i < size_used_; ++i) {
688  new_array[i] = data_[i];
689  }
690  delete[] data_;
691  data_ = new_array;
693 }

◆ 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  }

◆ 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  }
219  }

◆ Serialize() [1/2]

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

Definition at line 929 of file genericvector.h.

929  {
930  if (fwrite(&size_used_, sizeof(size_used_), 1, fp) != 1) {
931  return false;
932  }
933  if (fwrite(data_, sizeof(*data_), size_used_, fp) != unsigned_size()) {
934  return false;
935  }
936  return true;
937 }

◆ Serialize() [2/2]

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

Definition at line 939 of file genericvector.h.

939  {
940  if (fp->FWrite(&size_used_, sizeof(size_used_), 1) != 1) {
941  return false;
942  }
943  if (fp->FWrite(data_, sizeof(*data_), size_used_) != size_used_) {
944  return false;
945  }
946  return true;
947 }

◆ SerializeClasses() [1/2]

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

Definition at line 1008 of file genericvector.h.

1008  {
1009  if (fwrite(&size_used_, sizeof(size_used_), 1, fp) != 1) {
1010  return false;
1011  }
1012  for (int i = 0; i < size_used_; ++i) {
1013  if (!data_[i].Serialize(fp)) {
1014  return false;
1015  }
1016  }
1017  return true;
1018 }

◆ SerializeClasses() [2/2]

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

Definition at line 1020 of file genericvector.h.

1020  {
1021  if (fp->FWrite(&size_used_, sizeof(size_used_), 1) != 1) {
1022  return false;
1023  }
1024  for (int i = 0; i < size_used_; ++i) {
1025  if (!data_[i].Serialize(fp)) {
1026  return false;
1027  }
1028  }
1029  return true;
1030 }

◆ set()

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

Definition at line 741 of file genericvector.h.

741  {
742  assert(index >= 0 && index < size_used_);
743  data_[index] = t;
744 }

◆ set_clear_callback()

template<typename T>
void GenericVector< T >::set_clear_callback ( std::function< void(T)>  cb)
inline

Definition at line 140 of file genericvector.h.

140  {
141  clear_cb_ = cb;
142  }

◆ set_compare_callback()

template<typename T>
void GenericVector< T >::set_compare_callback ( std::function< bool(const T &, const T &)>  cb)
inline

Definition at line 146 of file genericvector.h.

146  {
147  compare_cb_ = cb;
148  }

◆ size()

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

Definition at line 71 of file genericvector.h.

71  {
72  return size_used_;
73  }

◆ 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  }

◆ SkipDeSerialize()

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

Definition at line 996 of file genericvector.h.

996  {
997  uint32_t reserved;
998  if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) {
999  return false;
1000  }
1001  return (uint32_t)fp->FRead(nullptr, sizeof(T), reserved) == reserved;
1002 }

◆ SkipDeSerializeClasses()

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

Definition at line 1071 of file genericvector.h.

1071  {
1072  int32_t reserved;
1073  if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) {
1074  return false;
1075  }
1076  for (int i = 0; i < reserved; ++i) {
1077  if (!T::SkipDeSerialize(fp)) {
1078  return false;
1079  }
1080  }
1081  return true;
1082 }

◆ sort() [1/2]

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

Definition at line 1102 of file genericvector.h.

1102  {
1103  sort(&tesseract::sort_cmp<T>);
1104 }

◆ sort() [2/2]

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

Definition at line 232 of file genericvector.h.

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

◆ swap()

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

Definition at line 301 of file genericvector.h.

301  {
302  if (index1 != index2) {
303  T tmp = data_[index1];
304  data_[index1] = data_[index2];
305  data_[index2] = tmp;
306  }
307  }

◆ truncate()

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

Definition at line 132 of file genericvector.h.

132  {
133  if (size < size_used_) {
134  size_used_ = size;
135  }
136  }

◆ 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  }

◆ WithinBounds()

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

Definition at line 310 of file genericvector.h.

310  {
311  for (int i = 0; i < size_used_; ++i) {
312  if (data_[i] < rangemin || rangemax < data_[i]) {
313  return false;
314  }
315  }
316  return true;
317  }

◆ write()

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

Definition at line 879 of file genericvector.h.

880  {
881  if (fwrite(&size_reserved_, sizeof(size_reserved_), 1, f) != 1) {
882  return false;
883  }
884  if (fwrite(&size_used_, sizeof(size_used_), 1, f) != 1) {
885  return false;
886  }
887  if (cb != nullptr) {
888  for (int i = 0; i < size_used_; ++i) {
889  if (!cb(f, data_[i])) {
890  return false;
891  }
892  }
893  } else {
894  if (fwrite(data_, sizeof(T), size_used_, f) != unsigned_size()) {
895  return false;
896  }
897  }
898  return true;
899 }

Member Data Documentation

◆ clear_cb_

template<typename T>
std::function<void(T)> GenericVector< T >::clear_cb_
protected

Definition at line 333 of file genericvector.h.

◆ compare_cb_

template<typename T>
std::function<bool(const T&, const T&)> GenericVector< T >::compare_cb_
protected

Definition at line 334 of file genericvector.h.

◆ data_

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

Definition at line 332 of file genericvector.h.

◆ kDefaultVectorSize

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

Definition at line 329 of file genericvector.h.

◆ size_reserved_

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

Definition at line 331 of file genericvector.h.

◆ size_used_

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

Definition at line 330 of file genericvector.h.


The documentation for this class was generated from the following files:
GenericVector::swap
void swap(int index1, int index2)
Definition: genericvector.h:301
rand_r
#define rand_r(seed)
Reverse32
void Reverse32(void *ptr)
Definition: helpers.h:200
GenericVector::unsigned_size
size_t unsigned_size() const
Definition: genericvector.h:75
tesseract::TFile::FReadEndian
int FReadEndian(void *buffer, size_t size, int count)
Definition: serialis.cpp:273
GenericVector::data_
T * data_
Definition: genericvector.h:332
GenericVector::choose_nth_item
int choose_nth_item(int target_index)
Definition: genericvector.h:289
GenericVector::Serialize
bool Serialize(FILE *fp) const
Definition: genericvector.h:929
GenericVector::size_reserved_
int32_t size_reserved_
Definition: genericvector.h:331
tesseract::TFile::FRead
int FRead(void *buffer, size_t size, int count)
Definition: serialis.cpp:284
GenericVector::push_back
int push_back(T object)
Definition: genericvector.h:799
GenericVector::DeSerialize
bool DeSerialize(bool swap, FILE *fp)
Definition: genericvector.h:954
GenericVector::empty
bool empty() const
Definition: genericvector.h:86
GenericVector::double_the_size
void double_the_size()
Definition: genericvector.h:696
GenericVector::operator+=
GenericVector< T > & operator+=(const GenericVector &other)
Definition: genericvector.h:838
GenericVector::get_index
int get_index(const T &object) const
Definition: genericvector.h:781
GenericVector::clear_cb_
std::function< void(T)> clear_cb_
Definition: genericvector.h:333
GenericVector::kDefaultVectorSize
static const int kDefaultVectorSize
Definition: genericvector.h:329
GenericVector::size_used_
int32_t size_used_
Definition: genericvector.h:330
GenericVector::reserve
void reserve(int size)
Definition: genericvector.h:679
GenericVector::init
void init(int size)
Definition: genericvector.h:655
GenericVector::compare_cb_
std::function< bool(const T &, const T &)> compare_cb_
Definition: genericvector.h:334
GenericVector::truncate
void truncate(int size)
Definition: genericvector.h:132
GenericVector::clear
void clear()
Definition: genericvector.h:857
GenericVector::init_to_size
void init_to_size(int size, const T &t)
Definition: genericvector.h:706
GenericVector::binary_search
int binary_search(const T &target) const
Definition: genericvector.h:252
Swap
void Swap(T *p1, T *p2)
Definition: helpers.h:93
ReverseN
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:183
tesseract::TFile::FWrite
int FWrite(const void *buffer, size_t size, int count)
Definition: serialis.cpp:332
GenericVector::sort
void sort()
Definition: genericvector.h:1102
GenericVector::size
int size() const
Definition: genericvector.h:71