Salome HOME
Fix problem of make distcheck
[modules/med.git] / src / MEDMEM / MEDMEM_SkyLineArray.cxx
1 // Copyright (C) 2007-2012  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 #include "MEDMEM_SkyLineArray.hxx"
24 #include "MEDMEM_Utilities.hxx"
25 #include <map>
26
27 using namespace std;
28 using namespace MEDMEM;
29
30 MEDSKYLINEARRAY::MEDSKYLINEARRAY(): _count(0), _length(0),
31                                 _index((int*)NULL),_value((int*)NULL)
32 {
33   MESSAGE_MED("Constructeur MEDSKYLINEARRAY sans parametre");
34 }
35
36 MEDSKYLINEARRAY::MEDSKYLINEARRAY(const MEDSKYLINEARRAY &myArray):
37                                 _count(myArray._count),_length(myArray._length),
38                                 _index(_count+1),_value(_length)
39 {
40   const char* LOC = "MEDSKYLINEARRAY(const MEDSKYLINEARRAY &)";
41   BEGIN_OF_MED(LOC);
42   if ( myArray._index )
43     memcpy(_index,myArray._index,sizeof(int)*(_count+1));
44   if ( myArray._value )
45     memcpy(_value,myArray._value,sizeof(int)*_length);
46   END_OF_MED(LOC);
47 }
48
49 MEDSKYLINEARRAY::~MEDSKYLINEARRAY()
50 {
51   MESSAGE_MED("Destructeur ~MEDSKYLINEARRAY");
52
53   //if (_index != NULL) delete [] _index;
54   //if (_value != NULL) delete [] _value;
55 }
56
57 MEDSKYLINEARRAY::MEDSKYLINEARRAY(const int count, const int length):
58                                 _count(count), _length(length),
59                                 _index(_count+1),_value(_length)
60 {
61         MESSAGE_MED("Constructeur MEDSKYLINEARRAY(count="<<count<<", length="<<length<<") avec parametres");
62 }
63
64 MEDSKYLINEARRAY::MEDSKYLINEARRAY(const int count, const int length,
65                                  const int* index, const int* value,bool shallowCopy):
66                                 _count(count), _length(length)
67 {
68 //      MESSAGE_MED("Constructeur MEDSKYLINEARRAY(count="<<count<<", length="<<length<<") avec parametres");
69                 if(shallowCopy)
70           {
71             _index.setShallowAndOwnership(index);
72             _value.setShallowAndOwnership(value);
73           }
74         else
75           {
76             _index.set(_count+1,index);
77             _value.set(_length,value);
78           }
79 }
80
81 //creates the reverse array
82 //creates an array with count maxvalue, where maxvalue
83 //is the maximum of the value_ of the present array
84 //length is the same as present array
85 //
86 // For instance
87 // 1 : 2 4
88 // 2 : 1 2
89 //
90 //will give
91 // 1 : 2
92 // 2 : 1 2
93 // 3 :
94 // 4 : 1
95
96 MEDSKYLINEARRAY* MEDSKYLINEARRAY::makeReverseArray()
97 {
98         multimap<int,int > reverse;
99         int size=0;
100   for (int i=0; i<_count;i++)
101                 for (int j=_index[i];j<_index[i+1];j++)
102                         {
103                                 int value=_value[j-1];
104                                 reverse.insert(make_pair(value,i+1));
105                                 if (value>size) size=value;
106                         }
107         int* r_index=new int [size+1];
108         int* r_value=new int [_length];
109         r_index[0]=1;
110         pair<multimap<int,int>::iterator,multimap<int,int>::iterator>piter;
111         int* ptr_value=r_value;
112         for (int i=0; i<size;i++)
113                 {
114                         piter=reverse.equal_range(i);
115                         int index_incr=0;
116                         for (multimap<int,int>::iterator iter=piter.first; iter!=piter.second; iter++)
117                                 {
118                                         *ptr_value++=iter->second;
119                                         index_incr++;
120                                 }
121                         r_index[i+1]=r_index[i]+index_incr;
122                 }
123         return new MEDSKYLINEARRAY(size,_length,r_index,r_value,true);
124 }
125 ostream& MEDMEM::operator<<(ostream &os, const MEDSKYLINEARRAY &sky) {
126   os << "_count : " << sky._count << " ,_length : " << sky._length;
127   for (int i = 0; i < sky._count ; i++) {
128     os << endl << "Values of type n°" << i+1 << " (index["<<i<<"]="<< sky._index[i]<<") :" << endl;
129     for (int j=sky._index[i]-1;j < sky._index[i+1]-1;j++)
130       os << sky._value[j] << " " ;
131   }
132   return os;
133 }
134
135 //  void MEDSKYLINEARRAY::setMEDSKYLINEARRAY( const int count , const int length, int* index , int* value )
136 //  {
137 //    MESSAGE_MED("void MEDSKYLINEARRAY::setMEDSKYLINEARRAY(count, length, index, value)");
138 //    _count  = count  ;
139 //    _length = length ;
140
141 //    //if (_index != NULL) delete [] _index;
142 //    //if (_value != NULL) delete [] _value;
143
144 //        _index.set(index);
145 //        _value.set(value);
146 //  }