VVector.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @file VVector.h
  3. * @brief vector of vector
  4. * @author Erik Rodner, Alexander Freytag
  5. * @date 02/15/2008
  6. */
  7. #ifndef VVECTORINCLUDE
  8. #define VVECTORINCLUDE
  9. #include <vector>
  10. #include <ostream>
  11. #include "VectorT.h"
  12. #include "MatrixT.h"
  13. #include "core/basics/Persistent.h"
  14. namespace NICE {
  15. /**
  16. * @class VVector
  17. * @brief STL vector of NICE vectors
  18. * @author Erik Rodner, Alexander Freytag
  19. * @date 02/15/2008
  20. * @NOTE We make the inheritance of Persisent virtual, in order to allow classes to implement VVector and Persistent separately, as it is done for example in CodebookPrototypes in vislearning/features/simplefeatures
  21. */
  22. class VVector : public std::vector<NICE::Vector>, virtual public Persistent
  23. {
  24. protected:
  25. size_t bufsize;
  26. bool ioUntilEndOfFile;
  27. public:
  28. /** possible file formats */
  29. enum {
  30. /** use standard NICE vector I/O */
  31. FILEFORMAT_NICE = 0,
  32. FILEFORMAT_LINE,
  33. /** write binary double values, use this for large-scale datasets */
  34. FILEFORMAT_BINARY_DOUBLE,
  35. /** write values as unsigned char, be careful with truncation */
  36. FILEFORMAT_BINARY_CHAR
  37. };
  38. /** simple constructor */
  39. VVector();
  40. VVector( const std::string & filename );
  41. /**
  42. * constructor creates $count NICE::Vectors of size dim
  43. * @param count number of vectors
  44. * @param dim size of each Vector
  45. */
  46. VVector( int count, int dim);
  47. /**
  48. * resizes the VVector and clear everything
  49. * @param count number of vectors
  50. * @param dim size of each Vector
  51. */
  52. void reSize(int count, int dim);
  53. /** simple destructor */
  54. virtual ~VVector();
  55. virtual void clear ();
  56. virtual void restore ( std::istream & is, int format = 0 );
  57. virtual void store ( std::ostream & os, int format = 0 ) const;
  58. void setBufSize ( size_t bufsize );
  59. inline void setIoUntilEndOfFile(const bool & _ioUntilEndOfFile){ ioUntilEndOfFile = _ioUntilEndOfFile;};
  60. inline bool getIoUntilEndOfFile(){return ioUntilEndOfFile;};
  61. void sort ( size_t index );
  62. /** convert this data structure to a non-dynamic nice matrix
  63. * @param dst the destination matrix (will be resized)
  64. * @param rowOriented if true (std) the elements of VVector (of type NICE::Vector)
  65. * are stored as rows in the resulting matrix (otherwise as columns)
  66. * */
  67. void toMatrix ( NICE::Matrix & dst, bool rowOriented = true ) const;
  68. inline void print (std::ostream& output)
  69. {
  70. if (this->size() > 0)
  71. {
  72. output << this->size() << " x " << (*this)[0].size() << std::endl;
  73. for (size_t r = 0; r < this->size(); r++) {
  74. for (size_t c = 0; c < (*this)[r].size(); c++) {
  75. output << (*this)[r][c] << " ";
  76. }
  77. output << std::endl;
  78. }
  79. }
  80. else
  81. {
  82. output << "VVector is empty" << std::endl;
  83. }
  84. }
  85. /** Append another VVector to this
  86. * @param _2ndVec the destination matrix (will be resized)
  87. * @param _copyData whether to copy the actual data or just to re-direct this->end and 2ndVec.begin() (default: false, i.e., just re-direct)
  88. * @date 03-06-2013 (dd-mm-yyyy)
  89. * @author Alexander Freytag
  90. * */
  91. void append ( const NICE::VVector & _2ndVec, const bool & _copyData = false );
  92. /** compare a second VVector with this, return true is considered as equal
  93. * @param _B a second VVector
  94. * @param _tolerance threshold for acceptable difference in a single dimension (default: 10e-8)
  95. * @date 05-06-2013 (dd-mm-yyyy)
  96. * @author Alexander Freytag
  97. * */
  98. bool isEqual ( const NICE::VVector & _B, const double & _tolerance = 10e-8) ;
  99. };
  100. } // namespace
  101. #endif