Sfoglia il codice sorgente

more functionality and test cases for VVector

Alexander Freytag 12 anni fa
parent
commit
b825c3f23c
2 ha cambiato i file con 144 aggiunte e 71 eliminazioni
  1. 80 24
      core/vector/VVector.cpp
  2. 64 47
      core/vector/VVector.h

+ 80 - 24
core/vector/VVector.cpp

@@ -1,7 +1,7 @@
 /** 
  * @file VVector.h
  * @brief vector of vector
- * @author Erik Rodner
+ * @author Erik Rodner, Alexander Freytag
  * @date 02/15/2008
  */
 
@@ -297,28 +297,84 @@ void VVector::sort ( size_t index )
 	
 void VVector::toMatrix ( NICE::Matrix & M, bool rowOriented ) const
 {
-	if ( size() > 0 ) {
-		// check if there is a constant dimension
-		int dimension = -1;
-		for ( VVector::const_iterator i = begin(); i != end(); i++ )
-			if ( dimension < 0 )
-				dimension = i->size();
-			else if ( (int)(i->size()) != dimension )
-				fthrow(Exception, "Non-constant dimension inside of VVector, unable to convert this structure into a Matrix" );
+  if ( size() > 0 ) {
+    // check if there is a constant dimension
+    int dimension = -1;
+    for ( VVector::const_iterator i = begin(); i != end(); i++ )
+      if ( dimension < 0 )
+        dimension = i->size();
+      else if ( (int)(i->size()) != dimension )
+        fthrow(Exception, "Non-constant dimension inside of VVector, unable to convert this structure into a Matrix" );
 
-		if ( rowOriented ) 
-			M.resize ( size(), dimension );
-		else
-			M.resize ( dimension, size() );
-		uint k = 0;
-		for ( VVector::const_iterator i = begin(); i != end(); i++,k++ )
-		{
-			const Vector & x = *i;
-			for ( int l = 0 ; l < dimension; l++ )
-				if ( rowOriented )
-					M ( k, l ) = x[l];
-				else
-					M ( l, k ) = x[l];
-		}
-	}
+    if ( rowOriented ) 
+      M.resize ( size(), dimension );
+    else
+      M.resize ( dimension, size() );
+    uint k = 0;
+    for ( VVector::const_iterator i = begin(); i != end(); i++,k++ )
+    {
+      const Vector & x = *i;
+      for ( int l = 0 ; l < dimension; l++ )
+        if ( rowOriented )
+          M ( k, l ) = x[l];
+        else
+          M ( l, k ) = x[l];
+    }
+  }
+}
+
+void VVector::append ( const NICE::VVector & _2ndVec, const bool & _copyData )
+{
+ 
+  if ( _copyData )
+  {
+    //copy every single entry of the second VVector
+    for ( NICE::VVector::const_iterator it = _2ndVec.begin(); it != _2ndVec.end(); it++)
+    {
+       this->push_back( *it );
+    }
+  }
+  else //only re-direct this->end()
+  {
+    //fetch my current end
+    std::vector<NICE::Vector>::iterator myEnd = this->end();    
+    
+    this->insert(myEnd, _2ndVec.begin(), _2ndVec.end() );
+//     //re-direct it to the beginnign of the second vector
+//     myEnd = _2ndVec.begin();
+  }
+ 
+}
+
+bool VVector::isEqual ( const NICE::VVector & _B, const double & _tolerance )
+{
+  bool result(true);
+  
+  NICE::VVector::const_iterator itA = this->begin();
+  NICE::VVector::const_iterator itB = _B.begin();
+  
+  while ( (itA != this->end()) && ( itB != _B.end()) )
+  {
+    if (itA->size() != itB->size())
+    {
+      result = false;
+      break;
+    } 
+    
+    for(uint i = 0; (i < itA->size()) && (i < itB->size()); i++)
+    {
+      if (fabs((*itA)[i] - (*itB)[i]) > _tolerance)
+      {
+        result = false;
+        break;        
+      }
+    }
+
+    if (result == false)
+          break;        
+    itA++;
+    itB++;
+  }
+  
+  return result;  
 }

+ 64 - 47
core/vector/VVector.h

@@ -1,7 +1,7 @@
 /** 
 * @file VVector.h
 * @brief vector of vector
-* @author Erik Rodner
+* @author Erik Rodner, Alexander Freytag
 * @date 02/15/2008
 */
 #ifndef VVECTORINCLUDE
