- メインページ
- モジュール
- ネームスペース
- データ構造
- ファイル

- ファイル一覧
- グローバル

iterator.h
説明を見る。
1 //
2 // Copyright (c) 2003-2011, MIST Project, Nagoya University
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the Nagoya University nor the names of its contributors
16 // may be used to endorse or promote products derived from this software
17 // without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28
33
34 #ifndef __INCLUDE_MIST_ITERATOR_H__
35 #define __INCLUDE_MIST_ITERATOR_H__
36
37
38 #include <iterator>
39
40 #ifndef __INCLUDE_MIST_CONF_H__
41 #include "mist_conf.h"
42 #endif
43
44 // mist名前空間の始まり
45 _MIST_BEGIN
46
47
48
58 template< class T, class Distance = ptrdiff_t, class Pointer = T*, class Reference = T& >
59 #if defined( __MIST_MSVC__ ) && __MIST_MSVC__ <= 6
60 class mist_iterator1 : public std::iterator< std::random_access_iterator_tag, T, Distance >
61 #else
62 class mist_iterator1 : public std::iterator< std::random_access_iterator_tag, T, Distance, Pointer, Reference >
63 #endif
64 {
65 public:
66 typedef T value_type;
67 typedef Pointer pointer;
68 typedef Reference reference;
69 typedef size_t size_type;
70 typedef Distance difference_type;
71 typedef Reference const_reference;
72
73 private:
74 pointer data_;
75 size_type diff_pointer_;
76
77 public:
79 mist_iterator1( pointer p = NULL, size_type diff = 1 ) : data_( p ), diff_pointer_( diff ){ }
80
82 template< class TT, class DD, class PP, class RR >
83 mist_iterator1( const mist_iterator1< TT, DD, PP, RR > &ite ) : data_( ite.data( ) ), diff_pointer_( ite.diff( ) ){ }
84
85
87 template< class TT, class DD, class PP, class RR >
88 const mist_iterator1& operator =( const mist_iterator1< TT, DD, PP, RR > &ite )
89 {
90 data_ = ite.data( );
91 diff_pointer_ = ite.diff( );
92 return( *this );
93 }
94
95
97 reference operator *(){ return( *data_ ); }
98
100 const_reference operator *() const { return( *data_ ); }
101
103 pointer operator->( ){ return( &**this ); }
104
106 const pointer operator->( ) const { return( &**this ); }
107
109 reference operator []( difference_type dist ){ return( data_[ dist * diff_pointer_ ] ); }
110
112 const_reference operator []( difference_type dist ) const { return( data_[ dist * diff_pointer_ ] ); }
113
115 pointer data( )
116 {
117 return( data_ );
118 }
119
121 const pointer data( ) const
122 {
123 return( data_ );
124 }
125
127 size_type diff( ) const
128 {
129 return( diff_pointer_ );
130 }
131
133 mist_iterator1& operator ++( )
134 {
135 *this += 1;
136 return( *this );
137 }
138
140 const mist_iterator1 operator ++( int )
141 {
142 mist_iterator1 old_val( *this );
143 *this += 1;
144 return( old_val );
145 }
146
148 mist_iterator1& operator --( )
149 {
150 *this -= 1;
151 return( *this );
152 }
153
155 const mist_iterator1 operator --( int )
156 {
157 mist_iterator1 old_val( *this );
158 *this -= 1;
159 return( old_val );
160 }
161
162
164 const mist_iterator1& operator +=( difference_type dist )
165 {
166 data_ += dist * diff_pointer_;
167 return( *this );
168 }
169
171 const mist_iterator1& operator -=( difference_type dist )
172 {
173 data_ -= dist * diff_pointer_;
174 return( *this );
175 }
176
177
179 template< class TT, class DD, class PP, class RR >
180 const difference_type operator -( const mist_iterator1< TT, DD, PP, RR > &ite ) const
181 {
182 return( ( data_ - ite.data( ) ) / diff_pointer_ );
183 }
184
185
187 template< class TT, class DD, class PP, class RR >
188 bool operator ==( const mist_iterator1< TT, DD, PP, RR > &ite ) const { return( data_ == ite.data( ) ); }
189
191 template< class TT, class DD, class PP, class RR >
192 bool operator !=( const mist_iterator1< TT, DD, PP, RR > &ite ) const { return( !( *this == ite ) ); }
193
195 template< class TT, class DD, class PP, class RR >
196 bool operator < ( const mist_iterator1< TT, DD, PP, RR > &ite ) const { return( data_ < ite.data( ) ); }
197
199 template< class TT, class DD, class PP, class RR >
200 bool operator <=( const mist_iterator1< TT, DD, PP, RR > &ite ) const { return( data_ <= ite.data( ) ); }
201
203 template< class TT, class DD, class PP, class RR >
204 bool operator > ( const mist_iterator1< TT, DD, PP, RR > &ite ) const { return( data_ > ite.data( ) ); }
205
207 template< class TT, class DD, class PP, class RR >
208 bool operator >=( const mist_iterator1< TT, DD, PP, RR > &ite ) const { return( data_ >= ite.data( ) ); }
209 };
210
211
213 template< class T, class Distance, class Pointer, class Reference >
214 inline const mist_iterator1< T, Distance, Pointer, Reference > operator +( const mist_iterator1< T, Distance, Pointer, Reference > &ite, typename mist_iterator1< T, Distance, Pointer, Reference >::difference_type dist )
215 {
216 return( mist_iterator1< T, Distance, Pointer, Reference >( ite ) += dist );
217 }
218
220 template< class T, class Distance, class Pointer, class Reference >
221 inline const mist_iterator1< T, Distance, Pointer, Reference > operator +( typename mist_iterator1< T, Distance, Pointer, Reference >::difference_type dist, const mist_iterator1< T, Distance, Pointer, Reference > &ite )
222 {
223 return( mist_iterator1< T, Distance, Pointer, Reference >( ite ) += dist );
224 }
225
227 template< class T, class Distance, class Pointer, class Reference >
228 inline const mist_iterator1< T, Distance, Pointer, Reference > operator -( const mist_iterator1< T, Distance, Pointer, Reference > &ite, typename mist_iterator1< T, Distance, Pointer, Reference >::difference_type dist )
229 {
230 return( mist_iterator1< T, Distance, Pointer, Reference >( ite ) -= dist );
231 }
232
233
234
244 template< class T, class Distance = ptrdiff_t, class Pointer = T*, class Reference = T& >
245 #if defined( __MIST_MSVC__ ) && __MIST_MSVC__ <= 6
246 class mist_iterator2 : public std::iterator< std::random_access_iterator_tag, T, Distance >
247 #else
248 class mist_iterator2 : public std::iterator< std::random_access_iterator_tag, T, Distance, Pointer, Reference >
249 #endif
250 {
251 public:
252 typedef T value_type;
253 typedef Pointer pointer;
254 typedef Reference reference;
255 typedef size_t size_type;
256 typedef Distance difference_type;
257 typedef Reference const_reference;
258
259 private:
260 pointer data_;
261 difference_type index_;
262 difference_type width_;
263 difference_type step_;
264
265 public:
267 mist_iterator2( pointer p = NULL, difference_type index = 0, difference_type width = 1, difference_type step = 1 )
268 : data_( p ), index_( index ), width_( width ), step_( step )
269 {
270 }
271
273 template< class TT, class DD, class PP, class RR >
274 mist_iterator2( const mist_iterator2< TT, DD, PP, RR > &ite ) : data_( ite.data( ) ), index_( ite.index( ) ), width_( ite.width( ) ), step_( ite.step( ) )
275 {
276 }
277
278
280 template< class TT, class DD, class PP, class RR >
281 const mist_iterator2& operator =( const mist_iterator2< TT, DD, PP, RR > &ite )
282 {
283 if( &ite != this )
284 {
285 data_ = ite.data( );
286 index_ = ite.index( );
287 width_ = ite.width( );
288 step_ = ite.step( );
289 }
290 return( *this );
291 }
292
294 pointer data( )
295 {
296 return( data_ );
297 }
298
300 const pointer data( ) const
301 {
302 return( data_ );
303 }
304
306 difference_type index( ) const
307 {
308 return( index_ );
309 }
310
312 difference_type width( ) const
313 {
314 return( width_ );
315 }
316
318 difference_type step( ) const
319 {
320 return( step_ );
321 }
322
324 reference operator *( )
325 {
326 difference_type step = index_ / width_;
327 difference_type rest = index_ - step * width_;
328 return( *( data_ + rest + step * step_ ) );
329 }
330
332 const_reference operator *( ) const
333 {
334 difference_type step = index_ / width_;
335 difference_type rest = index_ - step * width_;
336 return( *( data_ + rest + step * step_ ) );
337 }
338
340 pointer operator->( ){ return( &**this ); }
341
343 const pointer operator->( ) const { return( &**this ); }
344
346 reference operator []( difference_type dist ){ return( *( *this += dist ) ); }
347
349 const_reference operator []( difference_type dist ) const { return( *( *this += dist ) ); }
350
351
353 mist_iterator2& operator ++( )
354 {
355 *this += 1;
356 return( *this );
357 }
358
360 const mist_iterator2 operator ++( int )
361 {
362 mist_iterator2 old_val( *this );
363 *this += 1;
364 return( old_val );
365 }
366
368 mist_iterator2& operator --( )
369 {
370 *this -= 1;
371 return( *this );
372 }
373
375 const mist_iterator2 operator --( int )
376 {
377 mist_iterator2 old_val( *this );
378 *this -= 1;
379 return( old_val );
380 }
381
383 const mist_iterator2& operator +=( difference_type dist )
384 {
385 index_ += dist;
386 return( *this );
387 }
388
390 const mist_iterator2& operator -=( difference_type dist )
391 {
392 index_ -= dist;
393 return( *this );
394 }
395
396
398 template< class TT, class DD, class PP, class RR >
399 const difference_type operator -( const mist_iterator2< TT, DD, PP, RR > &ite ) const
400 {
401 return( index_ - ite.index( ) );
402 }
403
405 template< class TT, class DD, class PP, class RR >
406 bool operator ==( const mist_iterator2< TT, DD, PP, RR > &ite ) const { return( *this - ite == 0 ); }
407
409 template< class TT, class DD, class PP, class RR >
410 bool operator !=( const mist_iterator2< TT, DD, PP, RR > &ite ) const { return( !( *this == ite ) ); }
411
413 template< class TT, class DD, class PP, class RR >
414 bool operator < ( const mist_iterator2< TT, DD, PP, RR > &ite ) const { return( *this - ite < 0 ); }
415
417 template< class TT, class DD, class PP, class RR >
418 bool operator <=( const mist_iterator2< TT, DD, PP, RR > &ite ) const { return( !( *this > ite ) ); }
419
421 template< class TT, class DD, class PP, class RR >
422 bool operator > ( const mist_iterator2< TT, DD, PP, RR > &ite ) const { return( ite < *this ); }
423
425 template< class TT, class DD, class PP, class RR >
426 bool operator >=( const mist_iterator2< TT, DD, PP, RR > &ite ) const { return( !( *this < ite ) ); }
427 };
428
429
430
432 template< class T, class Distance, class Pointer, class Reference >
433 inline const mist_iterator2< T, Distance, Pointer, Reference > operator +( const mist_iterator2< T, Distance, Pointer, Reference > &ite, typename mist_iterator2< T, Distance, Pointer, Reference >::difference_type dist )
434 {
435 return( mist_iterator2< T, Distance, Pointer, Reference >( ite ) += dist );
436 }
437
439 template< class T, class Distance, class Pointer, class Reference >
440 inline const mist_iterator2< T, Distance, Pointer, Reference > operator +( typename mist_iterator2< T, Distance, Pointer, Reference >::difference_type dist, const mist_iterator2< T, Distance, Pointer, Reference > &ite )
441 {
442 return( mist_iterator2< T, Distance, Pointer, Reference >( ite ) += dist );
443 }
444
446 template< class T, class Distance, class Pointer, class Reference >
447 inline const mist_iterator2< T, Distance, Pointer, Reference > operator -( const mist_iterator2< T, Distance, Pointer, Reference > &ite, typename mist_iterator2< T, Distance, Pointer, Reference >::difference_type dist )
448 {
449 return( mist_iterator2< T, Distance, Pointer, Reference >( ite ) -= dist );
450 }
451
452
453
460 template< class T >
461 class mist_reverse_iterator :
462 #if defined( __MIST_MSVC__ ) && __MIST_MSVC__ <= 6
463 public std::iterator<
464 typename T::iterator_category,
465 typename T::value_type,
466 typename T::difference_type
467 >
468 #else
469 public std::iterator<
470 typename T::iterator_category,
471 typename T::value_type,
472 typename T::difference_type,
473 typename T::pointer,
474 typename T::reference
475 >
476 #endif
477 {
478 private:
479 typedef T iterator_type;
480
481 public:
482 typedef typename T::size_type size_type;
483 typedef typename T::difference_type difference_type;
484 typedef typename T::value_type value_type;
485 typedef typename T::pointer pointer;
486 typedef typename T::reference reference;
487 typedef typename T::const_reference const_reference;
488
489 protected:
490 iterator_type current_iterator_;
491
492 public:
494 mist_reverse_iterator( ){ }
495
497 mist_reverse_iterator( const iterator_type &ite ) : current_iterator_( ite ){ }
498
500 mist_reverse_iterator( const mist_reverse_iterator &ite ) : current_iterator_( ite.current_iterator_ ){ }
501
502
504 const mist_reverse_iterator& operator =( const iterator_type &ite )
505 {
506 current_iterator_ = ite;
507 return( *this );
508 }
509
511 const mist_reverse_iterator& operator =( const mist_reverse_iterator &ite )
512 {
513 current_iterator_ = ite.current_iterator_;
514 return( *this );
515 }
516
517
519 reference operator *()
520 {
521 iterator_type _tmp = current_iterator_;
522 return( *( --_tmp ) );
523 }
524
526 const_reference operator *() const
527 {
528 iterator_type _tmp = current_iterator_;
529 return( *( --_tmp ) );
530 }
531
533 pointer operator->( ){ return( &**this ); }
534
536 const pointer operator->( ) const { return( &**this ); }
537
539 reference operator []( difference_type dist ){ return( *( *this + dist ) ); }
540
542 const_reference operator []( difference_type dist ) const { return( *( *this + dist ) ); }
543
544
546 mist_reverse_iterator& operator ++( )
547 {
548 --current_iterator_;
549 return( *this );
550 }
551
553 const mist_reverse_iterator operator ++( int )
554 {
555 mist_reverse_iterator old_val( *this );
556 current_iterator_--;
557 return( old_val );
558 }
559
561 mist_reverse_iterator& operator --( )
562 {
563 ++current_iterator_;
564 return( *this );
565 }
566
568 const mist_reverse_iterator operator --( int )
569 {
570 mist_reverse_iterator old_val( *this );
571 current_iterator_++;
572 return( old_val );
573 }
574
576 const mist_reverse_iterator& operator +=( difference_type dist )
577 {
578 current_iterator_ -= dist;
579 return( *this );
580 }
581
583 const mist_reverse_iterator& operator -=( difference_type dist )
584 {
585 current_iterator_ += dist;
586 return( *this );
587 }
588
590 const difference_type operator -( const mist_reverse_iterator &ite ) const
591 {
592 return( ite.current_iterator_ - current_iterator_ );
593 }
594
595
596
598 bool operator ==( const mist_reverse_iterator &ite ) const { return( current_iterator_ == ite.current_iterator_ ); }
599
601 bool operator !=( const mist_reverse_iterator &ite ) const { return( !( *this == ite ) ); }
602
604 bool operator < ( const mist_reverse_iterator &ite ) const { return( ite.current_iterator_ < current_iterator_ ); }
605
607 bool operator <=( const mist_reverse_iterator &ite ) const { return( !( *this > ite ) ); }
608
610 bool operator > ( const mist_reverse_iterator &ite ) const { return( ite < *this ); }
611
613 bool operator >=( const mist_reverse_iterator &ite ) const { return( !( *this < ite ) ); }
614 };
615
616
618 template< class T >
619 inline const mist_reverse_iterator< T > operator +( const mist_reverse_iterator< T > &ite1, const mist_reverse_iterator< T > ite2 )
620 {
621 return( mist_reverse_iterator< T >( ite1 ) += ite2 );
622 }
623
625 template< class T >
626 inline const mist_reverse_iterator< T > operator +( const mist_reverse_iterator< T > &ite, typename T::difference_type dist )
627 {
628 return( mist_reverse_iterator< T >( ite ) += dist );
629 }
630
632 template< class T >
633 inline const mist_reverse_iterator< T > operator +( typename T::difference_type dist, const mist_reverse_iterator< T > &ite )
634 {
635 return( mist_reverse_iterator< T >( ite ) += dist );
636 }
637
638
639 //template< class T >
640 //inline const typename mist_reverse_iterator< T >::difference_type operator -( const mist_reverse_iterator< T > &ite1, const mist_reverse_iterator< T > ite2 )
641 //{
642 // return( ite1 - ite2 );
643 //}
644
645
647 template< class T >
648 inline const mist_reverse_iterator< T > operator -( const mist_reverse_iterator< T > &ite, typename T::difference_type dist )
649 {
650 return( mist_reverse_iterator< T >( ite ) -= dist );
651 }
652
653 // mist名前空間の終わり
654 _MIST_END
655
656
657 #endif // __INCLUDE_MIST_ITERATOR_H__
658