Tesseract  3.02
GenericVector< T > Class Template Reference

#include <genericvector.h>

Inheritance diagram for GenericVector< T >:
GenericVectorEqEq< T >

List of all members.

Public Member Functions

 GenericVector ()
 GenericVector (int size)
 GenericVector (const GenericVector &other)
GenericVector< T > & operator+= (const GenericVector &other)
GenericVector< T > & operator= (const GenericVector &other)
virtual ~GenericVector ()
void reserve (int size)
void double_the_size ()
void init_to_size (int size, T t)
int size () const
int length () const
bool empty () const
T & get (int index) const
T & back () const
T & operator[] (int index) const
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)
virtual void remove (int index)
virtual void truncate (int size)
void set_clear_callback (TessCallback1< T > *cb)
void set_compare_callback (TessResultCallback2< bool, T const &, T const & > *cb)
virtual 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)
virtual bool Serialize (FILE *fp) const
virtual bool DeSerialize (bool swap, FILE *fp)
bool SerializeClasses (FILE *fp) const
bool DeSerializeClasses (bool swap, FILE *fp)
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

Static Public Member Functions

static T * double_the_size_memcpy (int current_size, T *data)

Protected Member Functions

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 35 of file genericvector.h.


Constructor & Destructor Documentation

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

Definition at line 37 of file genericvector.h.

template<typename T>
GenericVector< T >::GenericVector ( int  size) [inline, explicit]

Definition at line 38 of file genericvector.h.

{ this->init(size); }
template<typename T>
GenericVector< T >::GenericVector ( const GenericVector< T > &  other) [inline]

Definition at line 41 of file genericvector.h.

                                            {
    this->init(other.size());
    this->operator+=(other);
  }
template<typename T >
GenericVector< T >::~GenericVector ( ) [virtual]

Definition at line 471 of file genericvector.h.

                                 {
  clear();
}

Member Function Documentation

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

Definition at line 522 of file genericvector.h.

                                {
  ASSERT_HOST(size_used_ > 0);
  return data_[size_used_ - 1];
}
template<typename T>
int GenericVector< T >::binary_search ( const T &  target) const [inline]

Definition at line 209 of file genericvector.h.

                                           {
    int bottom = 0;
    int top = size_used_;
    do {
      int middle = (bottom + top) / 2;
      if (data_[middle] > target)
        top = middle;
      else
        bottom = middle;
    }
    while (top - bottom > 1);
    return bottom;
  }
template<typename T>
bool GenericVector< T >::bool_binary_search ( const T &  target) const [inline]

Definition at line 197 of file genericvector.h.

                                                 {
    int index = binary_search(target);
    if (index >= size_used_)
      return false;
    return data_[index] == target;
  }
template<typename T >
void GenericVector< T >::clear ( ) [virtual]

Reimplemented in tesseract::PointerVector< T >, tesseract::PointerVector< TrainingSample >, tesseract::PointerVector< Shape >, and tesseract::DawgInfoVector.

Definition at line 651 of file genericvector.h.

                             {
  if (size_reserved_ > 0) {
    if (clear_cb_ != NULL)
      for (int i = 0; i < size_used_; ++i)
        clear_cb_->Run(data_[i]);
    delete[] data_;
    data_ = NULL;
    size_used_ = 0;
    size_reserved_ = 0;
  }
  if (clear_cb_ != NULL) {
    delete clear_cb_;
    clear_cb_ = NULL;
  }
  if (compare_cb_ != NULL) {
    delete compare_cb_;
    compare_cb_ = NULL;
  }
}
template<typename T>
void GenericVector< T >::compact ( TessResultCallback1< bool, int > *  delete_cb) [inline]

Definition at line 242 of file genericvector.h.

                                                          {
    int new_size = 0;
    int old_index = 0;
    // Until the callback returns true, the elements stay the same.
    while (old_index < size_used_ && !delete_cb->Run(old_index++))
      ++new_size;
    // Now just copy anything else that gets false from delete_cb.
    for (; old_index < size_used_; ++old_index) {
      if (!delete_cb->Run(old_index)) {
        data_[new_size++] = data_[old_index];
      }
    }
    size_used_ = new_size;
    delete delete_cb;
  }
template<typename T>
void GenericVector< T >::compact_sorted ( ) [inline]

