Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / src / MEDMEM / MEDMEM_SkyLineArray.hxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 # ifndef __MEDSKYLINEARRAY_H__
24 # define __MEDSKYLINEARRAY_H__
25
26 #include "MEDMEM.hxx"
27
28 #include "MEDMEM_Exception.hxx"
29
30 #include "MEDMEM_PointerOf.hxx"
31 #include "MEDMEM_define.hxx"
32
33 #include <cstring>
34
35 namespace MEDMEM {
36   class MEDSKYLINEARRAY;
37   MEDMEM_EXPORT ostream& operator<<(ostream &os, const MEDSKYLINEARRAY &sky);
38
39 class MEDMEM_EXPORT MEDSKYLINEARRAY
40 {
41 private :
42   int   _count ;
43   int   _length ;
44   PointerOf <int> _index ; // array of size _count+1 : _index[0]=1 and
45                            // _index[_count]=length+1
46   PointerOf <int> _value ; // array of size _length
47
48 public :
49   // Attention, avec ce constructeur, il n'est possible de remplir le MEDSKYLINEARRAY 
50   MEDSKYLINEARRAY();
51
52   // Constructeur par recopie
53   MEDSKYLINEARRAY( const MEDSKYLINEARRAY &myArray );
54
55   // Avec ce constructeur la mémoire pour le tableau  de valeur et le
56   // tableau d'index est réservée. Il suffit d'effectuer les séquences
57   // d'appels suivantes pour initialiser le MEDSKYLINEARRAY
58   // 1) setIndex(index) puis <count> fois setI(i,&listValeurN°I) avec i dans 1..count
59   //    rem :   listValeurN°I est dupliquée
60   // 2) appeler <length> fois setIJ(i,j,valeur) avec i dans 1..count et avec j dans 1..count
61   MEDSKYLINEARRAY( const int count, const int length );
62
63   // Avec ce constructeur le MEDSKYLINEARRAY est complètement initialisé
64   // Si shallowCopy=false (par défaut) les tableaux d'index et de valeurs
65   // sont dupliqués
66   // Sinon le MEDSKYLINEARRAY prend directement les pointeurs et en devient 
67   // propriétaire
68   MEDSKYLINEARRAY( const int count, const int length,
69                    const int* index, const int* value, bool shallowCopy=false );
70
71   ~MEDSKYLINEARRAY();
72   //void setMEDSKYLINEARRAY( const int count, const int length, int* index , int* value ) ;
73
74   inline int  getNumberOf()       const;
75   inline int  getLength()         const;
76   inline const int*  getIndex()   const;
77   inline const int*  getValue()   const;
78   inline int  getNumberOfI(int i) const throw (MEDEXCEPTION) ;
79   inline const int*  getI(int i)  const throw (MEDEXCEPTION) ;
80   inline int  getIJ(int i, int j) const throw (MEDEXCEPTION) ;
81   inline int  getIndexValue(int i) const throw (MEDEXCEPTION) ;
82
83   inline void setIndex(const int* index) ;
84   inline void setI(const int i, const int* values) throw (MEDEXCEPTION) ;
85   inline void setIJ(int i, int j, int value) throw (MEDEXCEPTION) ;
86   inline void setIndexValue(int i, int value) throw (MEDEXCEPTION) ;
87
88   friend ostream& operator<<(ostream &os, const MEDSKYLINEARRAY &sky);
89         MEDSKYLINEARRAY* makeReverseArray();
90
91 };
92
93 // ---------------------------------------
94 //              Methodes Inline
95 // ---------------------------------------
96 inline int MEDSKYLINEARRAY::getNumberOf() const
97 {
98   return _count ;
99 }
100 inline int MEDSKYLINEARRAY::getLength() const
101 {
102   return _length ;
103 }
104 inline const int*  MEDSKYLINEARRAY::getIndex() const
105 {
106   return (const int*)_index ;
107
108 inline const int*  MEDSKYLINEARRAY::getValue() const
109 {
110   return (const int*)_value ;
111
112 inline int MEDSKYLINEARRAY::getNumberOfI(int i) const throw (MEDEXCEPTION)
113 {
114   if (i<1)
115     throw MEDEXCEPTION("MEDSKYLINEARRAY::getNumberOfI : argument must be >= 1");
116   if (i>_count)
117     throw MEDEXCEPTION("MEDSKYLINEARRAY::getNumberOfI : argument is out of range");
118   return _index[i]-_index[i-1] ;
119
120 inline const int* MEDSKYLINEARRAY::getI(int i) const throw (MEDEXCEPTION)
121 {
122     if (i<1)
123       throw MEDEXCEPTION("MEDSKYLINEARRAY::getI : argument must be >= 1");
124     if (i>_count)
125       throw MEDEXCEPTION("MEDSKYLINEARRAY::getI : argument is out of range");
126     return _value+_index[i-1]-1 ;
127 }
128 inline int MEDSKYLINEARRAY::getIJ(int i, int j) const throw (MEDEXCEPTION)
129 {
130     if (i<1)
131       throw MEDEXCEPTION("MEDSKYLINEARRAY::getIJ : first argument must be >= 1");
132     if (j<1)
133       throw MEDEXCEPTION("MEDSKYLINEARRAY::getIJ : second argument must be >= 1");
134     if (i>_count)
135       throw MEDEXCEPTION("MEDSKYLINEARRAY::getIJ : first argument is out of range") ;
136     if (j>_index[i])
137       throw MEDEXCEPTION("MEDSKYLINEARRAY::getIJ : second argument is out of range") ;
138     return _value[_index[i-1]+j-2] ;
139 }
140
141 inline int  MEDSKYLINEARRAY::getIndexValue(int i) const throw (MEDEXCEPTION)
142 {
143   if (i<1)
144     throw MEDEXCEPTION("MEDSKYLINEARRAY::getIndexValue : argument must be >= 1");
145   if (i>_index[_count])
146     throw MEDEXCEPTION("MEDSKYLINEARRAY::getIndexValue : argument is out of range") ;
147   return _value[i-1] ;
148 }
149
150 inline void MEDSKYLINEARRAY::setIndex(const int* index)
151 {
152   memcpy((int*)_index,index,(_count+1)*sizeof(int));
153 }
154
155
156 inline void MEDSKYLINEARRAY::setIJ(int i, int j, int value) throw (MEDEXCEPTION)
157 {
158   if (i<1)
159     throw MEDEXCEPTION("MEDSKYLINEARRAY::setIJ : first argument must be >= 1");
160   if (j<1)
161     throw MEDEXCEPTION("MEDSKYLINEARRAY::setIJ : second argument must be >= 1");
162   if (i>_count)
163     throw MEDEXCEPTION("MEDSKYLINEARRAY::setIJ : first argument is out of range") ;
164   if (j>_index[i])
165     throw MEDEXCEPTION("MEDSKYLINEARRAY::setIJ : second argument is out of range") ;
166   
167   _value[_index[i-1]+j-2]=value ;
168
169 }
170
171 inline void MEDSKYLINEARRAY::setI(const int i, const int * values) throw (MEDEXCEPTION)
172 {
173   if (i<1)
174     throw MEDEXCEPTION("MEDSKYLINEARRAY::setI : index must be >= 1");
175 ;
176   if (i>_count)
177     throw MEDEXCEPTION("MEDSKYLINEARRAY::setI : index is out of range") ;
178
179   memcpy(_value+_index[i-1]-1,values,(_index[i]-_index[i-1])*sizeof(int)) ;
180 }
181
182 inline void MEDSKYLINEARRAY::setIndexValue(int i, int value) throw (MEDEXCEPTION)
183 {
184   if (i<1)
185     throw MEDEXCEPTION("MEDSKYLINEARRAY::setIndexValue : argument must be >= 1");
186   if (i>_index[_count])
187     throw MEDEXCEPTION("MEDSKYLINEARRAY::setIndexValue : argument is out of range") ;
188   _value[i-1]=value ;
189 }
190 }
191 # endif