Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_Pair.hpp
Go to the documentation of this file.
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 
22 
23 #ifndef KOKKOS_PAIR_HPP
24 #define KOKKOS_PAIR_HPP
25 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
26 #define KOKKOS_IMPL_PUBLIC_INCLUDE
27 #define KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_PAIR
28 #endif
29 
30 #include <Kokkos_Macros.hpp>
31 #include <Kokkos_Swap.hpp>
32 #include <utility>
33 
34 namespace Kokkos {
43 template <class T1, class T2>
44 struct pair {
46  using first_type = T1;
48  using second_type = T2;
49 
54 
60  KOKKOS_DEFAULTED_FUNCTION constexpr pair() = default;
61 
66 #if defined(KOKKOS_COMPILER_NVHPC) && KOKKOS_COMPILER_NVHPC < 230700
67  KOKKOS_FORCEINLINE_FUNCTION
68 #else
69  KOKKOS_FORCEINLINE_FUNCTION constexpr
70 #endif
71  pair(first_type const& f, second_type const& s) : first(f), second(s) {}
72 
77  template <class U, class V>
78 #if defined(KOKKOS_COMPILER_NVHPC) && KOKKOS_COMPILER_NVHPC < 230700
79  KOKKOS_FORCEINLINE_FUNCTION
80 #else
81  KOKKOS_FORCEINLINE_FUNCTION constexpr
82 #endif
83  pair(const pair<U, V>& p)
84  : first(p.first), second(p.second) {
85  }
86 
87 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
88  template <class U, class V>
93  KOKKOS_DEPRECATED KOKKOS_FORCEINLINE_FUNCTION constexpr pair(
94  const volatile pair<U, V>& p)
95  : first(p.first), second(p.second) {}
96 #endif
97 
102  template <class U, class V>
103  KOKKOS_FORCEINLINE_FUNCTION pair<T1, T2>& operator=(const pair<U, V>& p) {
104  first = p.first;
105  second = p.second;
106  return *this;
107  }
108 
109 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
110  template <class U, class V>
122  KOKKOS_DEPRECATED KOKKOS_FORCEINLINE_FUNCTION void operator=(
123  const volatile pair<U, V>& p) volatile {
124  first = p.first;
125  second = p.second;
126  // We deliberately do not return anything here. See explanation
127  // in public documentation above.
128  }
129 #endif
130 
131  // from std::pair<U,V>
132  template <class U, class V>
133  pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
134 
144  std::pair<T1, T2> to_std_pair() const {
145  return std::make_pair(first, second);
146  }
147 };
148 
149 template <class T1, class T2>
150 struct pair<T1&, T2&> {
152  using first_type = T1&;
154  using second_type = T2&;
155 
157  first_type first;
159  second_type second;
160 
165  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type f, second_type s)
166  : first(f), second(s) {}
167 
172  template <class U, class V>
173  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
174  : first(p.first), second(p.second) {}
175 
176  // from std::pair<U,V>
177  template <class U, class V>
178  pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
179 
184  template <class U, class V>
185  KOKKOS_FORCEINLINE_FUNCTION pair<first_type, second_type>& operator=(
186  const pair<U, V>& p) {
187  first = p.first;
188  second = p.second;
189  return *this;
190  }
191 
201  std::pair<T1, T2> to_std_pair() const {
202  return std::make_pair(first, second);
203  }
204 };
205 
206 template <class T1, class T2>
207 struct pair<T1, T2&> {
209  using first_type = T1;
211  using second_type = T2&;
212 
214  first_type first;
216  second_type second;
217 
222  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type const& f, second_type s)
223  : first(f), second(s) {}
224 
229  template <class U, class V>
230  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
231  : first(p.first), second(p.second) {}
232 
233  // from std::pair<U,V>
234  template <class U, class V>
235  pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
236 
241  template <class U, class V>
242  KOKKOS_FORCEINLINE_FUNCTION pair<first_type, second_type>& operator=(
243  const pair<U, V>& p) {
244  first = p.first;
245  second = p.second;
246  return *this;
247  }
248 
258  std::pair<T1, T2> to_std_pair() const {
259  return std::make_pair(first, second);
260  }
261 };
262 
263 template <class T1, class T2>
264 struct pair<T1&, T2> {
266  using first_type = T1&;
268  using second_type = T2;
269 
271  first_type first;
273  second_type second;
274 
279  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type f, second_type const& s)
280  : first(f), second(s) {}
281 
286  template <class U, class V>
287  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
288  : first(p.first), second(p.second) {}
289 
290  // from std::pair<U,V>
291  template <class U, class V>
292  pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
293 
298  template <class U, class V>
299  KOKKOS_FORCEINLINE_FUNCTION pair<first_type, second_type>& operator=(
300  const pair<U, V>& p) {
301  first = p.first;
302  second = p.second;
303  return *this;
304  }
305 
315  std::pair<T1, T2> to_std_pair() const {
316  return std::make_pair(first, second);
317  }
318 };
319 
321 template <class T1, class T2>
322 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator==(const pair<T1, T2>& lhs,
323  const pair<T1, T2>& rhs) {
324  return lhs.first == rhs.first && lhs.second == rhs.second;
325 }
326 
328 template <class T1, class T2>
329 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator!=(const pair<T1, T2>& lhs,
330  const pair<T1, T2>& rhs) {
331  return !(lhs == rhs);
332 }
333 
335 template <class T1, class T2>
336 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<(const pair<T1, T2>& lhs,
337  const pair<T1, T2>& rhs) {
338  return lhs.first < rhs.first ||
339  (!(rhs.first < lhs.first) && lhs.second < rhs.second);
340 }
341 
343 template <class T1, class T2>
344 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<=(const pair<T1, T2>& lhs,
345  const pair<T1, T2>& rhs) {
346  return !(rhs < lhs);
347 }
348 
350 template <class T1, class T2>
351 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>(const pair<T1, T2>& lhs,
352  const pair<T1, T2>& rhs) {
353  return rhs < lhs;
354 }
355 
357 template <class T1, class T2>
358 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(const pair<T1, T2>& lhs,
359  const pair<T1, T2>& rhs) {
360  return !(lhs < rhs);
361 }
362 
367 template <class T1, class T2>
368 KOKKOS_FORCEINLINE_FUNCTION constexpr pair<T1, T2> make_pair(T1 x, T2 y) {
369  return (pair<T1, T2>(x, y));
370 }
371 
411 template <class T1, class T2>
412 KOKKOS_FORCEINLINE_FUNCTION pair<T1&, T2&> tie(T1& x, T2& y) {
413  return (pair<T1&, T2&>(x, y));
414 }
415 
416 //
417 // Specialization of Kokkos::pair for a \c void second argument. This
418 // is not actually a "pair"; it only contains one element, the first.
419 //
420 template <class T1>
421 struct pair<T1, void> {
422  using first_type = T1;
423  using second_type = void;
424 
426  enum { second = 0 };
427 
428  KOKKOS_DEFAULTED_FUNCTION constexpr pair() = default;
429 
430  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const first_type& f) : first(f) {}
431 
432  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const first_type& f, int)
433  : first(f) {}
434 
435  template <class U>
436  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, void>& p)
437  : first(p.first) {}
438 
439  template <class U>
440  KOKKOS_FORCEINLINE_FUNCTION pair<T1, void>& operator=(
441  const pair<U, void>& p) {
442  first = p.first;
443  return *this;
444  }
445 };
446 
447 //
448 // Specialization of relational operators for Kokkos::pair<T1,void>.
449 //
450 
451 template <class T1>
452 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator==(
453  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
454  return lhs.first == rhs.first;
455 }
456 
457 template <class T1>
458 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator!=(
459  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
460  return !(lhs == rhs);
461 }
462 
463 template <class T1>
464 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<(
465  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
466  return lhs.first < rhs.first;
467 }
468 
469 template <class T1>
470 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<=(
471  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
472  return !(rhs < lhs);
473 }
474 
475 template <class T1>
476 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>(
477  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
478  return rhs < lhs;
479 }
480 
481 template <class T1>
482 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(
483  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
484  return !(lhs < rhs);
485 }
486 
487 namespace Impl {
488 template <class T>
489 struct is_pair_like : std::false_type {};
490 template <class T, class U>
491 struct is_pair_like<Kokkos::pair<T, U>> : std::true_type {};
492 template <class T, class U>
493 struct is_pair_like<std::pair<T, U>> : std::true_type {};
494 
495 } // end namespace Impl
496 
497 } // namespace Kokkos
498 
499 #ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_PAIR
500 #undef KOKKOS_IMPL_PUBLIC_INCLUDE
501 #undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_PAIR
502 #endif
503 #endif // KOKKOS_PAIR_HPP
Replacement for std::pair that works on CUDA devices.
Definition: Kokkos_Pair.hpp:44
T2 second_type
The second template parameter of this class.
Definition: Kokkos_Pair.hpp:48
first_type first
The first element of the pair.
Definition: Kokkos_Pair.hpp:51
KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type const &f, second_type const &s)
Constructor that takes both elements of the pair.
Definition: Kokkos_Pair.hpp:71
T1 first_type
The first template parameter of this class.
Definition: Kokkos_Pair.hpp:46
std::pair< T1, T2 > to_std_pair() const
Return the std::pair version of this object.
KOKKOS_DEFAULTED_FUNCTION constexpr pair()=default
Default constructor.
KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair< U, V > &p)
Copy constructor.
Definition: Kokkos_Pair.hpp:83
second_type second
The second element of the pair.
Definition: Kokkos_Pair.hpp:53
KOKKOS_FORCEINLINE_FUNCTION pair< T1, T2 > & operator=(const pair< U, V > &p)
Assignment operator.