Definition at line 225 of file genericvector.h.

                        {
    if (size_used_ == 0)
      return;

    // First element is in no matter what, hence the i = 1.
    int last_write = 0;
    for (int i = 1; i < size_used_; ++i) {
      // Finds next unique item and writes it.
      if (data_[last_write] != data_[i])
        data_[++last_write] = data_[i];
    }
    // last_write is the index of a valid data cell, so add 1.
    size_used_ = last_write + 1;
  }
template<typename T>
bool GenericVector< T >::contains ( object) const

Definition at line 579 of file genericvector.h.

                                              {
  return get_index(object) != -1;
}
template<typename T >
T GenericVector< T >::contains_index ( int  index) const

Definition at line 562 of file genericvector.h.

                                                  {
  return index >= 0 && index < size_used_;
}
template<typename T >
void GenericVector< T >::delete_data_pointers ( )

Definition at line 672 of file genericvector.h.

                                            {
  for (int i = 0; i < size_used_; ++i)
    if (data_[i]) {
      delete data_[i];
    }
}
template<typename T >
bool GenericVector< T >::DeSerialize ( bool  swap,
FILE *  fp 
) [virtual]

Reimplemented in tesseract::PointerVector< T >, tesseract::PointerVector< TrainingSample >, and tesseract::PointerVector< Shape >.

Definition at line 741 of file genericvector.h.

                                                      {
  inT32 reserved;
  if (fread(&reserved, sizeof(reserved), 1, fp) != 1) return false;
  if (swap) Reverse32(&reserved);
  reserve(reserved);
  size_used_ = reserved;
  if (fread(data_, sizeof(T), size_used_, fp) != size_used_) return false;
  if (swap) {
    for (int i = 0; i < size_used_; ++i)
      ReverseN(&data_[i], sizeof(data_[i]));
  }
  return true;
}
template<typename T >
bool GenericVector< T >::DeSerializeClasses ( bool  swap,
FILE *  fp 
)

Definition at line 773 of file genericvector.h.

                                                             {
  uinT32 reserved;
  if (fread(&reserved, sizeof(reserved), 1, fp) != 1) return false;
  if (swap) Reverse32(&reserved);
  T empty;
  init_to_size(reserved, empty);
  for (int i = 0; i < reserved; ++i) {
    if (!data_[i].DeSerialize(swap, fp)) return false;
  }
  return true;
}
template<typename T>
T GenericVector< T >::dot_product ( const GenericVector< T > &  other) const [inline]

Definition at line 258 of file genericvector.h.

                                                     {
    T result = static_cast<T>(0);
    for (int i = MIN(size_used_, other.size_used_) - 1; i >= 0; --i)
      result += data_[i] * other.data_[i];
    return result;
  }
template<typename T >
void GenericVector< T >::double_the_size ( )

Definition at line 490 of file genericvector.h.

                                       {
  if (size_reserved_ == 0) {
    reserve(kDefaultVectorSize);
  }
  else {
    reserve(2 * size_reserved_);
  }
}
template<typename T>
static T* GenericVector< T >::double_the_size_memcpy ( int  current_size,
T *  data 
) [inline, static]

Definition at line 172 of file genericvector.h.

                                                              {
    T *data_new = new T[current_size * 2];
    memcpy(data_new, data, sizeof(T) * current_size);
    delete[] data;
    return data_new;
  }
template<typename T>
bool GenericVector< T >::empty ( ) const [inline]

Definition at line 68 of file genericvector.h.

                     {
    return size_used_ == 0;
  }
template<typename T >
T & GenericVector< T >::get ( int  index) const

Definition at line 511 of file genericvector.h.

                                        {
  ASSERT_HOST(index >= 0 && index < size_used_);
  return data_[index];
}
template<typename T>
int GenericVector< T >::get_index ( object) const

Definition at line 568 of file genericvector.h.

                                              {
  for (int i = 0; i < size_used_; ++i) {
    ASSERT_HOST(compare_cb_ != NULL);
    if (compare_cb_->Run(object, data_[i]))
      return i;
  }
  return -1;
}
template<typename T >
void GenericVector< T >::init ( int  size) [protected]

Definition at line 461 of file genericvector.h.

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

