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

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

allow doxygen tag internal that creates text 'FOR INTERNAL USE ONLY'

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1#ifndef _theplu_yat_utility_iterator_traits_
2#define _theplu_yat_utility_iterator_traits_
3
4// $Id: iterator_traits.h 1150 2008-02-25 22:27:38Z 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     \internal
62
63    Metafunction that works on a pair weighted-unweighted types and
64    return weighted_type. The metafunction is specialized for
65    unweighted unweighted in which case unweighted is returned.
66   */
67  template <class T1, class T2>
68  struct unweighted_type_and {
69    /**
70       default return weighted_iterator_tag
71     */
72    typedef weighted_iterator_tag type;
73  };
74
75  /**
76     \internal
77
78    Specialization that sets type to be unweighted when both arguments
79    are unweighted
80   */
81  template <>
82  struct unweighted_type_and<unweighted_iterator_tag, unweighted_iterator_tag> {
83    /**
84       return unweighted_iterator_tag
85     */
86    typedef unweighted_iterator_tag type;
87  };
88
89  /**
90    struct used to determine if a pair of iterators should be treated
91    as weighted. If both iterators are unweighted, type is set to
92    unweighted else weighted.
93  */
94  template <class T1, class T2>
95  struct weighted_if_any2 {
96  private:
97    typedef typename weighted_iterator_traits<T1>::type w_type1;
98    typedef typename weighted_iterator_traits<T2>::type w_type2;
99  public:
100    /// return unweighted if both are unweighted
101    typedef typename unweighted_type_and<w_type1, w_type2>::type type;
102  };
103
104  /**
105    Same as weighted_iterator_traits2 but for 3 arguments.
106   */
107  template <class T1, class T2, class T3>
108  struct weighted_if_any3 {
109  private:
110    typedef typename weighted_if_any2<T1, T2>::type tmp;
111    typedef typename weighted_iterator_traits<T3>::type w_type3;
112  public:
113    /// return unweighted if all are unweighted
114    typedef typename unweighted_type_and<tmp, w_type3>::type type;
115  };
116
117  /// check (at compile time) that iterator is unweighted.
118  inline void check_iterator_is_unweighted(unweighted_iterator_tag x){} 
119
120  /**
121     check (at compile time) that iterator \a iter is unweighted. This
122     function is strictly not needed, but exists only for convenience.
123   */
124  template <class Iter>
125  void check_iterator_is_unweighted(Iter iter) 
126  { check_iterator_is_unweighted(typename 
127                                 weighted_iterator_traits<Iter>::type());
128  }
129
130
131  /**
132     \brief traits to make unweighted iterator work as in a weighted
133
134     This class must be implemented for every iterator that can be weighted.
135
136     \see StrideIterator and Iterator
137   */ 
138  template <class Iter>
139  struct iterator_traits {
140    // Peter, perhaps these should be templatized, but for now return
141    // type double is hard coded.
142    /**
143       \return data that is *iter
144    */
145    double data(Iter iter) const 
146    { check_iterator_is_unweighted(iter); return *iter; }
147
148    /**
149       \return 1.0
150    */
151    double weight(Iter iter) const 
152    { check_iterator_is_unweighted(iter); return 1.0; }
153
154  };
155
156}}} // of namespace utility, yat, and theplu
157
158#endif
Note: See TracBrowser for help on using the repository browser.