Kokkos Core Kernels Package Version of the Day
Loading...
Searching...
No Matches
Kokkos_Swap.hpp
1//@HEADER
2// ************************************************************************
3//
4// Kokkos v. 4.0
5// Copyright (2022) National Technology & Engineering
6// Solutions of Sandia, LLC (NTESS).
7//
8// Under the terms of Contract DE-NA0003525 with NTESS,
9// the U.S. Government retains certain rights in this software.
10//
11// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12// See https://kokkos.org/LICENSE for license information.
13// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14//
15//@HEADER
16
17#ifndef KOKKOS_SWAP_HPP
18#define KOKKOS_SWAP_HPP
19
20#include <Kokkos_Macros.hpp>
21
22#include <cstddef>
23#include <type_traits>
24#include <utility>
25
26namespace Kokkos {
27
28template <class T>
29KOKKOS_FUNCTION constexpr std::enable_if_t<std::is_move_constructible_v<T> &&
30 std::is_move_assignable_v<T>>
31kokkos_swap(T& a, T& b) noexcept(std::is_nothrow_move_constructible_v<T>&&
32 std::is_nothrow_move_assignable_v<T>) {
33 T t(std::move(a));
34 a = std::move(b);
35 b = std::move(t);
36}
37
38namespace Impl {
39
40template <class T>
41struct is_swappable {
42 template <class U>
43 static decltype(kokkos_swap(std::declval<T&>(), std::declval<T&>()))
44 test_swap(int);
45 struct Nope;
46 template <class U>
47 static Nope test_swap(long);
48 static constexpr bool value =
49 !std::is_same_v<decltype(test_swap<T>(0)), Nope>;
50};
51
52template <class T>
53inline constexpr bool is_nothrow_swappable_v =
54 noexcept(kokkos_swap(std::declval<T&>(), std::declval<T&>()));
55
56} // namespace Impl
57
58template <class T, std::size_t N>
59KOKKOS_FUNCTION constexpr std::enable_if_t<Impl::is_swappable<T>::value>
60kokkos_swap(T (&a)[N], T (&b)[N]) noexcept(Impl::is_nothrow_swappable_v<T>) {
61 for (std::size_t i = 0; i < N; ++i) {
62 kokkos_swap(a[i], b[i]);
63 }
64}
65
66} // namespace Kokkos
67
68#endif