Salome HOME
Version ok de MED avec MEDGUI.
[modules/med.git] / src / MEDMEM / MEDMEM_ModulusArray.hxx
1 #ifndef __MEDMODULUSARRAY_H__
2 #define __MEDMODULUSARRAY_H__
3
4 #include "utilities.h"
5
6 /*
7   This class is use to set cyclic (modulus length) array.
8   (array[length+1]==array[1])
9
10   We use it in particular to compare faces nodes lists to know if
11   they are the same (positive or negative cycle) !
12 */
13
14
15 class MEDMODULUSARRAY {
16 private:
17   int   _length ;
18   int * _array ;
19
20 public:
21   MEDMODULUSARRAY(int length, int * array) ;
22   ~MEDMODULUSARRAY() ;
23   
24   int & operator[](const int &i) const ;
25
26   int compare(const MEDMODULUSARRAY &modulusArray) const;
27
28 };
29
30 MEDMODULUSARRAY::MEDMODULUSARRAY(int length, int * array) : 
31   _length(length), _array(array)
32 {
33   //  MESSAGE("MEDMODULUSARRAY::MEDMODULUSARRAY("<<length<<", "<<array<<")") ;
34 };
35
36 MEDMODULUSARRAY::~MEDMODULUSARRAY()
37 {
38   // do nothing because nothing is allocated !
39   //  MESSAGE("MEDMODULUSARRAY::~MEDMODULUSARRAY()") ;
40 };
41
42
43 int & MEDMODULUSARRAY::operator[](const int &i) const
44 {
45   int position = i%_length ;
46   if (position<0)
47     position+=_length ;
48   return _array[position] ;
49 };
50
51 int MEDMODULUSARRAY::compare(const MEDMODULUSARRAY &modulusArray) const
52 {
53   int ret = 0 ;
54
55   if (modulusArray._length != _length)
56     return ret ;
57
58   if (_length==1)
59     if (_array[0]==modulusArray[0])
60       return 1;
61     else 
62       return 0;
63
64   if (_length==2)
65     if ((_array[0]==modulusArray[0])&(_array[1]==modulusArray[1]))
66       return 1;
67     else if ((_array[0]==modulusArray[1])&(_array[1]==modulusArray[0]))
68       return -1;
69     else
70       return 0;
71
72   //search if there is one start point in common in two array
73   for(int i=0;i<_length;i++)
74     if ( _array[0] == modulusArray[i] ) {
75       // we search if cycle is the same
76       if (_array[1]==modulusArray[i+1]){ // positive order
77         ret=1;
78         for(int j=2;j<_length;j++)
79           if (_array[j]!=modulusArray[i+j]) {
80             ret = 0 ;
81             break ;
82           }
83       } else if(_array[1]==modulusArray[i-1]) { //negative order
84         ret=-1;
85         for(int j=2;j<_length;j++)
86           if (_array[j]!=modulusArray[i-j]) {
87             ret = 0 ;
88             break ;
89           }
90       }
91       if (ret!=0) {// we have found it !
92         break ;
93       }
94       // else we continue if there is another start point i
95     }
96     return ret ;
97 }
98
99 # endif         /* # ifndef __MEDMODULUSARRAY_H__ */
100