1 | #ifndef _theplu_yat_utility_iterator_traits_ |
---|
2 | #define _theplu_yat_utility_iterator_traits_ |
---|
3 | |
---|
4 | // $Id: iterator_traits.h 966 2007-10-11 17:01:01Z 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 | // |
---|
59 | // if you add a new weighted iterator, add a new specialization like |
---|
60 | // this one. |
---|
61 | template <class U, class V> |
---|
62 | struct weighted_iterator_traits<yat::utility::IteratorWeighted<U,V> > { |
---|
63 | typedef weighted_type type; |
---|
64 | }; |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | /** |
---|
69 | Metafunction that works on a pair weighted-unweighted types and |
---|
70 | return weighted_type. The metafunction is specialized for |
---|
71 | unweighted unweighted in which case unweighted is returned. |
---|
72 | */ |
---|
73 | template <class T1, class T2> |
---|
74 | struct unweighted_type_and { |
---|
75 | typedef weighted_type type; |
---|
76 | }; |
---|
77 | |
---|
78 | /** |
---|
79 | Specialization that sets type to be unweighted when both arguments |
---|
80 | are unweighted |
---|
81 | */ |
---|
82 | template <> |
---|
83 | struct unweighted_type_and<unweighted_type, unweighted_type> { |
---|
84 | typedef unweighted_type type; |
---|
85 | }; |
---|
86 | |
---|
87 | /** |
---|
88 | struct used to determine if a pair of iterators should be treated |
---|
89 | as weighted. If both iterators are unweighted, type is set to |
---|
90 | unweighted else weighted. |
---|
91 | */ |
---|
92 | template <class T1, class T2> |
---|
93 | struct weighted_if_any2 { |
---|
94 | typedef typename weighted_iterator_traits<T1>::type w_type1; |
---|
95 | typedef typename weighted_iterator_traits<T2>::type w_type2; |
---|
96 | typedef typename unweighted_type_and<w_type1, w_type2>::type type; |
---|
97 | }; |
---|
98 | |
---|
99 | /** |
---|
100 | Same as weighted_iterator_traits2 but for 3 arguments. |
---|
101 | */ |
---|
102 | template <class T1, class T2, class T3> |
---|
103 | struct weighted_if_any3 { |
---|
104 | typedef typename weighted_if_any2<T1, T2>::type tmp; |
---|
105 | typedef typename weighted_iterator_traits<T3>::type w_type3; |
---|
106 | typedef typename unweighted_type_and<tmp, w_type3>::type type; |
---|
107 | }; |
---|
108 | |
---|
109 | /// check (at compile time) that iterator is unweighted. |
---|
110 | inline void check_iterator_is_unweighted(unweighted_type x){} |
---|
111 | |
---|
112 | /** |
---|
113 | check (at compile time) that iterator \a iter is unweighted. This |
---|
114 | function is strictly not needed, but exists only for convenience. |
---|
115 | */ |
---|
116 | template <class Iter> |
---|
117 | void check_iterator_is_unweighted(Iter iter) |
---|
118 | { check_iterator_is_unweighted(typename |
---|
119 | weighted_iterator_traits<Iter>::type()); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | /** |
---|
124 | Function to be used to make compile-time decision how to return |
---|
125 | data from an iterator separating two cases: weighted and |
---|
126 | unweighted. |
---|
127 | |
---|
128 | In this way unweighted iterators can be used in a weighted context. |
---|
129 | */ |
---|
130 | // Peter, perhaps these should be templatized, but for now return |
---|
131 | // type double is hard coded. |
---|
132 | template <class Iter> |
---|
133 | double iterator_traits_data(Iter iter) |
---|
134 | { return iterator_traits_data(iter, typename |
---|
135 | weighted_iterator_traits<Iter>::type()); } |
---|
136 | |
---|
137 | /** |
---|
138 | \return data for weighted iterator |
---|
139 | */ |
---|
140 | template <class Iter> |
---|
141 | double iterator_traits_data(Iter i, weighted_type) |
---|
142 | { return i.data(); } |
---|
143 | |
---|
144 | /** |
---|
145 | \return data for unweighted data |
---|
146 | */ |
---|
147 | template <class Iter> |
---|
148 | double iterator_traits_data(Iter i, unweighted_type) |
---|
149 | { return *i; } |
---|
150 | |
---|
151 | /** |
---|
152 | Function to be used to make compile-time decision how to return |
---|
153 | data from an iterator separating two cases: weighted and |
---|
154 | unweighted. |
---|
155 | |
---|
156 | In this way unweighted iterators can be used in a weighted context |
---|
157 | and weight will be set to unity. |
---|
158 | */ |
---|
159 | // Peter, perhaps these should be templatized, but for now return |
---|
160 | // type double is hard coded. |
---|
161 | template <class Iter> |
---|
162 | double iterator_traits_weight(Iter iter) |
---|
163 | { return iterator_traits_weight(iter, typename |
---|
164 | weighted_iterator_traits<Iter>::type()); } |
---|
165 | |
---|
166 | /** |
---|
167 | \return weight for weighted iterator |
---|
168 | */ |
---|
169 | template <class Iter> |
---|
170 | double iterator_traits_weight(Iter i, weighted_type) |
---|
171 | { return i.weight(); } |
---|
172 | |
---|
173 | /** |
---|
174 | \return weight for unweighted iterator |
---|
175 | */ |
---|
176 | template <class Iter> |
---|
177 | double iterator_traits_weight(Iter i, unweighted_type) |
---|
178 | { return 1.0; } |
---|
179 | |
---|
180 | |
---|
181 | }}} // of namespace utility, yat, and theplu |
---|
182 | |
---|
183 | #endif |
---|