[libcxx-commits] [libcxx] dc1c271 - [libc++] Cast to the right `difference_type` in various algorithms

Arthur O'Dwyer via libcxx-commits libcxx-commits at lists.llvm.org
Thu Nov 18 14:08:01 PST 2021


Author: Fabian Wolff
Date: 2021-11-18T17:07:36-05:00
New Revision: dc1c27149f214ff099e99930226ae312b0cf1910

URL: https://github.com/llvm/llvm-project/commit/dc1c27149f214ff099e99930226ae312b0cf1910
DIFF: https://github.com/llvm/llvm-project/commit/dc1c27149f214ff099e99930226ae312b0cf1910.diff

LOG: [libc++] Cast to the right `difference_type` in various algorithms

Differential Revision: https://reviews.llvm.org/D113868

Added: 
    

Modified: 
    libcxx/include/__algorithm/copy_n.h
    libcxx/include/__algorithm/find_end.h
    libcxx/include/__algorithm/search.h
    libcxx/include/__algorithm/search_n.h
    libcxx/include/__algorithm/sift_down.h
    libcxx/include/__algorithm/sort.h
    libcxx/test/std/algorithms/robust_re_difference_type.compile.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__algorithm/copy_n.h b/libcxx/include/__algorithm/copy_n.h
index 6417a0543e5af..38a84a4105a44 100644
--- a/libcxx/include/__algorithm/copy_n.h
+++ b/libcxx/include/__algorithm/copy_n.h
@@ -57,9 +57,10 @@ typename enable_if
 >::type
 copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result)
 {
+    typedef typename iterator_traits<_InputIterator>::
diff erence_type 
diff erence_type;
     typedef decltype(_VSTD::__convert_to_integral(__orig_n)) _IntegralSize;
     _IntegralSize __n = __orig_n;
-    return _VSTD::copy(__first, __first + __n, __result);
+    return _VSTD::copy(__first, __first + 
diff erence_type(__n), __result);
 }
 
 _LIBCPP_END_NAMESPACE_STD

