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