@@ -22,64 +22,68 @@ namespace NICE {
    * @brief STL vector of NICE vectors
    * @author Erik Rodner, Alexander Freytag
    * @date 02/15/2008
+   * @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
   */  
 
-class VVector : public std::vector<NICE::Vector>, public Persistent
+class VVector : public std::vector<NICE::Vector>, virtual public Persistent
 {
 
     protected:
-	size_t bufsize;
+  size_t bufsize;
   bool ioUntilEndOfFile;
 
     public:
 
-	/** possible file formats */
-	enum {
-		/** use standard NICE vector I/O */
-	    FILEFORMAT_NICE = 0,
-	    FILEFORMAT_LINE,
-		/** write binary double values, use this for large-scale datasets */
-	    FILEFORMAT_BINARY_DOUBLE,
-		/** write values as unsigned char, be careful with truncation */
-	    FILEFORMAT_BINARY_CHAR
-	};
+  /** possible file formats */
+  enum {
+    /** use standard NICE vector I/O */
+      FILEFORMAT_NICE = 0,
+      FILEFORMAT_LINE,
+    /** write binary double values, use this for large-scale datasets */
+      FILEFORMAT_BINARY_DOUBLE,
+    /** write values as unsigned char, be careful with truncation */
+      FILEFORMAT_BINARY_CHAR
+  };
   
-	/** simple constructor */
-	VVector();
-	
-	VVector( const std::string & filename );
-	
-	/**
-	 * constructor creates $count NICE::Vectors of size dim
-	 * @param count number of vectors
-	 * @param dim size of each Vector
-	 */
-	VVector( int count, int dim);
-	
-	/**
-	 * resizes the VVector and clear everything
-	 * @param count number of vectors
-	 * @param dim size of each Vector
-	 */
-	void reSize(int count, int dim);
+  /** simple constructor */
+  VVector();
+  
+  VVector( const std::string & filename );
+  
+  /**
+  * constructor creates $count NICE::Vectors of size dim
+  * @param count number of vectors
+  * @param dim size of each Vector
+  */
+  VVector( int count, int dim);
+  
+  /**
+  * resizes the VVector and clear everything
+  * @param count number of vectors
+  * @param dim size of each Vector
+  */
+  void reSize(int count, int dim);
       
-	/** simple destructor */
-	virtual ~VVector();
-     
-	virtual void clear ();
-	virtual void restore ( std::istream & is, int format = 0 );
-	virtual void store ( std::ostream & os, int format = 0 ) const;
+  /** simple destructor */
+  virtual ~VVector();
+    
+  virtual void clear ();
+  virtual void restore ( std::istream & is, int format = 0 );
+  virtual void store ( std::ostream & os, int format = 0 ) const;
 
-	void setBufSize ( size_t bufsize );
+  void setBufSize ( size_t bufsize );
+  
+  inline void setIoUntilEndOfFile(const bool & _ioUntilEndOfFile){ ioUntilEndOfFile = _ioUntilEndOfFile;};
+  inline bool getIoUntilEndOfFile(){return ioUntilEndOfFile;};  
 
-	void sort ( size_t index );
+  void sort ( size_t index );
 
-	/** convert this data structure to a non-dynamic nice matrix 
-	 * @param dst the destination matrix (will be resized)
-	 * @param rowOriented if true (std) the elements of VVector (of type NICE::Vector)
-	 * are stored as rows in the resulting matrix (otherwise as columns)
-	 * */
-	void toMatrix ( NICE::Matrix & dst, bool rowOriented = true ) const;
+  /** convert this data structure to a non-dynamic nice matrix 
+  * @param dst the destination matrix (will be resized)
+  * @param rowOriented if true (std) the elements of VVector (of type NICE::Vector)
+  * are stored as rows in the resulting matrix (otherwise as columns)
+  * */
+  void toMatrix ( NICE::Matrix & dst, bool rowOriented = true ) const;
   
   inline void print (std::ostream& output)
   {
@@ -99,8 +103,21 @@ class VVector : public std::vector<NICE::Vector>, public Persistent
     }
   }
   
-  inline void setIoUntilEndOfFile(const bool & _ioUntilEndOfFile){ ioUntilEndOfFile = _ioUntilEndOfFile;};
-  inline bool getIoUntilEndOfFile(){return ioUntilEndOfFile;};
+  /** Append another VVector to this
+  * @param _2ndVec the destination matrix (will be resized)
+  * @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)
+  * @date 03-06-2013 (dd-mm-yyyy) 
+  * @author Alexander Freytag
+  * */  
+  void append ( const NICE::VVector & _2ndVec, const bool & _copyData = false );
+  
+  /** compare a second VVector with this, return true is considered as equal
+  * @param _B a second VVector
+  * @param _tolerance threshold for acceptable difference in a single dimension (default: 10e-8)
+  * @date 05-06-2013 (dd-mm-yyyy) 
+  * @author Alexander Freytag
+  * */   
+  bool isEqual ( const NICE::VVector & _B, const double & _tolerance = 10e-8) ;
   
 };