[libcxx-commits] [libcxx] f877e88 - [libc++][spaceship] P1614R2: Added `operator==` to `slice`

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jun 11 09:50:37 PDT 2023


Author: Hristo Hristov
Date: 2023-06-11T19:50:31+03:00
New Revision: f877e88c354eacba1db81b82270ff8c27f000ea6

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

LOG: [libc++][spaceship] P1614R2: Added `operator==` to `slice`

Implements parts of P1614R2:
- Added `operator==` to `slice`

Reviewed By: #libc, Mordante

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

Added: 
    libcxx/test/std/numerics/numarray/class.slice/slice.ops/slice.ops.pass.cpp

Modified: 
    libcxx/docs/Status/SpaceshipProjects.csv
    libcxx/include/valarray

Removed: 
    


################################################################################
diff  --git a/libcxx/docs/Status/SpaceshipProjects.csv b/libcxx/docs/Status/SpaceshipProjects.csv
index 97c7d4e3273ad..214ab8b0c243e 100644
--- a/libcxx/docs/Status/SpaceshipProjects.csv
+++ b/libcxx/docs/Status/SpaceshipProjects.csv
@@ -139,7 +139,7 @@ Section,Description,Dependencies,Assignee,Complete
 "| `[complex.syn] <https://wg21.link/complex.syn>`_
 | `[complex.ops] <https://wg21.link/complex.ops>`_",| remove ops `complex <https://reviews.llvm.org/D152615>`_,None,Hristo Hristov,|In Progress|
 "| `[class.slice.overview] <https://wg21.link/class.slice.overview>`_
-| `[slice.ops] <https://wg21.link/slice.ops>`_",| `slice <https://reviews.llvm.org/D152617>`_,None,Hristo Hristov,|In Progress|
+| `[slice.ops] <https://wg21.link/slice.ops>`_",| `slice <https://reviews.llvm.org/D152617>`_,None,Hristo Hristov,|Complete|
 - `5.12 Clause 27: Time library <https://wg21.link/p1614r2#clause-27-time-library>`_,,,,
 | `[time.syn] <https://wg21.link/time.syn>`_,|,None,Unassigned,|Not Started|
 | `[time.duration.comparisons] <https://wg21.link/time.duration.comparisons>`_, `chrono::duration <https://reviews.llvm.org/D145881>`_, None, Hristo Hristov, |Complete|

diff  --git a/libcxx/include/valarray b/libcxx/include/valarray
index 001e111a07d5b..df55e9508043a 100644
--- a/libcxx/include/valarray
+++ b/libcxx/include/valarray
@@ -116,6 +116,8 @@ public:
     size_t start()  const;
     size_t size()   const;
     size_t stride() const;
+
+    friend bool operator==(const slice& x, const slice& y); // since C++20
 };
 
 template <class T>
@@ -402,6 +404,14 @@ public:
     _LIBCPP_INLINE_VISIBILITY size_t start()  const {return __start_;}
     _LIBCPP_INLINE_VISIBILITY size_t size()   const {return __size_;}
     _LIBCPP_INLINE_VISIBILITY size_t stride() const {return __stride_;}
+
+#if _LIBCPP_STD_VER >= 20
+
+    _LIBCPP_HIDE_FROM_ABI friend bool operator==(const slice& __x, const slice& __y) {
+      return __x.start() == __y.start() && __x.size() == __y.size() && __x.stride() == __y.stride();
+    }
+
+#endif
 };
 
 template <class _Tp> class _LIBCPP_TEMPLATE_VIS slice_array;

diff  --git a/libcxx/test/std/numerics/numarray/class.slice/slice.ops/slice.ops.pass.cpp b/libcxx/test/std/numerics/numarray/class.slice/slice.ops/slice.ops.pass.cpp
new file mode 100644
index 0000000000000..37aa426941168
--- /dev/null
+++ b/libcxx/test/std/numerics/numarray/class.slice/slice.ops/slice.ops.pass.cpp
@@ -0,0 +1,65 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// <valarray>
+
+// class slice;
+
+// friend bool operator==(const slice& x, const slice& y);
+
+#include <cassert>
+#include <valarray>
+
+#include "test_comparisons.h"
+
+void test() {
+  {
+    std::slice s1;
+    std::slice s2;
+
+    assert(testEquality(s1, s2, true));
+  }
+  {
+    std::slice s1{1, 2, 3};
+    std::slice s2{1, 2, 3};
+
+    assert(testEquality(s1, s2, true));
+  }
+  {
+    std::slice s1;
+    std::slice s2{1, 2, 3};
+
+    assert(testEquality(s1, s2, false));
+  }
+  {
+    std::slice s1{0, 2, 3};
+    std::slice s2{1, 2, 3};
+
+    assert(testEquality(s1, s2, false));
+  }
+  {
+    std::slice s1{1, 0, 3};
+    std::slice s2{1, 2, 3};
+
+    assert(testEquality(s1, s2, false));
+  }
+  {
+    std::slice s1{1, 2, 0};
+    std::slice s2{1, 2, 3};
+
+    assert(testEquality(s1, s2, false));
+  }
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}


        


More information about the libcxx-commits mailing list