Definition at line 501 of file genericvector.h.

                                                 {
  reserve(size);
  size_used_ = size;
  for (int i = 0; i < size; ++i)
    data_[i] = t;
}
template<typename T>
void GenericVector< T >::insert ( t,
int  index 
)

Definition at line 538 of file genericvector.h.

                                            {
  ASSERT_HOST(index >= 0 && index < size_used_);
  if (size_reserved_ == size_used_)
    double_the_size();
  for (int i = size_used_; i > index; --i) {
    data_[i] = data_[i-1];
  }
  data_[index] = t;
  size_used_++;
}
template<typename T>
int GenericVector< T >::length ( ) const [inline]

Definition at line 63 of file genericvector.h.

                     {
    return size_used_;
  }
template<typename T>
void GenericVector< T >::move ( GenericVector< T > *  from)

Definition at line 788 of file genericvector.h.

                                                  {
  this->clear();
  this->data_ = from->data_;
  this->size_reserved_ = from->size_reserved_;
  this->size_used_ = from->size_used_;
  this->compare_cb_ = from->compare_cb_;
  this->clear_cb_ = from->clear_cb_;
  from->data_ = NULL;
  from->clear_cb_ = NULL;
  from->compare_cb_ = NULL;
  from->size_used_ = 0;
  from->size_reserved_ = 0;
}
template<typename T >
GenericVector< T > & GenericVector< T >::operator+= ( const GenericVector< T > &  other)

Definition at line 620 of file genericvector.h.

                                                                         {
  this->reserve(size_used_ + other.size_used_);
  for (int i = 0; i < other.size(); ++i) {
    this->operator+=(other.data_[i]);
  }
  return *this;
}
template<typename T>
void GenericVector< T >::operator+= ( t)

Definition at line 615 of file genericvector.h.

                                     {
  push_back(t);
}
template<typename T >
GenericVector< T > & GenericVector< T >::operator= ( const GenericVector< T > &  other)

Definition at line 629 of file genericvector.h.

                                                                        {
  this->truncate(0);
  this->operator+=(other);
  return *this;
}
template<typename T >
T & GenericVector< T >::operator[] ( int  index) const

Definition at line 517 of file genericvector.h.

                                               {
 return data_[index];
}
template<typename T>
int GenericVector< T >::push_back ( object)

Definition at line 585 of file genericvector.h.

                                        {
  int index = 0;
  if (size_used_ == size_reserved_)
    double_the_size();
  index = size_used_++;
  data_[index] = object;
  return index;
}
template<typename T>
int GenericVector< T >::push_back_new ( object)

Definition at line 595 of file genericvector.h.

                                            {
  int index = get_index(object);
  if (index >= 0)
    return index;
  return push_back(object);
}
template<typename T>
int GenericVector< T >::push_front ( object)

Definition at line 604 of file genericvector.h.

                                         {
  if (size_used_ == size_reserved_)
    double_the_size();
  for (int i = size_used_; i > 0; --i)
    data_[i] = data_[i-1];
  data_[0] = object;
  ++size_used_;
  return 0;
}
template<typename T>
bool GenericVector< T >::read ( FILE *  f,
TessResultCallback3< bool, FILE *, T *, bool > *  cb,
bool  swap 
)

Definition at line 700 of file genericvector.h.

                                       {
  inT32 reserved;
  if (fread(&reserved, sizeof(reserved), 1, f) != 1) return false;
  if (swap) Reverse32(&reserved);
  reserve(reserved);
  if (fread(&size_used_, sizeof(size_used_), 1, f) != 1) return false;
  if (swap) Reverse32(&size_used_);
  if (cb != NULL) {
    for (int i = 0; i < size_used_; ++i) {
      if (!cb->Run(f, data_ + i, swap)) {
        delete cb;
        return false;
      }
    }
    delete cb;
  } else {
    if (fread(data_, sizeof(T), size_used_, f) != size_used_) return false;
    if (swap) {
      for (int i = 0; i < size_used_; ++i)
        ReverseN(&data_[i], sizeof(T));
    }
  }
  return true;
}
template<typename T >
void GenericVector< T >::remove ( int  index) [virtual]

Reimplemented in tesseract::PointerVector< T >, tesseract::PointerVector< TrainingSample >, and tesseract::PointerVector< Shape >.

