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 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
417 //
418 // Specialization of Kokkos::pair for a \c void second argument. This
419 // is not actually a "pair"; it only contains one element, the first.
420 //
421 template <class T1>
422 struct KOKKOS_DEPRECATED pair<T1, void> {
423  using first_type = T1;
424  using second_type = void;
425 
426  first_type first;
427  enum { second = 0 };
428 
429  KOKKOS_DEFAULTED_FUNCTION constexpr pair() = default;
430 
431  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const first_type& f) : first(f) {}
432 
433  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const first_type& f, int)
434  : first(f) {}
435 
436  template <class U>
437  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, void>& p)
438  : first(p.first) {}
439 
440  template <class U>
441  KOKKOS_FORCEINLINE_FUNCTION pair<T1, void>& operator=(
442  const pair<U, void>& p) {
443  first = p.first;
444  return *this;
445  }
446 };
447 
448 //
449 // Specialization of relational operators for Kokkos::pair<T1,void>.
450 //
451 
452 #if defined(KOKKOS_COMPILER_GNU) && (KOKKOS_COMPILER_GNU < 1110)
453 KOKKOS_IMPL_DISABLE_DEPRECATED_WARNINGS_PUSH()
454 #endif
455 template <class T1>
456 KOKKOS_DEPRECATED KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator==(
457  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
458  return lhs.first == rhs.first;
459 }
460 
461 template <class T1>
462 KOKKOS_DEPRECATED KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator!=(
463  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
464  return !(lhs == rhs);
465 }
466 
467 template <class T1>
468 KOKKOS_DEPRECATED KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<(
469  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
470  return lhs.first < rhs.first;
471 }
472 
473 template <class T1>
474 KOKKOS_DEPRECATED KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<=(
475  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
476  return !(rhs < lhs);
477 }
478 
479 template <class T1>
480 KOKKOS_DEPRECATED KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>(
481  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
482  return rhs < lhs;
483 }
484 
485 template <class T1>
486 KOKKOS_DEPRECATED KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(
487  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
488  return !(lhs < rhs);
489 }
490 #if defined(KOKKOS_COMPILER_GNU) && (KOKKOS_COMPILER_GNU < 1110)
491 KOKKOS_IMPL_DISABLE_DEPRECATED_WARNINGS_POP()
492 #endif
493 #endif
494 
495 namespace Impl {
496 template <class T>
497 struct is_pair_like : std::false_type {};
498 template <class T, class U>
499 struct is_pair_like<Kokkos::pair<T, U>> : std::true_type {};
500 template <class T, class U>
501 struct is_pair_like<std::pair<T, U>> : std::true_type {};
502 
503 } // end namespace Impl
504 
505 } // namespace Kokkos
506 
507 #ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_PAIR
508 #undef KOKKOS_IMPL_PUBLIC_INCLUDE
509 #undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_PAIR
510 #endif
511 #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.