1 | #ifndef _theplu_yat_utility_iterator_traits_ |
---|
2 | #define _theplu_yat_utility_iterator_traits_ |
---|
3 | |
---|
4 | // $Id: IteratorTraits.h 916 2007-09-30 00:50:10Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2007 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://trac.thep.lu.se/trac/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 | |
---|
27 | #include "yat/utility/IteratorWeighted.h" |
---|
28 | |
---|
29 | namespace theplu { |
---|
30 | namespace yat { |
---|
31 | namespace utility { |
---|
32 | |
---|
33 | /* |
---|
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 | |
---|
41 | /* |
---|
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 | |
---|
51 | // All iterators should default to unweighted type ... |
---|
52 | template <class T> |
---|
53 | struct weighted_iterator_traits { |
---|
54 | typedef unweighted_type type; |
---|
55 | }; |
---|
56 | |
---|
57 | // but specialized to return weighted type for some things |
---|
58 | template <class U, class V> |
---|
59 | struct weighted_iterator_traits<yat::utility::IteratorWeighted<U,V> > { |
---|
60 | typedef weighted_type type; |
---|
61 | }; |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | /* |
---|
66 | struct used to determine if a pair of iterators should be treated |
---|
67 | as weighted |
---|
68 | |
---|
69 | template <class T1, class T2> |
---|
70 | struct weighted_iterator_traits2 { |
---|
71 | typedef weighted_type type; |
---|
72 | }; |
---|
73 | |
---|
74 | // but specialized to return weighted type for some things |
---|
75 | template <class T1, class T2> |
---|
76 | struct weighted_iterator_traits2<yat::utility::IteratorWeighted<U,V> > { |
---|
77 | typedef weighted_type type; |
---|
78 | }; |
---|
79 | */ |
---|
80 | |
---|
81 | // check (at compile time) that iterator is unweighted. |
---|
82 | inline void check_iterator_is_unweighted(unweighted_type x){} |
---|
83 | template <class Iter> |
---|
84 | void check_iterator_is_unweighted(Iter iter) |
---|
85 | { check_iterator_is_unweighted(typename |
---|
86 | weighted_iterator_traits<Iter>::type()); |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | /* |
---|
91 | Function to be used to make compile-time decision how to return |
---|
92 | data from an iterator separating two cases: weighted and |
---|
93 | unweighted. |
---|
94 | |
---|
95 | In this way unweighted iterators can be used in a weighted context. |
---|
96 | */ |
---|
97 | // Peter, perhaps these should be templatized, but for now return |
---|
98 | // type double is hard coded. |
---|
99 | template <class Iter> |
---|
100 | double iterator_traits_data(Iter iter) |
---|
101 | { return iterator_traits_data(iter, typename |
---|
102 | weighted_iterator_traits<Iter>::type()); } |
---|
103 | |
---|
104 | template <class Iter> |
---|
105 | double iterator_traits_data(Iter i, weighted_type) |
---|
106 | { return i.data(); } |
---|
107 | |
---|
108 | template <class Iter> |
---|
109 | double iterator_traits_data(Iter i, unweighted_type) |
---|
110 | { return *i; } |
---|
111 | |
---|
112 | /* |
---|
113 | Function to be used to make compile-time decision how to return |
---|
114 | data from an iterator separating two cases: weighted and |
---|
115 | unweighted. |
---|
116 | |
---|
117 | In this way unweighted iterators can be used in a weighted context |
---|
118 | and weight will be set to unity. |
---|
119 | */ |
---|
120 | // Peter, perhaps these should be templatized, but for now return |
---|
121 | // type double is hard coded. |
---|
122 | template <class Iter> |
---|
123 | double iterator_traits_weight(Iter iter) |
---|
124 | { return iterator_traits_weight(iter, typename |
---|
125 | weighted_iterator_traits<Iter>::type()); } |
---|
126 | |
---|
127 | template <class Iter> |
---|
128 | double iterator_traits_weight(Iter i, weighted_type) |
---|
129 | { return i.weight(); } |
---|
130 | |
---|
131 | template <class Iter> |
---|
132 | double iterator_traits_weight(Iter i, unweighted_type) |
---|
133 | { return 1.0; } |
---|
134 | |
---|
135 | |
---|
136 | }}} // of namespace utility, yat, and theplu |
---|
137 | |
---|
138 | #endif |
---|