Salome HOME
updated copyright message
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeTable.hxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  File   : SALOMEDSImpl_AttributeTable.hxx
21 //  Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
22 //
23 #ifndef _SALOMEDSImpl_AttributeTable_HeaderFile
24 #define _SALOMEDSImpl_AttributeTable_HeaderFile
25
26 #include "SALOMEDSImpl_Defines.hxx"
27
28 class SALOMEDSIMPL_EXPORT SALOMEDSImpl_AttributeTable
29 {
30 public:
31   //! Sort order
32   typedef enum { 
33     AscendingOrder,  //!< The items are sorted ascending
34     DescendingOrder, //!< The items are sorted descending
35   } SortOrder;
36
37   //! Sort policy (specifies how empty cells are taken into account when sorting)
38   typedef enum {
39     EmptyLowest,     //!< Empty cells are considered as lowest values
40     EmptyHighest,    //!< Empty cells are considered as highest values
41     EmptyFirst,      //!< Empty cells are always first
42     EmptyLast,       //!< Empty cells are always last
43     EmptyIgnore,     //!< Empty cells are ignored (stay at initial positions)
44   } SortPolicy;
45 };
46
47 template <class TTable> class TableSorter
48 {
49   TTable*                                 myTable;
50   SALOMEDSImpl_AttributeTable::SortOrder  mySortOrder;
51   SALOMEDSImpl_AttributeTable::SortPolicy mySortPolicy;
52   int                                     myIndex;
53   bool                                    myIsRow;
54
55 public:
56   TableSorter( TTable*                                 table,
57                SALOMEDSImpl_AttributeTable::SortOrder  so,
58                SALOMEDSImpl_AttributeTable::SortPolicy sp,
59                int                                     index,
60                bool                                    sortRow)
61     : myTable( table ), mySortOrder( so ), mySortPolicy( sp ), 
62       myIndex( index ), myIsRow( sortRow ) {}
63
64   bool operator() ( int idx1, int idx2 ) 
65   {
66     bool hasValue1 = myIsRow ? myTable->HasValue( myIndex, idx1 ) : myTable->HasValue( idx1, myIndex );
67     bool hasValue2 = myIsRow ? myTable->HasValue( myIndex, idx2 ) : myTable->HasValue( idx2, myIndex );
68     if ( !hasValue1 && !hasValue2 ) {
69       return false;
70     }
71     else if ( !hasValue1 || !hasValue2 ) {
72       switch ( mySortPolicy ) {
73       case SALOMEDSImpl_AttributeTable::EmptyLowest:
74         return (!hasValue1) ? 
75           (mySortOrder == SALOMEDSImpl_AttributeTable::AscendingOrder) : 
76           (mySortOrder == SALOMEDSImpl_AttributeTable::DescendingOrder);
77       case SALOMEDSImpl_AttributeTable::EmptyHighest:
78         return (!hasValue1) ? 
79           (mySortOrder != SALOMEDSImpl_AttributeTable::AscendingOrder) : 
80           (mySortOrder != SALOMEDSImpl_AttributeTable::DescendingOrder);
81       case SALOMEDSImpl_AttributeTable::EmptyFirst:
82         return (!hasValue1);
83       case SALOMEDSImpl_AttributeTable::EmptyLast:
84         return hasValue1;
85       case SALOMEDSImpl_AttributeTable::EmptyIgnore:
86       default:
87         // should not go here
88         return false;
89       }
90     }
91     else {
92       if ( myIsRow ) {
93         return mySortOrder == SALOMEDSImpl_AttributeTable::AscendingOrder ? 
94           myTable->GetValue( myIndex, idx1 ) < myTable->GetValue( myIndex, idx2 ) :
95           myTable->GetValue( myIndex, idx2 ) < myTable->GetValue( myIndex, idx1 );
96       }
97       else {
98         return mySortOrder == SALOMEDSImpl_AttributeTable::AscendingOrder ? 
99           myTable->GetValue( idx1, myIndex ) < myTable->GetValue( idx2, myIndex ) :
100           myTable->GetValue( idx2, myIndex ) < myTable->GetValue( idx1, myIndex );
101       }
102     }
103   }
104 };
105
106 #endif // _SALOMEDSImpl_AttributeTable_HeaderFile