tesseract  4.1.1
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 456 of file genericvector.h.

Constructor & Destructor Documentation

◆ PointerVector() [1/3]

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

Definition at line 458 of file genericvector.h.

◆ PointerVector() [2/3]

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

Definition at line 459 of file genericvector.h.

◆ ~PointerVector()

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

Definition at line 460 of file genericvector.h.

460  {
461  // Clear must be called here, even though it is called again by the base,
462  // as the base will call the wrong clear.
463  clear();
464  }

◆ PointerVector() [3/3]

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

Definition at line 467 of file genericvector.h.

467  : GenericVector<T*>(other) {
468  this->init(other.size());
469  this->operator+=(other);
470  }
void init(int size)
PointerVector< T > & operator+=(const PointerVector &other)

Member Function Documentation

◆ clear()

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

Definition at line 530 of file genericvector.h.

◆ compact()

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

Definition at line 505 of file genericvector.h.

505  {
506  int new_size = 0;
507  int old_index = 0;
508  // Until the callback returns true, the elements stay the same.
509  while (old_index < GenericVector<T*>::size_used_ &&
510  !delete_cb->Run(GenericVector<T*>::data_[old_index++])) {
511  ++new_size;
512  }
513  // Now just copy anything else that gets false from delete_cb.
514  for (; old_index < GenericVector<T*>::size_used_; ++old_index) {
515  if (!delete_cb->Run(GenericVector<T*>::data_[old_index])) {
516  GenericVector<T*>::data_[new_size++] =
517  GenericVector<T*>::data_[old_index];
518  } else {
519  delete GenericVector<T*>::data_[old_index];
520  }
521  }
523  delete delete_cb;
524  }
virtual R Run(A1)=0

◆ DeSerialize() [1/2]

template<typename T >
bool tesseract::PointerVector< T >::DeSerialize ( bool  swap,
FILE *  fp 
)
inline

Definition at line 579 of file genericvector.h.

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

◆ DeSerialize() [2/2]

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

Definition at line 614 of file genericvector.h.

614  {
615  int32_t reserved;
616  if (!DeSerializeSize(fp, &reserved)) {
617  return false;
618  }
619  GenericVector<T*>::reserve(reserved);
620  truncate(0);
621  for (int i = 0; i < reserved; ++i) {
622  if (!DeSerializeElement(fp)) {
623  return false;
624  }
625  }
626  return true;
627  }
bool DeSerializeElement(TFile *fp)
static bool DeSerializeSize(TFile *fp, int32_t *size)

◆ DeSerializeElement()

template<typename T >
bool tesseract::PointerVector< T >::DeSerializeElement ( TFile fp)
inline

Definition at line 636 of file genericvector.h.

636  {
637  int8_t non_null;
638  if (fp->FRead(&non_null, sizeof(non_null), 1) != 1) {
639  return false;
640  }
641  T* item = nullptr;
642  if (non_null != 0) {
643  item = new T;
644  if (!item->DeSerialize(fp)) {
645  delete item;
646  return false;
647  }
648  this->push_back(item);
649  } else {
650  // Null elements should keep their place in the vector.
651  this->push_back(nullptr);
652  }
653  return true;
654  }

◆ DeSerializeSize()

template<typename T >
static bool tesseract::PointerVector< T >::DeSerializeSize ( TFile fp,
int32_t *  size 
)
inlinestatic

Definition at line 632 of file genericvector.h.

632  {
633  return fp->FReadEndian(size, sizeof(*size), 1) == 1;
634  }

◆ DeSerializeSkip()

template<typename T >
static bool tesseract::PointerVector< T >::DeSerializeSkip ( TFile fp)
inlinestatic

Definition at line 656 of file genericvector.h.

656  {
657  int8_t non_null;
658  if (fp->FRead(&non_null, sizeof(non_null), 1) != 1) {
659  return false;
660  }
661  if (non_null != 0) {
662  if (!T::SkipDeSerialize(fp)) {
663  return false;
664  }
665  }
666  return true;
667  }

◆ operator+=()

template<typename T >
PointerVector<T>& tesseract::PointerVector< T >::operator+= ( const PointerVector< T > &  other)
inline

Definition at line 471 of file genericvector.h.

471  {
472  this->reserve(this->size_used_ + other.size_used_);
473  for (int i = 0; i < other.size(); ++i) {
474  this->push_back(new T(*other.data_[i]));
475  }
476  return *this;
477  }

◆ operator=()

template<typename T >
PointerVector<T>& tesseract::PointerVector< T >::operator= ( const PointerVector< T > &  other)
inline

Definition at line 479 of file genericvector.h.

479  {
480  if (&other != this) {
481  this->truncate(0);
482  this->operator+=(other);
483  }
484  return *this;
485  }

◆ remove()

template<typename T >
void tesseract::PointerVector< T >::remove ( int  index)
inline

Definition at line 489 of file genericvector.h.

489  {
490  delete GenericVector<T*>::data_[index];
492  }
void remove(int index)

◆ Serialize() [1/2]

template<typename T >
bool tesseract::PointerVector< T >::Serialize ( FILE *  fp) const
inline

Definition at line 540 of file genericvector.h.

540  {
541  int32_t used = GenericVector<T*>::size_used_;
542  if (fwrite(&used, sizeof(used), 1, fp) != 1) {
543  return false;
544  }
545  for (int i = 0; i < used; ++i) {
546  int8_t non_null = GenericVector<T*>::data_[i] != nullptr;
547  if (fwrite(&non_null, sizeof(non_null), 1, fp) != 1) {
548  return false;
549  }
550  if (non_null && !GenericVector<T*>::data_[i]->Serialize(fp)) {
551  return false;
552  }
553  }
554  return true;
555  }
bool Serialize(FILE *fp) const

◆ Serialize() [2/2]

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

Definition at line 556 of file genericvector.h.

556  {
557  int32_t used = GenericVector<T*>::size_used_;
558  if (fp->FWrite(&used, sizeof(used), 1) != 1) {
559  return false;
560  }
561  for (int i = 0; i < used; ++i) {
562  int8_t non_null = GenericVector<T*>::data_[i] != nullptr;
563  if (fp->FWrite(&non_null, sizeof(non_null), 1) != 1) {
564  return false;
565  }
566  if (non_null && !GenericVector<T*>::data_[i]->Serialize(fp)) {
567  return false;
568  }
569  }
570  return true;
571  }

◆ sort()

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

Definition at line 671 of file genericvector.h.

671  {
672  this->GenericVector<T*>::sort(&sort_ptr_cmp<T>);
673  }

◆ truncate()

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

Definition at line 496 of file genericvector.h.

496  {
497  for (int i = size; i < GenericVector<T*>::size_used_; ++i) {
498  delete GenericVector<T*>::data_[i];
499  }
501  }
void truncate(int size)

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