tesseract  3.05.02
GenericVector< T > Class Template Reference

#include <baseapi.h>

Inheritance diagram for GenericVector< T >:
GenericVectorEqEq< T > tesseract::PointerVector< tesseract::BaselineBlock > tesseract::PointerVector< tesseract::BaselineRow > tesseract::PointerVector< tesseract::DocumentData > tesseract::PointerVector< tesseract::ImageData > tesseract::PointerVector< tesseract::LanguageModelState > tesseract::PointerVector< tesseract::Shape > tesseract::PointerVector< tesseract::TrainingSample > tesseract::PointerVector< WERD_RES >

Public Member Functions

 GenericVector ()
 
 GenericVector (int size, 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, T t)
 
void resize_no_init (int size)
 
int 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 (T object) const
 
bool contains (T object) const
 
contains_index (int index) const
 
int push_back (T object)
 
void operator+= (T t)
 
int push_back_new (T object)
 
int push_front (T object)
 
void set (T t, int index)
 
void insert (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 (FILE *f, TessResultCallback3< bool, FILE *, T *, bool > *cb, bool swap)
 
bool Serialize (FILE *fp) const
 
bool Serialize (tesseract::TFile *fp) const
 
bool DeSerialize (bool swap, FILE *fp)
 
bool DeSerialize (bool swap, tesseract::TFile *fp)
 
bool SerializeClasses (FILE *fp) const
 
bool SerializeClasses (tesseract::TFile *fp) const
 
bool DeSerializeClasses (bool swap, FILE *fp)
 
bool DeSerializeClasses (bool swap, 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 (bool swap, tesseract::TFile *fp)
 
static bool SkipDeSerializeClasses (bool swap, 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 size_used_
 
inT32 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 41 of file baseapi.h.

Constructor & Destructor Documentation

◆ GenericVector() [1/3]

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

Definition at line 40 of file genericvector.h.

40  : size_used_(0), size_reserved_(0), data_(NULL),
41  clear_cb_(NULL), compare_cb_(NULL) {}
TessResultCallback2< bool, T const &, T const & > * compare_cb_
TessCallback1< T > * clear_cb_
inT32 size_reserved_

◆ GenericVector() [2/3]

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

Definition at line 43 of file genericvector.h.

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

◆ GenericVector() [3/3]

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

Definition at line 49 of file genericvector.h.

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

◆ ~GenericVector()

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

Definition at line 644 of file genericvector.h.

644  {
645  clear();
646 }

Member Function Documentation

◆ back()

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

Definition at line 697 of file genericvector.h.

697  {
698  ASSERT_HOST(size_used_ > 0);
699  return data_[size_used_ - 1];
700 }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ binary_search()

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

Definition at line 242 of file genericvector.h.

242  {
243  int bottom = 0;
244  int top = size_used_;
245  while (top - bottom > 1) {
246  int middle = (bottom + top) / 2;
247  if (data_[middle] > target)
248  top = middle;
249  else
250  bottom = middle;
251  }
252  return bottom;
253  }

◆ bool_binary_search()

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

Definition at line 230 of file genericvector.h.

230  {
231  int index = binary_search(target);
232  if (index >= size_used_)
233  return false;
234  return data_[index] == target;
235  }
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 301 of file genericvector.h.

301  {
302  // Make sure target_index is legal.
303  if (target_index < 0)
304  target_index = 0; // ensure legal
305  else if (target_index >= size_used_)
306  target_index = size_used_ - 1;
307  unsigned int seed = 1;
308  return choose_nth_item(target_index, 0, size_used_, &seed);
309  }
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 1062 of file genericvector.h.

1063  {
1064  // Number of elements to process.
1065  int num_elements = end - start;
1066  // Trivial cases.
1067  if (num_elements <= 1)
1068  return start;
1069  if (num_elements == 2) {
1070  if (data_[start] < data_[start + 1]) {
1071  return target_index > start ? start + 1 : start;
1072  } else {
1073  return target_index > start ? start : start + 1;
1074  }
1075  }
1076  // Place the pivot at start.
1077  #ifndef rand_r // _MSC_VER, ANDROID
1078  srand(*seed);
1079  #define rand_r(seed) rand()
1080  #endif // _MSC_VER
1081  int pivot = rand_r(seed) % num_elements + start;
1082  swap(pivot, start);
1083  // The invariant condition here is that items [start, next_lesser) are less
1084  // than the pivot (which is at index next_lesser) and items
1085  // [prev_greater, end) are greater than the pivot, with items
1086  // [next_lesser, prev_greater) being equal to the pivot.
1087  int next_lesser = start;
1088  int prev_greater = end;
1089  for (int next_sample = start + 1; next_sample < prev_greater;) {
1090  if (data_[next_sample] < data_[next_lesser]) {
1091  swap(next_lesser++, next_sample++);
1092  } else if (data_[next_sample] == data_[next_lesser]) {
1093  ++next_sample;
1094  } else {
1095  swap(--prev_greater, next_sample);
1096  }
1097  }
1098  // Now the invariant is set up, we recurse on just the section that contains
1099  // the desired index.
1100  if (target_index < next_lesser)
1101  return choose_nth_item(target_index, start, next_lesser, seed);
1102  else if (target_index < prev_greater)
1103  return next_lesser; // In equal bracket.
1104  else
1105  return choose_nth_item(target_index, prev_greater, end, seed);
1106 }
void swap(int index1, int index2)
#define rand_r(seed)
int choose_nth_item(int target_index)

◆ clear()

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

Definition at line 835 of file genericvector.h.

835  {
836  if (size_reserved_ > 0) {
837  if (clear_cb_ != NULL)
838  for (int i = 0; i < size_used_; ++i)
839  clear_cb_->Run(data_[i]);
840  delete[] data_;
841  data_ = NULL;
842  size_used_ = 0;
843  size_reserved_ = 0;
844  }
845  if (clear_cb_ != NULL) {
846  delete clear_cb_;
847  clear_cb_ = NULL;
848  }
849  if (compare_cb_ != NULL) {
850  delete compare_cb_;
851  compare_cb_ = NULL;
852  }
853 }
TessResultCallback2< bool, T const &, T const & > * compare_cb_
virtual void Run(A1)=0
TessCallback1< T > * clear_cb_
inT32 size_reserved_

◆ compact()

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

Definition at line 274 of file genericvector.h.

274  {
275  int new_size = 0;
276  int old_index = 0;
277  // Until the callback returns true, the elements stay the same.
278  while (old_index < size_used_ && !delete_cb->Run(old_index++))
279  ++new_size;
280  // Now just copy anything else that gets false from delete_cb.
281  for (; old_index < size_used_; ++old_index) {
282  if (!delete_cb->Run(old_index)) {
283  data_[new_size++] = data_[old_index];
284  }
285  }
286  size_used_ = new_size;
287  delete delete_cb;
288  }
virtual R Run(A1)=0

◆ compact_sorted()

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

Definition at line 257 of file genericvector.h.

257  {
258  if (size_used_ == 0)
259  return;
260 
261  // First element is in no matter what, hence the i = 1.
262  int last_write = 0;
263  for (int i = 1; i < size_used_; ++i) {
264  // Finds next unique item and writes it.
265  if (data_[last_write] != data_[i])
266  data_[++last_write] = data_[i];
267  }
268  // last_write is the index of a valid data cell, so add 1.
269  size_used_ = last_write + 1;
270  }

◆ contains()

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

Definition at line 760 of file genericvector.h.

760  {
761  return get_index(object) != -1;
762 }
int get_index(T object) const

◆ contains_index()

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

Definition at line 743 of file genericvector.h.

743  {
744  return index >= 0 && index < size_used_;
745 }

◆ delete_data_pointers()

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

Definition at line 856 of file genericvector.h.

856  {
857  for (int i = 0; i < size_used_; ++i)
858  if (data_[i]) {
859  delete data_[i];
860  }
861 }

◆ DeSerialize() [1/2]

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

Definition at line 931 of file genericvector.h.

931  {
932  inT32 reserved;
933  if (fread(&reserved, sizeof(reserved), 1, fp) != 1) return false;
934  if (swap) Reverse32(&reserved);
935  reserve(reserved);
936  size_used_ = reserved;
937  if (fread(data_, sizeof(T), size_used_, fp) != size_used_) return false;
938  if (swap) {
939  for (int i = 0; i < size_used_; ++i)
940  ReverseN(&data_[i], sizeof(data_[i]));
941  }
942  return true;
943 }
void Reverse32(void *ptr)
Definition: helpers.h:193
void swap(int index1, int index2)
int inT32
Definition: host.h:35
void reserve(int size)
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177

◆ DeSerialize() [2/2]

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

Definition at line 945 of file genericvector.h.

945  {
946  inT32 reserved;
947  if (fp->FRead(&reserved, sizeof(reserved), 1) != 1) return false;
948  if (swap) Reverse32(&reserved);
949  reserve(reserved);
950  size_used_ = reserved;
951  if (fp->FRead(data_, sizeof(T), size_used_) != size_used_) return false;
952  if (swap) {
953  for (int i = 0; i < size_used_; ++i)
954  ReverseN(&data_[i], sizeof(data_[i]));
955  }
956  return true;
957 }
void Reverse32(void *ptr)
Definition: helpers.h:193
void swap(int index1, int index2)
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:91
int inT32
Definition: host.h:35
void reserve(int size)
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177

◆ DeSerializeClasses() [1/2]

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

Definition at line 992 of file genericvector.h.

992  {
993  uinT32 reserved;
994  if (fread(&reserved, sizeof(reserved), 1, fp) != 1) return false;
995  if (swap) Reverse32(&reserved);
996  T empty;
997  init_to_size(reserved, empty);
998  for (int i = 0; i < reserved; ++i) {
999  if (!data_[i].DeSerialize(swap, fp)) return false;
1000  }
1001  return true;
1002 }
void Reverse32(void *ptr)
Definition: helpers.h:193
void swap(int index1, int index2)
unsigned int uinT32
Definition: host.h:36
bool empty() const
Definition: genericvector.h:84
void init_to_size(int size, T t)
bool DeSerialize(bool swap, FILE *fp)

◆ DeSerializeClasses() [2/2]

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

Definition at line 1004 of file genericvector.h.

1004  {
1005  uinT32 reserved;
1006  if (fp->FRead(&reserved, sizeof(reserved), 1) != 1) return false;
1007  if (swap) Reverse32(&reserved);
1008  T empty;
1009  init_to_size(reserved, empty);
1010  for (int i = 0; i < reserved; ++i) {
1011  if (!data_[i].DeSerialize(swap, fp)) return false;
1012  }
1013  return true;
1014 }
void Reverse32(void *ptr)
Definition: helpers.h:193
void swap(int index1, int index2)
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:91
unsigned int uinT32
Definition: host.h:36
bool empty() const
Definition: genericvector.h:84
void init_to_size(int size, T t)
bool DeSerialize(bool swap, FILE *fp)

◆ dot_product()

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

Definition at line 290 of file genericvector.h.

290  {
291  T result = static_cast<T>(0);
292  for (int i = MIN(size_used_, other.size_used_) - 1; i >= 0; --i)
293  result += data_[i] * other.data_[i];
294  return result;
295  }
#define MIN(x, y)
Definition: ndminx.h:28

◆ double_the_size()

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

Definition at line 664 of file genericvector.h.

664  {
665  if (size_reserved_ == 0) {
667  }
668  else {
669  reserve(2 * size_reserved_);
670  }
671 }
static const int kDefaultVectorSize
void reserve(int size)
inT32 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 199 of file genericvector.h.

199  {
200  T *data_new = new T[current_size * 2];
201  memcpy(data_new, data, sizeof(T) * current_size);
202  delete[] data;
203  return data_new;
204  }

◆ empty()

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

Definition at line 84 of file genericvector.h.

84  {
85  return size_used_ == 0;
86  }

◆ get()

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

Definition at line 685 of file genericvector.h.

685  {
686  ASSERT_HOST(index >= 0 && index < size_used_);
687  return data_[index];
688 }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ get_index()

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

Definition at line 749 of file genericvector.h.

749  {
750  for (int i = 0; i < size_used_; ++i) {
751  ASSERT_HOST(compare_cb_ != NULL);
752  if (compare_cb_->Run(object, data_[i]))
753  return i;
754  }
755  return -1;
756 }
TessResultCallback2< bool, T const &, T const & > * compare_cb_
virtual R Run(A1, A2)=0
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ init()

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

Definition at line 634 of file genericvector.h.

634  {
635  size_used_ = 0;
636  size_reserved_ = 0;
637  data_ = 0;
638  clear_cb_ = 0;
639  compare_cb_ = 0;
640  reserve(size);
641 }
TessResultCallback2< bool, T const &, T const & > * compare_cb_
TessCallback1< T > * clear_cb_
int size() const
Definition: genericvector.h:72
void reserve(int size)
inT32 size_reserved_

◆ init_to_size()

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

Definition at line 675 of file genericvector.h.

675  {
676  reserve(size);
677  size_used_ = size;
678  for (int i = 0; i < size; ++i)
679  data_[i] = t;
680 }
int size() const
Definition: genericvector.h:72
void reserve(int size)

◆ insert()

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

Definition at line 719 of file genericvector.h.

719  {
720  ASSERT_HOST(index >= 0 && index <= size_used_);
721  if (size_reserved_ == size_used_)
722  double_the_size();
723  for (int i = size_used_; i > index; --i) {
724  data_[i] = data_[i-1];
725  }
726  data_[index] = t;
727  size_used_++;
728 }
void double_the_size()
#define ASSERT_HOST(x)
Definition: errcode.h:84
inT32 size_reserved_

◆ length()

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

Definition at line 79 of file genericvector.h.

79  {
80  return size_used_;
81  }

◆ move()

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

Definition at line 1029 of file genericvector.h.

1029  {
1030  this->clear();
1031  this->data_ = from->data_;
1032  this->size_reserved_ = from->size_reserved_;
1033  this->size_used_ = from->size_used_;
1034  this->compare_cb_ = from->compare_cb_;
1035  this->clear_cb_ = from->clear_cb_;
1036  from->data_ = NULL;
1037  from->clear_cb_ = NULL;
1038  from->compare_cb_ = NULL;
1039  from->size_used_ = 0;
1040  from->size_reserved_ = 0;
1041 }
TessResultCallback2< bool, T const &, T const & > * compare_cb_
TessCallback1< T > * clear_cb_
inT32 size_reserved_

◆ operator+=() [1/2]

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

Definition at line 801 of file genericvector.h.

801  {
802  this->reserve(size_used_ + other.size_used_);
803  for (int i = 0; i < other.size(); ++i) {
804  this->operator+=(other.data_[i]);
805  }
806  return *this;
807 }
GenericVector< T > & operator+=(const GenericVector &other)
int size() const
Definition: genericvector.h:72
void reserve(int size)

◆ operator+=() [2/2]

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

Definition at line 796 of file genericvector.h.

796  {
797  push_back(t);
798 }
int push_back(T object)

◆ operator=()

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

Definition at line 810 of file genericvector.h.

810  {
811  if (&other != this) {
812  this->truncate(0);
813  this->operator+=(other);
814  }
815  return *this;
816 }
GenericVector< T > & operator+=(const GenericVector &other)
void truncate(int size)

◆ operator[]()

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

Definition at line 691 of file genericvector.h.

691  {
692  assert(index >= 0 && index < size_used_);
693  return data_[index];
694 }

◆ pop_back()

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

Definition at line 703 of file genericvector.h.

703  {
704  ASSERT_HOST(size_used_ > 0);
705  return data_[--size_used_];
706 }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ push_back()

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

Definition at line 766 of file genericvector.h.

766  {
767  int index = 0;
768  if (size_used_ == size_reserved_)
769  double_the_size();
770  index = size_used_++;
771  data_[index] = object;
772  return index;
773 }
void double_the_size()
inT32 size_reserved_

◆ push_back_new()

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

Definition at line 776 of file genericvector.h.

776  {
777  int index = get_index(object);
778  if (index >= 0)
779  return index;
780  return push_back(object);
781 }
int get_index(T object) const
int push_back(T object)

◆ push_front()

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

Definition at line 785 of file genericvector.h.

785  {
786  if (size_used_ == size_reserved_)
787  double_the_size();
788  for (int i = size_used_; i > 0; --i)
789  data_[i] = data_[i-1];
790  data_[0] = object;
791  ++size_used_;
792  return 0;
793 }
void double_the_size()
inT32 size_reserved_

◆ read()

template<typename T>
bool GenericVector< T >::read ( FILE *  f,
TessResultCallback3< bool, FILE *, T *, bool > *  cb,
bool  swap 
)

Definition at line 884 of file genericvector.h.

886  {
887  inT32 reserved;
888  if (fread(&reserved, sizeof(reserved), 1, f) != 1) return false;
889  if (swap) Reverse32(&reserved);
890  reserve(reserved);
891  if (fread(&size_used_, sizeof(size_used_), 1, f) != 1) return false;
892  if (swap) Reverse32(&size_used_);
893  if (cb != NULL) {
894  for (int i = 0; i < size_used_; ++i) {
895  if (!cb->Run(f, data_ + i, swap)) {
896  delete cb;
897  return false;
898  }
899  }
900  delete cb;
901  } else {
902  if (fread(data_, sizeof(T), size_used_, f) != size_used_) return false;
903  if (swap) {
904  for (int i = 0; i < size_used_; ++i)
905  ReverseN(&data_[i], sizeof(T));
906  }
907  }
908  return true;
909 }
void Reverse32(void *ptr)
Definition: helpers.h:193
void swap(int index1, int index2)
virtual R Run(A1, A2, A3)=0
int inT32
Definition: host.h:35
void reserve(int size)
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:177

◆ remove()

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

Definition at line 733 of file genericvector.h.

733  {
734  ASSERT_HOST(index >= 0 && index < size_used_);
735  for (int i = index; i < size_used_ - 1; ++i) {
736  data_[i] = data_[i+1];
737  }
738  size_used_--;
739 }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ reserve()

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

Definition at line 651 of file genericvector.h.

651  {
652  if (size_reserved_ >= size || size <= 0)
653  return;
655  T* new_array = new T[size];
656  for (int i = 0; i < size_used_; ++i)
657  new_array[i] = data_[i];
658  delete[] data_;
659  data_ = new_array;
661 }
static const int kDefaultVectorSize
int size() const
Definition: genericvector.h:72
inT32 size_reserved_

◆ resize_no_init()

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

Definition at line 66 of file genericvector.h.

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

◆ reverse()

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

Definition at line 207 of file genericvector.h.

207  {
208  for (int i = 0; i < size_used_ / 2; ++i)
209  Swap(&data_[i], &data_[size_used_ - 1 - i]);
210  }
void Swap(T *p1, T *p2)
Definition: helpers.h:90

◆ Serialize() [1/2]

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

Definition at line 914 of file genericvector.h.

914  {
915  if (fwrite(&size_used_, sizeof(size_used_), 1, fp) != 1) return false;
916  if (fwrite(data_, sizeof(*data_), size_used_, fp) != size_used_) return false;
917  return true;
918 }

◆ Serialize() [2/2]

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

Definition at line 920 of file genericvector.h.

920  {
921  if (fp->FWrite(&size_used_, sizeof(size_used_), 1) != 1) return false;
922  if (fp->FWrite(data_, sizeof(*data_), size_used_) != size_used_) return false;
923  return true;
924 }
int FWrite(const void *buffer, int size, int count)
Definition: serialis.cpp:131

◆ SerializeClasses() [1/2]

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

Definition at line 970 of file genericvector.h.

970  {
971  if (fwrite(&size_used_, sizeof(size_used_), 1, fp) != 1) return false;
972  for (int i = 0; i < size_used_; ++i) {
973  if (!data_[i].Serialize(fp)) return false;
974  }
975  return true;
976 }
bool Serialize(FILE *fp) const

◆ SerializeClasses() [2/2]

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

Definition at line 978 of file genericvector.h.

978  {
979  if (fp->FWrite(&size_used_, sizeof(size_used_), 1) != 1) return false;
980  for (int i = 0; i < size_used_; ++i) {
981  if (!data_[i].Serialize(fp)) return false;
982  }
983  return true;
984 }
int FWrite(const void *buffer, int size, int count)
Definition: serialis.cpp:131
bool Serialize(FILE *fp) const

◆ set()

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

Definition at line 710 of file genericvector.h.

710  {
711  ASSERT_HOST(index >= 0 && index < size_used_);
712  data_[index] = t;
713 }
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ set_clear_callback()

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

Definition at line 821 of file genericvector.h.

821  {
822  clear_cb_ = cb;
823 }
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 828 of file genericvector.h.

829  {
830  compare_cb_ = cb;
831 }
TessResultCallback2< bool, T const &, T const & > * compare_cb_

◆ size()

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

Definition at line 72 of file genericvector.h.

72  {
73  return size_used_;
74  }

◆ size_reserved()

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

Definition at line 75 of file genericvector.h.

75  {
76  return size_reserved_;
77  }
inT32 size_reserved_

◆ SkipDeSerialize()

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

Definition at line 959 of file genericvector.h.

959  {
960  inT32 reserved;
961  if (fp->FRead(&reserved, sizeof(reserved), 1) != 1) return false;
962  if (swap) Reverse32(&reserved);
963  return fp->FRead(NULL, sizeof(T), reserved) == reserved;
964 }
void Reverse32(void *ptr)
Definition: helpers.h:193
void swap(int index1, int index2)
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:91
int inT32
Definition: host.h:35

◆ SkipDeSerializeClasses()

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

Definition at line 1016 of file genericvector.h.

1016  {
1017  uinT32 reserved;
1018  if (fp->FRead(&reserved, sizeof(reserved), 1) != 1) return false;
1019  if (swap) Reverse32(&reserved);
1020  for (int i = 0; i < reserved; ++i) {
1021  if (!T::SkipDeSerialize(swap, fp)) return false;
1022  }
1023  return true;
1024 }
void Reverse32(void *ptr)
Definition: helpers.h:193
void swap(int index1, int index2)
int FRead(void *buffer, int size, int count)
Definition: serialis.cpp:91
unsigned int uinT32
Definition: host.h:36

◆ sort() [1/2]

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

Definition at line 1044 of file genericvector.h.

1044  {
1045  sort(&tesseract::sort_cmp<T>);
1046 }

◆ sort() [2/2]

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

Definition at line 223 of file genericvector.h.

223  {
224  qsort(data_, size_used_, sizeof(*data_), comparator);
225  }

◆ swap()

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

Definition at line 312 of file genericvector.h.

312  {
313  if (index1 != index2) {
314  T tmp = data_[index1];
315  data_[index1] = data_[index2];
316  data_[index2] = tmp;
317  }
318  }

◆ truncate()

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

Definition at line 130 of file genericvector.h.

130  {
131  if (size < size_used_)
132  size_used_ = size;
133  }
int size() const
Definition: genericvector.h:72

◆ WithinBounds()

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

Definition at line 321 of file genericvector.h.

321  {
322  for (int i = 0; i < size_used_; ++i) {
323  if (data_[i] < rangemin || rangemax < data_[i])
324  return false;
325  }
326  return true;
327  }

◆ write()

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

Definition at line 865 of file genericvector.h.

866  {
867  if (fwrite(&size_reserved_, sizeof(size_reserved_), 1, f) != 1) return false;
868  if (fwrite(&size_used_, sizeof(size_used_), 1, f) != 1) return false;
869  if (cb != NULL) {
870  for (int i = 0; i < size_used_; ++i) {
871  if (!cb->Run(f, data_[i])) {
872  delete cb;
873  return false;
874  }
875  }
876  delete cb;
877  } else {
878  if (fwrite(data_, sizeof(T), size_used_, f) != size_used_) return false;
879  }
880  return true;
881 }
virtual R Run(A1, A2)=0
inT32 size_reserved_

Member Data Documentation

◆ clear_cb_

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

Definition at line 343 of file genericvector.h.

◆ compare_cb_

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

Definition at line 345 of file genericvector.h.

◆ data_

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

Definition at line 342 of file genericvector.h.

◆ kDefaultVectorSize

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

Definition at line 339 of file genericvector.h.

◆ size_reserved_

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

Definition at line 341 of file genericvector.h.

◆ size_used_

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

Definition at line 340 of file genericvector.h.


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