Imported Upstream version 0.12.7
[bowtie.git] / SeqAn-1.1 / seqan / basic / basic_iterator_adapt_std.h
1  /*==========================================================================
2                 SeqAn - The Library for Sequence Analysis
3                           http://www.seqan.de 
4  ============================================================================
5   Copyright (C) 2007
6
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public
9   License as published by the Free Software Foundation; either
10   version 3 of the License, or (at your option) any later version.
11
12   This library is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17  ============================================================================
18   $Id: basic_iterator_adapt_std.h,v 1.1 2008/08/25 16:20:01 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_BASIC_ITERATOR_ADAPT_STD_H
22 #define SEQAN_HEADER_BASIC_ITERATOR_ADAPT_STD_H
23
24 //////////////////////////////////////////////////////////////////////////////
25
26 //adapt SeqAn iterator to std
27 namespace std
28 {
29         template<typename TContainer, typename TSpec>
30         struct iterator_traits<seqan::Iter<TContainer, TSpec> >
31         {
32                 typedef ::seqan::Iter<TContainer, TSpec> TIter;
33
34                 typedef random_access_iterator_tag iterator_category;
35                 typedef typename ::seqan::Value<TIter>::Type value_type;
36                 typedef typename ::seqan::Difference<TIter>::Type difference_type;
37                 typedef typename ::seqan::Value<TIter>::Type * pointer;
38                 typedef typename ::seqan::Reference<TIter>::Type reference;
39         };
40 }
41
42 //////////////////////////////////////////////////////////////////////////////
43 //////////////////////////////////////////////////////////////////////////////
44
45 namespace SEQAN_NAMESPACE_MAIN
46 {
47
48 //helper Metafunction
49
50 /* This simple, general implementation cannot be used due to strange VC++ 2003 behavior
51
52 template <typename TStdContainer>
53 struct StdContainerIterator
54 {
55         typedef typename TStdContainer::iterator Type;
56 };
57
58 template <typename TStdContainer>
59 struct StdContainerIterator<TStdContainer const>
60 {
61         typedef typename TStdContainer::const_iterator Type;
62 };
63 */
64
65 //we use this instead: specialize StdContainerIterator for each std-container
66 template <typename TStdContainer>
67 struct StdContainerIterator
68 {
69         typedef void * Type; //dummy, just to make VC++ 2003 happy
70 };
71
72 template <typename TChar, typename TCharTraits, typename TAlloc>
73 struct StdContainerIterator< ::std::basic_string<TChar, TCharTraits, TAlloc> >
74 {
75         typedef ::std::basic_string<TChar, TCharTraits, TAlloc> TContainer;
76         typedef typename TContainer::iterator Type;
77 };
78 template <typename TChar, typename TCharTraits, typename TAlloc>
79 struct StdContainerIterator< ::std::basic_string<TChar, TCharTraits, TAlloc> const>
80 {
81         typedef ::std::basic_string<TChar, TCharTraits, TAlloc> TContainer;
82         typedef typename TContainer::const_iterator Type;
83 };
84
85 //////////////////////////////////////////////////////////////////////////////
86 //adapt std iterator to SeqAn
87
88
89 struct StdIteratorAdaptor;
90
91 template <typename TContainer>
92 class Iter<TContainer, StdIteratorAdaptor>
93 {
94 public:
95         typedef typename StdContainerIterator<TContainer>::Type TIterator;
96         TIterator data_iterator;
97
98         Iter() {}
99         Iter(Iter const & other_): data_iterator(other_.data_iterator) {}
100         Iter(TIterator const & iter_): data_iterator(iter_) {}
101         Iter(TContainer const & cont_): data_iterator(begin(cont_)) {}
102
103         Iter const & operator = (Iter const & other_)
104         {
105                 data_iterator = other_.data_iterator;
106                 return *this;
107         }
108         Iter const & operator = (TIterator const & iter_)
109         {
110                 data_iterator = iter_;
111                 return *this;
112         }
113
114         operator TIterator &()
115         {
116                 return data_iterator;
117         }
118
119         ~Iter() {}
120 };
121
122 //////////////////////////////////////////////////////////////////////////////
123 // hostIterator
124 //////////////////////////////////////////////////////////////////////////////
125
126 template <typename TContainer>
127 inline typename StdContainerIterator<TContainer>::Type &
128 hostIterator(Iter<TContainer, StdIteratorAdaptor> & me)
129 {
130         return me.data_iterator;
131 }
132 template <typename TContainer>
133 inline typename StdContainerIterator<TContainer>::Type const &
134 hostIterator(Iter<TContainer, StdIteratorAdaptor> const & me)
135 {
136         return me.data_iterator;
137 }
138
139 //////////////////////////////////////////////////////////////////////////////
140 // value
141 //////////////////////////////////////////////////////////////////////////////
142
143 template <typename TContainer>
144 inline typename Reference<Iter<TContainer, StdIteratorAdaptor> >::Type 
145 value(Iter<TContainer, StdIteratorAdaptor> & me)
146 {
147         return *(me.data_iterator);
148 }
149 template <typename TContainer>
150 inline typename Reference<Iter<TContainer, StdIteratorAdaptor> const>::Type 
151 value(Iter<TContainer, StdIteratorAdaptor> const & me)
152 {
153         return *(me.data_iterator);
154 }
155
156
157 /////////////////////////////////////////////////////////////////////////////
158 // assignValue
159 //////////////////////////////////////////////////////////////////////////////
160
161 template <typename TContainer, typename TValue>
162 inline void 
163 assignValue(Iter<TContainer, StdIteratorAdaptor> & me,
164                         TValue & val)
165 {
166         *(me.data_iterator) = val;
167 }
168 template <typename TContainer, typename TValue>
169 inline void 
170 assignValue(Iter<TContainer, StdIteratorAdaptor> & me,
171                         TValue const & val)
172 {
173         *(me.data_iterator) = val;
174 }
175
176 /////////////////////////////////////////////////////////////////////////////
177 // moveValue
178 //////////////////////////////////////////////////////////////////////////////
179
180 template <typename TContainer, typename TValue>
181 inline void 
182 moveValue(Iter<TContainer, StdIteratorAdaptor> & me,
183                   TValue & val)
184 {
185         move(*(me.data_iterator), val);
186 }
187 template <typename TContainer, typename TValue>
188 inline void 
189 moveValue(Iter<TContainer, StdIteratorAdaptor> & me,
190                   TValue const & val)
191 {
192         move(*(me.data_iterator), val);
193 }
194
195 //////////////////////////////////////////////////////////////////////////////
196 // operator ==
197 //////////////////////////////////////////////////////////////////////////////
198
199 template <typename TContainer>
200 inline bool 
201 operator == (Iter<TContainer, StdIteratorAdaptor> const & left,
202                          Iter<TContainer, StdIteratorAdaptor> const & right)
203 {
204 SEQAN_CHECKPOINT
205         return hostIterator(left) == hostIterator(right);
206 }
207
208 //////////////////////////////////////////////////////////////////////////////
209 // operator !=
210 //////////////////////////////////////////////////////////////////////////////
211
212 template <typename TContainer>
213 inline bool 
214 operator != (Iter<TContainer, StdIteratorAdaptor> const & left,
215                          Iter<TContainer, StdIteratorAdaptor> const & right)
216 {
217 SEQAN_CHECKPOINT
218         return hostIterator(left) != hostIterator(right);
219 }
220
221 //////////////////////////////////////////////////////////////////////////////
222 // operator <
223 //////////////////////////////////////////////////////////////////////////////
224
225 template <typename TContainer>
226 inline bool 
227 operator < (Iter<TContainer, StdIteratorAdaptor> const & left,
228                         Iter<TContainer, StdIteratorAdaptor> const & right)
229 {
230 SEQAN_CHECKPOINT
231         return hostIterator(left) < hostIterator(right);
232 }
233
234 //////////////////////////////////////////////////////////////////////////////
235 // operator >
236 //////////////////////////////////////////////////////////////////////////////
237
238 template <typename TContainer>
239 inline bool 
240 operator > (Iter<TContainer, StdIteratorAdaptor> const & left,
241                         Iter<TContainer, StdIteratorAdaptor> const & right)
242 {
243 SEQAN_CHECKPOINT
244         return hostIterator(left) > hostIterator(right);
245 }
246
247 //////////////////////////////////////////////////////////////////////////////
248 // operator <=
249 //////////////////////////////////////////////////////////////////////////////
250
251 template <typename TContainer>
252 inline bool 
253 operator <= (Iter<TContainer, StdIteratorAdaptor> const & left,
254                         Iter<TContainer, StdIteratorAdaptor> const & right)
255 {
256 SEQAN_CHECKPOINT
257         return hostIterator(left) <= hostIterator(right);
258 }
259
260 //////////////////////////////////////////////////////////////////////////////
261 // operator >=
262 //////////////////////////////////////////////////////////////////////////////
263
264 template <typename TContainer>
265 inline bool 
266 operator >= (Iter<TContainer, StdIteratorAdaptor> const & left,
267                         Iter<TContainer, StdIteratorAdaptor> const & right)
268 {
269 SEQAN_CHECKPOINT
270         return hostIterator(left) >= hostIterator(right);
271 }
272
273 //////////////////////////////////////////////////////////////////////////////
274 // goNext
275 //////////////////////////////////////////////////////////////////////////////
276
277 template <typename TContainer>
278 inline void
279 goNext(Iter<TContainer, StdIteratorAdaptor> & me)
280 {
281 SEQAN_CHECKPOINT
282         goNext(hostIterator(me));
283 }
284
285 //////////////////////////////////////////////////////////////////////////////
286 // goPrevious
287 //////////////////////////////////////////////////////////////////////////////
288
289 template <typename TContainer>
290 inline void
291 goPrevious(Iter<TContainer, StdIteratorAdaptor> & me)
292 {
293 SEQAN_CHECKPOINT
294         goPrevious(hostIterator(me));
295 }
296
297 //////////////////////////////////////////////////////////////////////////////
298 // operator +
299 //////////////////////////////////////////////////////////////////////////////
300
301 template <typename TContainer, typename TIntegral>
302 inline Iter<TContainer, StdIteratorAdaptor>  
303 operator + (Iter<TContainer, StdIteratorAdaptor> const & left,
304                         TIntegral right)
305 {
306 SEQAN_CHECKPOINT
307         return Iter<TContainer, StdIteratorAdaptor>(hostIterator(left) + right);
308 }
309 // for <anonymous enum> types
310 template <typename TContainer>
311 inline Iter<TContainer, StdIteratorAdaptor>  
312 operator + (Iter<TContainer, StdIteratorAdaptor> const & left,
313                         int right)
314 {
315 SEQAN_CHECKPOINT
316         return Iter<TContainer, StdIteratorAdaptor>(hostIterator(left) + right);
317 }
318
319 template <typename TContainer, typename TIntegral>
320 inline Iter<TContainer, StdIteratorAdaptor>  
321 operator + (TIntegral left,
322                         Iter<TContainer, StdIteratorAdaptor> const & right)
323 {
324 SEQAN_CHECKPOINT
325         return Iter<TContainer, StdIteratorAdaptor>(hostIterator(right) + left);
326 }
327 // for <anonymous enum> types
328 template <typename TContainer>
329 inline Iter<TContainer, StdIteratorAdaptor>  
330 operator + (int left,
331                         Iter<TContainer, StdIteratorAdaptor> const & right)
332 {
333 SEQAN_CHECKPOINT
334         return Iter<TContainer, StdIteratorAdaptor>(hostIterator(right) + left);
335 }
336
337 //////////////////////////////////////////////////////////////////////////////
338 // operator +=
339 //////////////////////////////////////////////////////////////////////////////
340
341 template <typename TContainer, typename TIntegral>
342 inline Iter<TContainer, StdIteratorAdaptor> &
343 operator += (Iter<TContainer, StdIteratorAdaptor> & left,
344                          TIntegral right)
345 {
346 SEQAN_CHECKPOINT
347         hostIterator(left) += right;
348         return left;
349 }
350 // for <anonymous enum> types
351 template <typename TContainer>
352 inline Iter<TContainer, StdIteratorAdaptor> &
353 operator += (Iter<TContainer, StdIteratorAdaptor> & left,
354                          int right)
355 {
356 SEQAN_CHECKPOINT
357         hostIterator(left) += right;
358         return left;
359 }
360
361 //////////////////////////////////////////////////////////////////////////////
362 // operator -
363 //////////////////////////////////////////////////////////////////////////////
364
365 template <typename TContainer, typename TIntegral>
366 inline Iter<TContainer, StdIteratorAdaptor>  
367 operator - (Iter<TContainer, StdIteratorAdaptor> const & left,
368                         TIntegral right)
369 {
370 SEQAN_CHECKPOINT
371         return Iter<TContainer, StdIteratorAdaptor>(hostIterator(left) - right);
372 }
373 // for <anonymous enum> types
374 template <typename TContainer>
375 inline Iter<TContainer, StdIteratorAdaptor>  
376 operator - (Iter<TContainer, StdIteratorAdaptor> const & left,
377                         int right)
378 {
379 SEQAN_CHECKPOINT
380         return Iter<TContainer, StdIteratorAdaptor>(hostIterator(left) - right);
381 }
382
383 //____________________________________________________________________________
384
385 template <typename TContainer>
386 inline typename Difference<Iter<TContainer, StdIteratorAdaptor> >::Type  
387 operator - (Iter<TContainer, StdIteratorAdaptor> const & left,
388                         Iter<TContainer, StdIteratorAdaptor> const & right)
389 {
390 SEQAN_CHECKPOINT
391         return hostIterator(left) - hostIterator(right);
392 }
393
394 //////////////////////////////////////////////////////////////////////////////
395 // operator -=
396 //////////////////////////////////////////////////////////////////////////////
397
398 template <typename TContainer, typename TIntegral>
399 inline Iter<TContainer, StdIteratorAdaptor> &
400 operator -= (Iter<TContainer, StdIteratorAdaptor> & left,
401                          TIntegral right)
402 {
403 SEQAN_CHECKPOINT
404         hostIterator(left) -= right;
405         return left;
406 }
407 // for <anonymous enum> types
408 template <typename TContainer>
409 inline Iter<TContainer, StdIteratorAdaptor> &
410 operator -= (Iter<TContainer, StdIteratorAdaptor> & left,
411                          int right)
412 {
413 SEQAN_CHECKPOINT
414         hostIterator(left) -= right;
415         return left;
416 }
417
418 //////////////////////////////////////////////////////////////////////////////
419 //////////////////////////////////////////////////////////////////////////////
420 // assign (Conversion)
421 //////////////////////////////////////////////////////////////////////////////
422
423 template <typename TTargetContainer, typename TSource>
424 inline void
425 assign(Iter<TTargetContainer, StdIteratorAdaptor> & target,
426            TSource const & source)
427 {
428 SEQAN_CHECKPOINT
429         target.data_iterator = begin(container(source)) + position(source);
430 }
431
432 //////////////////////////////////////////////////////////////////////////////
433
434 } //namespace SEQAN_NAMESPACE_MAIN
435
436 #endif //#ifndef SEQAN_HEADER_...