@@ -74,6 +74,7 @@ namespace chen
74
74
virtual Reference operator *() const = 0 ;
75
75
virtual void operator ++() = 0 ;
76
76
virtual bool operator ==(const base &o) const = 0 ;
77
+ virtual Distance distance () const = 0;
77
78
};
78
79
79
80
/* *
@@ -90,6 +91,12 @@ namespace chen
90
91
virtual Reference operator *() const = 0 ;
91
92
virtual void operator ++() = 0 ;
92
93
virtual bool operator ==(const base &o) const = 0 ;
94
+ virtual Distance distance () const
95
+ {
96
+ // only valid in input iterator
97
+ // use std::distance in other iterators
98
+ return 0 ;
99
+ }
93
100
94
101
public:
95
102
virtual void operator --() = 0 ;
@@ -109,6 +116,12 @@ namespace chen
109
116
virtual Reference operator *() const = 0 ;
110
117
virtual void operator ++() = 0 ;
111
118
virtual bool operator ==(const base &o) const = 0 ;
119
+ virtual Distance distance () const
120
+ {
121
+ // only valid in input iterator
122
+ // use std::distance in other iterators
123
+ return 0 ;
124
+ }
112
125
113
126
public:
114
127
virtual void operator --() = 0 ;
@@ -157,6 +170,7 @@ namespace chen
157
170
virtual void operator ++() override
158
171
{
159
172
++this ->_it ;
173
+ ++this ->_distance ;
160
174
}
161
175
162
176
virtual bool operator ==(const super_class &o) const override
@@ -165,8 +179,14 @@ namespace chen
165
179
return this ->_it == tmp._it ;
166
180
}
167
181
182
+ virtual Distance distance () const override
183
+ {
184
+ return this ->_distance ;
185
+ }
186
+
168
187
protected:
169
188
Iterator _it;
189
+ Distance _distance = 0 ;
170
190
};
171
191
172
192
/* *
@@ -371,6 +391,14 @@ namespace chen
371
391
return !(*this == o);
372
392
}
373
393
394
+ Distance distance () const
395
+ {
396
+ // since input iterator is single-pass, we can't use std::distance on it
397
+ // however, in some cases we want to know input iterator's position, so I add this method
398
+ // @caution don't use this method if your category is not input iterator
399
+ return this ->_ptr ->distance ();
400
+ }
401
+
374
402
/* *
375
403
* Bidirectional iterator
376
404
*/
@@ -459,67 +487,4 @@ namespace chen
459
487
460
488
template <typename Value, typename Reference = Value&, typename Pointer = Value*, typename Distance = std::ptrdiff_t >
461
489
using random_iterator = iterator<std::random_access_iterator_tag, Value, Reference, Pointer, Distance>;
462
-
463
- /* *
464
- * Position iterator
465
- * since input iterator is single-pass, we can't use std::distance on it
466
- * however, in some cases we want to know input iterator's position, so I add this iterator
467
- */
468
- template <typename Value, typename Reference = Value&, typename Pointer = Value*, typename Distance = std::ptrdiff_t >
469
- class position_iterator : public input_iterator <Value, Reference, Pointer, Distance>
470
- {
471
- public:
472
- typedef input_iterator<Value, Reference, Pointer, Distance> super_class;
473
-
474
- using typename super_class::value_type;
475
- using typename super_class::difference_type;
476
- using typename super_class::pointer;
477
- using typename super_class::reference;
478
- using typename super_class::iterator_category;
479
- using typename super_class::data_type;
480
-
481
- public:
482
- /* *
483
- * Construct by proxy
484
- */
485
- position_iterator (const iterator_helper::proxy<Value, data_type> &p) : super_class(p)
486
- {
487
- }
488
-
489
- /* *
490
- * Wrap another iterator
491
- */
492
- template <typename Iterator>
493
- position_iterator (Iterator it) : super_class(it)
494
- {
495
- }
496
-
497
- public:
498
- /* *
499
- * Current position after increment
500
- */
501
- Distance distance () const
502
- {
503
- return this ->_distance ;
504
- }
505
-
506
- public:
507
- /* *
508
- * Input & Forward iterator
509
- */
510
- super_class& operator ++()
511
- {
512
- ++this ->_distance ;
513
- return super_class::operator ++();
514
- }
515
-
516
- iterator_helper::proxy<Value, data_type> operator ++(int )
517
- {
518
- ++this ->_distance ; // don't effect on proxy
519
- return super_class::operator ++(0 );
520
- }
521
-
522
- private:
523
- Distance _distance = 0 ;
524
- };
525
490
}
0 commit comments