tesseract  4.1.0
tesseract::PointerVector< T > Class Template Reference

#include <genericvector.h>

Inheritance diagram for tesseract::PointerVector< T >:
GenericVector< T * >

Public Member Functions

 PointerVector ()
 
 PointerVector (int size)
 
 ~PointerVector ()
 
 PointerVector (const PointerVector &other)
 
PointerVector< T > & operator+= (const PointerVector &other)
 
PointerVector< T > & operator= (const PointerVector &other)
 
void remove (int index)
 
void truncate (int size)
 
void compact (TessResultCallback1< bool, const T * > *delete_cb)
 
void clear ()
 
bool Serialize (FILE *fp) const
 
bool Serialize (TFile *fp) const
 
bool DeSerialize (bool swap, FILE *fp)
 
bool DeSerialize (TFile *fp)
 
bool DeSerializeElement (TFile *fp)
 
void sort ()
 
- Public Member Functions inherited from GenericVector< T * >
 GenericVector ()
 
 GenericVector (int size, const T * &init_val)
 
 GenericVector (const GenericVector &other)
 
GenericVector< T * > & operator+= (const GenericVector &other)
 
void operator+= (const T * &t)
 
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
 
T * pop_back ()
 
int get_index (const T * &object) const
 
bool contains (const T * &object) const
 
T * contains_index (int index) const
 
int push_back (T * object)
 
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)
 
T * 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 DeSerializeSize (TFile *fp, int32_t *size)
 
static bool DeSerializeSkip (TFile *fp)
 
- Static Public Member Functions inherited from GenericVector< T * >
static bool SkipDeSerialize (tesseract::TFile *fp)
 
static bool SkipDeSerializeClasses (tesseract::TFile *fp)
 
static T * * double_the_size_memcpy (int current_size, T * *data)
 

Additional Inherited Members

- Protected Member Functions inherited from GenericVector< T * >
int choose_nth_item (int target_index, int start, int end, unsigned int *seed)
 
void init (int size)
 
- Protected Attributes inherited from GenericVector< T * >
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 inherited from GenericVector< T * >
static const int kDefaultVectorSize
 

Detailed Description

template<typename T>
class tesseract::PointerVector< T >

Definition at line 465 of file genericvector.h.

Constructor & Destructor Documentation

template<typename T>
tesseract::PointerVector< T >::PointerVector ( )
inline

Definition at line 467 of file genericvector.h.

template<typename T>
tesseract::PointerVector< T >::PointerVector ( int  size)
inlineexplicit

Definition at line 468 of file genericvector.h.

template<typename T>
tesseract::PointerVector< T >::~PointerVector ( )
inline

Definition at line 469 of file genericvector.h.

469  {
470  // Clear must be called here, even though it is called again by the base,
471  // as the base will call the wrong clear.
472  clear();
473  }
template<typename T>
tesseract::PointerVector< T >::PointerVector ( const PointerVector< T > &  other)
inline

Definition at line 476 of file genericvector.h.

476  : GenericVector<T*>(other) {
477  this->init(other.size());
478  this->operator+=(other);
479  }
PointerVector< T > & operator+=(const PointerVector &other)
void init(int size)

Member Function Documentation

template<typename T>
void tesseract::PointerVector< T >::clear ( )
inline

Definition at line 539 of file genericvector.h.

template<typename T>
void tesseract::PointerVector< T >::compact ( TessResultCallback1< bool, const T * > *  delete_cb)
inline

Definition at line 514 of file genericvector.h.

514  {
515  int new_size = 0;
516  int old_index = 0;
517  // Until the callback returns true, the elements stay the same.
518  while (old_index < GenericVector<T*>::size_used_ &&
519  !delete_cb->Run(GenericVector<T*>::data_[old_index++])) {
520  ++new_size;
521  }
522  // Now just copy anything else that gets false from delete_cb.
523  for (; old_index < GenericVector<T*>::size_used_; ++old_index) {
524  if (!delete_cb->Run(GenericVector<T*>::data_[old_index])) {
525  GenericVector<T*>::data_[new_size++] =
526  GenericVector<T*>::data_[old_index];
527  } else {
528  delete GenericVector<T*>::data_[old_index];
529  }
530  }
532  delete delete_cb;
533  }
virtual R Run(A1)=0
template<typename T>
bool tesseract::PointerVector< T >::DeSerialize ( bool  swap,
FILE *  fp 
)
inline

Definition at line 588 of file genericvector.h.

588  {
589  uint32_t reserved;
590  if (fread(&reserved, sizeof(reserved), 1, fp) != 1) {
591  return false;
592  }
593  if (swap) {
594  Reverse32(&reserved);
595  }
596  // Arbitrarily limit the number of elements to protect against bad data.
597  assert(reserved <= UINT16_MAX);
598  if (reserved > UINT16_MAX) {
599  return false;
600  }
601  GenericVector<T*>::reserve(reserved);
602  truncate(0);
603  for (uint32_t i = 0; i < reserved; ++i) {
604  int8_t non_null;
605  if (fread(&non_null, sizeof(non_null), 1, fp) != 1) {
606  return false;
607  }
608  T* item = nullptr;
609  if (non_null != 0) {
610  item = new T;
611  if (!item->DeSerialize(swap, fp)) {
612  delete item;
613  return false;
614  }
615  this->push_back(item);
616  } else {
617  // Null elements should keep their place in the vector.
618  this->push_back(nullptr);
619  }
620  }
621  return true;
622  }
void swap(int index1, int index2)
void reserve(int size)
int push_back(T * object)
void Reverse32(void *ptr)
Definition: helpers.h:202
template<typename T>
bool tesseract::PointerVector< T >::DeSerialize ( TFile fp)
inline

