Salome HOME
Copyrights update
[modules/med.git] / src / MEDMEM / MEDMEM_ModulusArray.hxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20 #ifndef __MEDMODULUSARRAY_H__
21 #define __MEDMODULUSARRAY_H__
22
23 #include "MEDMEM_Utilities.hxx"
24
25 /*
26   This class is use to set cyclic (modulus length) array.
27   (array[length+1]==array[1])
28
29   We use it in particular to compare faces nodes lists to know if
30   they are the same (positive or negative cycle) !
31 */
32
33 namespace MEDMEM {
34 class MEDMODULUSARRAY {
35 private:
36   int   _length ;
37   const int * _array ;
38
39 public:
40   MEDMODULUSARRAY(int length, const int * array) ;
41   ~MEDMODULUSARRAY() ;
42   
43   const int & operator[](const int &i) const ;
44
45   int compare(const MEDMODULUSARRAY &modulusArray) const;
46
47 };
48
49 MEDMODULUSARRAY::MEDMODULUSARRAY(int length, const int * array) : 
50   _length(length), _array(array)
51 {
52 //    SCRUTE(_length);
53 //    for (int i=0;i<_length;i++){
54 //      MESSAGE("MEDMODULUSARRAY["<<i<<"]="<<_array[i]);
55 //    }
56 };
57
58 MEDMODULUSARRAY::~MEDMODULUSARRAY()
59 {
60   // do nothing because nothing is allocated !
61   //  MESSAGE("MEDMODULUSARRAY::~MEDMODULUSARRAY()") ;
62 };
63
64
65 const int & MEDMODULUSARRAY::operator[](const int &i) const
66 {
67   int position = i%_length ;
68   if (position<0)
69     position+=_length ;
70   return _array[position] ;
71 };
72
73 int MEDMODULUSARRAY::compare(const MEDMODULUSARRAY &modulusArray) const
74 {
75   int ret = 0 ;
76
77   if (modulusArray._length != _length)
78     return ret ;
79
80   if (_length==1)
81     if (_array[0]==modulusArray[0])
82       return 1;
83     else 
84       return 0;
85
86   if (_length==2)
87     if ((_array[0]==modulusArray[0])&(_array[1]==modulusArray[1]))
88       return 1;
89     else if ((_array[0]==modulusArray[1])&(_array[1]==modulusArray[0]))
90       return -1;
91     else
92       return 0;
93
94   //search if there is one start point in common in two array
95   for(int i=0;i<_length;i++)
96     if ( _array[0] == modulusArray[i] ) {
97       // we search if cycle is the same
98       if (_array[1]==modulusArray[i+1]){ // positive order
99         ret=1;
100         for(int j=2;j<_length;j++)
101           if (_array[j]!=modulusArray[i+j]) {
102             ret = 0 ;
103             break ;
104           }
105       } else if(_array[1]==modulusArray[i-1]) { //negative order
106         ret=-1;
107         for(int j=2;j<_length;j++)
108           if (_array[j]!=modulusArray[i-j]) {
109             ret = 0 ;
110             break ;
111           }
112       }
113       if (ret!=0) {// we have found it !
114         break ;
115       }
116       // else we continue if there is another start point i
117     }
118     return ret ;
119 }
120 }
121
122 # endif         /* # ifndef __MEDMODULUSARRAY_H__ */
123