tesseract  5.0.0-alpha-619-ge9db
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 (std::function< 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
 
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 (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 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_
 
std::function< void(T *)> clear_cb_
 
std::function< bool(const T * &, const T * &)> 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 417 of file genericvector.h.

Constructor & Destructor Documentation

◆ PointerVector() [1/3]

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

Definition at line 419 of file genericvector.h.

419 : GenericVector<T*>() {}

◆ PointerVector() [2/3]

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

Definition at line 420 of file genericvector.h.

◆ ~PointerVector()

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

Definition at line 421 of file genericvector.h.

421  {
422  // Clear must be called here, even though it is called again by the base,
423  // as the base will call the wrong clear.
424  clear();
425  }

◆ PointerVector() [3/3]

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

Definition at line 428 of file genericvector.h.

428  : GenericVector<T*>(other) {
429  this->init(other.size());
430  this->operator+=(other);
431  }

Member Function Documentation

◆ clear()

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

Definition at line 490 of file genericvector.h.

◆ compact()

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

Definition at line 466 of file genericvector.h.

466  {
467  int new_size = 0;
468  int old_index = 0;
469  // Until the callback returns true, the elements stay the same.
470  while (old_index < GenericVector<T*>::size_used_ &&
471  !delete_cb(GenericVector<T*>::data_[old_index++])) {
472  ++new_size;
473  }
474  // Now just copy anything else that gets false from delete_cb.
475  for (; old_index < GenericVector<T*>::size_used_; ++old_index) {
476  if (!delete_cb(GenericVector<T*>::data_[old_index])) {
477  GenericVector<T*>::data_[new_size++] =
478  GenericVector<T*>::data_[old_index];
479  } else {
480  delete GenericVector<T*>::data_[old_index];
481  }
482  }
484  }

◆ DeSerialize() [1/2]

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

Definition at line 539 of file genericvector.h.

539  {
540  uint32_t reserved;
541  if (fread(&reserved, sizeof(reserved), 1, fp) != 1) {
542  return false;
543  }
544  if (swap) {
545  Reverse32(&reserved);
546  }
547  // Arbitrarily limit the number of elements to protect against bad data.
548  assert(reserved <= UINT16_MAX);
549  if (reserved > UINT16_MAX) {
550  return false;
551  }
552  GenericVector<T*>::reserve(reserved);
553  truncate(0);
554  for (uint32_t i = 0; i < reserved; ++i) {
555  int8_t non_null;
556  if (fread(&non_null, sizeof(non_null), 1, fp) != 1) {
557  return false;
558  }
559  T* item = nullptr;
560  if (non_null != 0) {
561  item = new T;
562  if (!item->DeSerialize(swap, fp)) {
563  delete item;
564  return false;
565  }
566  this->push_back(item);
567  } else {
568  // Null elements should keep their place in the vector.
569  this->push_back(nullptr);
570  }
571  }
572  return true;
573  }

◆ DeSerialize() [2/2]

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

Definition at line 574 of file genericvector.h.

574  {
575  int32_t reserved;
576  if (!DeSerializeSize(fp, &reserved)) {
577  return false;
578  }
579  GenericVector<T*>::reserve(reserved);
580  truncate(0);
581  for (int i = 0; i < reserved; ++i) {
582  if (!DeSerializeElement(fp)) {
583  return false;
584  }
585  }
586  return true;
587  }

◆ DeSerializeElement()

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

Definition at line 596 of file genericvector.h.

596  {
597  int8_t non_null;
598  if (fp->FRead(&non_null, sizeof(non_null), 1) != 1) {
599  return false;
600  }
601  T* item = nullptr;
602  if (non_null != 0) {
603  item = new T;
604  if (!item->DeSerialize(fp)) {
605  delete item;
606  return false;
607  }
608  this->push_back(item);
609  } else {
610  // Null elements should keep their place in the vector.
611  this->push_back(nullptr);
612  }
613  return true;
614  }

◆ DeSerializeSize()

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

Definition at line 592 of file genericvector.h.

592  {
593  return fp->FReadEndian(size, sizeof(*size), 1) == 1;
594  }

◆ DeSerializeSkip()

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

Definition at line 616 of file genericvector.h.

616  {
617  int8_t non_null;
618  if (fp->FRead(&non_null, sizeof(non_null), 1) != 1) {
619  return false;
620  }
621  if (non_null != 0) {
622  if (!T::SkipDeSerialize(fp)) {
623  return false;
624  }
625  }
626  return true;
627  }

◆ operator+=()

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

Definition at line 432 of file genericvector.h.

432  {
433  this->reserve(this->size_used_ + other.size_used_);
434  for (int i = 0; i < other.size(); ++i) {
435  this->push_back(new T(*other.data_[i]));
436  }
437  return *this;
438  }

◆ operator=()

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

Definition at line 440 of file genericvector.h.

440  {
441  if (&other != this) {
442  this->truncate(0);
443  this->operator+=(other);
444  }
445  return *this;
446  }

◆ remove()

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

Definition at line 450 of file genericvector.h.

450  {
451  delete GenericVector<T*>::data_[index];
453  }

◆ Serialize() [1/2]

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

Definition at line 500 of file genericvector.h.

500  {
501  int32_t used = GenericVector<T*>::size_used_;
502  if (fwrite(&used, sizeof(used), 1, fp) != 1) {
503  return false;
504  }
505  for (int i = 0; i < used; ++i) {
506  int8_t non_null = GenericVector<T*>::data_[i] != nullptr;
507  if (fwrite(&non_null, sizeof(non_null), 1, fp) != 1) {
508  return false;
509  }
510  if (non_null && !GenericVector<T*>::data_[i]->Serialize(fp)) {
511  return false;
512  }
513  }
514  return true;
515  }

◆ Serialize() [2/2]

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

Definition at line 516 of file genericvector.h.

516  {
517  int32_t used = GenericVector<T*>::size_used_;
518  if (fp->FWrite(&used, sizeof(used), 1) != 1) {
519  return false;
520  }
521  for (int i = 0; i < used; ++i) {
522  int8_t non_null = GenericVector<T*>::data_[i] != nullptr;
523  if (fp->FWrite(&non_null, sizeof(non_null), 1) != 1) {
524  return false;
525  }
526  if (non_null && !GenericVector<T*>::data_[i]->Serialize(fp)) {
527  return false;
528  }
529  }
530  return true;
531  }

◆ sort()

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

Definition at line 631 of file genericvector.h.

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

◆ truncate()

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

Definition at line 457 of file genericvector.h.

457  {
458  for (int i = size; i < GenericVector<T*>::size_used_; ++i) {
459  delete GenericVector<T*>::data_[i];
460  }
462  }

The documentation for this class was generated from the following file:
GenericVector::delete_data_pointers
void delete_data_pointers()
Definition: genericvector.h:872
GenericVector::remove
void remove(int index)
Definition: genericvector.h:765
tesseract::PointerVector::DeSerializeSize
static bool DeSerializeSize(TFile *fp, int32_t *size)
Definition: genericvector.h:592
GenericVector< T * >::swap
void swap(int index1, int index2)
Definition: genericvector.h:301
Reverse32
void Reverse32(void *ptr)
Definition: helpers.h:200
tesseract::PointerVector::truncate
void truncate(int size)
Definition: genericvector.h:457
tesseract::PointerVector::clear
void clear()
Definition: genericvector.h:490
tesseract::PointerVector::operator+=
PointerVector< T > & operator+=(const PointerVector &other)
Definition: genericvector.h:432
tesseract::PointerVector::DeSerializeElement
bool DeSerializeElement(TFile *fp)
Definition: genericvector.h:596
GenericVector< T * >::push_back
int push_back(T * object)
Definition: genericvector.h:799
GenericVector< T * >::size_used_
int32_t size_used_
Definition: genericvector.h:330
GenericVector< T * >
GenericVector::reserve
void reserve(int size)
Definition: genericvector.h:679
GenericVector< T * >::init
void init(int size)
Definition: genericvector.h:655
GenericVector::truncate
void truncate(int size)
Definition: genericvector.h:132
GenericVector::clear
void clear()
Definition: genericvector.h:857
GenericVector::sort
void sort()
Definition: genericvector.h:1102
GenericVector< T * >::size
int size() const
Definition: genericvector.h:71
tesseract::PointerVector::Serialize
bool Serialize(FILE *fp) const
Definition: genericvector.h:500