| 1 | // Copyright (C) 2007-2015 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, or (at your option) any later version. |
| 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 : [email protected] |
| 21 | // |
| 22 | #ifndef __SMESHDS_SubMeshHolder_HXX__ |
| 23 | #define __SMESHDS_SubMeshHolder_HXX__ |
| 24 | |
| 25 | #include <vector> |
| 26 | #include <map> |
| 27 | |
| 28 | //======================================================================= |
| 29 | /*! |
| 30 | * \brief A binder of a sub-mesh to its ID which can be negative. Provides fast |
| 31 | * access to a sub-mesh by its ID. |
| 32 | * |
| 33 | * Issue 52457: Addition of hypotheses is 8 time longer than meshing. |
| 34 | */ |
| 35 | //======================================================================= |
| 36 | |
| 37 | template <class SUBMESH> |
| 38 | class SMESHDS_TSubMeshHolder |
| 39 | { |
| 40 | std::vector< SUBMESH* > myVec; // for ID >= 0 |
| 41 | std::map< int, SUBMESH* > myMap; // for ID < 0 |
| 42 | |
| 43 | public: |
| 44 | |
| 45 | ~SMESHDS_TSubMeshHolder() |
| 46 | { |
| 47 | DeleteAll(); |
| 48 | } |
| 49 | void Add( int id, SUBMESH* sm ) |
| 50 | { |
| 51 | if ( id < 0 ) |
| 52 | { |
| 53 | myMap[ id ] = sm; |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | if ( myVec.size() <= id ) |
| 58 | myVec.resize( id+1, (SUBMESH*) NULL ); |
| 59 | myVec[ id ] = sm; |
| 60 | } |
| 61 | } |
| 62 | SUBMESH* Get( int id ) const |
| 63 | { |
| 64 | if ( id < 0 ) |
| 65 | { |
| 66 | typename std::map< int, SUBMESH* >::const_iterator i2sm = myMap.find( id ); |
| 67 | return (SUBMESH*) ( i2sm == myMap.end() ? NULL : i2sm->second ); |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | return (SUBMESH*) ( id >= myVec.size() ? NULL : myVec[ id ]); |
| 72 | } |
| 73 | } |
| 74 | void DeleteAll() |
| 75 | { |
| 76 | for ( size_t i = 0; i < myVec.size(); ++i ) |
| 77 | if ( SUBMESH* sm = myVec[i] ) |
| 78 | { |
| 79 | myVec[i] = 0; // avoid access via Get(i) |
| 80 | delete sm; |
| 81 | } |
| 82 | myVec.clear(); |
| 83 | |
| 84 | typename std::map< int, SUBMESH* >::iterator i2sm = myMap.begin(); |
| 85 | for ( ; i2sm != myMap.end(); ++i2sm ) |
| 86 | if ( SUBMESH* sm = i2sm->second ) |
| 87 | { |
| 88 | i2sm->second = 0; // avoid access via Get(i) |
| 89 | delete sm; |
| 90 | } |
| 91 | myMap.clear(); |
| 92 | } |
| 93 | int GetMinID() const |
| 94 | { |
| 95 | return myMap.empty() ? 0 : myMap.begin()->first; |
| 96 | } |
| 97 | int GetMaxID() const |
| 98 | { |
| 99 | return myVec.empty() ? ( myMap.empty() ? 0 : myMap.rbegin()->first ) : myVec.size(); |
| 100 | } |
| 101 | |
| 102 | //----------------------------------------------------------------------- |
| 103 | struct Iterator : public SMDS_Iterator< SUBMESH* > |
| 104 | { |
| 105 | const SMESHDS_TSubMeshHolder<SUBMESH>* myHolder; |
| 106 | SUBMESH* myNext; |
| 107 | int myCurID, myEndID, myIDDelta; |
| 108 | |
| 109 | void init( const SMESHDS_TSubMeshHolder<SUBMESH>* holder, |
| 110 | int firstID, int endID, int delta ) |
| 111 | { |
| 112 | myHolder = holder; |
| 113 | myNext = 0; |
| 114 | myCurID = firstID; |
| 115 | myEndID = endID; |
| 116 | myIDDelta = delta; |
| 117 | |
| 118 | next(); |
| 119 | } |
| 120 | |
| 121 | bool more() |
| 122 | { |
| 123 | return myNext; |
| 124 | } |
| 125 | |
| 126 | SUBMESH* next() |
| 127 | { |
| 128 | SUBMESH* res = myNext; |
| 129 | myNext = 0; |
| 130 | while ( !myNext && myCurID != myEndID ) |
| 131 | { |
| 132 | myNext = myHolder->Get( myCurID ); |
| 133 | myCurID += myIDDelta; |
| 134 | } |
| 135 | return res; |
| 136 | } |
| 137 | virtual ~Iterator() {} |
| 138 | }; |
| 139 | //----------------------------------------------------------------------- |
| 140 | |
| 141 | SMDS_Iterator< SUBMESH* >* GetIterator(const bool reverse=false) const |
| 142 | { |
| 143 | Iterator* iter = new Iterator; |
| 144 | if ( reverse ) iter->init( this, GetMaxID(), GetMinID()-1, -1 ); |
| 145 | else iter->init( this, GetMinID(), GetMaxID()+1, +1 ); |
| 146 | return iter; |
| 147 | } |
| 148 | }; |
| 149 | |
| 150 | |
| 151 | #endif |