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

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

refs #391 generalized default implementation of iterator_traits by templatizing return types. Also added a default constructor that checks that the class only compiles with unweighted iterators (weighted iterators should have their specialized classes)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1#ifndef _theplu_yat_utility_iterator_traits_
2#define _theplu_yat_utility_iterator_traits_
3
4// $Id: iterator_traits.h 1373 2008-07-16 16:12:27Z 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://trac.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 <iterator>
29
30namespace theplu {
31namespace yat {
32namespace utility {
33
34  /**
35    Struct to be used to make compile-time decision that Iterator is
36    unweighted, which is the default.
37   */ 
38  struct unweighted_iterator_tag {};
39   
40  /**
41    Struct to be used to make compile-time decision that Iterator is
42    weighted. Some algorithms come also in a weighted version and
43    this tag could be used to decide on using them (rather than
44    the corresponding unweighted algorithm).
45   */ 
46  struct weighted_iterator_tag {};
47
48  /**
49      All iterators default to unweighted type. If you want your
50      iterator to behave as weighted, you need to provide a
51      specialization.
52
53      \see Iterator and StrideIterator
54  */
55  template <class T>
56  struct weighted_iterator_traits {
57  /**
58      All iterators default to unweighted type
59  */
60    typedef unweighted_iterator_tag type;
61  };
62
63  /**
64     \internal
65
66    Metafunction that works on a pair weighted-unweighted types and
67    return weighted_type. The metafunction is specialized for
68    unweighted unweighted in which case unweighted is returned.
69   */
70  template <class T1, class T2>
71  struct unweighted_type_and {
72    /**
73       default return weighted_iterator_tag
74     */
75    typedef weighted_iterator_tag type;
76  };
77
78  /**
79     \internal
80
81    Specialization that sets type to be unweighted when both arguments
82    are unweighted
83   */
84  template <>
85  struct unweighted_type_and<unweighted_iterator_tag, unweighted_iterator_tag> {
86    /**
87       return unweighted_iterator_tag
88     */
89    typedef unweighted_iterator_tag type;
90  };
91
92  /**
93    struct used to determine if a pair of iterators should be treated
94    as weighted. If both iterators are unweighted, type is set to
95    unweighted else weighted.
96  */
97  template <class T1, class T2>
98  struct weighted_if_any2 {
99  private:
100    typedef typename weighted_iterator_traits<T1>::type w_type1;
101    typedef typename weighted_iterator_traits<T2>::type w_type2;
102  public:
103    /// return unweighted if both are unweighted
104    typedef typename unweighted_type_and<w_type1, w_type2>::type type;
105  };
106
107  /**
108    Same as weighted_iterator_traits2 but for 3 arguments.
109   */
110  template <class T1, class T2, class T3>
111  struct weighted_if_any3 {
112  private:
113    typedef typename weighted_if_any2<T1, T2>::type tmp;
114    typedef typename weighted_iterator_traits<T3>::type w_type3;
115  public:
116    /// return unweighted if all are unweighted
117    typedef typename unweighted_type_and<tmp, w_type3>::type type;
118  };
119
120  /// check (at compile time) that iterator is unweighted.
121  inline void check_iterator_is_unweighted(unweighted_iterator_tag x){} 
122
123  /**
124     check (at compile time) that iterator \a iter is unweighted. This
125     function is strictly not needed, but exists only for convenience.
126   */
127  template <class Iter>
128  void check_iterator_is_unweighted(Iter iter) 
129  { check_iterator_is_unweighted(typename 
130                                 weighted_iterator_traits<Iter>::type());
131  }
132
133
134  /**
135     \brief traits to make unweighted iterator work as in a weighted
136
137     This class must be implemented for every iterator that can be weighted.
138
139     \see StrideIterator and Container2DIterator
140   */ 
141  template <class Iter>
142  struct iterator_traits {
143    /**
144       \brief Default constructor
145
146       Does nothing but check that Iter is unweighted (at compile time).
147     */
148    iterator_traits(void)
149    { 
150      check_iterator_is_unweighted(typename weighted_iterator_traits<Iter>::type()); }
151
152    /**
153       An unweighted iterator has data_reference = reference
154     */
155    typedef typename std::iterator_traits<Iter>::reference data_reference;
156
157    /**
158       An unweighted iterator has a weight = 1.0 and since this is a
159       temporary it is returned by value and weight_reference is double
160     */
161    typedef double weight_reference;
162
163    /**
164       \return data that is *iter
165    */
166    data_reference data(Iter iter) const 
167    { check_iterator_is_unweighted(iter); return *iter; }
168
169    /**
170       \return 1.0
171    */
172    weight_reference weight(Iter iter) const 
173    { check_iterator_is_unweighted(iter); return 1.0; }
174
175  };
176
177}}} // of namespace utility, yat, and theplu
178
179#endif
Note: See TracBrowser for help on using the repository browser.