tesseract  4.0.0-1-g2a2b
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 457 of file genericvector.h.

Constructor & Destructor Documentation

◆ PointerVector() [1/3]

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

Definition at line 459 of file genericvector.h.

◆ PointerVector() [2/3]

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

Definition at line 460 of file genericvector.h.

◆ ~PointerVector()

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

Definition at line 461 of file genericvector.h.

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

◆ PointerVector() [3/3]

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

Definition at line 468 of file genericvector.h.

468  : GenericVector<T*>(other) {
469  this->init(other.size());
470  this->operator+=(other);
471  }
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 529 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  // Now just copy anything else that gets false from delete_cb.
513  for (; old_index < GenericVector<T*>::size_used_; ++old_index) {
514  if (!delete_cb->Run(GenericVector<T*>::data_[old_index])) {
515  GenericVector<T*>::data_[new_size++] =
516  GenericVector<T*>::data_[old_index];
517  } else {
518  delete GenericVector<T*>::data_[old_index];
519  }
520  }
522  delete delete_cb;
523  }
virtual R Run(A1)=0

◆ DeSerialize() [1/2]

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

Definition at line 566 of file genericvector.h.

566  {
567  uint32_t reserved;
568  if (fread(&reserved, sizeof(reserved), 1, fp) != 1) return false;
569  if (swap) Reverse32(&reserved);
570  // Arbitrarily limit the number of elements to protect against bad data.
571  assert(reserved <= UINT16_MAX);
572  if (reserved > UINT16_MAX) {
573  return false;
574  }
575  GenericVector<T*>::reserve(reserved);
576  truncate(0);
577  for (uint32_t i = 0; i < reserved; ++i) {
578  int8_t non_null;
579  if (fread(&non_null, sizeof(non_null), 1, fp) != 1) return false;
580  T* item = nullptr;
581  if (non_null) {
582  item = new T;
583  if (!item->DeSerialize(swap, fp)) {
584  delete item;
585  return false;
586  }
587  this->push_back(item);
588  } else {
589  // Null elements should keep their place in the vector.
590  this->push_back(nullptr);
591  }
592  }
593  return true;
594  }
void reserve(int size)
void Reverse32(void *ptr)
Definition: helpers.h:195
void swap(int index1, int index2)
int push_back(T * object)

◆ DeSerialize() [2/2]

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

Definition at line 595 of file genericvector.h.

595  {
596  int32_t reserved;
597  if (!DeSerializeSize(fp, &reserved)) return false;
598  GenericVector<T*>::reserve(reserved);
599  truncate(0);
600  for (int i = 0; i < reserved; ++i) {
601  if (!DeSerializeElement(fp)) return false;
602  }
603  return true;
604  }
static bool DeSerializeSize(TFile *fp, int32_t *size)
void reserve(int size)
bool DeSerializeElement(TFile *fp)

◆ DeSerializeElement()

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

Definition at line 613 of file genericvector.h.

613  {
614  int8_t non_null;
615  if (fp->FRead(&non_null, sizeof(non_null), 1) != 1) return false;
616  T* item = nullptr;
617  if (non_null) {
618  item = new T;
619  if (!item->DeSerialize(fp)) {
620  delete item;
621  return false;
622  }
623  this->push_back(item);
624  } else {
625  // Null elements should keep their place in the vector.
626  this->push_back(nullptr);
627  }
628  return true;
629  }
int push_back(T * object)

◆ DeSerializeSize()

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

Definition at line 609 of file genericvector.h.

609  {
610  return fp->FReadEndian(size, sizeof(*size), 1) == 1;
611  }

◆ DeSerializeSkip()

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

Definition at line 631 of file genericvector.h.

631  {
632  int8_t non_null;
633  if (fp->FRead(&non_null, sizeof(non_null), 1) != 1) return false;
634  if (non_null) {
635  if (!T::SkipDeSerialize(fp)) return false;
636  }
637  return true;
638  }

◆ operator+=()

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

Definition at line 472 of file genericvector.h.

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

◆ operator=()

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

Definition at line 480 of file genericvector.h.

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

◆ remove()

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

Definition at line 490 of file genericvector.h.

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

◆ Serialize() [1/2]

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

Definition at line 539 of file genericvector.h.

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

◆ Serialize() [2/2]

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

Definition at line 549 of file genericvector.h.

549  {
550  int32_t used = GenericVector<T*>::size_used_;
551  if (fp->FWrite(&used, sizeof(used), 1) != 1) return false;
552  for (int i = 0; i < used; ++i) {
553  int8_t non_null = GenericVector<T*>::data_[i] != nullptr;
554  if (fp->FWrite(&non_null, sizeof(non_null), 1) != 1) return false;
555  if (non_null && !GenericVector<T*>::data_[i]->Serialize(fp)) return false;
556  }
557  return true;
558  }
bool Serialize(FILE *fp) const

◆ sort()

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

Definition at line 642 of file genericvector.h.

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

◆ truncate()

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

Definition at line 497 of file genericvector.h.

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

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