source: trunk/yat/utility/iterator_traits.h @ 1437

Last change on this file since 1437 was 1437, checked in by Peter, 13 years ago

merge patch release 0.4.2 to trunk. Delta 0.4.2-0.4.1

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.6 KB
Line 
1#ifndef _theplu_yat_utility_iterator_traits_
2#define _theplu_yat_utility_iterator_traits_
3
4// $Id: iterator_traits.h 1437 2008-08-25 17:55:00Z peter $
5
6/*
7  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2008 Peter Johansson, Markus Ringnér
9
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 2 of the
15  License, or (at your option) any later version.
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25  02111-1307, USA.
26*/
27
28#include "DataWeight.h"
29
30#include <iterator>
31#include <vector>
32
33namespace theplu {
34namespace yat {
35namespace utility {
36
37  /**
38    Struct to be used to make compile-time decision that Iterator is
39    unweighted, which is the default.
40   */ 
41  struct unweighted_iterator_tag {};
42   
43  /**
44    Struct to be used to make compile-time decision that Iterator is
45    weighted. Some algorithms come also in a weighted version and
46    this tag could be used to decide on using them (rather than
47    the corresponding unweighted algorithm).
48   */ 
49  struct weighted_iterator_tag {};
50
51  /**
52      All iterators default to unweighted type. If you want your
53      iterator to behave as weighted, you need to provide a
54      specialization.
55
56      \see Iterator and StrideIterator
57  */
58  template <class T>
59  struct weighted_iterator_traits {
60  /**
61      All iterators default to unweighted type
62  */
63    typedef unweighted_iterator_tag type;
64  };
65
66  /**
67     Specialization for std::vector<DataWeight>::iterator
68   */
69  template<>
70  struct weighted_iterator_traits<std::vector<DataWeight>::iterator> {
71    /**
72       iterator to vector<DataWeight> is weighted
73    */
74    typedef weighted_iterator_tag type;
75  };
76
77
78  /**
79     Specialization for std::vector<DataWeight>::const_iterator
80   */
81  template<>
82  struct weighted_iterator_traits<std::vector<DataWeight>::const_iterator> {
83    /**
84       const_iterator to vector<DataWeight> is weighted
85    */
86    typedef weighted_iterator_tag type;
87  };
88
89
90  /**
91     \internal
92
93    Metafunction that works on a pair weighted-unweighted types and
94    return weighted_type. The metafunction is specialized for
95    unweighted unweighted in which case unweighted is returned.
96   */
97  template <class T1, class T2>
98  struct unweighted_type_and {
99    /**
100       default return weighted_iterator_tag
101     */
102    typedef weighted_iterator_tag type;
103  };
104
105  /**
106     \internal
107
108    Specialization that sets type to be unweighted when both arguments
109    are unweighted
110   */
111  template <>
112  struct unweighted_type_and<unweighted_iterator_tag, unweighted_iterator_tag> {
113    /**
114       return unweighted_iterator_tag
115     */
116    typedef unweighted_iterator_tag type;
117  };
118
119  /**
120    struct used to determine if a pair of iterators should be treated
121    as weighted. If both iterators are unweighted, type is set to
122    unweighted else weighted.
123  */
124  template <class T1, class T2>
125  struct weighted_if_any2 {
126  private:
127    typedef typename weighted_iterator_traits<T1>::type w_type1;
128    typedef typename weighted_iterator_traits<T2>::type w_type2;
129  public:
130    /// return unweighted if both are unweighted
131    typedef typename unweighted_type_and<w_type1, w_type2>::type type;
132  };
133
134  /**
135    Same as weighted_iterator_traits2 but for 3 arguments.
136   */
137  template <class T1, class T2, class T3>
138  struct weighted_if_any3 {
139  private:
140    typedef typename weighted_if_any2<T1, T2>::type tmp;
141    typedef typename weighted_iterator_traits<T3>::type w_type3;
142  public:
143    /// return unweighted if all are unweighted
144    typedef typename unweighted_type_and<tmp, w_type3>::type type;
145  };
146
147  /// check (at compile time) that iterator is unweighted.
148  inline void check_iterator_is_unweighted(unweighted_iterator_tag x){} 
149
150  /**
151     check (at compile time) that iterator \a iter is unweighted. This
152     function is strictly not needed, but exists only for convenience.
153   */
154  template <class Iter>
155  void check_iterator_is_unweighted(Iter iter) 
156  { check_iterator_is_unweighted(typename 
157                                 weighted_iterator_traits<Iter>::type());
158  }
159
160
161  /**
162     \brief traits to make unweighted iterator work as in a weighted
163
164     This class must be implemented for every iterator that can be weighted.
165
166     \see StrideIterator and Container2DIterator
167   */ 
168  template <class Iter>
169  struct iterator_traits {
170    /**
171       \brief Default constructor
172
173       Does nothing but check that Iter is unweighted (at compile time).
174     */
175    iterator_traits(void)
176    { 
177      check_iterator_is_unweighted(typename weighted_iterator_traits<Iter>::type()); }
178
179    /**
180       An unweighted iterator has data_reference = reference
181     */
182    typedef typename std::iterator_traits<Iter>::reference data_reference;
183
184    /**
185       An unweighted iterator has a weight = 1.0 and since this is a
186       temporary it is returned by value and weight_reference is double
187     */
188    typedef double weight_reference;
189
190    /**
191       \return data that is *iter
192    */
193    data_reference data(Iter iter) const 
194    { check_iterator_is_unweighted(iter); return *iter; }
195
196    /**
197       \return 1.0
198    */
199    weight_reference weight(Iter iter) const 
200    { check_iterator_is_unweighted(iter); return 1.0; }
201
202  };
203
204
205  /**
206     Specialization for std::vector<DataWeight>::iterator
207   */ 
208  template <>
209  struct iterator_traits<std::vector<DataWeight>::iterator> {
210    /**
211       mutable iterator
212     */
213    typedef double& data_reference;
214
215    /**
216       mutable iterator
217     */
218    typedef double& weight_reference;
219
220    /**
221       \return data using DataWeight::data()
222    */
223    data_reference data(std::vector<DataWeight>::iterator iter) const 
224    { return iter->data(); }
225
226    /**
227       \return weight using DataWeight::weight()
228    */
229    weight_reference weight(std::vector<DataWeight>::iterator iter) const 
230    { return iter->weight(); }
231
232  };
233
234
235  /**
236     Specialization for std::vector<DataWeight>::const_iterator
237   */ 
238  template <>
239  struct iterator_traits<std::vector<DataWeight>::const_iterator> {
240    /**
241       const iterator
242     */
243    typedef const double& data_reference;
244
245    /**
246       const iterator
247     */
248    typedef const double& weight_reference;
249
250    /**
251       \return data using DataWeight::data()
252    */
253    data_reference data(std::vector<DataWeight>::iterator iter) const 
254    { return iter->data(); }
255
256    /**
257       \return weight using DataWeight::weight()
258    */
259    weight_reference weight(std::vector<DataWeight>::iterator iter) const 
260    { return iter->weight(); }
261
262  };
263
264}}} // of namespace utility, yat, and theplu
265
266#endif
Note: See TracBrowser for help on using the repository browser.