Salome HOME
dcbb24a4cc819b777a79f0bd44c3869b7309674e
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingSkyLineArray.cxx
1 // Copyright (C) 2007-2019  CEA/DEN, EDF R&D
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 #include "MEDCouplingSkyLineArray.hxx"
21
22 #include <sstream>
23 #include <deque>
24 #include <set>
25
26 using namespace MEDCoupling;
27
28 MEDCouplingSkyLineArray::MEDCouplingSkyLineArray():
29   _super_index( DataArrayIdType::New() ), _index( DataArrayIdType::New() ), _values( DataArrayIdType::New() )
30 {
31 }
32
33 MEDCouplingSkyLineArray::~MEDCouplingSkyLineArray()
34 {
35 }
36
37 MEDCouplingSkyLineArray* MEDCouplingSkyLineArray::New()
38 {
39   return new MEDCouplingSkyLineArray();
40 }
41
42 MEDCouplingSkyLineArray* MEDCouplingSkyLineArray::New( const std::vector<mcIdType>& index,
43                                                        const std::vector<mcIdType>& value )
44 {
45   MEDCouplingSkyLineArray * ret = new MEDCouplingSkyLineArray();
46   ret->_index->reserve( index.size() );
47   ret->_index->insertAtTheEnd( index.begin(), index.end() );
48   ret->_values->reserve( value.size() );
49   ret->_values->insertAtTheEnd( value.begin(), value.end() );
50   return ret;
51 }
52
53 MEDCouplingSkyLineArray* MEDCouplingSkyLineArray::New( DataArrayIdType* index, DataArrayIdType* value )
54 {
55   MEDCouplingSkyLineArray* ret = new MEDCouplingSkyLineArray();
56   ret->set(index, value);
57   return ret;
58 }
59
60 MEDCouplingSkyLineArray* MEDCouplingSkyLineArray::New( const MEDCouplingSkyLineArray & other )
61 {
62   MEDCouplingSkyLineArray* ret = new MEDCouplingSkyLineArray();
63   ret->_super_index = other._super_index;
64   ret->_index = other._index;
65   ret->_values = other._values;
66   return ret;
67 }
68
69 /**! Build a three level SkyLine array from the dynamic connectivity of a dynamic mesh (i.e. containing only
70  * polyhedrons or polygons).
71  * The input arrays are deep copied, contrary to the other ctors.
72  */
73 MEDCouplingSkyLineArray * MEDCouplingSkyLineArray::BuildFromPolyhedronConn( const DataArrayIdType* c, const DataArrayIdType* cI )
74 {
75   using namespace std;
76
77   MEDCouplingSkyLineArray* ret = new MEDCouplingSkyLineArray();
78
79   const mcIdType * cP(c->begin()), * cIP(cI->begin());
80   mcIdType prev = -1;
81   if (c->getNbOfElems() != *(cI->end()-1))
82     throw INTERP_KERNEL::Exception("MEDCouplingSkyLineArray::BuildFromDynamicConn: misformatted connectivity (wrong nb of tuples)!");
83   for (mcIdType i=0; i < cI->getNbOfElems(); i++)
84     {
85       mcIdType j = cIP[i];
86       if (cIP[i] < prev)
87         throw INTERP_KERNEL::Exception("MEDCouplingSkyLineArray::BuildFromDynamicConn: misformatted connectivity (indices not monotonic ascending)!");
88       prev = cIP[i];
89       if (i!=cI->getNbOfElems()-1)
90         if (cP[j] != INTERP_KERNEL::NORM_POLYHED)
91           throw INTERP_KERNEL::Exception("MEDCouplingSkyLineArray::BuildFromDynamicConn: connectivity containing other types than POLYHED!");
92     }
93
94   vector<mcIdType> superIdx, idx, vals;
95   mcIdType cnt = 0, cnt2 = 0;
96   superIdx.reserve(cI->getNbOfElems());
97   superIdx.push_back(0);
98   idx.push_back(0);
99   vals.resize(c->getNbOfElems()); // too much because of the type and the -1, but still better than push_back().
100   for (mcIdType i=0; i < cI->getNbOfElems()-1; i++)
101     {
102       mcIdType start = cIP[i]+1, end = cIP[i+1];
103       mcIdType * work = vals.data() + cnt;
104       const mcIdType * w = cP+start;
105       const mcIdType * w2 = find(w, cP+end, -1);
106       while (w2 != cP+end)
107         {
108           copy(w, w2, work);
109           mcIdType d = ToIdType(distance(w, w2));
110           cnt += d; work +=d;
111           idx.push_back(cnt); cnt2++;
112           w = w2+1;  // skip the -1
113           w2 = find(w, cP+end, -1);
114         }
115       copy(w, cP+end, work);
116       cnt += ToIdType(distance(w, cP+end));
117       idx.push_back(cnt); cnt2++;
118       superIdx.push_back(cnt2);
119     }
120   ret->_super_index->alloc(superIdx.size(),1);
121   copy(superIdx.begin(), superIdx.end(), ret->_super_index->getPointer());
122   ret->_index->alloc(idx.size(),1);
123   copy(idx.begin(), idx.end(), ret->_index->getPointer());
124   ret->_values->alloc(cnt,1);
125   copy(vals.begin(), vals.begin()+cnt, ret->_values->getPointer());
126
127   return ret;
128 }
129
130 /**
131  * Convert a three-level SkyLineArray into a polyhedral connectivity.
132  * The super-packs are interpreted as cell description, and the packs represent the face connectivity.
133  */
134 void MEDCouplingSkyLineArray::convertToPolyhedronConn( MCAuto<DataArrayIdType>& c,  MCAuto<DataArrayIdType>& cI) const
135 {
136   // TODO: in this case an iterator would be nice
137   using namespace std;
138
139   checkSuperIndex("convertToPolyhedronConn");
140
141   const mcIdType * siP(_super_index->begin()), * iP(_index->begin()), *vP(_values->begin());
142   mcIdType cnt = 0;
143   cI->alloc(_super_index->getNbOfElems(),1);  // same number of super packs as number of cells
144   mcIdType * cIVecP(cI->getPointer());
145   MCAuto <DataArrayIdType> dsi = _index->deltaShiftIndex();
146   mcIdType sz = dsi->accumulate((std::size_t)0) + ToIdType(dsi->getNbOfElems());  // think about it: one slot for the type, -1 at the end of each face of the cell
147   c->alloc(sz, 1);
148   mcIdType * cVecP(c->getPointer());
149
150   for ( mcIdType i=0; i < _super_index->getNbOfElems()-1; i++)
151      {
152        cIVecP[i]= cnt;
153        mcIdType endId = siP[i+1];
154        cVecP[cnt++] = INTERP_KERNEL::NORM_POLYHED;
155        for (mcIdType j=siP[i]; j < endId; j++)
156          {
157            mcIdType startId2 = iP[j], endId2 = iP[j+1];
158            copy(vP+startId2, vP+endId2, cVecP+cnt);
159            cnt += endId2-startId2;
160            if(j != endId-1)
161              cVecP[cnt++] = -1;
162          }
163      }
164   cIVecP[_super_index->getNbOfElems()-1] = cnt;
165 }
166
167 std::size_t MEDCouplingSkyLineArray::getHeapMemorySizeWithoutChildren() const
168 {
169   return _index->getHeapMemorySizeWithoutChildren()+_values->getHeapMemorySizeWithoutChildren()+_super_index->getHeapMemorySizeWithoutChildren();
170 }
171
172 std::vector<const BigMemoryObject *> MEDCouplingSkyLineArray::getDirectChildrenWithNull() const
173 {
174   std::vector<const BigMemoryObject *> ret;
175   ret.push_back(_super_index);
176   ret.push_back(_index);
177   ret.push_back(_values);
178   return ret;
179 }
180
181
182 void MEDCouplingSkyLineArray::set( DataArrayIdType* index, DataArrayIdType* value )
183 {
184   _index=index;
185   _values=value;
186   if ( (DataArrayIdType*)_index ) _index->incrRef();
187   else                            _index = DataArrayIdType::New();
188   if ( (DataArrayIdType*)_values ) _values->incrRef();
189   else                             _values = DataArrayIdType::New();
190 }
191
192 void MEDCouplingSkyLineArray::set3( DataArrayIdType* superIndex, DataArrayIdType* index, DataArrayIdType* value )
193 {
194   _super_index=superIndex;
195   if ( (DataArrayIdType*)_super_index ) _super_index->incrRef();
196   else                                  _super_index = DataArrayIdType::New();
197   set(index, value);
198 }
199
200 DataArrayIdType* MEDCouplingSkyLineArray::getSuperIndexArray() const
201 {
202   return const_cast<MEDCouplingSkyLineArray*>(this)->_super_index;
203 }
204
205
206 DataArrayIdType* MEDCouplingSkyLineArray::getIndexArray() const
207 {
208   return const_cast<MEDCouplingSkyLineArray*>(this)->_index;
209 }
210
211 DataArrayIdType* MEDCouplingSkyLineArray::getValuesArray() const
212 {
213   return const_cast<MEDCouplingSkyLineArray*>(this)->_values;
214 }
215
216 void MEDCouplingSkyLineArray::checkSuperIndex(const std::string& func) const
217 {
218   if (!_super_index->getNbOfElems())
219     {
220       std::ostringstream oss;
221       oss << "MEDCouplingSkyLineArray::"<< func << ": not a three level SkyLineArray! Method is not available for two-level SkyLineArray.";
222       throw INTERP_KERNEL::Exception(oss.str());
223     }
224 }
225
226 void MEDCouplingSkyLineArray::validSuperIndex(const std::string& func, mcIdType superIndex) const
227 {
228   if(superIndex < 0 || superIndex >= _super_index->getNbOfElems())
229     {
230       std::ostringstream oss;
231       oss << "MEDCouplingSkyLineArray::" << func <<  ": invalid super index!";
232       throw INTERP_KERNEL::Exception(oss.str());
233     }
234 }
235
236 void MEDCouplingSkyLineArray::validIndex(const std::string& func, mcIdType idx) const
237 {
238   if(idx < 0 || idx >= _index->getNbOfElems())
239     {
240       std::ostringstream oss;
241       oss << "MEDCouplingSkyLineArray::" << func <<  ": invalid index!";
242       throw INTERP_KERNEL::Exception(oss.str());
243     }
244 }
245
246 void MEDCouplingSkyLineArray::validSuperIndexAndIndex(const std::string& func, mcIdType superIndex, mcIdType index) const
247 {
248   validSuperIndex(func, superIndex);
249   mcIdType idx = _super_index->begin()[superIndex] + index;
250   if(idx < 0 || idx >= _index->getNbOfElems())
251     {
252       std::ostringstream oss;
253       oss << "MEDCouplingSkyLineArray::" << func <<  ": invalid index!";
254       throw INTERP_KERNEL::Exception(oss.str());
255     }
256 }
257
258 std::string MEDCouplingSkyLineArray::simpleRepr() const
259 {
260   std::ostringstream oss;
261   oss << "MEDCouplingSkyLineArray (" << this << ")" << std::endl;
262   MCAuto<DataArrayIdType> super_index = _super_index->deepCopy();
263   if (_super_index->getNbOfElems())
264     oss << "   Nb of super-packs: " << getSuperNumberOf() << std::endl;
265   else
266     {
267       super_index->alloc(2,1);
268       super_index->setIJSilent(0,0,0);
269       super_index->setIJSilent(1,0,_index->getNbOfElems()-1);
270     }
271   oss << "   Nb of packs: " << getNumberOf() << std::endl;
272   oss << "   Nb of values: " << getLength() << std::endl;
273
274   if (_super_index->getNbOfElems())
275     {
276       oss << "   Super-indices:" << std::endl;
277       oss << "   ";
278       const mcIdType * i = _super_index->begin();
279       for ( ; i != _super_index->end(); ++i )
280         oss << *i << " ";
281       oss << std::endl;
282     }
283
284   oss << "   Indices:" << std::endl;
285   oss << "   ";
286   const mcIdType * i = _index->begin();
287   for ( ; i != _index->end(); ++i )
288     oss << *i << " ";
289   oss << std::endl;
290   oss << "   Values:" << std::endl;
291   oss << "     ";
292   const mcIdType * v = _values->begin();
293   mcIdType cnt = 0, cntI = 0;
294   i = _index->begin();
295   for ( const mcIdType * si = super_index->begin()+1; v != _values->end(); ++v, ++cnt )
296     {
297       if ( cnt == *i )
298         {
299           if ( cntI == *si && cnt != 0)
300             {
301               oss << std::endl << "     ";
302               ++si;
303             }
304
305           oss << "| ";
306           ++i; ++cntI;
307         }
308       oss << *v << " ";
309     }
310   oss << std::endl;
311
312   return oss.str();
313 }
314
315 /**
316  * For a 2- or 3-level SkyLine array, return a copy of the absolute pack with given identifier.
317  */
318 void MEDCouplingSkyLineArray::getSimplePackSafe(const mcIdType absolutePackId, std::vector<mcIdType> & pack) const
319 {
320   if(absolutePackId < 0 || absolutePackId >= _index->getNbOfElems())
321     throw INTERP_KERNEL::Exception("MEDCouplingSkyLineArray::getPackSafe: invalid index!");
322   const mcIdType * iP(_index->begin()), *vP(_values->begin());
323   mcIdType sz = iP[absolutePackId+1]-iP[absolutePackId];
324   pack.resize(sz);
325   std::copy(vP+iP[absolutePackId], vP+iP[absolutePackId+1],pack.begin());
326 }
327
328 /**
329  * Same as getPackSafe, but directly returns a pointer to the internal data with the size of the pack.
330  */
331 const mcIdType * MEDCouplingSkyLineArray::getSimplePackSafePtr(const mcIdType absolutePackId, mcIdType & packSize) const
332 {
333   if(absolutePackId < 0 || absolutePackId >= _index->getNbOfElems())
334     throw INTERP_KERNEL::Exception("MEDCouplingSkyLineArray::getPackSafe: invalid index!");
335   const mcIdType * iP(_index->begin()), *vP(_values->begin());
336   packSize = iP[absolutePackId+1]-iP[absolutePackId];
337   return vP+iP[absolutePackId];
338 }
339
340
341 /**!
342  * For each given super-pack ID, provide the sub-index of the first matching pack. If no matching pack is found for the
343  * given super-pack -1 is returned.
344  * \param[in] superPackIndices the list of super-packs that should be inspected
345  * \param[in] packBg the pack that the function is looking for in each of the provided super-pack
346  * \param[in] packEnd the pack that the function is looking for in each of the provided super-pack
347  * \param[out] a vector of mcIdType, having the same size as superPackIndices and containing for each inspected super-pack
348  * the index of the first matching pack, or -1 if none found.
349  */
350 void MEDCouplingSkyLineArray::findPackIds(const std::vector<mcIdType> & superPackIndices,
351                                           const mcIdType *packBg, const mcIdType *packEnd,
352                                           std::vector<mcIdType>& out) const
353 {
354   using namespace std;
355
356   checkSuperIndex("findPackIds");
357
358   mcIdType packSz = ToIdType(std::distance(packBg, packEnd));
359   if (!packSz)
360     throw INTERP_KERNEL::Exception("MEDCouplingSkyLineArray::findPackIds: void pack!");
361
362   out.resize(superPackIndices.size());
363   mcIdType i = 0;
364   const mcIdType * siP(_super_index->begin()), * iP(_index->begin()), *vP(_values->begin());
365   for(vector<mcIdType>::const_iterator it=superPackIndices.begin(); it!=superPackIndices.end(); ++it, i++)
366     {
367       out[i] = -1;
368       const mcIdType sPackIdx = *it;
369       // for each pack
370       for (mcIdType idx=siP[sPackIdx], j=0; idx < siP[sPackIdx+1]; idx++, j++)
371         {
372           if (packSz == (iP[idx+1] - iP[idx]))
373             if (equal(&vP[iP[idx]], &vP[iP[idx+1]], packBg))
374               {
375                 out[i] = j;
376                 break;
377               }
378         }
379     }
380 }
381
382 /**!
383  * Delete pack number 'idx' in super-pack number 'superIdx'.
384  * \param[in] superIdx is the super-pack number
385  * \param[in] idx is the pack index inside the super-pack 'superIdx'.
386  */
387 void MEDCouplingSkyLineArray::deletePack(const mcIdType superIdx, const mcIdType idx)
388 {
389   checkSuperIndex("deletePack");
390   validSuperIndexAndIndex("deletePack", superIdx, idx);
391
392   mcIdType * vP = _values->getPointer();
393   mcIdType * siP(_super_index->getPointer()), *iP(_index->getPointer());
394   const mcIdType start = iP[siP[superIdx]+idx], end = iP[siP[superIdx]+idx+1];
395   // _values
396   std::copy(vP+end, vP+_values->getNbOfElems(), vP+start);
397   _values->reAlloc(_values->getNbOfElems() - (end-start));
398
399   // _index
400   mcIdType nt = _index->getNbOfElems();
401   std::copy(iP+siP[superIdx]+idx+1, iP+nt, iP+siP[superIdx]+idx);
402   _index->reAlloc(nt-1); iP = _index->getPointer();  // better not forget this ...
403   for(mcIdType ii = siP[superIdx]+idx; ii < nt-1; ii++)
404     iP[ii] -= (end-start);
405
406   // _super_index
407   for(mcIdType ii = superIdx+1; ii < _super_index->getNbOfElems(); ii++)
408     (siP[ii])--;
409 }
410
411 void MEDCouplingSkyLineArray::deleteSimplePack(const mcIdType idx)
412 {
413   validIndex("deleteSimplePack", idx);
414   
415   mcIdType* iP(_index->getPointer());
416   const mcIdType start(iP[idx]), end(iP[idx+1]);
417
418   // _values
419   mcIdType initValSz=_values->getNbOfElems();
420   mcIdType deltaSz( start-end );  // should be negative
421   mcIdType *vP(_values->getPointer());
422   if (deltaSz < 0)
423     {
424       std::copy(vP+end, vP+initValSz, vP+start);
425       _values->reAlloc(initValSz+deltaSz);
426     }
427   else
428     throw INTERP_KERNEL::Exception("MEDCouplingSkyLineArray::deleteSimplePack");
429   // _index
430   mcIdType nt=_index->getNbOfElems();
431   std::copy(iP+idx+1, iP+nt, iP+idx);
432   for(mcIdType ii = idx; ii < nt-1; ii++)
433     iP[ii] += deltaSz;
434   _index->reAlloc(nt-1);
435 }
436
437 void MEDCouplingSkyLineArray::replaceSimplePacks(const DataArrayIdType* idx, const std::vector<const DataArrayIdType*>& packs)
438 {    
439   if (idx->empty())
440     return;
441     
442   for (const mcIdType * id = idx->begin(); id != idx->end(); id++)
443     validIndex("deleteSimplePacks", *id);
444     
445   if (idx->getNbOfElems() != ToIdType( packs.size()))
446     throw INTERP_KERNEL::Exception("MEDCouplingSkyLineArray::deleteSimplePacks: size of list of pack is incorrect");
447     
448   // copy _index, _values into a deque<set<mcIdType>>
449   std::deque< std::set<mcIdType> > valuesByIdx;
450   mcIdType* vP(_values->getPointer());
451   mcIdType* iP(_index->getPointer());
452   mcIdType nt = _index->getNbOfElems();
453   for (mcIdType ii = 0; ii < nt-1; ii++)
454     valuesByIdx.push_back(std::set<mcIdType>(vP+iP[ii], vP+iP[ii+1]));
455     
456   // modify the deque<set<mcIdType>> according to idx and packs
457   mcIdType ii(0);
458   for (const mcIdType *id = idx->begin(); id != idx->end(); id++)
459     {
460       valuesByIdx[*id] = std::set<mcIdType>(packs[ii]->begin(), packs[ii]->end());
461       ii++;
462     }
463   // copy back the deque<set<mcIdType>> into _index, _values
464   mcIdType valSz(0);
465   *iP = 0;
466   for (std::deque< std::set<mcIdType> >::const_iterator values=valuesByIdx.begin();values!=valuesByIdx.end();values++)
467     {
468       valSz += ToIdType((*values).size());
469       *(++iP) = valSz;
470     }
471   _values->reAlloc(valSz);
472   iP = _index->getPointer();
473   vP = _values->getPointer();
474   for (auto values : valuesByIdx)
475     {
476       std::copy(values.begin(), values.end(), vP+(*iP));
477       iP++;
478     }
479 }
480
481 void MEDCouplingSkyLineArray::deleteSimplePacks(const DataArrayIdType* idx)
482 {    
483   for (auto id = idx->begin(); id != idx->end(); id++)
484     validIndex("deleteSimplePacks", *id);
485   
486   std::set<mcIdType> packsToDelete(idx->begin(), idx->end());
487     
488   // _values
489   mcIdType* iP(_index->getPointer());
490   mcIdType initValSz = _values->getNbOfElems();
491   mcIdType *vP(_values->getPointer());
492   mcIdType end_prec(0),start_prec(0);
493   for(std::set<mcIdType>::const_iterator ii=packsToDelete.begin();ii!=packsToDelete.end();ii++)
494     {
495       mcIdType start = iP[*ii];
496       if (end_prec != 0)
497         std::copy(vP+end_prec, vP+start, vP+start_prec);
498       start_prec += start-end_prec;
499       end_prec = iP[*ii+1];
500     }
501   if (end_prec != 0)
502     std::copy(vP+end_prec, vP+initValSz, vP+start_prec);
503   _values->reAlloc(initValSz-(end_prec-start_prec));
504     
505   // _index
506   mcIdType nt = _index->getNbOfElems();
507   mcIdType offset = 0;
508   end_prec = 0;
509   start_prec = 0;
510   mcIdType deleted = 0;
511   for(std::set<mcIdType>::const_iterator ii=packsToDelete.begin();ii!=packsToDelete.end();ii++)
512     {
513       if (end_prec != 0)
514         {
515           std::copy(iP+end_prec, iP+*ii, iP+start_prec);
516           for (mcIdType i=start_prec; i<*ii; i++)
517             iP[i] -= offset;
518         }
519       offset += iP[*ii+1] - iP[*ii];
520       start_prec = *ii-deleted;
521       end_prec = *ii+1;
522       deleted += 1;
523     }
524   if (end_prec != 0)
525     {
526       std::copy(iP+end_prec, iP+nt, iP+start_prec);
527       for (mcIdType i=start_prec; i<nt; i++)
528         iP[i] -= offset;
529     }
530   _index->reAlloc(nt-deleted);
531 }
532
533 /**!
534  * Insert a new pack in super-pack at index 'superIdx'. The pack is inserted at the end of the pack list of the chosen super-pack.
535  */
536 void MEDCouplingSkyLineArray::pushBackPack(const mcIdType superIdx, const mcIdType * packBg, const mcIdType * packEnd)
537 {
538   using namespace std;
539
540   checkSuperIndex("pushBackPack");
541   validSuperIndex("pushBackPack", superIdx);
542
543   mcIdType *siP(_super_index->getPointer()), *iP(_index->getPointer());
544   const mcIdType sz(ToIdType(distance(packBg, packEnd)));
545
546   // _values
547   _values->reAlloc(_values->getNbOfElems()+sz);
548   mcIdType * vPE(_values->getPointer()+_values->getNbOfElems());
549   mcIdType *vP(_values->getPointer());
550   copy(vP+iP[siP[superIdx+1]], vPE-sz, vP+iP[siP[superIdx+1]]+sz);
551   // insert pack
552   copy(packBg, packEnd, vP+iP[siP[superIdx+1]]);
553
554   // _index
555   mcIdType nt = ToIdType(_index->getNbOfElems());
556   _index->reAlloc(nt+1); iP = _index->getPointer();
557   copy(iP+siP[superIdx+1]+1, iP+nt, iP+siP[superIdx+1]+2);
558   iP[siP[superIdx+1]+1] = iP[siP[superIdx+1]] + sz;
559   for(mcIdType ii = siP[superIdx+1]+2; ii < nt+1; ii++)
560     iP[ii] += sz;
561
562   // _super_index
563   for(mcIdType ii = superIdx+1; ii < _super_index->getNbOfElems(); ii++)
564     (siP[ii])++;
565 }
566
567 /**
568  * Replace pack with absolute index 'idx' with the provided new pack. Function can be used either
569  * for 2-level SkyLine or 3-level SkyLine.
570  */
571 void MEDCouplingSkyLineArray::replaceSimplePack(const mcIdType idx, const mcIdType * packBg, const mcIdType * packEnd)
572 {
573   validIndex("replaceSimplePack", idx);
574
575   mcIdType * iP(_index->getPointer());
576   mcIdType newSz = ToIdType(std::distance(packBg, packEnd));
577   const mcIdType start = iP[idx], end = iP[idx+1];
578
579   // _values
580   mcIdType initValSz = _values->getNbOfElems();
581   mcIdType deltaSz = newSz-(end-start);  // can be negative
582   if (deltaSz)
583     {
584       if (deltaSz > 0)
585         _values->reAlloc(initValSz+deltaSz);
586       mcIdType *vP(_values->getPointer());
587       std::copy(vP+end, vP+initValSz, vP+end+deltaSz);
588       if (deltaSz < 0)
589         _values->reAlloc(initValSz+deltaSz);
590     }
591
592   // copy new pack
593   std::copy(packBg, packEnd, _values->getPointer()+start);
594
595   // _index
596   for(mcIdType ii = idx+1; ii < _index->getNbOfElems(); ii++)
597     iP[ii] += deltaSz;
598 }
599
600 /**
601  * Replace pack with super index 'superIdx' and index 'idx' with the provided new pack.
602  * Function can be used only for 3-level SkyLine.
603  */
604 void MEDCouplingSkyLineArray::replacePack(const mcIdType superIdx, const mcIdType idx, const mcIdType *packBg, const mcIdType *packEnd)
605 {
606   checkSuperIndex("replacePack");
607   validSuperIndexAndIndex("replacePack", superIdx, idx);
608
609   mcIdType * siP(_super_index->getPointer()), *iP(_index->getPointer());
610   mcIdType newSz = ToIdType(std::distance(packBg, packEnd));
611   const mcIdType start = iP[siP[superIdx]+idx], end = iP[siP[superIdx]+idx+1];
612
613   // _values
614   mcIdType initValSz = _values->getNbOfElems();
615   mcIdType deltaSz = newSz-(end-start);  // can be negative
616   if (deltaSz)
617     {
618       if (deltaSz > 0)
619         _values->reAlloc(initValSz+deltaSz);
620       mcIdType *vP(_values->getPointer());
621       std::copy(vP+end, vP+initValSz, vP+end+deltaSz);
622       if (deltaSz < 0)
623         _values->reAlloc(initValSz+deltaSz);
624     }
625
626   // copy new pack
627   std::copy(packBg, packEnd, _values->getPointer()+start);
628
629   // _index
630   for(mcIdType ii = siP[superIdx]+idx+1; ii < _index->getNbOfElems(); ii++)
631     iP[ii] += deltaSz;
632 }