Salome HOME
a56d94b9ad28d7ebfab79d15d675f0baf7d93db7
[tools/medcoupling.git] / src / MEDCoupling / MEDCouplingSkyLineArray.hxx
1 // Copyright (C) 2007-2020  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 #ifndef __PARAMEDMEM_MEDCOUPLINGSKYLINEARRAY_HXX__
21 #define __PARAMEDMEM_MEDCOUPLINGSKYLINEARRAY_HXX__
22
23 #include "MEDCoupling.hxx"
24 #include "MEDCouplingMemArray.hxx"
25 #include "MCAuto.hxx"
26 #include "NormalizedGeometricTypes"
27
28 #include <vector>
29 #include <functional>
30
31 namespace MEDCoupling
32 {
33   /**!
34    * Class allowing the easy manipulation of the indexed array format, where the first array (the "indices") is a set of offsets to
35    * be used in the second array (the "values"), to extract packs of values.
36    *
37    * Example:
38    *    index = [0,2,7,10]
39    *    values = [1,24,2,33,6,7,10,11,9,28]
40    * which describes 3 packs of (integer) values : [1,24] and [2,33,6,7,10] and [11,9,28]
41    *
42    * Thus the index array is always monotic ascendant.
43    *
44    * This class allows to pursue this logic up to 3 levels, i.e. the first array (the "indices") points to packs in the
45    * second (the "values"), which itself points to identifiers in a third array (the "sub-values").
46    *
47    * This particularly useful for connectivity of pure polygonal/polyhedral meshes.
48    *
49    * Example:
50    *     super-index = [0,1,3]
51    *     index = [0,3,6,10]
52    *     values = [28,1,4,2,35,8,9,10,1,12]
53    * which represent two 3 packs and two super-packs. The first super-pack is [[28,1,4]] and has only one pack [28,1,4].
54    * The second super-pack is [[2,35,8], [9,10,1,12]] and has two packs [2,35,8] and [9,10,1,12].
55    * Note that contrary to index, the integers in super-index are interpreted as being inclusive: the first super-pack
56    * goes from offset 0 to offset 1, inclusive. This is not the same for index, where the upper bound is exclusive.
57    */
58   class MEDCOUPLING_EXPORT MEDCouplingSkyLineArray : public RefCountObject
59   {
60   public:
61     static MEDCouplingSkyLineArray * New();
62     static MEDCouplingSkyLineArray * New( const std::vector<mcIdType>& index, const std::vector<mcIdType>& value);
63     static MEDCouplingSkyLineArray * New( DataArrayIdType* index, DataArrayIdType* value );
64     static MEDCouplingSkyLineArray * New( const MEDCouplingSkyLineArray & other );
65
66     static MEDCouplingSkyLineArray * BuildFromPolyhedronConn( const DataArrayIdType* c, const DataArrayIdType* cI );
67
68     static std::vector< MCAuto<DataArrayIdType> > RetrieveVecIndex(const std::vector< MCAuto<MEDCouplingSkyLineArray> >& vecSka)
69     {
70        auto fct = [](MEDCouplingSkyLineArray *ska) { return ska->getIndexArray(); };
71        return RetrieveVecOfSkyLineArrayGen(vecSka,fct);
72     }
73     
74     static std::vector< MCAuto<DataArrayIdType> > RetrieveVecValues(const std::vector< MCAuto<MEDCouplingSkyLineArray> >& vecSka)
75     {
76        auto fct = [](MEDCouplingSkyLineArray *ska) { return ska->getValuesArray(); };
77        return RetrieveVecOfSkyLineArrayGen(vecSka,fct);
78     }
79     
80     static std::vector< MCAuto<DataArrayIdType> > RetrieveVecOfSkyLineArrayGen(const std::vector< MCAuto<MEDCouplingSkyLineArray> >& vecSka, std::function<DataArrayIdType *(MEDCouplingSkyLineArray *)> fct)
81     {
82        std::size_t sz(vecSka.size());
83        std::vector< MCAuto<DataArrayIdType> > ret(sz);
84        std::vector< MCAuto<DataArrayIdType> >::iterator it(ret.begin());
85        std::for_each(vecSka.begin(),vecSka.end(),[&it,fct](MCAuto<MEDCouplingSkyLineArray> elt) { *it++ = MCAuto<DataArrayIdType>::TakeRef(fct(elt)); } );
86        return ret;
87     }
88
89     std::string getClassName() const override { return std::string("MEDCouplingSkyLineArray"); }
90     std::size_t getHeapMemorySizeWithoutChildren() const;
91     std::vector<const BigMemoryObject *> getDirectChildrenWithNull() const;
92
93     void set( DataArrayIdType* index, DataArrayIdType* value );
94     void set3( DataArrayIdType* superIndex, DataArrayIdType* index, DataArrayIdType* value );
95
96     mcIdType getSuperNumberOf()   const { return ToIdType(_super_index->getNbOfElems())-1; }
97     mcIdType getNumberOf() const { return ToIdType(_index->getNbOfElems())-1; }
98     mcIdType getLength()   const { return ToIdType(_values->getNbOfElems()); }
99
100     const mcIdType* getSuperIndex() const { return _super_index->begin(); }
101     const mcIdType* getIndex() const { return _index->begin(); }
102     const mcIdType* getValues() const { return _values->begin(); }
103
104     DataArrayIdType* getSuperIndexArray() const;
105     DataArrayIdType* getIndexArray() const;
106     DataArrayIdType* getValuesArray() const;
107
108     std::string simpleRepr() const;
109
110     MEDCouplingSkyLineArray *groupPacks(const DataArrayIdType *indexedPacks) const;
111     MEDCouplingSkyLineArray *uniqueNotSortedByPack() const;
112
113     void getSimplePackSafe(const mcIdType absolutePackId, std::vector<mcIdType> & pack) const;
114     const mcIdType * getSimplePackSafePtr(const mcIdType absolutePackId, mcIdType & packSize) const;
115     void findPackIds(const std::vector<mcIdType> & superPackIndices, const mcIdType *packBg, const mcIdType *packEnd,
116                      std::vector<mcIdType>& out) const;
117
118     void deletePack(const mcIdType superIdx, const mcIdType idx);
119     void deleteSimplePack(const mcIdType idx);
120     void pushBackPack(const mcIdType superIdx, const mcIdType * packBg, const mcIdType * packEnd);
121
122     void replaceSimplePack(const mcIdType idx, const mcIdType * packBg, const mcIdType * packEnd);
123     void replacePack(const mcIdType superIdx, const mcIdType idx, const mcIdType * packBg, const mcIdType * packEnd);
124
125     void deleteSimplePacks(const DataArrayIdType* idx);
126     void replaceSimplePacks(const DataArrayIdType* idx, const std::vector<const DataArrayIdType*>& packs);
127     
128     void convertToPolyhedronConn( MCAuto<DataArrayIdType>& c,  MCAuto<DataArrayIdType>& cI) const;
129
130   private:
131     MEDCouplingSkyLineArray();
132     ~MEDCouplingSkyLineArray();
133
134     void checkSuperIndex(const std::string& func) const;
135     void validSuperIndex(const std::string& func, mcIdType superIndex) const;
136     void validIndex(const std::string& func, mcIdType index) const;
137     void validSuperIndexAndIndex(const std::string& func, mcIdType superIndex, mcIdType index) const;
138
139     MCAuto<DataArrayIdType> _super_index;
140     MCAuto<DataArrayIdType> _index;
141     MCAuto<DataArrayIdType> _values;
142   };
143
144 }
145 # endif