Salome HOME
23544: SMESH's performance issues
[modules/smesh.git] / src / SMDS / SMDS_SetIterator.hxx
1 // Copyright (C) 2007-2016  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 : webmaster.salome@opencascade.com
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   template<typename VALUE,typename VALUE_SET_ITERATOR>
55   struct PointerAccessor {
56     static VALUE value(VALUE_SET_ITERATOR it) { return (VALUE) &(*it); }
57   };
58
59   ///////////////////////////////////////////////////////////////////////////////
60   /// Filters of value pointed by iterator
61   ///////////////////////////////////////////////////////////////////////////////
62
63   template <typename VALUE>
64   struct PassAllValueFilter
65   {
66     bool operator()(const VALUE& t ) { return true; }
67   };
68
69   template <typename VALUE>
70   struct NonNullFilter
71   {
72     bool operator()(const VALUE& t ) { return bool( t ); }
73   };
74 }
75
76 ///////////////////////////////////////////////////////////////////////////////
77 /// SMDS_Iterator iterating over abstract set of values like STL containers
78 ///
79 /// BE CAREFUL: iterator pointed value is static_cast'ed to VALUE
80 ///
81 ///////////////////////////////////////////////////////////////////////////////
82
83 template<typename VALUE,
84          typename VALUE_SET_ITERATOR,
85          typename ACCESOR=SMDS::SimpleAccessor<VALUE,VALUE_SET_ITERATOR>,
86          typename VALUE_FILTER=SMDS::PassAllValueFilter<VALUE> >
87 class SMDS_SetIterator : public SMDS_Iterator<VALUE>
88 {
89 protected:
90   VALUE_SET_ITERATOR _beg, _end;
91   VALUE_FILTER _filter;
92 public:
93   SMDS_SetIterator(const VALUE_SET_ITERATOR & begin,
94                    const VALUE_SET_ITERATOR & end,
95                    const VALUE_FILTER&        filter=VALUE_FILTER())
96   { init ( begin, end, filter ); }
97
98   /// Initialization
99   virtual void init(const VALUE_SET_ITERATOR & begin,
100                     const VALUE_SET_ITERATOR & end,
101                     const VALUE_FILTER&        filter=VALUE_FILTER())
102   {
103     _beg = begin;
104     _end = end;
105     _filter = filter;
106     if ( more() && !_filter( ACCESOR::value( _beg )))
107       next();
108   }
109   /// Return true iff there are other object in this iterator
110   virtual bool more()
111   {
112     return _beg != _end;
113   }
114   /// Return the current object and step to the next one
115   virtual VALUE next()
116   {
117     VALUE ret = ACCESOR::value( _beg++ );
118     while ( more() && !_filter( ACCESOR::value( _beg )))
119       ++_beg;
120     return ret;
121   }
122 };
123
124 ///////////////////////////////////////////////////////////////////////////////
125 /// map iterators
126 ///////////////////////////////////////////////////////////////////////////////
127
128 #include <map>
129 /*!
130  * \brief iterator on values of a map
131  */
132 template<typename M>
133 struct SMDS_mapIterator : public SMDS_SetIterator< typename M::mapped_type, typename M::const_iterator,
134                                                    SMDS::ValueAccessor<typename M::mapped_type,
135                                                                        typename M::const_iterator> > {
136   typedef SMDS_SetIterator< typename M::mapped_type, typename M::const_iterator,
137                             SMDS::ValueAccessor<typename M::mapped_type,
138                                                 typename M::const_iterator> > parent_type;
139   SMDS_mapIterator(const M& m):parent_type(m.begin(),m.end()) {}
140 };
141 /*!
142  * \brief reverse iterator on values of a map
143  */
144 template<typename M>
145 struct SMDS_mapReverseIterator : public SMDS_SetIterator< typename M::mapped_type,
146                                                           typename M::const_reverse_iterator,
147                                                           SMDS::ValueAccessor<typename M::mapped_type,
148                                                                               typename M::const_reverse_iterator> > {
149   typedef SMDS_SetIterator< typename M::mapped_type, typename M::const_reverse_iterator,
150                             SMDS::ValueAccessor<typename M::mapped_type,
151                                                 typename M::const_reverse_iterator> > parent_type;
152   SMDS_mapReverseIterator(const M& m):parent_type(m.rbegin(),m.rend()) {}
153 };
154 /*!
155  * \brief iterator on keys of a map
156  */
157 template<typename M>
158 struct SMDS_mapKeyIterator : public SMDS_SetIterator< typename M::key_type, typename M::const_iterator,
159                                                       SMDS::KeyAccessor<typename M::key_type,
160                                                                         typename M::const_iterator> > {
161   typedef SMDS_SetIterator< typename M::key_type, typename M::const_iterator,
162                             SMDS::KeyAccessor<typename M::key_type,
163                                               typename M::const_iterator> > parent_type;
164   SMDS_mapKeyIterator(const M& m):parent_type(m.begin(),m.end()) {}
165 };
166 /*!
167  * \brief reverse iterator on keys of a map
168  */
169 template<typename M>
170 struct SMDS_mapKeyReverseIterator : public SMDS_SetIterator< typename M::key_type, typename M::const_iterator,
171                                                             SMDS::KeyAccessor<typename M::key_type,
172                                                                               typename M::const_iterator> > {
173   typedef SMDS_SetIterator< typename M::key_type, typename M::const_iterator,
174                             SMDS::KeyAccessor<typename M::key_type,
175                                               typename M::const_iterator> > parent_type;
176   SMDS_mapKeyReverseIterator(const M& m):parent_type(m.rbegin(),m.rend()) {}
177 };
178
179 ///////////////////////////////////////////////////////////////////////////////
180 // useful specifications
181 ///////////////////////////////////////////////////////////////////////////////
182
183 #include <vector>
184
185 class SMDS_MeshElement;
186 class SMDS_MeshNode;
187
188 typedef const SMDS_MeshElement* SMDS_pElement;
189 typedef const SMDS_MeshNode*    SMDS_pNode;
190
191 // element iterators
192
193 typedef SMDS_SetIterator< SMDS_pElement, std::vector< SMDS_pElement >::const_iterator>
194 SMDS_ElementVectorIterator;
195
196
197 typedef SMDS_SetIterator< SMDS_pElement, SMDS_pElement const *>
198 SMDS_ElementArrayIterator;
199
200
201 typedef SMDS_SetIterator< SMDS_pElement, std::vector< SMDS_pNode >::const_iterator>
202 SMDS_NodeVectorElemIterator;
203
204
205 typedef SMDS_SetIterator< SMDS_pElement, SMDS_pNode const * >
206 SMDS_NodeArrayElemIterator;
207
208 // node iterators
209
210 typedef SMDS_SetIterator< SMDS_pNode, std::vector< SMDS_pNode >::const_iterator >
211 SMDS_NodeVectorIterator;
212
213
214 typedef SMDS_SetIterator< SMDS_pNode, SMDS_pNode const * >
215 SMDS_NodeArrayIterator;
216
217
218 #endif