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