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

Last change on this file since 1275 was 1275, checked in by Jari Häkkinen, 13 years ago

Updating copyright statements.

  • 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 1275 2008-04-11 06:10:12Z jari $
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
28namespace theplu {
29namespace yat {
30namespace utility {
31
32  /**
33    Struct to be used to make compile-time decision that Iterator is
34    unweighted, which is the default.
35   */ 
36  struct unweighted_iterator_tag {};
37   
38  /**
39    Struct to be used to make compile-time decision that Iterator is
40    weighted. Some algorithms come also in a weighted version and
41    this tag could be used to decide on using them (rather than
42    the corresponding unweighted algorithm).
43   */ 
44  struct weighted_iterator_tag {};
45
46  /**
47      All iterators default to unweighted type. If you want your
48      iterator to behave as weighted, you need to provide a
49      specialization.
50
51      \see Iterator and StrideIterator
52  */
53  template <class T>
54  struct weighted_iterator_traits {
55  /**
56      All iterators default to unweighted type
57  */
58    typedef unweighted_iterator_tag type;
59  };
60
61  /**
62     \internal
63
64    Metafunction that works on a pair weighted-unweighted types and
65    return weighted_type. The metafunction is specialized for
66    unweighted unweighted in which case unweighted is returned.
67   */
68  template <class T1, class T2>
69  struct unweighted_type_and {
70    /**
71       default return weighted_iterator_tag
72     */
73    typedef weighted_iterator_tag type;
74  };
75
76  /**
77     \internal
78
79    Specialization that sets type to be unweighted when both arguments
80    are unweighted
81   */
82  template <>
83  struct unweighted_type_and<unweighted_iterator_tag, unweighted_iterator_tag> {
84    /**
85       return unweighted_iterator_tag
86     */
87    typedef unweighted_iterator_tag type;
88  };
89
90  /**
91    struct used to determine if a pair of iterators should be treated
92    as weighted. If both iterators are unweighted, type is set to
93    unweighted else weighted.
94  */
95  template <class T1, class T2>
96  struct weighted_if_any2 {
97  private:
98    typedef typename weighted_iterator_traits<T1>::type w_type1;
99    typedef typename weighted_iterator_traits<T2>::type w_type2;
100  public:
101    /// return unweighted if both are unweighted
102    typedef typename unweighted_type_and<w_type1, w_type2>::type type;
103  };
104
105  /**
106    Same as weighted_iterator_traits2 but for 3 arguments.
107   */
108  template <class T1, class T2, class T3>
109  struct weighted_if_any3 {
110  private:
111    typedef typename weighted_if_any2<T1, T2>::type tmp;
112    typedef typename weighted_iterator_traits<T3>::type w_type3;
113  public:
114    /// return unweighted if all are unweighted
115    typedef typename unweighted_type_and<tmp, w_type3>::type type;
116  };
117
118  /// check (at compile time) that iterator is unweighted.
119  inline void check_iterator_is_unweighted(unweighted_iterator_tag x){} 
120
121  /**
122     check (at compile time) that iterator \a iter is unweighted. This
123     function is strictly not needed, but exists only for convenience.
124   */
125  template <class Iter>
126  void check_iterator_is_unweighted(Iter iter) 
127  { check_iterator_is_unweighted(typename 
128                                 weighted_iterator_traits<Iter>::type());
129  }
130
131
132  /**
133     \brief traits to make unweighted iterator work as in a weighted
134
135     This class must be implemented for every iterator that can be weighted.
136
137     \see StrideIterator and Iterator
138   */ 
139  template <class Iter>
140  struct iterator_traits {
141    // Peter, perhaps these should be templatized, but for now return
142    // type double is hard coded.
143    /**
144       \return data that is *iter
145    */
146    double data(Iter iter) const 
147    { check_iterator_is_unweighted(iter); return *iter; }
148
149    /**
150       \return 1.0
151    */
152    double weight(Iter iter) const 
153    { check_iterator_is_unweighted(iter); return 1.0; }
154
155  };
156
157}}} // of namespace utility, yat, and theplu
158
159#endif
Note: See TracBrowser for help on using the repository browser.