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

Last change on this file since 1115 was 1115, checked in by Markus Ringnér, 14 years ago

Fixes #254 and #295

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