#ifndef __BESS_ARRAY_H #define __BESS_ARRAY_H #include "vtkBaseArray.h" //! Default memory increase for dynamic memory administration #define ARRAY_INC_MEM_STEP 6 template class vtkBESSArray : public vtkBaseArray { //***** c o n s t r u c t o r s / d e s t r u c t o r public: // vtkTypeRevisionMacro(vtkBESSArray, vtkBaseArray); // static vtkBESSArray *New() {return new vtkBESSArray}; // const char *GetClassName() {return "vtkBESSArray"}; //! Constructor /*! Allowing the specification of array sizes, additional memory reservation, and initialization with other arrays */ vtkBESSArray() { m_uMemSize = 0; // #ifdef _DEBUG // m_yNLocks = 0; // #endif }; //! Constructor /*! Allowing the specification of array sizes, additional memory reservation, and initialization with other arrays */ vtkBESSArray( const UINT uSize, const UINT uMinimumReserve = 0 ); //! Constructor /*! Allowing the specification of array sizes, additional memory reservation, and initialization with other arrays */ vtkBESSArray( const vtkBaseArray&, const UINT uMinimumReserve = 0 ); //! Destructor freeing the occupied memory and asserting that no more subarrays containing portions of the array are still defined ~vtkBESSArray() { // assert( !m_yNLocks ); if( m_uMemSize ) delete[] m_pData; }; //***** p u b l i c m e t h o d s //! Append a new element or the copy of another array at the end of the array void Append( const type Element ) { IncreaseSizeBy( 1 ); Last() = Element; } //! Append a new element or the copy of another array at the end of the array void Append( const vtkBaseArray& ); //! Clear array, i.e. set size to zero and free all memory void Clear(); // { // assert( !m_yNLocks ); // if( m_uMemSize ) // { // delete[] m_pData; // m_pData = NULL; // m_uSize = m_uMemSize = 0; // } // } //! Assignment of other arrays void CopyOf( const vtkBaseArray& a ) { BaseCopyOf( a ); } //! Copy part of another array void Extract( const vtkBaseArray&, const UINT uFirstElement, const UINT uNumberOfElements = 3 ); //! Increase array size by a certain increment, maintaining data and initializing the new parts of the array with zeros void IncreaseSizeBy( const UINT uIncrement ); //! Read in array from file void Read( const char* pcFilePath, const UINT uMinimumReserve = 0 ); //! Remove portion of array, beginning at certain element and deleting the indicated # of elem. void Remove( const UINT uFirstElement, const UINT uNumberOfElements ); //! Assert a minimum amount of memory reserved for the array; /*! Sets actual size to zero, since for the memory reservation possibly all data is lost */ void Reserve( const UINT uMinimumReserve ); //! Set array size allowing additional memory reservation possible loss of all current data void SetSize( const UINT uSize, const UINT uMinimumReserve = 0 ) { Reserve( (UINT)(uSize + uMinimumReserve) ); m_uSize = uSize; } //! Write array to file void Write( const char* pcFilePath ) const; //***** p r o t e c t e d m e t h o d s protected: //! Assure a certain array size, i.e. set array size void AssureSize( const UINT uSize ) { SetSize( uSize ); } //! Size of actually assigned memory UINT m_uMemSize; // vtkBESSArray operator =(const vtkBESSArray &); //***** p r i v a t e m e t h o d s private: }; // template class CBESSArray : public template vtkBESSArray::vtkBESSArray( const UINT c_uSize, const UINT c_uReserve /* = 0 */ ) { m_pData = NULL; if( (m_uMemSize = c_uSize + c_uReserve) && (m_pData = new type[m_uMemSize]) == NULL ) throw;// CString( "Insufficient memory for array handling." ); m_uSize = c_uSize; } template vtkBESSArray::vtkBESSArray( const vtkBaseArray& c_a, const UINT c_uReserve /* = 0 */ ) { m_pData = NULL; if( (m_uMemSize = c_a.Size() + c_uReserve) && (m_pData = new type[m_uMemSize]) == NULL ) throw;// CString( "Insufficient memory for array handling." ); m_uSize = c_a.Size(); for( UINT uEl = 0; uEl < c_a.Size(); uEl++ ) m_pData[uEl] = c_a[uEl]; } template void vtkBESSArray::Clear() { assert( !m_yNLocks ); if( m_uMemSize ) { delete[] m_pData; m_pData = NULL; m_uSize = m_uMemSize = 0; }; }; //***** A p p e n d template void vtkBESSArray::Append( const vtkBaseArray& c_a ) { assert( !m_yNLocks ); UINT uOldSize = m_uSize, uEl; IncreaseSizeBy( c_a.Size() ); for( uEl = 0; uEl < c_a.Size(); uEl++ ) m_pData[uOldSize + uEl] = c_a[uEl]; } //***** E x t r a c t // private template void vtkBESSArray::Extract( const vtkBaseArray& c_a, const UINT c_uFirst, const UINT c_uNEl /* = 3 */ ) { // assert array defined assert( c_a.Defined() ); // assert valid range of rows and columns of the input matrix addressed assert( c_uFirst + c_uNEl <= c_a.Size() ); // set array size SetSize( c_uNEl ); // install new data for( UINT uEl = 0; uEl < m_uSize; uEl++ ) m_pData[uEl] = c_a[c_uFirst + uEl]; } //***** I n c r e a s e S i z e B y // private template void vtkBESSArray::IncreaseSizeBy( const UINT c_uInc ) { // array size should be increased assert( c_uInc > 0 ); // assert no subarray defined containing data of the array assert( !m_yNLocks ); // assure sufficient memory UINT uEl; if( m_uSize + c_uInc > m_uMemSize ) { // reserve new memory type* pOldData = m_pData; if( (m_pData = new type[m_uMemSize = m_uSize + c_uInc + ARRAY_INC_MEM_STEP]) == NULL ) { if( pOldData != NULL ) { delete[] pOldData; m_uSize = m_uMemSize = 0; } throw; } // copy old part of array for( uEl = 0; uEl < m_uSize; uEl++ ) m_pData[uEl] = pOldData[uEl]; // install new array and delete old one if( pOldData != NULL ) delete[] pOldData; } // initialize new part of array for( uEl = 0; uEl < c_uInc; uEl++ ) m_pData[m_uSize + uEl] = 0; // update dimension m_uSize += c_uInc; } //***** R e a d template void vtkBESSArray::Read( const char *c_pcFilePath, const UINT c_uReserve /* = 0 */ ) { // assert no subarray defined containing data of the array assert( !m_yNLocks ); // open file FILE *pDataFile; if( (pDataFile = fopen( c_pcFilePath,"rb" )) == NULL ) { Clear(); throw;// sError; } // read array size UINT uSize; if( fread( &uSize,sizeof( UINT ),1,pDataFile ) != 1 ) { Clear(); fclose( pDataFile ); throw;// sError; } SetSize( uSize,c_uReserve ); // read array elements if( fread( m_pData,sizeof( type ),uSize,pDataFile ) != uSize ) { fclose( pDataFile ); Clear(); throw;// sError; } fclose( pDataFile ); } //***** R e m o v e template void vtkBESSArray::Remove( const UINT c_uFirst, const UINT c_uN ) { // assert no subarray defined containing data of the array assert( !m_yNLocks ); // assert valid range of array to be removed if( c_uFirst + c_uN > m_uSize ) throw;// CString( "Removing invalid range of array." ); // shift maintained array elements to the left to overwrite the removed elements UINT uNewIndex, uOldIndex; for( uNewIndex = c_uFirst, uOldIndex = c_uFirst + c_uN; uOldIndex < m_uSize; uNewIndex++, uOldIndex++ ) m_pData[uNewIndex] = m_pData[uOldIndex]; // update array size m_uSize -= c_uN; } //***** R e s e r v e template void vtkBESSArray::Reserve( const UINT c_uMem ) { m_uSize = 0; if( c_uMem > m_uMemSize ) { if( m_uMemSize ) delete[] m_pData; if( (m_pData = new type[m_uMemSize = c_uMem]) == NULL ) { m_uSize = m_uMemSize = 0; throw;// CString( "Insufficient memory for array." ); } } } //***** W r i t e template void vtkBESSArray::Write( const char* c_pcFilePath ) const { FILE *pDataFile; // open file if( !(pDataFile = fopen( c_pcFilePath,"wb" )) ) { throw; //sError; } // write array size if( fwrite( &m_uSize,sizeof( UINT ),1,pDataFile ) != 1 ) { fclose( pDataFile ); _unlink( c_pcFilePath ); throw; //sError; } // write array elements if( fwrite( m_pData,sizeof( type ),m_uSize,pDataFile ) != m_uSize ) { fclose( pDataFile ); _unlink( c_pcFilePath ); throw;// sError; } fclose( pDataFile ); } #endif