diff  --git a/libcxx/include/__algorithm/find_end.h b/libcxx/include/__algorithm/find_end.h
index 95ac2199fd377..5d971c57a4e0e 100644
--- a/libcxx/include/__algorithm/find_end.h
+++ b/libcxx/include/__algorithm/find_end.h
@@ -95,14 +95,16 @@ template <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAcc
 _LIBCPP_CONSTEXPR_AFTER_CXX11 _RandomAccessIterator1 __find_end(
     _RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _RandomAccessIterator2 __first2,
     _RandomAccessIterator2 __last2, _BinaryPredicate __pred, random_access_iterator_tag, random_access_iterator_tag) {
+  typedef typename iterator_traits<_RandomAccessIterator1>::
diff erence_type _D1;
+  typedef typename iterator_traits<_RandomAccessIterator2>::
diff erence_type _D2;
   // Take advantage of knowing source and pattern lengths.  Stop short when source is smaller than pattern
-  typename iterator_traits<_RandomAccessIterator2>::
diff erence_type __len2 = __last2 - __first2;
+  _D2 __len2 = __last2 - __first2;
   if (__len2 == 0)
     return __last1;
-  typename iterator_traits<_RandomAccessIterator1>::
diff erence_type __len1 = __last1 - __first1;
+  _D1 __len1 = __last1 - __first1;
   if (__len1 < __len2)
     return __last1;
-  const _RandomAccessIterator1 __s = __first1 + (__len2 - 1); // End of pattern match can't go before here
+  const _RandomAccessIterator1 __s = __first1 + _D1(__len2 - 1); // End of pattern match can't go before here
   _RandomAccessIterator1 __l1 = __last1;
   _RandomAccessIterator2 __l2 = __last2;
   --__l2;

diff  --git a/libcxx/include/__algorithm/search.h b/libcxx/include/__algorithm/search.h
index 367a5bf1c8981..cfaec0ed1e17e 100644
--- a/libcxx/include/__algorithm/search.h
+++ b/libcxx/include/__algorithm/search.h
@@ -68,7 +68,7 @@ __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _Rando
   const _D1 __len1 = __last1 - __first1;
   if (__len1 < __len2)
     return _VSTD::make_pair(__last1, __last1);
-  const _RandomAccessIterator1 __s = __last1 - (__len2 - 1); // Start of pattern match can't go beyond here
+  const _RandomAccessIterator1 __s = __last1 - _D1(__len2 - 1); // Start of pattern match can't go beyond here
 
   while (true) {
     while (true) {
@@ -83,7 +83,7 @@ __search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1, _Rando
     _RandomAccessIterator2 __m2 = __first2;
     while (true) {
       if (++__m2 == __last2)
-        return _VSTD::make_pair(__first1, __first1 + __len2);
+        return _VSTD::make_pair(__first1, __first1 + _D1(__len2));
       ++__m1; // no need to check range on __m1 because __s guarantees we have enough source
       if (!__pred(*__m1, *__m2)) {
         ++__first1;

diff  --git a/libcxx/include/__algorithm/search_n.h b/libcxx/include/__algorithm/search_n.h
index 663edb37b4172..67d066aa43d51 100644
--- a/libcxx/include/__algorithm/search_n.h
+++ b/libcxx/include/__algorithm/search_n.h
@@ -59,12 +59,13 @@ _LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator __search_n(_RandomAccessIter
                                                                _RandomAccessIterator __last, _Size __count,
                                                                const _Tp& __value_, _BinaryPredicate __pred,
                                                                random_access_iterator_tag) {
+  typedef typename iterator_traits<_RandomAccessIterator>::
diff erence_type 
diff erence_type;
   if (__count <= 0)
     return __first;
   _Size __len = static_cast<_Size>(__last - __first);
   if (__len < __count)
     return __last;
-  const _RandomAccessIterator __s = __last - (__count - 1); // Start of pattern match can't go beyond here
+  const _RandomAccessIterator __s = __last - 
diff erence_type(__count - 1); // Start of pattern match can't go beyond here
   while (true) {
     // Find first element in sequence that matchs __value_, with a mininum of loop checks
     while (true) {

diff  --git a/libcxx/include/__algorithm/sift_down.h b/libcxx/include/__algorithm/sift_down.h
index 6e801b48b44cc..4d99ff237c966 100644
--- a/libcxx/include/__algorithm/sift_down.h
+++ b/libcxx/include/__algorithm/sift_down.h
@@ -38,7 +38,7 @@ __sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
     __child = 2 * __child + 1;
     _RandomAccessIterator __child_i = __first + __child;
 
-    if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
+    if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 
diff erence_type(1)))) {
         // right-child exists and is greater than left-child
         ++__child_i;
         ++__child;
@@ -63,7 +63,7 @@ __sift_down(_RandomAccessIterator __first, _RandomAccessIterator /*__last*/,
         __child = 2 * __child + 1;
         __child_i = __first + __child;
 
-        if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 1))) {
+        if ((__child + 1) < __len && __comp(*__child_i, *(__child_i + 
diff erence_type(1)))) {
             // right-child exists and is greater than left-child
             ++__child_i;
             ++__child;

diff  --git a/libcxx/include/__algorithm/sort.h b/libcxx/include/__algorithm/sort.h
index c2f6a62b61667..bc127689a6748 100644
--- a/libcxx/include/__algorithm/sort.h
+++ b/libcxx/include/__algorithm/sort.h
@@ -160,10 +160,11 @@ template <class _Compare, class _RandomAccessIterator>
 void
 __insertion_sort_3(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
 {
+    typedef typename iterator_traits<_RandomAccessIterator>::
diff erence_type 
diff erence_type;
     typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
-    _RandomAccessIterator __j = __first+2;
-    _VSTD::__sort3<_Compare>(__first, __first+1, __j, __comp);
-    for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
+    _RandomAccessIterator __j = __first+
diff erence_type(2);
+    _VSTD::__sort3<_Compare>(__first, __first+
diff erence_type(1), __j, __comp);
+    for (_RandomAccessIterator __i = __j+
diff erence_type(1); __i != __last; ++__i)
     {
         if (__comp(*__i, *__j))
         {
@@ -185,6 +186,7 @@ template <class _Compare, class _RandomAccessIterator>
 bool
 __insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
 {
+    typedef typename iterator_traits<_RandomAccessIterator>::
diff erence_type 
diff erence_type;
     switch (__last - __first)
     {
     case 0:
@@ -195,21 +197,21 @@ __insertion_sort_incomplete(_RandomAccessIterator __first, _RandomAccessIterator
             swap(*__first, *__last);
         return true;
     case 3:
-        _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
+        _VSTD::__sort3<_Compare>(__first, __first+
diff erence_type(1), --__last, __comp);
         return true;
     case 4:
-        _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
+        _VSTD::__sort4<_Compare>(__first, __first+
diff erence_type(1), __first+
diff erence_type(2), --__last, __comp);
         return true;
     case 5:
-        _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
+        _VSTD::__sort5<_Compare>(__first, __first+
diff erence_type(1), __first+
diff erence_type(2), __first+
diff erence_type(3), --__last, __comp);
         return true;
     }
     typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
-    _RandomAccessIterator __j = __first+2;
-    _VSTD::__sort3<_Compare>(__first, __first+1, __j, __comp);
+    _RandomAccessIterator __j = __first+
diff erence_type(2);
+    _VSTD::__sort3<_Compare>(__first, __first+
diff erence_type(1), __j, __comp);
     const unsigned __limit = 8;
     unsigned __count = 0;
-    for (_RandomAccessIterator __i = __j+1; __i != __last; ++__i)
+    for (_RandomAccessIterator __i = __j+
diff erence_type(1); __i != __last; ++__i)
     {
         if (__comp(*__i, *__j))
         {
@@ -288,13 +290,13 @@ __introsort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compar
                 swap(*__first, *__last);
             return;
         case 3:
-            _VSTD::__sort3<_Compare>(__first, __first+1, --__last, __comp);
+            _VSTD::__sort3<_Compare>(__first, __first+
diff erence_type(1), --__last, __comp);
             return;
         case 4:
-            _VSTD::__sort4<_Compare>(__first, __first+1, __first+2, --__last, __comp);
+            _VSTD::__sort4<_Compare>(__first, __first+
diff erence_type(1), __first+
diff erence_type(2), --__last, __comp);
             return;
         case 5:
-            _VSTD::__sort5<_Compare>(__first, __first+1, __first+2, __first+3, --__last, __comp);
+            _VSTD::__sort5<_Compare>(__first, __first+
diff erence_type(1), __first+
diff erence_type(2), __first+
diff erence_type(3), --__last, __comp);
             return;
         }
         if (__len <= __limit)
@@ -433,7 +435,7 @@ __introsort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compar
         if (__n_swaps == 0)
         {
             bool __fs = _VSTD::__insertion_sort_incomplete<_Compare>(__first, __i, __comp);
-            if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+1, __last, __comp))
+            if (_VSTD::__insertion_sort_incomplete<_Compare>(__i+
diff erence_type(1), __last, __comp))
             {
                 if (__fs)
                     return;
@@ -457,7 +459,7 @@ __introsort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compar
         }
         else
         {
-          _VSTD::__introsort<_Compare>(__i + 1, __last, __comp, __depth);
+          _VSTD::__introsort<_Compare>(__i + 
diff erence_type(1), __last, __comp, __depth);
           __last = __i;
         }
     }

diff  --git a/libcxx/test/std/algorithms/robust_re_
diff erence_type.compile.pass.cpp b/libcxx/test/std/algorithms/robust_re_
diff erence_type.compile.pass.cpp
index cf3396743c593..d7590d7b49e1a 100644
--- a/libcxx/test/std/algorithms/robust_re_
diff erence_type.compile.pass.cpp
+++ b/libcxx/test/std/algorithms/robust_re_
diff erence_type.compile.pass.cpp
@@ -78,6 +78,7 @@ TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
     auto mid = PickyIterator<void**, long>(a+5);
     auto last = PickyIterator<void**, long>(a+10);
     auto first2 = PickyIterator<void**, long long>(b);
+    auto mid2 = PickyIterator<void**, long long>(b+5);
     auto last2 = PickyIterator<void**, long long>(b+10);
     void *value = nullptr;
     int count = 1;
@@ -96,7 +97,7 @@ TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
 #endif
     (void)std::copy(first, last, first2);
     (void)std::copy_backward(first, last, last2);
-    // TODO FIXME (void)std::copy_n(first, count, first2);
+    (void)std::copy_n(first, count, first2);
     (void)std::count(first, last, value);
     (void)std::count_if(first, last, UnaryTrue());
     (void)std::distance(first, last);
@@ -111,8 +112,8 @@ TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
     (void)std::fill(first, last, value);
     (void)std::fill_n(first, count, value);
     (void)std::find(first, last, value);
-    // TODO FIXME (void)std::find_end(first, last, first2, mid2);
-    // TODO FIXME (void)std::find_end(first, last, first2, mid2, std::equal_to<void*>());
+    (void)std::find_end(first, last, first2, mid2);
+    (void)std::find_end(first, last, first2, mid2, std::equal_to<void*>());
     (void)std::find_if(first, last, UnaryTrue());
     (void)std::find_if_not(first, last, UnaryTrue());
     (void)std::for_each(first, last, UnaryVoid());
@@ -146,8 +147,8 @@ TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
     // TODO: lexicographical_compare_three_way
     (void)std::lower_bound(first, last, value);
     (void)std::lower_bound(first, last, value, std::less<void*>());
-    // TODO FIXME (void)std::make_heap(first, last);
-    // TODO FIXME (void)std::make_heap(first, last, std::less<void*>());
+    (void)std::make_heap(first, last);
+    (void)std::make_heap(first, last, std::less<void*>());
     (void)std::max(value, value);
     (void)std::max(value, value, std::less<void*>());
 #if TEST_STD_VER >= 11
@@ -187,15 +188,15 @@ TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
     (void)std::none_of(first, last, UnaryTrue());
     (void)std::nth_element(first, mid, last);
     (void)std::nth_element(first, mid, last, std::less<void*>());
-    // TODO FIXME (void)std::partial_sort(first, mid, last);
-    // TODO FIXME (void)std::partial_sort(first, mid, last, std::less<void*>());
-    // TODO FIXME (void)std::partial_sort_copy(first, last, first2, mid2);
-    // TODO FIXME (void)std::partial_sort_copy(first, last, first2, mid2, std::less<void*>());
+    (void)std::partial_sort(first, mid, last);
+    (void)std::partial_sort(first, mid, last, std::less<void*>());
+    (void)std::partial_sort_copy(first, last, first2, mid2);
+    (void)std::partial_sort_copy(first, last, first2, mid2, std::less<void*>());
     (void)std::partition(first, last, UnaryTrue());
     (void)std::partition_copy(first, last, first2, last2, UnaryTrue());
     (void)std::partition_point(first, last, UnaryTrue());
-    // TODO FIXME (void)std::pop_heap(first, last);
-    // TODO FIXME (void)std::pop_heap(first, last, std::less<void*>());
+    (void)std::pop_heap(first, last);
+    (void)std::pop_heap(first, last, std::less<void*>());
     (void)std::prev_permutation(first, last);
     (void)std::prev_permutation(first, last, std::less<void*>());
     (void)std::push_heap(first, last);
@@ -212,10 +213,10 @@ TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
     (void)std::reverse_copy(first, last, first2);
     (void)std::rotate(first, mid, last);
     (void)std::rotate_copy(first, mid, last, first2);
-    // TODO FIXME (void)std::search(first, last, first2, mid2);
-    // TODO FIXME (void)std::search(first, last, first2, mid2, std::equal_to<void*>());
-    // TODO FIXME (void)std::search_n(first, last, count, value);
-    // TODO FIXME (void)std::search_n(first, last, count, value, std::equal_to<void*>());
+    (void)std::search(first, last, first2, mid2);
+    (void)std::search(first, last, first2, mid2, std::equal_to<void*>());
+    (void)std::search_n(first, last, count, value);
+    (void)std::search_n(first, last, count, value, std::equal_to<void*>());
     (void)std::set_
diff erence(first, mid, mid, last, first2);
     (void)std::set_
diff erence(first, mid, mid, last, first2, std::less<void*>());
     (void)std::set_intersection(first, mid, mid, last, first2);
@@ -228,10 +229,10 @@ TEST_CONSTEXPR_CXX20 bool all_the_algorithms()
     (void)std::shift_left(first, last, count);
     (void)std::shift_right(first, last, count);
 #endif
-    // TODO FIXME (void)std::sort(first, last);
-    // TODO FIXME (void)std::sort(first, last, std::less<void*>());
-    // TODO FIXME (void)std::sort_heap(first, last);
-    // TODO FIXME (void)std::sort_heap(first, last, std::less<void*>());
+    (void)std::sort(first, last);
+    (void)std::sort(first, last, std::less<void*>());
+    (void)std::sort_heap(first, last);
+    (void)std::sort_heap(first, last, std::less<void*>());
     if (!TEST_IS_CONSTANT_EVALUATED) (void)std::stable_partition(first, last, UnaryTrue());
     if (!TEST_IS_CONSTANT_EVALUATED) (void)std::stable_sort(first, last);
     if (!TEST_IS_CONSTANT_EVALUATED) (void)std::stable_sort(first, last, std::less<void*>());


        


More information about the libcxx-commits mailing list