Imported Upstream version 0.12.7
[bowtie.git] / SeqAn-1.1 / seqan / basic / basic_iterator_position.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_position.h,v 1.1 2008/08/25 16:20:01 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_BASIC_ITERATOR_POSITION_H
22 #define SEQAN_HEADER_BASIC_ITERATOR_POSITION_H
23
24 namespace SEQAN_NAMESPACE_MAIN
25 {
26 //////////////////////////////////////////////////////////////////////////////
27 // Tag
28
29 struct PositionIterator;
30
31 //////////////////////////////////////////////////////////////////////////////
32 // Position Iterator
33 //////////////////////////////////////////////////////////////////////////////
34
35 /**
36 .Spec.Position Iterator:
37 ..cat:Iterators
38 ..general:Class.Iter
39 ..summary:Adapts @Metafunction.Position.position@ to @Concept.Rooted Iterator.iterator@.
40 ..signature:Iter<TContainer, PositionIterator>
41 ..param.TContainer:Type of the container.
42 ...metafunction:Metafunction.Container
43 ..remarks
44 ...text:Position Iterators provide the concept @Concept.Rooted Iterator@.
45 ..see:Metafunction.Position
46 */
47
48 template <typename TContainer>
49 class Iter<TContainer, PositionIterator>
50 {
51 public:
52         typedef typename Position<TContainer>::Type TPosition;
53
54         typename _Pointer<TContainer>::Type data_container;
55         TPosition data_position;
56 //____________________________________________________________________________
57
58 public:
59 /**
60 .Memfunc.PositionIterator#Iter:
61 ..class:Spec.Position Iterator
62 ..summary:Constructor
63 ..signature:Iter()
64 ..signature:Iter(iter)
65 ..signature:Iter(container [, position])
66 ..param.iter:Another position iterator object.
67 ..param.container:The corresponding container object.
68 ...metafunction:Metafunction.Container
69 ..param.position:A position in $container$. (optional)
70 ...metafunction:Metafunction.Position
71 ...remarks.text:If this argument is omitted, the adaptor iterator is initialized to the @Function.beginPosition.begin position@ of $container$.
72 */
73         Iter()
74         {
75 SEQAN_CHECKPOINT
76         }
77         Iter(typename _Parameter<TContainer>::Type container_, TPosition position_ = 0):
78                 data_container(_toPointer(container_)),
79                 data_position(position_)
80         {
81 SEQAN_CHECKPOINT
82         }
83         Iter(Iter const & other_):
84                 data_container(other_.data_container),
85                 data_position(other_.data_position)
86         {
87 SEQAN_CHECKPOINT
88         }
89         template <typename TContainer2, typename TSpec2>
90         Iter(Iter<TContainer2, TSpec2> const & other_)
91         {
92 SEQAN_CHECKPOINT
93                 assign(*this, other_);
94         }
95         ~Iter()
96         {
97 SEQAN_CHECKPOINT
98         }
99         Iter const & 
100         operator = (Iter const & other_)
101         {
102 SEQAN_CHECKPOINT
103                 data_container = other_.data_container;
104                 data_position = other_.data_position;
105                 return *this;
106         }
107 //____________________________________________________________________________
108
109         friend inline typename _Parameter<TContainer>::Type 
110         container(Iter & me)
111         {
112 SEQAN_CHECKPOINT
113                 return _toParameter<TContainer>(me.data_container);
114         }
115         friend inline typename _Parameter<TContainer>::Type 
116         container(Iter const & me)
117         {
118 SEQAN_CHECKPOINT
119                 return _toParameter<TContainer>(me.data_container);
120         }
121 //____________________________________________________________________________
122
123         friend inline void
124         setContainer(Iter & me, typename _Parameter<TContainer>::Type container_)
125         {
126 SEQAN_CHECKPOINT
127                 typename Position<Iter>::Type pos = position(me);
128                 me.data_container = _toPointer(container_);
129                 setPosition(me, pos);
130         }
131
132 //____________________________________________________________________________
133
134         friend inline TPosition &
135         position(Iter & me)
136         {
137 SEQAN_CHECKPOINT
138                 return me.data_position;
139         }
140         friend inline TPosition const &
141         position(Iter const & me)
142         {
143 SEQAN_CHECKPOINT
144                 return me.data_position;
145         }
146 //____________________________________________________________________________
147
148         friend inline void
149         setPosition(Iter & me, TPosition position_)
150         {
151 SEQAN_CHECKPOINT
152                 me.data_position = position_;
153         }
154 //____________________________________________________________________________
155 };
156
157 //////////////////////////////////////////////////////////////////////////////
158 // value
159 //////////////////////////////////////////////////////////////////////////////
160
161 template <typename TContainer>
162 inline typename Reference<Iter<TContainer, PositionIterator> >::Type 
163 value(Iter<TContainer, PositionIterator> & me)
164 {
165 SEQAN_CHECKPOINT
166         return value(container(me), position(me));
167 }
168 template <typename TContainer>
169 inline typename Reference<Iter<TContainer, PositionIterator> >::Type 
170 value(Iter<TContainer, PositionIterator> const & me)
171 {
172 SEQAN_CHECKPOINT
173         return value(container(me), position(me));
174 }
175
176 /////////////////////////////////////////////////////////////////////////////
177 // assignValue
178 //////////////////////////////////////////////////////////////////////////////
179
180 template <typename TContainer, typename TValue>
181 inline void
182 assignValue(Iter<TContainer, PositionIterator> & me,
183                         TValue _value)
184 {
185 SEQAN_CHECKPOINT
186         assignValue(container(me), position(me), _value);
187 }
188 template <typename TContainer, typename TValue>
189 inline void
190 assignValue(Iter<TContainer, PositionIterator> const & me,
191                         TValue _value)
192 {
193 SEQAN_CHECKPOINT
194         assignValue(container(me), position(me), _value);
195 }
196
197 /////////////////////////////////////////////////////////////////////////////
198 // moveValue
199 //////////////////////////////////////////////////////////////////////////////
200
201 template <typename TContainer, typename TValue>
202 inline void
203 moveValue(Iter<TContainer, PositionIterator> & me,
204                   TValue _value)
205 {
206 SEQAN_CHECKPOINT
207         moveValue(container(me), position(me), _value);
208 }
209 template <typename TContainer, typename TValue>
210 inline void
211 moveValue(Iter<TContainer, PositionIterator> const & me,
212                   TValue _value)
213 {
214 SEQAN_CHECKPOINT
215         moveValue(container(me), position(me), _value);
216 }
217
218 //////////////////////////////////////////////////////////////////////////////
219 // operator ==
220 //////////////////////////////////////////////////////////////////////////////
221
222 template <typename TContainer>
223 inline bool 
224 operator == (Iter<TContainer, PositionIterator> const & left,
225                          Iter<TContainer, PositionIterator> const & right)
226 {
227 SEQAN_CHECKPOINT
228         return position(left) == position(right);
229 }
230
231 //////////////////////////////////////////////////////////////////////////////
232 // operator !=
233 //////////////////////////////////////////////////////////////////////////////
234
235 template <typename TContainer>
236 inline bool 
237 operator != (Iter<TContainer, PositionIterator> const & left,
238                          Iter<TContainer, PositionIterator> const & right)
239 {
240 SEQAN_CHECKPOINT
241         return position(left) != position(right);
242 }
243
244 //////////////////////////////////////////////////////////////////////////////
245 // operator < / >
246 //////////////////////////////////////////////////////////////////////////////
247
248 template <typename TContainer>
249 inline bool 
250 operator < (Iter<TContainer, PositionIterator> const & left,
251                         Iter<TContainer, PositionIterator> const & right)
252 {
253 SEQAN_CHECKPOINT
254         return position(left) < position(right);
255 }
256
257 template <typename TContainer>
258 inline bool 
259 operator > (Iter<TContainer, PositionIterator> const & left,
260                         Iter<TContainer, PositionIterator> const & right)
261 {
262 SEQAN_CHECKPOINT
263         return position(left) > position(right);
264 }
265
266 //////////////////////////////////////////////////////////////////////////////
267 // operator <= / >=
268 //////////////////////////////////////////////////////////////////////////////
269
270 template <typename TContainer>
271 inline bool 
272 operator <= (Iter<TContainer, PositionIterator> const & left,
273                          Iter<TContainer, PositionIterator> const & right)
274 {
275 SEQAN_CHECKPOINT
276         return position(left) <= position(right);
277 }
278
279 template <typename TContainer>
280 inline bool 
281 operator >= (Iter<TContainer, PositionIterator> const & left,
282                          Iter<TContainer, PositionIterator> const & right)
283 {
284 SEQAN_CHECKPOINT
285         return position(left) >= position(right);
286 }
287
288 //////////////////////////////////////////////////////////////////////////////
289 // goNext
290 //////////////////////////////////////////////////////////////////////////////
291
292 template <typename TContainer>
293 inline void
294 goNext(Iter<TContainer, PositionIterator> & me)
295 {
296 SEQAN_CHECKPOINT
297         setPosition(me, position(me) + 1);
298 }
299
300 //////////////////////////////////////////////////////////////////////////////
301 // goPrevious
302 //////////////////////////////////////////////////////////////////////////////
303
304 template <typename TContainer>
305 inline void
306 goPrevious(Iter<TContainer, PositionIterator> & me)
307 {
308 SEQAN_CHECKPOINT
309         setPosition(me, position(me) - 1);
310 }
311
312 //////////////////////////////////////////////////////////////////////////////
313 // operator +
314 //////////////////////////////////////////////////////////////////////////////
315 template <typename TContainer, typename TIntegral>
316 inline Iter<TContainer, PositionIterator>  
317 operator + (Iter<TContainer, PositionIterator> const & left,
318                         TIntegral right)
319 {
320 SEQAN_CHECKPOINT
321         return Iter<TContainer, PositionIterator>(container(left), position(left) + right);
322 }
323 // for <anonymous enum> types
324 template <typename TContainer>
325 inline Iter<TContainer, PositionIterator>  
326 operator + (Iter<TContainer, PositionIterator> const & left,
327                         int right)
328 {
329 SEQAN_CHECKPOINT
330         return Iter<TContainer, PositionIterator>(container(left), position(left) + right);
331 }
332
333 template <typename TContainer, typename TIntegral>
334 inline Iter<TContainer, PositionIterator>  
335 operator + (TIntegral left,
336                         Iter<TContainer, PositionIterator> const & right)
337 {
338 SEQAN_CHECKPOINT
339         return Iter<TContainer, PositionIterator>(container(right), position(right) + left);
340 }
341 // for <anonymous enum> types
342 template <typename TContainer>
343 inline Iter<TContainer, PositionIterator>  
344 operator + (int left,
345                         Iter<TContainer, PositionIterator> const & right)
346 {
347 SEQAN_CHECKPOINT
348         return Iter<TContainer, PositionIterator>(container(right), position(right) + left);
349 }
350
351 //////////////////////////////////////////////////////////////////////////////
352 // operator +=
353 //////////////////////////////////////////////////////////////////////////////
354
355 template <typename TContainer, typename TIntegral>
356 inline Iter<TContainer, PositionIterator> &
357 operator += (Iter<TContainer, PositionIterator> & left,
358                          TIntegral right)
359 {
360 SEQAN_CHECKPOINT
361         setPosition(left, position(left) + right);
362         return left;
363 }
364 // for <anonymous enum> types
365 template <typename TContainer>
366 inline Iter<TContainer, PositionIterator> &
367 operator += (Iter<TContainer, PositionIterator> & left,
368                          int right)
369 {
370 SEQAN_CHECKPOINT
371         setPosition(left, position(left) + right);
372         return left;
373 }
374
375 //////////////////////////////////////////////////////////////////////////////
376 // operator -
377 //////////////////////////////////////////////////////////////////////////////
378
379 template <typename TContainer, typename TIntegral>
380 inline Iter<TContainer, PositionIterator>  
381 operator - (Iter<TContainer, PositionIterator> const & left,
382                         TIntegral right)
383 {
384 SEQAN_CHECKPOINT
385         return Iter<TContainer, PositionIterator>(container(left), position(left) - right);
386 }
387 // for <anonymous enum> types
388 template <typename TContainer>
389 inline Iter<TContainer, PositionIterator>  
390 operator - (Iter<TContainer, PositionIterator> const & left,
391                         int right)
392 {
393 SEQAN_CHECKPOINT
394         return Iter<TContainer, PositionIterator>(container(left), position(left) - right);
395 }
396
397 //____________________________________________________________________________
398
399 template <typename TContainer>
400 inline typename Difference<TContainer>::Type  
401 operator - (Iter<TContainer, PositionIterator> const & left,
402                         Iter<TContainer, PositionIterator> const & right)
403 {
404 SEQAN_CHECKPOINT
405         return position(left) - position(right);
406 }
407
408 //////////////////////////////////////////////////////////////////////////////
409 // operator -=
410 //////////////////////////////////////////////////////////////////////////////
411
412 template <typename TContainer, typename TIntegral>
413 inline Iter<TContainer, PositionIterator> &
414 operator -= (Iter<TContainer, PositionIterator> & left,
415                          TIntegral right)
416 {
417 SEQAN_CHECKPOINT
418         setPosition(left, position(left) - right);
419         return left;
420 }
421 // for <anonymous enum> types
422 template <typename TContainer>
423 inline Iter<TContainer, PositionIterator> &
424 operator -= (Iter<TContainer, PositionIterator> & left,
425                          int right)
426 {
427 SEQAN_CHECKPOINT
428         setPosition(left, position(left) - right);
429         return left;
430 }
431
432 //////////////////////////////////////////////////////////////////////////////
433 // assign (Conversion)
434 //////////////////////////////////////////////////////////////////////////////
435
436 template <typename TTargetContainer, typename TSource>
437 inline void
438 assign(Iter<TTargetContainer, PositionIterator> & target,
439            TSource const & source)
440 {
441 SEQAN_CHECKPOINT
442         target.data_container = container(source);
443         target.data_position = position(source);
444 }
445
446 //////////////////////////////////////////////////////////////////////////////
447
448 } //namespace SEQAN_NAMESPACE_MAIN
449
450 #endif //#ifndef SEQAN_HEADER_...