-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathiterator.h
1281 lines (1035 loc) · 43.2 KB
/
iterator.h
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_ITERATOR_H
#define EASTL_ITERATOR_H
#include <eastl/internal/config.h>
#include <eastl/internal/move_help.h>
#include <eastl/internal/type_detected.h>
#include <eastl/internal/type_void_t.h>
#include <eastl/internal/memory_base.h>
#include <eastl/initializer_list.h>
EA_DISABLE_ALL_VC_WARNINGS();
#include <stddef.h>
EA_RESTORE_ALL_VC_WARNINGS();
// If the user has specified that we use std iterator
// categories instead of EASTL iterator categories,
// then #include <iterator>.
#if EASTL_STD_ITERATOR_CATEGORY_ENABLED
EA_DISABLE_ALL_VC_WARNINGS();
#include <iterator>
EA_RESTORE_ALL_VC_WARNINGS();
#endif
EA_DISABLE_VC_WARNING(4619); // There is no warning number 'number'.
EA_DISABLE_VC_WARNING(4217); // Member template functions cannot be used for copy-assignment or copy-construction.
#if defined(EASTL_PRAGMA_ONCE_SUPPORTED)
#pragma once // Some compilers (e.g. VC++) benefit significantly from using this. We've measured 3-4% build speed improvements in apps as a result.
#endif
namespace eastl
{
/// iterator_status_flag
///
/// Defines the validity status of an iterator. This is primarily used for
/// iterator validation in debug builds. These are implemented as OR-able
/// flags (as opposed to mutually exclusive values) in order to deal with
/// the nature of iterator status. In particular, an iterator may be valid
/// but not dereferencable, as in the case with an iterator to container end().
/// An iterator may be valid but also dereferencable, as in the case with an
/// iterator to container begin().
///
enum iterator_status_flag
{
isf_none = 0x00, /// This is called none and not called invalid because it is not strictly the opposite of invalid.
isf_valid = 0x01, /// The iterator is valid, which means it is in the range of [begin, end].
isf_current = 0x02, /// The iterator is valid and points to the same element it did when created. For example, if an iterator points to vector::begin() but an element is inserted at the front, the iterator is valid but not current. Modification of elements in place do not make iterators non-current.
isf_can_dereference = 0x04 /// The iterator is dereferencable, which means it is in the range of [begin, end). It may or may not be current.
};
// The following declarations are taken directly from the C++ standard document.
// input_iterator_tag, etc.
// iterator
// iterator_traits
// reverse_iterator
// Iterator categories
// Every iterator is defined as belonging to one of the iterator categories that
// we define here. These categories come directly from the C++ standard.
#if !EASTL_STD_ITERATOR_CATEGORY_ENABLED // If we are to use our own iterator category definitions...
struct input_iterator_tag { };
struct output_iterator_tag { };
struct forward_iterator_tag : public input_iterator_tag { };
struct bidirectional_iterator_tag : public forward_iterator_tag { };
struct random_access_iterator_tag : public bidirectional_iterator_tag { };
// Originally an extension to the C++ standard, standardized in C++20.
// Contiguous ranges are more than random access, they are physically contiguous.
// Note: Pointers are contiguous but the specialization of iterator_traits for pointers defines
// iterator_traits<T>::iterator_category as random_access_iterator_tag and thus users must
// explicitly check both the iterator_category and the type.
struct contiguous_iterator_tag : public random_access_iterator_tag { };
#endif
// struct iterator
template <typename Category, typename T, typename Distance = ptrdiff_t,
typename Pointer = T*, typename Reference = T&>
struct EASTL_REMOVE_AT_2024_APRIL iterator
{
typedef Category iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef Pointer pointer;
typedef Reference reference;
};
// struct iterator_traits
namespace internal
{
// Helper to make iterator_traits SFINAE friendly as N3844 requires.
template <typename Iterator, class = void>
struct default_iterator_traits {};
template <typename Iterator>
struct default_iterator_traits<
Iterator,
void_t<
typename Iterator::iterator_category,
typename Iterator::value_type,
typename Iterator::difference_type,
typename Iterator::pointer,
typename Iterator::reference
>
>
{
typedef typename Iterator::iterator_category iterator_category;
typedef typename Iterator::value_type value_type;
typedef typename Iterator::difference_type difference_type;
typedef typename Iterator::pointer pointer;
typedef typename Iterator::reference reference;
};
}
template <typename Iterator>
struct iterator_traits : internal::default_iterator_traits<Iterator> {};
template <typename T>
struct iterator_traits<T*>
{
typedef EASTL_ITC_NS::random_access_iterator_tag iterator_category; // To consider: Change this to contiguous_iterator_tag for the case that
typedef T value_type; // EASTL_ITC_NS is "eastl" instead of "std".
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
};
template <typename T>
struct iterator_traits<const T*>
{
typedef EASTL_ITC_NS::random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef const T* pointer;
typedef const T& reference;
};
/// is_iterator_wrapper
///
/// Tells if an Iterator type is a wrapper type as opposed to a regular type.
/// Relies on the class declaring a member function called unwrap.
///
/// Examples of wrapping iterators:
/// generic_iterator
/// move_iterator
/// reverse_iterator<T> (if T is a wrapped iterator)
/// Examples of non-wrapping iterators:
/// iterator
/// list::iterator
/// char*
///
/// Example behavior:
/// is_iterator_wrapper(int*)::value => false
/// is_iterator_wrapper(eastl::array<char>*)::value => false
/// is_iterator_wrapper(eastl::vector<int>::iterator)::value => false
/// is_iterator_wrapper(eastl::generic_iterator<int*>)::value => true
/// is_iterator_wrapper(eastl::move_iterator<eastl::array<int>::iterator>)::value => true
/// is_iterator_wrapper(eastl::reverse_iterator<int*>)::value => false
/// is_iterator_wrapper(eastl::reverse_iterator<eastl::move_iterator<int*>>)::value => true
///
template<typename Iterator>
class EASTL_REMOVE_AT_2024_SEPT is_iterator_wrapper
{
#if defined(EA_COMPILER_CLANG) || defined(EA_COMPILER_CLANG_CL)
// Using a default template type parameter trick here because
// of a bug in clang that makes the other implementation not
// work when unwrap() is private and this is class is a
// friend.
// See: https://bugs.llvm.org/show_bug.cgi?id=25334
template<typename T, typename U = decltype(eastl::declval<T>().unwrap())>
using detect_has_unwrap = U;
#else
// Note: the above implementation does not work on GCC when
// unwrap() is private and this class is a friend. So we're
// forced to diverge here to support both GCC and clang.
template<typename T>
using detect_has_unwrap = decltype(eastl::declval<T>().unwrap());
#endif
public:
static const bool value = eastl::is_detected<detect_has_unwrap, Iterator>::value;
};
/// unwrap_iterator
///
/// Takes a wrapper Iterator (e.g. move_iterator, reverse_iterator, generic_iterator) instance
/// and returns the wrapped iterator type. If Iterator is not a wrapper (including being a pointer),
/// or is not an iterator, then this function returns it as-is.
/// unwrap_iterator unwraps only a single layer of iterator at a time. You need to call it twice,
/// for example, to unwrap two layers of iterators.
///
/// Example usage:
/// int* pInt = unwrap_iterator(&pIntArray[15]);
/// int* pInt = unwrap_iterator(generic_iterator(&pIntArray[15]));
/// MyVector::iterator it = unwrap_iterator(myVector.begin());
/// MyVector::iterator it = unwrap_iterator(move_iterator(myVector.begin()));
///
template <typename Iterator, bool isWrapper>
struct EASTL_REMOVE_AT_2024_SEPT is_iterator_wrapper_helper
{
using iterator_type = Iterator;
static iterator_type get_unwrapped(Iterator it) { return it; }
};
// Note: some compilers (notably GCC) trigger deprecation warnings when doing template
// specialization if the main template is derpecated, so turn the warning off here. If this
// specialization is used, the warning will still trigger in the user code, this just
// disables the warning in this declaration.
EASTL_INTERNAL_DISABLE_DEPRECATED()
template <typename Iterator>
struct EASTL_REMOVE_AT_2024_SEPT is_iterator_wrapper_helper<Iterator, true>
{
// get_unwrapped must return by value since we're returning
// it.unwrap(), and `it` will be out of scope as soon as
// get_unwrapped returns.
using iterator_type =
typename eastl::remove_cvref<decltype(eastl::declval<Iterator>().unwrap())>::type;
static iterator_type get_unwrapped(Iterator it) { return it.unwrap(); }
};
template <typename Iterator>
EASTL_REMOVE_AT_2024_SEPT inline typename is_iterator_wrapper_helper<Iterator, eastl::is_iterator_wrapper<Iterator>::value>::iterator_type unwrap_iterator(Iterator it)
{ return eastl::is_iterator_wrapper_helper<Iterator, eastl::is_iterator_wrapper<Iterator>::value>::get_unwrapped(it); }
EASTL_INTERNAL_RESTORE_DEPRECATED()
/// reverse_iterator
///
/// From the C++ standard:
/// Bidirectional and random access iterators have corresponding reverse
/// iterator adaptors that iterate through the data structure in the
/// opposite direction. They have the same signatures as the corresponding
/// iterators. The fundamental relation between a reverse iterator and its
/// corresponding iterator i is established by the identity:
/// &*(reverse_iterator(i)) == &*(i - 1).
/// This mapping is dictated by the fact that while there is always a pointer
/// past the end of an array, there might not be a valid pointer before the
/// beginning of an array.
///
template <typename Iterator>
class reverse_iterator
{
public:
typedef Iterator iterator_type;
typedef typename eastl::iterator_traits<Iterator>::iterator_category iterator_category;
typedef typename eastl::iterator_traits<Iterator>::value_type value_type;
typedef typename eastl::iterator_traits<Iterator>::difference_type difference_type;
typedef typename eastl::iterator_traits<Iterator>::pointer pointer;
typedef typename eastl::iterator_traits<Iterator>::reference reference;
protected:
Iterator mIterator;
public:
EA_CPP14_CONSTEXPR reverse_iterator() // It's important that we construct mIterator, because if Iterator
: mIterator() { } // is a pointer, there's a difference between doing it and not.
EA_CPP14_CONSTEXPR explicit reverse_iterator(iterator_type i)
: mIterator(i) { }
template <typename U>
EA_CPP14_CONSTEXPR reverse_iterator(const reverse_iterator<U>& ri)
: mIterator(ri.base()) { }
template <typename U>
EA_CPP14_CONSTEXPR reverse_iterator<Iterator>& operator=(const reverse_iterator<U>& ri)
{ mIterator = ri.base(); return *this; }
EA_CPP14_CONSTEXPR iterator_type base() const
{ return mIterator; }
EA_CPP14_CONSTEXPR reference operator*() const
{
iterator_type i(mIterator);
return *--i;
}
EA_CPP14_CONSTEXPR pointer operator->() const
{ return &(operator*()); }
EA_CPP14_CONSTEXPR reverse_iterator& operator++()
{ --mIterator; return *this; }
EA_CPP14_CONSTEXPR reverse_iterator operator++(int)
{
reverse_iterator ri(*this);
--mIterator;
return ri;
}
EA_CPP14_CONSTEXPR reverse_iterator& operator--()
{ ++mIterator; return *this; }
EA_CPP14_CONSTEXPR reverse_iterator operator--(int)
{
reverse_iterator ri(*this);
++mIterator;
return ri;
}
EA_CPP14_CONSTEXPR reverse_iterator operator+(difference_type n) const
{ return reverse_iterator(mIterator - n); }
EA_CPP14_CONSTEXPR reverse_iterator& operator+=(difference_type n)
{ mIterator -= n; return *this; }
EA_CPP14_CONSTEXPR reverse_iterator operator-(difference_type n) const
{ return reverse_iterator(mIterator + n); }
EA_CPP14_CONSTEXPR reverse_iterator& operator-=(difference_type n)
{ mIterator += n; return *this; }
// http://cplusplus.github.io/LWG/lwg-defects.html#386,
// http://llvm.org/bugs/show_bug.cgi?id=17883
// random_access_iterator operator[] is merely required to return something convertible to reference.
// reverse_iterator operator[] can't necessarily know what to return as the underlying iterator
// operator[] may return something other than reference.
EA_CPP14_CONSTEXPR reference operator[](difference_type n) const
{ return mIterator[-n - 1]; }
EASTL_INTERNAL_DISABLE_DEPRECATED() // 'is_iterator_wrapper': was declared deprecated
private:
using base_wrapped_iterator_type =
typename eastl::is_iterator_wrapper_helper<Iterator,
eastl::is_iterator_wrapper<Iterator>::value>::iterator_type;
// Unwrapping interface, not part of the public API.
template <typename U = iterator_type>
EASTL_REMOVE_AT_2024_SEPT EA_CPP14_CONSTEXPR typename eastl::enable_if<eastl::is_iterator_wrapper<U>::value, reverse_iterator<base_wrapped_iterator_type>>::type unwrap() const
{ return reverse_iterator<base_wrapped_iterator_type>(unwrap_iterator(mIterator)); }
// The unwrapper helpers need access to unwrap() (when it exists).
using this_type = reverse_iterator<Iterator>;
friend is_iterator_wrapper_helper<this_type, is_iterator_wrapper<iterator_type>::value>;
friend is_iterator_wrapper<this_type>;
EASTL_INTERNAL_RESTORE_DEPRECATED()
};
// The C++ library working group has tentatively approved the usage of two
// template parameters (Iterator1 and Iterator2) in order to allow reverse_iterators
// and const_reverse iterators to be comparable. This is a similar issue to the
// C++ defect report #179 regarding comparison of container iterators and const_iterators.
//
// libstdc++ reports that std::relops breaks the usage of two iterator types and if we
// want to support relops then we need to also make versions of each of below with
// a single template parameter to placate std::relops. But relops is hardly used due to
// the troubles it causes and so we are avoiding support here until somebody complains about it.
template <typename Iterator1, typename Iterator2>
EA_CPP14_CONSTEXPR inline bool
operator==(const reverse_iterator<Iterator1>& a, const reverse_iterator<Iterator2>& b)
{ return a.base() == b.base(); }
template <typename Iterator1, typename Iterator2>
EA_CPP14_CONSTEXPR inline bool
operator<(const reverse_iterator<Iterator1>& a, const reverse_iterator<Iterator2>& b)
{ return a.base() > b.base(); }
template <typename Iterator1, typename Iterator2>
EA_CPP14_CONSTEXPR inline bool
operator!=(const reverse_iterator<Iterator1>& a, const reverse_iterator<Iterator2>& b)
{ return a.base() != b.base(); }
template <typename Iterator1, typename Iterator2>
EA_CPP14_CONSTEXPR inline bool
operator>(const reverse_iterator<Iterator1>& a, const reverse_iterator<Iterator2>& b)
{ return a.base() < b.base(); }
template <typename Iterator1, typename Iterator2>
EA_CPP14_CONSTEXPR inline bool
operator<=(const reverse_iterator<Iterator1>& a, const reverse_iterator<Iterator2>& b)
{ return a.base() >= b.base(); }
template <typename Iterator1, typename Iterator2>
EA_CPP14_CONSTEXPR inline bool
operator>=(const reverse_iterator<Iterator1>& a, const reverse_iterator<Iterator2>& b)
{ return a.base() <= b.base(); }
template <typename Iterator1, typename Iterator2>
EA_CPP14_CONSTEXPR inline typename reverse_iterator<Iterator1>::difference_type
operator-(const reverse_iterator<Iterator1>& a, const reverse_iterator<Iterator2>& b)
{ return b.base() - a.base(); }
template <typename Iterator>
EA_CPP14_CONSTEXPR inline reverse_iterator<Iterator>
operator+(typename reverse_iterator<Iterator>::difference_type n, const reverse_iterator<Iterator>& a)
{ return reverse_iterator<Iterator>(a.base() - n); }
/// is_reverse_iterator
///
/// This is a type traits extension utility.
/// Given an iterator, tells if it's a reverse_iterator vs anything else.
/// If it's a reverse iterator wrapped by another iterator then value is false.
/// To consider: Detect that if it's a move_iterator<reverse_iterator> and unwrap
/// move_iterator so we can detect that underneath it's reverse_iterator.
///
template <typename T>
struct EASTL_REMOVE_AT_2024_SEPT is_reverse_iterator
: public eastl::false_type {};
// Note: some compilers (notably GCC) trigger deprecation warnings when doing template
// specialization if the main template is derpecated, so turn the warning off here. If this
// specialization is used, the warning will still trigger in the user code, this just
// disables the warning in this declaration.
EASTL_INTERNAL_DISABLE_DEPRECATED()
template<typename Iterator>
struct EASTL_REMOVE_AT_2024_SEPT is_reverse_iterator<eastl::reverse_iterator<Iterator>>
: public eastl::true_type {};
EASTL_INTERNAL_RESTORE_DEPRECATED()
/// unwrap_reverse_iterator is not implemented since there's no
/// good use case and there's some abiguitiy. Note that
/// unwrap_iterator(reverse_iterator<T>) returns
/// reverse_iterator<unwrap(T)>. However, given what
/// unwrap_generic_iterator and unwrap_move_iterator do, one might
/// expect unwrap_reverse_iterator(reverse_iterator<T>) to return
/// T, which is not the same. To avoid that confusion, and because
/// there's no current use case for this, we don't provide
/// unwrap_reverse_iterator.
/// move_iterator
///
/// From the C++11 Standard, section 24.5.3.1:
/// Class template move_iterator is an iterator adaptor with the same behavior as the underlying iterator
/// except that its dereference operator implicitly converts the value returned by the underlying iterator's
/// dereference operator to an rvalue reference. Some generic algorithms can be called with move iterators to
/// replace copying with moving.
template<typename Iterator>
class move_iterator // Don't inherit from iterator.
{
private:
using WrappedIteratorReference = typename iterator_traits<Iterator>::reference;
public:
typedef Iterator iterator_type;
typedef iterator_traits<Iterator> traits_type;
typedef typename traits_type::iterator_category iterator_category; // todo: use is_contiguous_iterator<Iterator> to correctly identify pointers as contiguous?
typedef typename traits_type::value_type value_type;
typedef typename traits_type::difference_type difference_type;
typedef Iterator pointer;
using reference = conditional_t<is_reference<WrappedIteratorReference>::value,
remove_reference_t<WrappedIteratorReference>&&,
WrappedIteratorReference>;
protected:
iterator_type mIterator;
public:
move_iterator()
: mIterator()
{
}
explicit move_iterator(iterator_type mi)
: mIterator(mi) { }
template<typename U>
move_iterator(const move_iterator<U>& mi)
: mIterator(mi.base())
{
}
template <typename U>
move_iterator& operator=(const move_iterator<U>& mi)
{
mIterator = mi.mIterator;
return *this;
}
iterator_type base() const
{ return mIterator; }
reference operator*() const { return static_cast<reference>(*mIterator); }
pointer operator->() const
{ return mIterator; }
move_iterator& operator++()
{
++mIterator;
return *this;
}
move_iterator operator++(int)
{
move_iterator tempMoveIterator = *this;
++mIterator;
return tempMoveIterator;
}
move_iterator& operator--()
{
--mIterator;
return *this;
}
move_iterator operator--(int)
{
move_iterator tempMoveIterator = *this;
--mIterator;
return tempMoveIterator;
}
move_iterator operator+(difference_type n) const
{ return move_iterator(mIterator + n); }
move_iterator& operator+=(difference_type n)
{
mIterator += n;
return *this;
}
move_iterator operator-(difference_type n) const
{ return move_iterator(mIterator - n); }
move_iterator& operator-=(difference_type n)
{
mIterator -= n;
return *this;
}
reference operator[](difference_type n) const
{ return eastl::move(mIterator[n]); }
EASTL_INTERNAL_DISABLE_DEPRECATED() // 'is_iterator_wrapper': was declared deprecated
private:
// Unwrapping interface, not part of the public API.
EASTL_REMOVE_AT_2024_SEPT iterator_type unwrap() const
{ return mIterator; }
// The unwrapper helpers need access to unwrap().
using this_type = move_iterator<Iterator>;
friend is_iterator_wrapper_helper<this_type, true>;
friend is_iterator_wrapper<this_type>;
EASTL_INTERNAL_RESTORE_DEPRECATED()
};
template<typename Iterator1, typename Iterator2>
inline bool
operator==(const move_iterator<Iterator1>& a, const move_iterator<Iterator2>& b)
{ return a.base() == b.base(); }
template<typename Iterator1, typename Iterator2>
inline bool
operator!=(const move_iterator<Iterator1>& a, const move_iterator<Iterator2>& b)
{ return !(a == b); }
template<typename Iterator1, typename Iterator2>
inline bool
operator<(const move_iterator<Iterator1>& a, const move_iterator<Iterator2>& b)
{ return a.base() < b.base(); }
template<typename Iterator1, typename Iterator2>
inline bool
operator<=(const move_iterator<Iterator1>& a, const move_iterator<Iterator2>& b)
{ return !(b < a); }
template<typename Iterator1, typename Iterator2>
inline bool
operator>(const move_iterator<Iterator1>& a, const move_iterator<Iterator2>& b)
{ return b < a; }
template<typename Iterator1, typename Iterator2>
inline bool
operator>=(const move_iterator<Iterator1>& a, const move_iterator<Iterator2>& b)
{ return !(a < b); }
template<typename Iterator1, typename Iterator2>
inline auto
operator-(const move_iterator<Iterator1>& a, const move_iterator<Iterator2>& b) -> decltype(a.base() - b.base())
{ return a.base() - b.base(); }
template<typename Iterator>
inline move_iterator<Iterator>
operator+(typename move_iterator<Iterator>::difference_type n, const move_iterator<Iterator>& a)
{ return a + n; }
template<typename Iterator>
inline move_iterator<Iterator> make_move_iterator(Iterator i)
{ return move_iterator<Iterator>(i); }
// make_move_if_noexcept_iterator returns move_iterator<Iterator> if the Iterator is of a noexcept type;
// otherwise returns Iterator as-is. The point of this is to be able to avoid moves that can generate exceptions and instead
// fall back to copies or whatever the default IteratorType::operator* returns for use by copy/move algorithms.
// To consider: merge the conditional expression usage here with the one used by move_if_noexcept, as they are the same condition.
#if EASTL_EXCEPTIONS_ENABLED
template <typename Iterator, typename IteratorType = typename eastl::conditional<eastl::is_nothrow_move_constructible<typename eastl::iterator_traits<Iterator>::value_type>::value ||
!eastl::is_copy_constructible<typename eastl::iterator_traits<Iterator>::value_type>::value,
eastl::move_iterator<Iterator>, Iterator>::type>
inline IteratorType make_move_if_noexcept_iterator(Iterator i)
{ return IteratorType(i); }
#else
// Else there are no exceptions and thus we always return a move_iterator.
template <typename Iterator>
inline eastl::move_iterator<Iterator> make_move_if_noexcept_iterator(Iterator i)
{ return eastl::move_iterator<Iterator>(i); }
#endif
/// is_move_iterator
///
/// This is a type traits extension utility.
/// Given an iterator, tells if it's a move iterator vs anything else.
/// Example usage (though somewhat useless):
/// template <typename T>
/// bool IsMoveIterator() { return typename eastl::is_move_iterator<T>::value; }
///
template <typename T>
struct EASTL_REMOVE_AT_2024_SEPT is_move_iterator
: public eastl::false_type {};
// Note: some compilers (notably GCC) trigger deprecation warnings when doing template
// specialization if the main template is derpecated, so turn the warning off here. If this
// specialization is used, the warning will still trigger in the user code, this just
// disables the warning in this declaration.
EASTL_INTERNAL_DISABLE_DEPRECATED()
template<typename Iterator>
struct EASTL_REMOVE_AT_2024_SEPT is_move_iterator<eastl::move_iterator<Iterator>>
: public eastl::true_type {};
EASTL_INTERNAL_RESTORE_DEPRECATED()
/// unwrap_move_iterator
///
/// Returns `it.base()` if it's a move_iterator, else returns `it` as-is.
///
/// Example usage:
/// vector<int> intVector;
/// eastl::move_iterator<vector<int>::iterator> moveIterator(intVector.begin());
/// vector<int>::iterator it = unwrap_move_iterator(moveIterator);
///
EASTL_INTERNAL_DISABLE_DEPRECATED() // is_iterator_wrapper_helper is deprecated
template <typename Iterator>
EASTL_REMOVE_AT_2024_SEPT inline typename eastl::is_iterator_wrapper_helper<Iterator, eastl::is_move_iterator<Iterator>::value>::iterator_type unwrap_move_iterator(Iterator it)
{
// get_unwrapped(it) -> it.unwrap() which is equivalent to `it.base()` for move_iterator and to `it` otherwise.
return eastl::is_iterator_wrapper_helper<Iterator, eastl::is_move_iterator<Iterator>::value>::get_unwrapped(it);
}
EASTL_INTERNAL_RESTORE_DEPRECATED()
/// back_insert_iterator
///
/// A back_insert_iterator is simply a class that acts like an iterator but when you
/// assign a value to it, it calls pushBack on the container with the value.
///
template <typename Container>
class back_insert_iterator
{
public:
typedef back_insert_iterator<Container> this_type;
typedef Container container_type;
typedef typename Container::const_reference const_reference;
typedef EASTL_ITC_NS::output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
protected:
Container* container;
public:
explicit back_insert_iterator(Container& x)
: container(eastl::addressof(x)) { }
back_insert_iterator(const back_insert_iterator&) = default;
back_insert_iterator& operator=(const back_insert_iterator&) = default;
back_insert_iterator& operator=(const typename Container::value_type& value)
{ container->pushBack(value); return *this; }
back_insert_iterator& operator=(typename Container::value_type&& value)
{ container->pushBack(eastl::move(value)); return *this; }
back_insert_iterator& operator*()
{ return *this; }
back_insert_iterator& operator++()
{ return *this; } // This is by design.
back_insert_iterator operator++(int)
{ return *this; } // This is by design.
};
/// back_inserter
///
/// Creates an instance of a back_insert_iterator.
///
template <typename Container>
inline back_insert_iterator<Container>
back_inserter(Container& x)
{ return back_insert_iterator<Container>(x); }
/// front_insert_iterator
///
/// A front_insert_iterator is simply a class that acts like an iterator but when you
/// assign a value to it, it calls pushFront on the container with the value.
///
template <typename Container>
class front_insert_iterator
{
public:
typedef front_insert_iterator<Container> this_type;
typedef Container container_type;
typedef typename Container::const_reference const_reference;
typedef EASTL_ITC_NS::output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
protected:
Container* container;
public:
explicit front_insert_iterator(Container& x) : container(eastl::addressof(x)) {}
front_insert_iterator(const front_insert_iterator&) = default;
front_insert_iterator& operator=(const front_insert_iterator&) = default;
front_insert_iterator& operator=(const typename Container::value_type& value)
{ container->pushFront(value); return *this; }
front_insert_iterator& operator*()
{ return *this; }
front_insert_iterator& operator++()
{ return *this; } // This is by design.
front_insert_iterator operator++(int)
{ return *this; } // This is by design.
};
/// front_inserter
///
/// Creates an instance of a front_insert_iterator.
///
template <typename Container>
inline front_insert_iterator<Container>
front_inserter(Container& x)
{ return front_insert_iterator<Container>(x); }
/// insert_iterator
///
/// An insert_iterator is like an iterator except that when you assign a value to it,
/// the insert_iterator inserts the value into the container and increments the iterator.
///
/// insert_iterator is an iterator adaptor that functions as an OutputIterator:
/// assignment through an insert_iterator inserts an object into a container.
/// Specifically, if ii is an insert_iterator, then ii keeps track of a container c and
/// an insertion point p; the expression *ii = x performs the insertion container.insert(p, x).
///
/// If you assign through an insert_iterator several times, then you will be inserting
/// several elements into the underlying container. In the case of a sequence, they will
/// appear at a particular location in the underlying sequence, in the order in which
/// they were inserted: one of the arguments to insert_iterator's constructor is an
/// iterator p, and the new range will be inserted immediately before p.
///
template <typename Container>
class insert_iterator
{
public:
typedef Container container_type;
typedef typename Container::iterator iterator_type;
typedef typename Container::const_reference const_reference;
typedef EASTL_ITC_NS::output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
protected:
Container* container;
iterator_type it;
public:
insert_iterator(Container& x, iterator_type itNew)
: container(eastl::addressof(x)), it(itNew) {}
insert_iterator(const insert_iterator&) = default;
insert_iterator& operator=(const insert_iterator&) = default;
insert_iterator& operator=(const typename Container::value_type& value)
{
it = container->insert(it, value);
++it;
return *this;
}
insert_iterator& operator*()
{ return *this; }
insert_iterator& operator++()
{ return *this; } // This is by design.
insert_iterator& operator++(int)
{ return *this; } // This is by design.
}; // insert_iterator
/// inserter
///
/// Creates an instance of an insert_iterator.
///
template <typename Container, typename Iterator>
inline eastl::insert_iterator<Container>
inserter(Container& x, Iterator i)
{
typedef typename Container::iterator iterator;
return eastl::insert_iterator<Container>(x, iterator(i));
}
/// is_insert_iterator
///
/// This is a type traits extension utility.
/// Given an iterator, tells if it's an insert_iterator vs anything else.
/// If it's a insert_iterator wrapped by another iterator then value is false.
///
template <typename T>
struct EASTL_REMOVE_AT_2024_SEPT is_insert_iterator
: public eastl::false_type {};
// Note: some compilers (notably GCC) trigger deprecation warnings when doing template
// specialization if the main template is derpecated, so turn the warning off here. If this
// specialization is used, the warning will still trigger in the user code, this just
// disables the warning in this declaration.
EASTL_INTERNAL_DISABLE_DEPRECATED()
template<typename Iterator>
struct EASTL_REMOVE_AT_2024_SEPT is_insert_iterator<eastl::insert_iterator<Iterator>>
: public eastl::true_type {};
EASTL_INTERNAL_RESTORE_DEPRECATED()
//////////////////////////////////////////////////////////////////////////////////
/// distance
///
/// Implements the distance() function. There are two versions, one for
/// random access iterators (e.g. with vector) and one for regular input
/// iterators (e.g. with list). The former is more efficient.
///
template <typename InputIterator>
EA_CONSTEXPR
inline typename eastl::iterator_traits<InputIterator>::difference_type
distance_impl(InputIterator first, InputIterator last, EASTL_ITC_NS::input_iterator_tag)
{
typename eastl::iterator_traits<InputIterator>::difference_type n = 0;
while(first != last)
{
++first;
++n;
}
return n;
}
template <typename RandomAccessIterator>
EA_CONSTEXPR
inline typename eastl::iterator_traits<RandomAccessIterator>::difference_type
distance_impl(RandomAccessIterator first, RandomAccessIterator last, EASTL_ITC_NS::random_access_iterator_tag)
{
return last - first;
}
// Special version defined so that std C++ iterators can be recognized by
// this function. Unfortunately, this function treats all foreign iterators
// as InputIterators and thus can seriously hamper performance in the case
// of large ranges of bidirectional_iterator_tag iterators.
//template <typename InputIterator>
//inline typename eastl::iterator_traits<InputIterator>::difference_type
//distance_impl(InputIterator first, InputIterator last, ...)
//{
// typename eastl::iterator_traits<InputIterator>::difference_type n = 0;
//
// while(first != last)
// {
// ++first;
// ++n;
// }
// return n;
//}
template <typename InputIterator>
EA_CONSTEXPR
inline typename eastl::iterator_traits<InputIterator>::difference_type
distance(InputIterator first, InputIterator last)
{
typedef typename eastl::iterator_traits<InputIterator>::iterator_category IC;
return eastl::distance_impl(first, last, IC());
}
//////////////////////////////////////////////////////////////////////////////////
/// advance
///
/// Implements the advance() function. There are three versions, one for
/// random access iterators (e.g. with vector), one for bidirectional
/// iterators (list) and one for regular input iterators (e.g. with slist).
///
template <typename InputIterator, typename Distance>
inline void
advance_impl(InputIterator& i, Distance n, EASTL_ITC_NS::input_iterator_tag)
{
while(n--)
++i;
}
template <bool signedDistance>
struct advance_bi_impl
{
template <typename BidirectionalIterator, typename Distance>
static void advance_impl(BidirectionalIterator& i, Distance n) // Specialization for unsigned distance type.
{
while(n--)
++i;
}
};
template <>
struct advance_bi_impl<true>
{
template <typename BidirectionalIterator, typename Distance>
static void advance_impl(BidirectionalIterator& i, Distance n) // Specialization for signed distance type.
{
if(n > 0)
{
while(n--)
++i;
}
else
{
while(n++)
--i;
}
}
};
template <typename BidirectionalIterator, typename Distance>
inline void
advance_impl(BidirectionalIterator& i, Distance n, EASTL_ITC_NS::bidirectional_iterator_tag)