Skip to content

Commit dc1c271

Browse files
FabianWolffArthur O'Dwyer
authored andcommitted
[libc++] Cast to the right difference_type in various algorithms
Differential Revision: https://reviews.llvm.org/D113868
1 parent 721bb73 commit dc1c271

File tree

7 files changed

+49
-42
lines changed

7 files changed

+49
-42
lines changed

libcxx/include/__algorithm/copy_n.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ typename enable_if
5757
>::type
5858
copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
5959
{
60+
typedef typename iterator_traits<_InputIterator>::difference_type difference_type;
6061
typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
6162
_IntegralSize __n = __orig_n;
62-
return _VSTD::copy(__first, __first + __n, __result);
63+
return _VSTD::copy(__first, __first + difference_type(__n), __result);
6364
}
6465

6566
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__algorithm/find_end.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,16 @@ template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAcc
9595
_LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1 __find_end(
9696
_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2,
9797
_RandomAccessIterator2 __last2, _BinaryPredicate __pred, random_access_iterator_tag, random_access_iterator_tag) {
98+
typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
99+
typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
98100
// Take advantage of knowing source and pattern lengths. Stop short when source is smaller than pattern
99-
typename iterator_traits<_RandomAccessIterator2>::difference_type __len2 = __last2 - __first2;
101+
_D2 __len2 = __last2 - __first2;
100102
if (__len2 == 0)
101103
return __last1;
102-
typename iterator_traits<_RandomAccessIterator1>::difference_type __len1 = __last1 - __first1;
104+
_D1 __len1 = __last1 - __first1;
103105
if (__len1 < __len2)
104106
return __last1;
105-
const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
107+
const _RandomAccessIterator1 __s = __first1 + _D1(__len2 - 1); // End of pattern match can't go before here
106108
_RandomAccessIterator1 __l1 = __last1;
107109
_RandomAccessIterator2 __l2 = __last2;
108110
--__l2;

libcxx/include/__algorithm/search.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _Rando
6868
const _D1 __len1 = __last1 - __first1;
6969
if (__len1 < __len2)
7070
return _VSTD::make_pair(__last1, __last1);
71-
const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
71+
const _RandomAccessIterator1 __s = __last1 - _D1(__len2 - 1); // Start of pattern match can't go beyond here
7272

7373
while (true) {
7474
while (true) {
@@ -83,7 +83,7 @@ __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _Rando
8383
_RandomAccessIterator2 __m2 = __first2;
8484
while (true) {
8585
if (++__m2 == __last2)
86-
return _VSTD::make_pair(__first1, __first1 + __len2);
86+
return _VSTD::make_pair(__first1, __first1 + _D1(__len2));
8787
++__m1; // no need to check range on __m1 because __s guarantees we have enough source
8888
if (!__pred(*__m1, *__m2)) {
8989
++__first1;

libcxx/include/__algorithm/search_n.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator __search_n(_RandomAccessIter
5959
_RandomAccessIterator __last, _Size __count,
6060
const _Tp& __value_, _BinaryPredicate __pred,
6161
random_access_iterator_tag) {
62+
typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
6263
if (__count <= 0)
6364
return __first;
6465
_Size __len = static_cast<_Size>(__last - __first);
6566
if (__len < __count)
6667
return __last;
67-
const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
68+
const _RandomAccessIterator __s = __last - difference_type(__count - 1); // Start of pattern match can't go beyond here
6869
while (true) {
6970
// Find first element in sequence that matchs __value_, with a mininum of loop checks
7071
while (true) {

libcxx/include/__algorithm/sift_down.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ __sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
3838
__child = 2 * __child + 1;
3939
_RandomAccessIterator __child_i = __first + __child;
4040

41-
if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
41+
if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
4242
// right-child exists and is greater than left-child
4343
++__child_i;
4444
++__child;
@@ -63,7 +63,7 @@ __sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
6363
__child = 2 * __child + 1;
6464
__child_i = __first + __child;
6565

66-
if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
66+
if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + difference_type(1)))) {
6767
// right-child exists and is greater than left-child
6868
++__child_i;
6969
++__child;

libcxx/include/__algorithm/sort.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,11 @@ template <class _Compare, class _RandomAccessIterator>
160160
void
161161
__insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
162162
{
163+
typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
163164
typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
164-
_RandomAccessIterator __j = __first+2;
165-
_VSTD::__sort3<_Compare>(__first, __first+1, __j, __comp);
166-
for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
165+
_RandomAccessIterator __j = __first+difference_type(2);
166+
_VSTD::__sort3<_Compare>(__first, __first+difference_type(1), __j, __comp);
167+
for (_RandomAccessIterator __i = __j+difference_type(1); __i != __last; ++__i)
167168
{
168169
if (__comp(*__i, *__j))
169170
{
@@ -185,6 +186,7 @@ template <class _Compare, class _RandomAccessIterator>
185186
bool
186187
__insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
187188
{
189+
typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
188190
switch (__last - __first)
189191
{
190192
case 0:
@@ -195,21 +197,21 @@ __insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator
195197
swap(*__first, *__last);
196198
return true;
197199
case 3:
198-
_VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
200+
_VSTD::__sort3<_Compare>(__first, __first+difference_type(1), --__last, __comp);
199201
return true;
200202
case 4:
201-
_VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
203+
_VSTD::__sort4<_Compare>(__first, __first+difference_type(1), __first+difference_type(2), --__last, __comp);
202204
return true;
203205
case 5:
204-
_VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
206+
_VSTD::__sort5<_Compare>(__first, __first+difference_type(1), __first+difference_type(2), __first+difference_type(3), --__last, __comp);
205207
return true;
206208
}
207209
typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
208-
_RandomAccessIterator __j = __first+2;
209-
_VSTD::__sort3<_Compare>(__first, __first+1, __j, __comp);
210+
_RandomAccessIterator __j = __first+difference_type(2);
211+
_VSTD::__sort3<_Compare>(__first, __first+difference_type(1), __j, __comp);
210212
const unsigned __limit = 8;
211213
unsigned __count = 0;
212-
for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
214+
for (_RandomAccessIterator __i = __j+difference_type(1); __i != __last; ++__i)
213215
{
214216
if (__comp(*__i, *__j))
215217
{
@@ -288,13 +290,13 @@ __introsort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compar
288290
swap(*__first, *__last);
289291
return;
290292
case 3:
291-
_VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
293+
_VSTD::__sort3<_Compare>(__first, __first+difference_type(1), --__last, __comp);
292294
return;
293295
case 4:
294-
_VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
296+
_VSTD::__sort4<_Compare>(__first, __first+difference_type(1), __first+difference_type(2), --__last, __comp);
295297
return;
296298
case 5:
297-
_VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
299+
_VSTD::__sort5<_Compare>(__first, __first+difference_type(1), __first+difference_type(2), __first+difference_type(3), --__last, __comp);
298300
return;
299301
}
300302
if (__len <= __limit)
@@ -433,7 +435,7 @@ __introsort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compar
433435
if (__n_swaps == 0)
434436
{
435437
bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
436-
if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
438+
if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+difference_type(1), __last, __comp))
437439
{
438440
if (__fs)
439441
return;
@@ -457,7 +459,7 @@ __introsort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compar
457459
}
458460
else
459461
{
460-
_VSTD::__introsort<_Compare>(__i + 1, __last, __comp, __depth);
462+
_VSTD::__introsort<_Compare>(__i + difference_type(1), __last, __comp, __depth);
461463
__last = __i;
462464
}
463465
}

libcxx/test/std/algorithms/robust_re_difference_type.compile.pass.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
7878
auto mid = PickyIterator<void**, long>(a+5);
7979
auto last = PickyIterator<void**, long>(a+10);
8080
auto first2 = PickyIterator<void**, long long>(b);
81+
auto mid2 = PickyIterator<void**, long long>(b+5);
8182
auto last2 = PickyIterator<void**, long long>(b+10);
8283
void *value = nullptr;
8384
int count = 1;
@@ -96,7 +97,7 @@ TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
9697
#endif
9798
(void)std::copy(first, last, first2);
9899
(void)std::copy_backward(first, last, last2);
99-
// TODO FIXME (void)std::copy_n(first, count, first2);
100+
(void)std::copy_n(first, count, first2);
100101
(void)std::count(first, last, value);
101102
(void)std::count_if(first, last, UnaryTrue());
102103
(void)std::distance(first, last);
@@ -111,8 +112,8 @@ TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
111112
(void)std::fill(first, last, value);
112113
(void)std::fill_n(first, count, value);
113114
(void)std::find(first, last, value);
114-
// TODO FIXME (void)std::find_end(first, last, first2, mid2);
115-
// TODO FIXME (void)std::find_end(first, last, first2, mid2, std::equal_to<void*>());
115+
(void)std::find_end(first, last, first2, mid2);
116+
(void)std::find_end(first, last, first2, mid2, std::equal_to<void*>());
116117
(void)std::find_if(first, last, UnaryTrue());
117118
(void)std::find_if_not(first, last, UnaryTrue());
118119
(void)std::for_each(first, last, UnaryVoid());
@@ -146,8 +147,8 @@ TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
146147
// TODO: lexicographical_compare_three_way
147148
(void)std::lower_bound(first, last, value);
148149
(void)std::lower_bound(first, last, value, std::less<void*>());
149-
// TODO FIXME (void)std::make_heap(first, last);
150-
// TODO FIXME (void)std::make_heap(first, last, std::less<void*>());
150+
(void)std::make_heap(first, last);
151+
(void)std::make_heap(first, last, std::less<void*>());
151152
(void)std::max(value, value);
152153
(void)std::max(value, value, std::less<void*>());
153154
#if TEST_STD_VER >= 11
@@ -187,15 +188,15 @@ TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
187188
(void)std::none_of(first, last, UnaryTrue());
188189
(void)std::nth_element(first, mid, last);
189190
(void)std::nth_element(first, mid, last, std::less<void*>());
190-
// TODO FIXME (void)std::partial_sort(first, mid, last);
191-
// TODO FIXME (void)std::partial_sort(first, mid, last, std::less<void*>());
192-
// TODO FIXME (void)std::partial_sort_copy(first, last, first2, mid2);
193-
// TODO FIXME (void)std::partial_sort_copy(first, last, first2, mid2, std::less<void*>());
191+
(void)std::partial_sort(first, mid, last);
192+
(void)std::partial_sort(first, mid, last, std::less<void*>());
193+
(void)std::partial_sort_copy(first, last, first2, mid2);
194+
(void)std::partial_sort_copy(first, last, first2, mid2, std::less<void*>());
194195
(void)std::partition(first, last, UnaryTrue());
195196
(void)std::partition_copy(first, last, first2, last2, UnaryTrue());
196197
(void)std::partition_point(first, last, UnaryTrue());
197-
// TODO FIXME (void)std::pop_heap(first, last);
198-
// TODO FIXME (void)std::pop_heap(first, last, std::less<void*>());
198+
(void)std::pop_heap(first, last);
199+
(void)std::pop_heap(first, last, std::less<void*>());
199200
(void)std::prev_permutation(first, last);
200201
(void)std::prev_permutation(first, last, std::less<void*>());
201202
(void)std::push_heap(first, last);
@@ -212,10 +213,10 @@ TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
212213
(void)std::reverse_copy(first, last, first2);
213214
(void)std::rotate(first, mid, last);
214215
(void)std::rotate_copy(first, mid, last, first2);
215-
// TODO FIXME (void)std::search(first, last, first2, mid2);
216-
// TODO FIXME (void)std::search(first, last, first2, mid2, std::equal_to<void*>());
217-
// TODO FIXME (void)std::search_n(first, last, count, value);
218-
// TODO FIXME (void)std::search_n(first, last, count, value, std::equal_to<void*>());
216+
(void)std::search(first, last, first2, mid2);
217+
(void)std::search(first, last, first2, mid2, std::equal_to<void*>());
218+
(void)std::search_n(first, last, count, value);
219+
(void)std::search_n(first, last, count, value, std::equal_to<void*>());
219220
(void)std::set_difference(first, mid, mid, last, first2);
220221
(void)std::set_difference(first, mid, mid, last, first2, std::less<void*>());
221222
(void)std::set_intersection(first, mid, mid, last, first2);
@@ -228,10 +229,10 @@ TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
228229
(void)std::shift_left(first, last, count);
229230
(void)std::shift_right(first, last, count);
230231
#endif
231-
// TODO FIXME (void)std::sort(first, last);
232-
// TODO FIXME (void)std::sort(first, last, std::less<void*>());
233-
// TODO FIXME (void)std::sort_heap(first, last);
234-
// TODO FIXME (void)std::sort_heap(first, last, std::less<void*>());
232+
(void)std::sort(first, last);
233+
(void)std::sort(first, last, std::less<void*>());
234+
(void)std::sort_heap(first, last);
235+
(void)std::sort_heap(first, last, std::less<void*>());
235236
if (!TEST_IS_CONSTANT_EVALUATED) (void)std::stable_partition(first, last, UnaryTrue());
236237
if (!TEST_IS_CONSTANT_EVALUATED) (void)std::stable_sort(first, last);
237238
if (!TEST_IS_CONSTANT_EVALUATED) (void)std::stable_sort(first, last, std::less<void*>());

0 commit comments

Comments
 (0)