| 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 |
|
| 23 |
// SMESH SMDS : implementation of Salome mesh data structure |
| 24 |
// File : SMDS_SetIterator.hxx |
| 25 |
// Created : Feb 27 2006 |
| 26 |
// Author : Edward AGAPOV (eap) |
| 27 |
// |
| 28 |
#ifndef SMDS_SetIterator_HeaderFile |
| 29 |
#define SMDS_SetIterator_HeaderFile |
| 30 |
|
| 31 |
#include "SMDS_Iterator.hxx" |
| 32 |
|
| 33 |
namespace SMDS { |
| 34 |
|
| 35 |
/////////////////////////////////////////////////////////////////////////////// |
| 36 |
/// Accessors to value pointed by iterator |
| 37 |
/////////////////////////////////////////////////////////////////////////////// |
| 38 |
|
| 39 |
template<typename VALUE,typename VALUE_SET_ITERATOR> |
| 40 |
struct SimpleAccessor { |
| 41 |
static VALUE value(VALUE_SET_ITERATOR it) { return (VALUE) *it; } |
| 42 |
}; |
| 43 |
|
| 44 |
template<typename VALUE,typename VALUE_SET_ITERATOR> |
| 45 |
struct KeyAccessor { |
| 46 |
static VALUE value(VALUE_SET_ITERATOR it) { return (VALUE) it->first; } |
| 47 |
}; |
| 48 |
|
| 49 |
template<typename VALUE,typename VALUE_SET_ITERATOR> |
| 50 |
struct ValueAccessor { |
| 51 |
static VALUE value(VALUE_SET_ITERATOR it) { return (VALUE) it->second; } |
| 52 |
}; |
| 53 |
|
| 54 |
/////////////////////////////////////////////////////////////////////////////// |
| 55 |
/// Filters of value pointed by iterator |
| 56 |
/////////////////////////////////////////////////////////////////////////////// |
| 57 |
|
| 58 |
template <typename VALUE> |
| 59 |
struct PassAllValueFilter |
| 60 |
{ |
| 61 |
bool operator()(const VALUE& t ) { return true; } |
| 62 |
}; |
| 63 |
|
| 64 |
template <typename VALUE> |
| 65 |
struct NonNullFilter |
| 66 |
{ |
| 67 |
bool operator()(const VALUE& t ) { return bool( t ); } |
| 68 |
}; |
| 69 |
} |
| 70 |
|
| 71 |
/////////////////////////////////////////////////////////////////////////////// |
| 72 |
/// SMDS_Iterator iterating over abstract set of values like STL containers |
| 73 |
/// |
| 74 |
/// BE CAREFUL: iterator pointed value is static_cast'ed to VALUE |
| 75 |
/// |
| 76 |
/////////////////////////////////////////////////////////////////////////////// |
| 77 |
|
| 78 |
template<typename VALUE, |
| 79 |
typename VALUE_SET_ITERATOR, |
| 80 |
typename ACCESSOR =SMDS::SimpleAccessor<VALUE,VALUE_SET_ITERATOR>, |
| 81 |
typename VALUE_FILTER=SMDS::PassAllValueFilter<VALUE> > |
| 82 |
class SMDS_SetIterator : public SMDS_Iterator<VALUE> |
| 83 |
{ |
| 84 |
protected: |
| 85 |
VALUE_SET_ITERATOR _beg, _end; |
| 86 |
VALUE_FILTER _filter; |
| 87 |
public: |
| 88 |
SMDS_SetIterator(const VALUE_SET_ITERATOR & begin, |
| 89 |
const VALUE_SET_ITERATOR & end, |
| 90 |
const VALUE_FILTER& filter=VALUE_FILTER()) |
| 91 |
{ init ( begin, end, filter ); } |
| 92 |
|
| 93 |
/// Initialization |
| 94 |
virtual void init(const VALUE_SET_ITERATOR & begin, |
| 95 |
const VALUE_SET_ITERATOR & end, |
| 96 |
const VALUE_FILTER& filter=VALUE_FILTER()) |
| 97 |
{ |
| 98 |
_beg = begin; |
| 99 |
_end = end; |
| 100 |
_filter = filter; |
| 101 |
if ( more() && !_filter(ACCESSOR::value( _beg ))) |
| 102 |
next(); |
| 103 |
} |
| 104 |
/// Return true iff there are other object in this iterator |
| 105 |
virtual bool more() |
| 106 |
{ |
| 107 |
return _beg != _end; |
| 108 |
} |
| 109 |
/// Return the current object and step to the next one |
| 110 |
virtual VALUE next() |
| 111 |
{ |
| 112 |
VALUE ret = ACCESSOR::value( _beg++ ); |
| 113 |
while ( more() && !_filter(ACCESSOR::value( _beg ))) |
| 114 |
++_beg; |
| 115 |
return ret; |
| 116 |
} |
| 117 |
}; |
| 118 |
|
| 119 |
/////////////////////////////////////////////////////////////////////////////// |
| 120 |
/// map iterators |
| 121 |
/////////////////////////////////////////////////////////////////////////////// |
| 122 |
|
| 123 |
#include <map> |
| 124 |
/*! |
| 125 |
* \brief iterator on values of a map |
| 126 |
*/ |
| 127 |
template<typename M> |
| 128 |
struct SMDS_mapIterator : public SMDS_SetIterator< typename M::mapped_type, typename M::const_iterator, |
| 129 |
SMDS::ValueAccessor<typename M::mapped_type, |
| 130 |
typename M::const_iterator> > { |
| 131 |
typedef SMDS_SetIterator< typename M::mapped_type, typename M::const_iterator, |
| 132 |
SMDS::ValueAccessor<typename M::mapped_type, |
| 133 |
typename M::const_iterator> > parent_type; |
| 134 |
SMDS_mapIterator(const M& m):parent_type(m.begin(),m.end()) {} |
| 135 |
}; |
| 136 |
/*! |
| 137 |
* \brief reverse iterator on values of a map |
| 138 |
*/ |
| 139 |
template<typename M> |
| 140 |
struct SMDS_mapReverseIterator : public SMDS_SetIterator< typename M::mapped_type, |
| 141 |
typename M::const_reverse_iterator, |
| 142 |
SMDS::ValueAccessor<typename M::mapped_type, |
| 143 |
typename M::const_reverse_iterator> > { |
| 144 |
typedef SMDS_SetIterator< typename M::mapped_type, typename M::const_reverse_iterator, |
| 145 |
SMDS::ValueAccessor<typename M::mapped_type, |
| 146 |
typename M::const_reverse_iterator> > parent_type; |
| 147 |
SMDS_mapReverseIterator(const M& m):parent_type(m.rbegin(),m.rend()) {} |
| 148 |
}; |
| 149 |
/*! |
| 150 |
* \brief iterator on keys of a map |
| 151 |
*/ |
| 152 |
template<typename M> |
| 153 |
struct SMDS_mapKeyIterator : public SMDS_SetIterator< typename M::key_type, typename M::const_iterator, |
| 154 |
SMDS::KeyAccessor<typename M::key_type, |
| 155 |
typename M::const_iterator> > { |
| 156 |
typedef SMDS_SetIterator< typename M::key_type, typename M::const_iterator, |
| 157 |
SMDS::KeyAccessor<typename M::key_type, |
| 158 |
typename M::const_iterator> > parent_type; |
| 159 |
SMDS_mapKeyIterator(const M& m):parent_type(m.begin(),m.end()) {} |
| 160 |
}; |
| 161 |
/*! |
| 162 |
* \brief reverse iterator on keys of a map |
| 163 |
*/ |
| 164 |
template<typename M> |
| 165 |
struct SMDS_mapKeyReverseIterator : public SMDS_SetIterator< typename M::key_type, typename M::const_iterator, |
| 166 |
SMDS::KeyAccessor<typename M::key_type, |
| 167 |
typename M::const_iterator> > { |
| 168 |
typedef SMDS_SetIterator< typename M::key_type, typename M::const_iterator, |
| 169 |
SMDS::KeyAccessor<typename M::key_type, |
| 170 |
typename M::const_iterator> > parent_type; |
| 171 |
SMDS_mapKeyReverseIterator(const M& m):parent_type(m.rbegin(),m.rend()) {} |
| 172 |
}; |
| 173 |
|
| 174 |
/////////////////////////////////////////////////////////////////////////////// |
| 175 |
// useful specifications |
| 176 |
/////////////////////////////////////////////////////////////////////////////// |
| 177 |
|
| 178 |
#include <vector> |
| 179 |
|
| 180 |
class SMDS_MeshElement; |
| 181 |
class SMDS_MeshNode; |
| 182 |
|
| 183 |
typedef const SMDS_MeshElement* SMDS_pElement; |
| 184 |
typedef const SMDS_MeshNode* SMDS_pNode; |
| 185 |
|
| 186 |
// element iterators |
| 187 |
|
| 188 |
typedef SMDS_SetIterator< SMDS_pElement, std::vector< SMDS_pElement >::const_iterator> |
| 189 |
SMDS_ElementVectorIterator; |
| 190 |
|
| 191 |
|
| 192 |
typedef SMDS_SetIterator< SMDS_pElement, SMDS_pElement const *> |
| 193 |
SMDS_ElementArrayIterator; |
| 194 |
|
| 195 |
|
| 196 |
typedef SMDS_SetIterator< SMDS_pElement, std::vector< SMDS_pNode >::const_iterator> |
| 197 |
SMDS_NodeVectorElemIterator; |
| 198 |
|
| 199 |
|
| 200 |
typedef SMDS_SetIterator< SMDS_pElement, SMDS_pNode const * > |
| 201 |
SMDS_NodeArrayElemIterator; |
| 202 |
|
| 203 |
// node iterators |
| 204 |
|
| 205 |
typedef SMDS_SetIterator< SMDS_pNode, std::vector< SMDS_pNode >::const_iterator > |
| 206 |
SMDS_NodeVectorIterator; |
| 207 |
|
| 208 |
|
| 209 |
typedef SMDS_SetIterator< SMDS_pNode, SMDS_pNode const * > |
| 210 |
SMDS_NodeArrayIterator; |
| 211 |
|
| 212 |
|
| 213 |
#endif |