Definition at line 623 of file genericvector.h.

623  {
624  int32_t reserved;
625  if (!DeSerializeSize(fp, &reserved)) {
626  return false;
627  }
628  GenericVector<T*>::reserve(reserved);
629  truncate(0);
630  for (int i = 0; i < reserved; ++i) {
631  if (!DeSerializeElement(fp)) {
632  return false;
633  }
634  }
635  return true;
636  }
void reserve(int size)
static bool DeSerializeSize(TFile *fp, int32_t *size)
bool DeSerializeElement(TFile *fp)
template<typename T>
bool tesseract::PointerVector< T >::DeSerializeElement ( TFile fp)
inline

Definition at line 645 of file genericvector.h.

645  {
646  int8_t non_null;
647  if (fp->FRead(&non_null, sizeof(non_null), 1) != 1) {
648  return false;
649  }
650  T* item = nullptr;
651  if (non_null != 0) {
652  item = new T;
653  if (!item->DeSerialize(fp)) {
654  delete item;
655  return false;
656  }
657  this->push_back(item);
658  } else {
659  // Null elements should keep their place in the vector.
660  this->push_back(nullptr);
661  }
662  return true;
663  }
int push_back(T * object)
template<typename T>
static bool tesseract::PointerVector< T >::DeSerializeSize ( TFile fp,
int32_t *  size 
)
inlinestatic

Definition at line 641 of file genericvector.h.

641  {
642  return fp->FReadEndian(size, sizeof(*size), 1) == 1;
643  }
template<typename T>
static bool tesseract::PointerVector< T >::DeSerializeSkip ( TFile fp)
inlinestatic

Definition at line 665 of file genericvector.h.

665  {
666  int8_t non_null;
667  if (fp->FRead(&non_null, sizeof(non_null), 1) != 1) {
668  return false;
669  }
670  if (non_null != 0) {
671  if (!T::SkipDeSerialize(fp)) {
672  return false;
673  }
674  }
675  return true;
676  }
template<typename T>
PointerVector<T>& tesseract::PointerVector< T >::operator+= ( const PointerVector< T > &  other)
inline

Definition at line 480 of file genericvector.h.

480  {
481  this->reserve(this->size_used_ + other.size_used_);
482  for (int i = 0; i < other.size(); ++i) {
483  this->push_back(new T(*other.data_[i]));
484  }
485  return *this;
486  }
void reserve(int size)
int push_back(T * object)
template<typename T>
PointerVector<T>& tesseract::PointerVector< T >::operator= ( const PointerVector< T > &  other)
inline

Definition at line 488 of file genericvector.h.

488  {
489  if (&other != this) {
490  this->truncate(0);
491  this->operator+=(other);
492  }
493  return *this;
494  }
PointerVector< T > & operator+=(const PointerVector &other)
template<typename T>
void tesseract::PointerVector< T >::remove ( int  index)
inline

Definition at line 498 of file genericvector.h.

498  {
499  delete GenericVector<T*>::data_[index];
501  }
void remove(int index)
template<typename T>
bool tesseract::PointerVector< T >::Serialize ( FILE *  fp) const
inline

Definition at line 549 of file genericvector.h.

549  {
550  int32_t used = GenericVector<T*>::size_used_;
551  if (fwrite(&used, sizeof(used), 1, fp) != 1) {
552  return false;
553  }
554  for (int i = 0; i < used; ++i) {
555  int8_t non_null = GenericVector<T*>::data_[i] != nullptr;
556  if (fwrite(&non_null, sizeof(non_null), 1, fp) != 1) {
557  return false;
558  }
559  if (non_null && !GenericVector<T*>::data_[i]->Serialize(fp)) {
560  return false;
561  }
562  }
563  return true;
564  }
bool Serialize(FILE *fp) const
template<typename T>
bool tesseract::PointerVector< T >::Serialize ( TFile fp) const
inline

Definition at line 565 of file genericvector.h.

565  {
566  int32_t used = GenericVector<T*>::size_used_;
567  if (fp->FWrite(&used, sizeof(used), 1) != 1) {
568  return false;
569  }
570  for (int i = 0; i < used; ++i) {
571  int8_t non_null = GenericVector<T*>::data_[i] != nullptr;
572  if (fp->FWrite(&non_null, sizeof(non_null), 1) != 1) {
573  return false;
574  }
575  if (non_null && !GenericVector<T*>::data_[i]->Serialize(fp)) {
576  return false;
577  }
578  }
579  return true;
580  }
bool Serialize(FILE *fp) const
template<typename T>
void tesseract::PointerVector< T >::sort ( )
inline

Definition at line 680 of file genericvector.h.

680  {
681  this->GenericVector<T*>::sort(&sort_ptr_cmp<T>);
682  }
template<typename T>
void tesseract::PointerVector< T >::truncate ( int  size)
inline

Definition at line 505 of file genericvector.h.

505  {
506  for (int i = size; i < GenericVector<T*>::size_used_; ++i) {
507  delete GenericVector<T*>::data_[i];
508  }
510  }
void truncate(int size)

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