This repository has been archived by the owner on May 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathiterator
741 lines (621 loc) · 17.8 KB
/
iterator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
// iterator standard header
#pragma once
#ifndef _ITERATOR_
#define _ITERATOR_
#ifndef RC_INVOKED
#include <istream>
#include <xutility>
#pragma pack(push,_CRT_PACKING)
#pragma warning(push,3)
#pragma push_macro("new")
#undef new
_STD_BEGIN
// TEMPLATE CLASS back_insert_iterator
template<class _Container>
class back_insert_iterator
: public _Outit
{ // wrap pushes to back of container as output iterator
public:
typedef back_insert_iterator<_Container> _Myt;
typedef _Container container_type;
typedef typename _Container::value_type _Valty;
explicit back_insert_iterator(_Container& _Cont)
: container(_STD addressof(_Cont))
{ // construct with container
}
_Myt& operator=(const _Valty& _Val)
{ // push value into container
container->push_back(_Val);
return (*this);
}
_Myt& operator=(_Valty&& _Val)
{ // push value into container
container->push_back(_STD forward<_Valty>(_Val));
return (*this);
}
_Myt& operator*()
{ // pretend to return designated value
return (*this);
}
_Myt& operator++()
{ // pretend to preincrement
return (*this);
}
_Myt operator++(int)
{ // pretend to postincrement
return (*this);
}
protected:
_Container *container; // pointer to container
};
template<class _Container>
struct _Is_checked_helper<back_insert_iterator<_Container> >
: public true_type
{ // mark back_insert_iterator as checked
};
// TEMPLATE FUNCTION back_inserter
template<class _Container> inline
back_insert_iterator<_Container> back_inserter(_Container& _Cont)
{ // return a back_insert_iterator
return (back_insert_iterator<_Container>(_Cont));
}
// TEMPLATE CLASS front_insert_iterator
template<class _Container>
class front_insert_iterator
: public _Outit
{ // wrap pushes to front of container as output iterator
public:
typedef front_insert_iterator<_Container> _Myt;
typedef _Container container_type;
typedef typename _Container::value_type _Valty;
explicit front_insert_iterator(_Container& _Cont)
: container(_STD addressof(_Cont))
{ // construct with container
}
_Myt& operator=(const _Valty& _Val)
{ // push value into container
container->push_front(_Val);
return (*this);
}
_Myt& operator=(_Valty&& _Val)
{ // push value into container
container->push_front(_STD forward<_Valty>(_Val));
return (*this);
}
_Myt& operator*()
{ // pretend to return designated value
return (*this);
}
_Myt& operator++()
{ // pretend to preincrement
return (*this);
}
_Myt operator++(int)
{ // pretend to postincrement
return (*this);
}
protected:
_Container *container; // pointer to container
};
template<class _Container>
struct _Is_checked_helper<front_insert_iterator<_Container> >
: public true_type
{ // mark front_insert_iterator as checked
};
// TEMPLATE FUNCTION front_inserter
template<class _Container> inline
front_insert_iterator<_Container> front_inserter(_Container& _Cont)
{ // return front_insert_iterator
return (front_insert_iterator<_Container>(_Cont));
}
// TEMPLATE CLASS insert_iterator
template<class _Container>
class insert_iterator
: public _Outit
{ // wrap inserts into container as output iterator
public:
typedef insert_iterator<_Container> _Myt;
typedef _Container container_type;
typedef typename _Container::value_type _Valty;
insert_iterator(_Container& _Cont, typename _Container::iterator _Where)
: container(_STD addressof(_Cont)), iter(_Where)
{ // construct with container and iterator
}
_Myt& operator=(const _Valty& _Val)
{ // insert into container and increment stored iterator
iter = container->insert(iter, _Val);
++iter;
return (*this);
}
_Myt& operator=(_Valty&& _Val)
{ // push value into container
iter = container->insert(iter, _STD forward<_Valty>(_Val));
++iter;
return (*this);
}
_Myt& operator*()
{ // pretend to return designated value
return (*this);
}
_Myt& operator++()
{ // pretend to preincrement
return (*this);
}
_Myt& operator++(int)
{ // pretend to postincrement
return (*this);
}
protected:
_Container *container; // pointer to container
typename _Container::iterator iter; // iterator into container
};
template<class _Container>
struct _Is_checked_helper<insert_iterator<_Container> >
: public true_type
{ // mark insert_iterator as checked
};
// TEMPLATE FUNCTION inserter
template<class _Container> inline
insert_iterator<_Container> inserter(_Container& _Cont,
typename _Container::iterator _Where)
{ // return insert_iterator
return (insert_iterator<_Container>(_Cont, _Where));
}
// TEMPLATE CLASS istream_iterator
template<class _Ty,
class _Elem = char,
class _Traits = char_traits<_Elem>,
class _Diff = ptrdiff_t>
class istream_iterator
: public iterator<input_iterator_tag, _Ty, _Diff,
const _Ty *, const _Ty&>
{ // wrap _Ty extracts from input stream as input iterator
typedef istream_iterator<_Ty, _Elem, _Traits, _Diff> _Myt;
public:
typedef _Elem char_type;
typedef _Traits traits_type;
typedef basic_istream<_Elem, _Traits> istream_type;
typedef const _Ty *pointer;
_CONST_FUN istream_iterator()
: _Myistr(0), _Myval()
{ // construct singular iterator
}
istream_iterator(istream_type& _Istr)
: _Myistr(&_Istr)
{ // construct with input stream
_Getval();
}
const _Ty& operator*() const
{ // return designated value
return (_Myval);
}
pointer operator->() const
{ // return pointer to class object
return (_STD pointer_traits<pointer>::pointer_to(**this));
}
_Myt& operator++()
{ // preincrement
_Getval();
return (*this);
}
_Myt operator++(int)
{ // postincrement
_Myt _Tmp = *this;
++*this;
return (_Tmp);
}
bool _Equal(const _Myt& _Right) const
{ // test for iterator equality
return (_Myistr == _Right._Myistr);
}
protected:
void _Getval()
{ // get a _Ty value if possible
if (_Myistr != 0 && !(*_Myistr >> _Myval))
_Myistr = 0;
}
istream_type *_Myistr; // pointer to input stream
_Ty _Myval; // lookahead value (valid if _Myistr is not null)
};
template<class _Ty,
class _Elem,
class _Traits,
class _Diff>
struct _Is_checked_helper<istream_iterator<_Ty, _Elem, _Traits, _Diff> >
: public true_type
{ // mark istream_iterator as checked
};
// istream_iterator TEMPLATE OPERATORS
template<class _Ty,
class _Elem,
class _Traits,
class _Diff> inline
bool operator==(
const istream_iterator<_Ty, _Elem, _Traits, _Diff>& _Left,
const istream_iterator<_Ty, _Elem, _Traits, _Diff>& _Right)
{ // test for istream_iterator equality
return (_Left._Equal(_Right));
}
template<class _Ty,
class _Elem,
class _Traits,
class _Diff> inline
bool operator!=(
const istream_iterator<_Ty, _Elem, _Traits, _Diff>& _Left,
const istream_iterator<_Ty, _Elem, _Traits, _Diff>& _Right)
{ // test for istream_iterator inequality
return (!(_Left == _Right));
}
// TEMPLATE CLASS ostream_iterator
template<class _Ty,
class _Elem = char,
class _Traits = char_traits<_Elem> >
class ostream_iterator
: public _Outit
{ // wrap _Ty inserts to output stream as output iterator
public:
typedef _Elem char_type;
typedef _Traits traits_type;
typedef basic_ostream<_Elem, _Traits> ostream_type;
ostream_iterator(ostream_type& _Ostr,
const _Elem *_Delim = 0)
: _Myostr(&_Ostr), _Mydelim(_Delim)
{ // construct from output stream and delimiter
}
ostream_iterator<_Ty, _Elem, _Traits>& operator=(const _Ty& _Val)
{ // insert value into output stream, followed by delimiter
*_Myostr << _Val;
if (_Mydelim != 0)
*_Myostr << _Mydelim;
return (*this);
}
ostream_iterator<_Ty, _Elem, _Traits>& operator*()
{ // pretend to return designated value
return (*this);
}
ostream_iterator<_Ty, _Elem, _Traits>& operator++()
{ // pretend to preincrement
return (*this);
}
ostream_iterator<_Ty, _Elem, _Traits>& operator++(int)
{ // pretend to postincrement
return (*this);
}
protected:
const _Elem *_Mydelim; // pointer to delimiter string (NB: not freed)
ostream_type *_Myostr; // pointer to output stream
};
template<class _Ty,
class _Elem,
class _Traits>
struct _Is_checked_helper<ostream_iterator<_Ty, _Elem, _Traits> >
: public true_type
{ // mark ostream_iterator as checked
};
_STD_END
_STDEXT_BEGIN
using _STD iterator_traits;
using _STD size_t;
// TEMPLATE CLASS checked_array_iterator
template<class _Iterator>
class checked_array_iterator
{ // wrap an iterator (actually, a pointer) with checking
public:
typedef checked_array_iterator<_Iterator> _Myt;
typedef typename iterator_traits<_Iterator>::iterator_category
iterator_category;
typedef typename iterator_traits<_Iterator>::value_type
value_type;
typedef typename iterator_traits<_Iterator>::difference_type
difference_type;
typedef typename iterator_traits<_Iterator>::pointer
pointer;
typedef typename iterator_traits<_Iterator>::reference
reference;
checked_array_iterator()
: _Myarray(), _Mysize(0), _Myindex(0)
{ // default construct
}
checked_array_iterator(_Iterator _Array, size_t _Size,
size_t _Index = 0)
: _Myarray(_Array), _Mysize(_Size), _Myindex(_Index)
{ // construct with array, size, and optional index
_SCL_SECURE_ALWAYS_VALIDATE(_Index <= _Size);
}
_Iterator base() const
{ // return unwrapped iterator
return (_Myarray + _Myindex);
}
typedef _Iterator _Unchecked_type;
_Myt& _Rechecked(_Unchecked_type _Right)
{ // reset from unchecked iterator
_Myindex = _Right - _Myarray;
return (*this);
}
_Unchecked_type _Unchecked() const
{ // make an unchecked iterator
return (base());
}
reference operator*() const
{ // return designated object
_SCL_SECURE_ALWAYS_VALIDATE_RANGE(_Myarray != 0
&& _Myindex < _Mysize);
return (_Myarray[_Myindex]);
}
pointer operator->() const
{ // return pointer to class object
return (_STD pointer_traits<pointer>::pointer_to(**this));
}
_Myt& operator++()
{ // preincrement
_SCL_SECURE_ALWAYS_VALIDATE_RANGE(_Myarray != 0
&& _Myindex < _Mysize);
++_Myindex;
return (*this);
}
_Myt operator++(int)
{ // postincrement
_Myt _Tmp = *this;
++*this;
return (_Tmp);
}
_Myt& operator--()
{ // predecrement
_SCL_SECURE_ALWAYS_VALIDATE_RANGE(_Myarray != 0
&& 0 < _Myindex);
--_Myindex;
return (*this);
}
_Myt operator--(int)
{ // postdecrement
_Myt _Tmp = *this;
--*this;
return (_Tmp);
}
_Myt& operator+=(difference_type _Off)
{ // increment by integer
_SCL_SECURE_ALWAYS_VALIDATE_RANGE(_Myarray != 0
&& _Myindex + _Off <= _Mysize);
_Myindex += _Off;
return (*this);
}
_Myt operator+(difference_type _Off) const
{ // return this + integer
_Myt _Tmp = *this;
return (_Tmp += _Off);
}
_Myt& operator-=(difference_type _Off)
{ // decrement by integer
return (*this += -_Off);
}
_Myt operator-(difference_type _Off) const
{ // return this - integer
_Myt _Tmp = *this;
return (_Tmp -= _Off);
}
difference_type operator-(const _Myt& _Right) const
{ // return difference of iterators
_SCL_SECURE_ALWAYS_VALIDATE(_Myarray == _Right._Myarray);
return (_Myindex - _Right._Myindex);
}
reference operator[](difference_type _Off) const
{ // subscript
return (*(*this + _Off));
}
bool operator==(const _Myt& _Right) const
{ // test for iterator equality
_SCL_SECURE_ALWAYS_VALIDATE(_Myarray == _Right._Myarray);
return (_Myindex == _Right._Myindex);
}
bool operator!=(const _Myt& _Right) const
{ // test for iterator inequality
return (!(*this == _Right));
}
bool operator<(const _Myt& _Right) const
{ // test if this < _Right
_SCL_SECURE_ALWAYS_VALIDATE(_Myarray == _Right._Myarray);
return (_Myindex < _Right._Myindex);
}
bool operator>(const _Myt& _Right) const
{ // test if this > _Right
return (_Right < *this);
}
bool operator<=(const _Myt& _Right) const
{ // test if this <= _Right
return (!(_Right < *this));
}
bool operator>=(const _Myt& _Right) const
{ // test if this >= _Right
return (!(*this < _Right));
}
private:
_Iterator _Myarray; // beginning of array
size_t _Mysize; // size of array
size_t _Myindex; // offset into array
};
template<class _Iterator> inline
typename checked_array_iterator<_Iterator>::_Unchecked_type
_Unchecked(checked_array_iterator<_Iterator> _Iter)
{ // convert to unchecked
return (_Iter._Unchecked());
}
template<class _Iterator> inline
checked_array_iterator<_Iterator>&
_Rechecked(checked_array_iterator<_Iterator>& _Iter,
typename checked_array_iterator<_Iterator>
::_Unchecked_type _Right)
{ // convert to checked
return (_Iter._Rechecked(_Right));
}
template<class _Iterator> inline
checked_array_iterator<_Iterator> operator+(
typename checked_array_iterator<_Iterator>::difference_type _Off,
checked_array_iterator<_Iterator> _Next)
{ // add offset to iterator
return (_Next += _Off);
}
template<class _Iterator> inline
checked_array_iterator<_Iterator> make_checked_array_iterator(
_Iterator _Array, size_t _Size, size_t _Index = 0)
{ // construct with array, size, and optional index
return (checked_array_iterator<_Iterator>(_Array, _Size, _Index));
}
// TEMPLATE CLASS unchecked_array_iterator
template<class _Iterator>
class unchecked_array_iterator
{ // wrap an iterator (pointer) without checking, to silence warnings
public:
typedef unchecked_array_iterator<_Iterator> _Myt;
typedef typename iterator_traits<_Iterator>::iterator_category
iterator_category;
typedef typename iterator_traits<_Iterator>::value_type
value_type;
typedef typename iterator_traits<_Iterator>::difference_type
difference_type;
typedef typename iterator_traits<_Iterator>::pointer
pointer;
typedef typename iterator_traits<_Iterator>::reference
reference;
unchecked_array_iterator()
: _Myptr()
{ // default construct
}
explicit unchecked_array_iterator(_Iterator _Ptr)
: _Myptr(_Ptr)
{ // construct with pointer
}
_Iterator base() const
{ // return unwrapped iterator
return (_Myptr);
}
typedef _Iterator _Unchecked_type;
_Myt& _Rechecked(_Unchecked_type _Right)
{ // reset from unchecked iterator
_Myptr = _Right;
return (*this);
}
_Unchecked_type _Unchecked() const
{ // make an unchecked iterator
return (base());
}
reference operator*() const
{ // return designated object
return (*_Myptr);
}
pointer operator->() const
{ // return pointer to class object
return (_STD pointer_traits<pointer>::pointer_to(**this));
}
_Myt& operator++()
{ // preincrement
++_Myptr;
return (*this);
}
_Myt operator++(int)
{ // postincrement
_Myt _Tmp = *this;
++*this;
return (_Tmp);
}
_Myt& operator--()
{ // predecrement
--_Myptr;
return (*this);
}
_Myt operator--(int)
{ // postdecrement
_Myt _Tmp = *this;
--*this;
return (_Tmp);
}
_Myt& operator+=(difference_type _Off)
{ // increment by integer
_Myptr += _Off;
return (*this);
}
_Myt operator+(difference_type _Off) const
{ // return this + integer
_Myt _Tmp = *this;
return (_Tmp += _Off);
}
_Myt& operator-=(difference_type _Off)
{ // decrement by integer
return (*this += -_Off);
}
_Myt operator-(difference_type _Off) const
{ // return this - integer
_Myt _Tmp = *this;
return (_Tmp -= _Off);
}
difference_type operator-(const _Myt& _Right) const
{ // return difference of iterators
return (_Myptr - _Right._Myptr);
}
reference operator[](difference_type _Off) const
{ // subscript
return (*(*this + _Off));
}
bool operator==(const _Myt& _Right) const
{ // test for iterator equality
return (_Myptr == _Right._Myptr);
}
bool operator!=(const _Myt& _Right) const
{ // test for iterator inequality
return (!(*this == _Right));
}
bool operator<(const _Myt& _Right) const
{ // test if this < _Right
return (_Myptr < _Right._Myptr);
}
bool operator>(const _Myt& _Right) const
{ // test if this > _Right
return (_Right < *this);
}
bool operator<=(const _Myt& _Right) const
{ // test if this <= _Right
return (!(_Right < *this));
}
bool operator>=(const _Myt& _Right) const
{ // test if this >= _Right
return (!(*this < _Right));
}
private:
_Iterator _Myptr; // underlying pointer
};
template<class _Iterator> inline
typename unchecked_array_iterator<_Iterator>::_Unchecked_type
_Unchecked(unchecked_array_iterator<_Iterator> _Iter)
{ // convert to unchecked
return (_Iter._Unchecked());
}
template<class _Iterator> inline
unchecked_array_iterator<_Iterator>&
_Rechecked(unchecked_array_iterator<_Iterator>& _Iter,
typename unchecked_array_iterator<_Iterator>
::_Unchecked_type _Right)
{ // convert to checked
return (_Iter._Rechecked(_Right));
}
template<class _Iterator> inline
unchecked_array_iterator<_Iterator> operator+(
typename unchecked_array_iterator<_Iterator>::difference_type _Off,
unchecked_array_iterator<_Iterator> _Next)
{ // add offset to iterator
return (_Next += _Off);
}
template<class _Iterator> inline
unchecked_array_iterator<_Iterator> make_unchecked_array_iterator(
_Iterator _Ptr)
{ // construct with pointer
return (unchecked_array_iterator<_Iterator>(_Ptr));
}
_STDEXT_END
#pragma pop_macro("new")
#pragma warning(pop)
#pragma pack(pop)
#endif /* RC_INVOKED */
#endif /* _ITERATOR_ */
/*
* Copyright (c) by P.J. Plauger. All rights reserved.
* Consult your license regarding permissions and restrictions.
V6.50:0009 */