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