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

Last change on this file since 1125 was 1125, checked in by Peter, 14 years ago

fixing Doxygen parsing

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1#ifndef _theplu_yat_utility_iterator_traits_
2#define _theplu_yat_utility_iterator_traits_
3
4// $Id: iterator_traits.h 1125 2008-02-22 21:31:22Z peter $
5
6/*
7  Copyright (C) 2007 Peter Johansson
8
9  This file is part of the yat library, http://trac.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27namespace theplu {
28namespace yat {
29namespace utility {
30
31  /**
32    Struct to be used to make compile-time decision that Iterator is
33    unweighted, which is the default.
34   */ 
35  struct unweighted_iterator_tag {};
36   
37  /**
38    Struct to be used to make compile-time decision that Iterator is
39    weighted. Some algorithms come also in a weighted version and
40    this tag could be used to decide on using them (rather than
41    the corresponding unweighted algorithm).
42   */ 
43  struct weighted_iterator_tag {};
44
45  /**
46      All iterators default to unweighted type. If you want your
47      iterator to behave as weighted, you need to provide a
48      specialization.
49
50      \see Iterator and StrideIterator
51  */
52  template <class T>
53  struct weighted_iterator_traits {
54  /**
55      All iterators default to unweighted type
56  */
57    typedef unweighted_iterator_tag type;
58  };
59
60  /**
61    Metafunction that works on a pair weighted-unweighted types and
62    return weighted_type. The metafunction is specialized for
63    unweighted unweighted in which case unweighted is returned.
64   */
65  template <class T1, class T2>
66  struct unweighted_type_and {
67    /**
68       default return weighted_iterator_tag
69     */
70    typedef weighted_iterator_tag type;
71  };
72
73  /**
74    Specialization that sets type to be unweighted when both arguments
75    are unweighted
76   */
77  template <>
78  struct unweighted_type_and<unweighted_iterator_tag, unweighted_iterator_tag> {
79    /**
80       return unweighted_iterator_tag
81     */
82    typedef unweighted_iterator_tag type;
83  };
84
85  /**
86    struct used to determine if a pair of iterators should be treated
87    as weighted. If both iterators are unweighted, type is set to
88    unweighted else weighted.
89  */
90  template <class T1, class T2>
91  struct weighted_if_any2 {
92  private:
93    typedef typename weighted_iterator_traits<T1>::type w_type1;
94    typedef typename weighted_iterator_traits<T2>::type w_type2;
95  public:
96    /// return unweighted if both are unweighted
97    typedef typename unweighted_type_and<w_type1, w_type2>::type type;
98  };
99
100  /**
101    Same as weighted_iterator_traits2 but for 3 arguments.
102   */
103  template <class T1, class T2, class T3>
104  struct weighted_if_any3 {
105  private:
106    typedef typename weighted_if_any2<T1, T2>::type tmp;
107    typedef typename weighted_iterator_traits<T3>::type w_type3;
108  public:
109    /// return unweighted if all are unweighted
110    typedef typename unweighted_type_and<tmp, w_type3>::type type;
111  };
112
113  /// check (at compile time) that iterator is unweighted.
114  inline void check_iterator_is_unweighted(unweighted_iterator_tag x){} 
115
116  /**
117     check (at compile time) that iterator \a iter is unweighted. This
118     function is strictly not needed, but exists only for convenience.
119   */
120  template <class Iter>
121  void check_iterator_is_unweighted(Iter iter) 
122  { check_iterator_is_unweighted(typename 
123                                 weighted_iterator_traits<Iter>::type());
124  }
125
126
127  /**
128     \brief traits to make unweighted iterator work as in a weighted
129
130     This class must be implemented for every iterator that can be weighted.
131
132     \see StrideIterator and Iterator
133   */ 
134  template <class Iter>
135  struct iterator_traits {
136    // Peter, perhaps these should be templatized, but for now return
137    // type double is hard coded.
138    /**
139       \return data that is *iter
140    */
141    double data(Iter iter) const 
142    { check_iterator_is_unweighted(iter); return *iter; }
143
144    /**
145       \return 1.0
146    */
147    double weight(Iter iter) const 
148    { check_iterator_is_unweighted(iter); return 1.0; }
149
150  };
151
152}}} // of namespace utility, yat, and theplu
153
154#endif
Note: See TracBrowser for help on using the repository browser.