Definition at line 552 of file genericvector.h.

                                       {
  ASSERT_HOST(index >= 0 && index < size_used_);
  for (int i = index; i < size_used_ - 1; ++i) {
    data_[i] = data_[i+1];
  }
  size_used_--;
}
template<typename T >
void GenericVector< T >::reserve ( int  size)

Definition at line 478 of file genericvector.h.

                                       {
  if (size_reserved_ >= size || size <= 0)
    return;
  T* new_array = new T[size];
  for (int i = 0; i < size_used_; ++i)
    new_array[i] = data_[i];
  if (data_ != NULL) delete[] data_;
  data_ = new_array;
  size_reserved_ = size;
}
template<typename T >
bool GenericVector< T >::Serialize ( FILE *  fp) const [virtual]

Reimplemented in tesseract::PointerVector< T >, tesseract::PointerVector< TrainingSample >, and tesseract::PointerVector< Shape >.

Definition at line 730 of file genericvector.h.

                                               {
  if (fwrite(&size_used_, sizeof(size_used_), 1, fp) != 1) return false;
  if (fwrite(data_, sizeof(*data_), size_used_, fp) != size_used_) return false;
  return true;
}
template<typename T >
bool GenericVector< T >::SerializeClasses ( FILE *  fp) const

Definition at line 759 of file genericvector.h.

                                                      {
  if (fwrite(&size_used_, sizeof(size_used_), 1, fp) != 1) return false;
  for (int i = 0; i < size_used_; ++i) {
    if (!data_[i].Serialize(fp)) return false;
  }
  return true;
}
template<typename T>
void GenericVector< T >::set ( t,
int  index 
)

Definition at line 529 of file genericvector.h.

                                         {
  ASSERT_HOST(index >= 0 && index < size_used_);
  data_[index] = t;
}
template<typename T>
void GenericVector< T >::set_clear_callback ( TessCallback1< T > *  cb)

Definition at line 638 of file genericvector.h.

                                                              {
  clear_cb_ = cb;
}
template<typename T>
void GenericVector< T >::set_compare_callback ( TessResultCallback2< bool, T const &, T const & > *  cb)

Definition at line 645 of file genericvector.h.

                                                                                               {
  compare_cb_ = cb;
}
template<typename T>
int GenericVector< T >::size ( ) const [inline]

Definition at line 59 of file genericvector.h.

                   {
    return size_used_;
  }
template<typename T >
void GenericVector< T >::sort ( )

Reimplemented in tesseract::PointerVector< T >, tesseract::PointerVector< TrainingSample >, and tesseract::PointerVector< Shape >.

Definition at line 803 of file genericvector.h.

                            {
  sort(&tesseract::sort_cmp<T>);
}
template<typename T>
void GenericVector< T >::sort ( int(*)(const void *, const void *)  comparator) [inline]

Definition at line 190 of file genericvector.h.

                                                         {
    qsort(data_, size_used_, sizeof(*data_), comparator);
  }
template<typename T>
virtual void GenericVector< T >::truncate ( int  size) [inline, virtual]
template<typename T>
bool GenericVector< T >::write ( FILE *  f,
TessResultCallback2< bool, FILE *, T const & > *  cb 
) const

Definition at line 681 of file genericvector.h.

                                                                    {
  if (fwrite(&size_reserved_, sizeof(size_reserved_), 1, f) != 1) return false;
  if (fwrite(&size_used_, sizeof(size_used_), 1, f) != 1) return false;
  if (cb != NULL) {
    for (int i = 0; i < size_used_; ++i) {
      if (!cb->Run(f, data_[i])) {
        delete cb;
        return false;
      }
    }
    delete cb;
  } else {
    if (fwrite(data_, sizeof(T), size_used_, f) != size_used_) return false;
  }
  return true;
}

Member Data Documentation

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

Definition at line 277 of file genericvector.h.

template<typename T>
TessResultCallback2<bool, T const &, T const &>* GenericVector< T >::compare_cb_ [mutable, protected]

Definition at line 279 of file genericvector.h.

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

Definition at line 276 of file genericvector.h.

template<typename T>
const int GenericVector< T >::kDefaultVectorSize = 4 [static, protected]

Definition at line 273 of file genericvector.h.

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

Definition at line 275 of file genericvector.h.

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

Definition at line 274 of file genericvector.h.


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