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

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

generalized StrideIterator? to work with Iterators as well - not only pointers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.1 KB
RevLine 
[908]1#ifndef _theplu_yat_utility_iterator_traits_
2#define _theplu_yat_utility_iterator_traits_
3
4// $Id: iterator_traits.h 1041 2008-02-06 17:36:54Z peter $
5
6/*
7  Copyright (C) 2007 Peter Johansson
8
[1000]9  This file is part of the yat library, http://trac.thep.lu.se/yat
[908]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
[1041]27#include "IteratorWeighted.h"
[910]28
[908]29namespace theplu {
30namespace yat {
31namespace utility {
32
[966]33  /**
[908]34    Struct to be used to make compile-time decision that Iterator is
35    unweighted, which is the default.
36   */ 
37  struct unweighted_type {
38    typedef unweighted_type type;
39  };
40   
[966]41  /**
[908]42    Struct to be used to make compile-time decision that Iterator is
43    weighted. Some algorithms come in also in a weighted version in
44    this tag could be used to decide on using them (rather than
45    unweighted cousin).
46   */ 
47  struct weighted_type {
48    typedef weighted_type type;
49  };
50
[966]51  /// All iterators should default to unweighted type ...
[908]52  template <class T>
53  struct weighted_iterator_traits {
54    typedef unweighted_type type;
55  };
56
[966]57  /// but specialized to return weighted type for some things
[958]58  //
59  // if you add a new weighted iterator, add a new specialization like
60  // this one.
[908]61  template <class U, class V>
62  struct weighted_iterator_traits<yat::utility::IteratorWeighted<U,V> > {
63    typedef weighted_type type;
[909]64  };
[908]65
[966]66  /**
[958]67    Metafunction that works on a pair weighted-unweighted types and
68    return weighted_type. The metafunction is specialized for
69    unweighted unweighted in which case unweighted is returned.
70   */
[916]71  template <class T1, class T2>
[958]72  struct unweighted_type_and {
[916]73    typedef weighted_type type;
74  };
75
[966]76  /**
[958]77    Specialization that sets type to be unweighted when both arguments
78    are unweighted
79   */
80  template <>
81  struct unweighted_type_and<unweighted_type, unweighted_type> {
82    typedef unweighted_type type;
83  };
84
[966]85  /**
[958]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  */
[916]90  template <class T1, class T2>
[958]91  struct weighted_if_any2 {
92    typedef typename weighted_iterator_traits<T1>::type w_type1;
93    typedef typename weighted_iterator_traits<T2>::type w_type2;
94    typedef typename unweighted_type_and<w_type1, w_type2>::type type;
[916]95  };
96
[966]97  /**
[958]98    Same as weighted_iterator_traits2 but for 3 arguments.
99   */
100  template <class T1, class T2, class T3>
101  struct weighted_if_any3 {
102    typedef typename weighted_if_any2<T1, T2>::type tmp;
103    typedef typename weighted_iterator_traits<T3>::type w_type3;
104    typedef typename unweighted_type_and<tmp, w_type3>::type type;
105  };
106
[966]107  /// check (at compile time) that iterator is unweighted.
[914]108  inline void check_iterator_is_unweighted(unweighted_type x){} 
[966]109
110  /**
111     check (at compile time) that iterator \a iter is unweighted. This
112     function is strictly not needed, but exists only for convenience.
113   */
[914]114  template <class Iter>
115  void check_iterator_is_unweighted(Iter iter) 
[916]116  { check_iterator_is_unweighted(typename 
117                                 weighted_iterator_traits<Iter>::type());
118  }
[914]119
120
[966]121  /**
[914]122    Function to be used to make compile-time decision how to return
123    data from an iterator separating two cases: weighted and
124    unweighted.
125
126    In this way unweighted iterators can be used in a weighted context.
127   */ 
128  // Peter, perhaps these should be templatized, but for now return
129  // type double is hard coded.
130  template <class Iter>
131  double iterator_traits_data(Iter iter)
[916]132  { return iterator_traits_data(iter, typename 
133                                weighted_iterator_traits<Iter>::type()); } 
[914]134
[966]135  /**
136     \return data for weighted iterator
137   */
[914]138  template <class Iter>
139  double iterator_traits_data(Iter i, weighted_type)
140  { return i.data(); } 
141
[966]142  /**
143     \return data for unweighted data
144   */
[914]145  template <class Iter>
146  double iterator_traits_data(Iter i, unweighted_type)
147  { return *i; } 
148
[966]149  /**
[914]150    Function to be used to make compile-time decision how to return
151    data from an iterator separating two cases: weighted and
152    unweighted.
153
154    In this way unweighted iterators can be used in a weighted context
155    and weight will be set to unity.
156   */ 
157  // Peter, perhaps these should be templatized, but for now return
158  // type double is hard coded.
159  template <class Iter>
160  double iterator_traits_weight(Iter iter)
[916]161  { return iterator_traits_weight(iter, typename 
162                                  weighted_iterator_traits<Iter>::type()); } 
[914]163
[966]164  /**
165     \return weight for weighted iterator
166   */
[914]167  template <class Iter>
168  double iterator_traits_weight(Iter i, weighted_type)
169  { return i.weight(); } 
170
[966]171  /**
172     \return weight for unweighted iterator
173   */
[914]174  template <class Iter>
175  double iterator_traits_weight(Iter i, unweighted_type)
176  { return 1.0; } 
177
178
[908]179}}} // of namespace utility, yat, and theplu
180
181#endif
Note: See TracBrowser for help on using the repository browser.