Back to index

salome-kernel  6.5.0
SALOMEDSImpl_AttributeTable.hxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 //  File   : SALOMEDSImpl_AttributeTable.hxx
00021 //  Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00022 //
00023 #ifndef _SALOMEDSImpl_AttributeTable_HeaderFile
00024 #define _SALOMEDSImpl_AttributeTable_HeaderFile
00025 
00026 #include "SALOMEDSImpl_Defines.hxx"
00027 
00028 class SALOMEDSIMPL_EXPORT SALOMEDSImpl_AttributeTable
00029 {
00030 public:
00032   typedef enum { 
00033     AscendingOrder,  
00034     DescendingOrder, 
00035   } SortOrder;
00036 
00038   typedef enum {
00039     EmptyLowest,     
00040     EmptyHighest,    
00041     EmptyFirst,      
00042     EmptyLast,       
00043     EmptyIgnore,     
00044   } SortPolicy;
00045 };
00046 
00047 template <class TTable> class TableSorter
00048 {
00049   TTable*                                 myTable;
00050   SALOMEDSImpl_AttributeTable::SortOrder  mySortOrder;
00051   SALOMEDSImpl_AttributeTable::SortPolicy mySortPolicy;
00052   int                                     myIndex;
00053   bool                                    myIsRow;
00054 
00055 public:
00056   TableSorter( TTable*                                 table,
00057               SALOMEDSImpl_AttributeTable::SortOrder  so,
00058               SALOMEDSImpl_AttributeTable::SortPolicy sp,
00059               int                                     index,
00060               bool                                    sortRow)
00061     : myTable( table ), mySortOrder( so ), mySortPolicy( sp ), 
00062       myIndex( index ), myIsRow( sortRow ) {}
00063 
00064   bool operator() ( int idx1, int idx2 ) 
00065   {
00066     bool hasValue1 = myIsRow ? myTable->HasValue( myIndex, idx1 ) : myTable->HasValue( idx1, myIndex );
00067     bool hasValue2 = myIsRow ? myTable->HasValue( myIndex, idx2 ) : myTable->HasValue( idx2, myIndex );
00068     if ( !hasValue1 && !hasValue2 ) {
00069       return false;
00070     }
00071     else if ( !hasValue1 || !hasValue2 ) {
00072       switch ( mySortPolicy ) {
00073       case SALOMEDSImpl_AttributeTable::EmptyLowest:
00074        return (!hasValue1) ? 
00075          (mySortOrder == SALOMEDSImpl_AttributeTable::AscendingOrder) : 
00076          (mySortOrder == SALOMEDSImpl_AttributeTable::DescendingOrder);
00077       case SALOMEDSImpl_AttributeTable::EmptyHighest:
00078        return (!hasValue1) ? 
00079          (mySortOrder != SALOMEDSImpl_AttributeTable::AscendingOrder) : 
00080          (mySortOrder != SALOMEDSImpl_AttributeTable::DescendingOrder);
00081       case SALOMEDSImpl_AttributeTable::EmptyFirst:
00082        return (!hasValue1);
00083       case SALOMEDSImpl_AttributeTable::EmptyLast:
00084        return hasValue1;
00085       case SALOMEDSImpl_AttributeTable::EmptyIgnore:
00086       default:
00087        // should not go here
00088        return false;
00089       }
00090     }
00091     else {
00092       if ( myIsRow ) {
00093        return mySortOrder == SALOMEDSImpl_AttributeTable::AscendingOrder ? 
00094          myTable->GetValue( myIndex, idx1 ) < myTable->GetValue( myIndex, idx2 ) :
00095          myTable->GetValue( myIndex, idx2 ) < myTable->GetValue( myIndex, idx1 );
00096       }
00097       else {
00098        return mySortOrder == SALOMEDSImpl_AttributeTable::AscendingOrder ? 
00099          myTable->GetValue( idx1, myIndex ) < myTable->GetValue( idx2, myIndex ) :
00100          myTable->GetValue( idx2, myIndex ) < myTable->GetValue( idx1, myIndex );
00101       }
00102     }
00103   }
00104 };
00105 
00106 #endif // _SALOMEDSImpl_AttributeTable_HeaderFile