Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_Pair.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 
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 <utility>
32 
33 namespace Kokkos {
42 template <class T1, class T2>
43 struct pair {
45  using first_type = T1;
47  using second_type = T2;
48 
53 
59  KOKKOS_DEFAULTED_FUNCTION constexpr pair() = default;
60 
65 #if defined(KOKKOS_COMPILER_NVHPC) && KOKKOS_COMPILER_NVHPC < 230700
66  KOKKOS_FORCEINLINE_FUNCTION
67 #else
68  KOKKOS_FORCEINLINE_FUNCTION constexpr
69 #endif
70  pair(first_type const& f, second_type const& s) : first(f), second(s) {}
71 
76  template <class U, class V>
77 #if defined(KOKKOS_COMPILER_NVHPC) && KOKKOS_COMPILER_NVHPC < 230700
78  KOKKOS_FORCEINLINE_FUNCTION
79 #else
80  KOKKOS_FORCEINLINE_FUNCTION constexpr
81 #endif
82  pair(const pair<U, V>& p)
83  : first(p.first), second(p.second) {
84  }
85 
86 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
87  template <class U, class V>
92  KOKKOS_DEPRECATED KOKKOS_FORCEINLINE_FUNCTION constexpr pair(
93  const volatile pair<U, V>& p)
94  : first(p.first), second(p.second) {}
95 #endif
96 
101  template <class U, class V>
102  KOKKOS_FORCEINLINE_FUNCTION pair<T1, T2>& operator=(const pair<U, V>& p) {
103  first = p.first;
104  second = p.second;
105  return *this;
106  }
107 
108 #ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
109  template <class U, class V>
121  KOKKOS_DEPRECATED KOKKOS_FORCEINLINE_FUNCTION void operator=(
122  const volatile pair<U, V>& p) volatile {
123  first = p.first;
124  second = p.second;
125  // We deliberately do not return anything here. See explanation
126  // in public documentation above.
127  }
128 #endif
129 
130  // from std::pair<U,V>
131  template <class U, class V>
132  pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
133 
143  std::pair<T1, T2> to_std_pair() const {
144  return std::make_pair(first, second);
145  }
146 };
147 
148 template <class T1, class T2>
149 struct pair<T1&, T2&> {
151  using first_type = T1&;
153  using second_type = T2&;
154 
156  first_type first;
158  second_type second;
159 
164  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type f, second_type s)
165  : first(f), second(s) {}
166 
171  template <class U, class V>
172  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
173  : first(p.first), second(p.second) {}
174 
175  // from std::pair<U,V>
176  template <class U, class V>
177  pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
178 
183  template <class U, class V>
184  KOKKOS_FORCEINLINE_FUNCTION pair<first_type, second_type>& operator=(
185  const pair<U, V>& p) {
186  first = p.first;
187  second = p.second;
188  return *this;
189  }
190 
200  std::pair<T1, T2> to_std_pair() const {
201  return std::make_pair(first, second);
202  }
203 };
204 
205 template <class T1, class T2>
206 struct pair<T1, T2&> {
208  using first_type = T1;
210  using second_type = T2&;
211 
213  first_type first;
215  second_type second;
216 
221  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type const& f, second_type s)
222  : first(f), second(s) {}
223 
228  template <class U, class V>
229  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
230  : first(p.first), second(p.second) {}
231 
232  // from std::pair<U,V>
233  template <class U, class V>
234  pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
235 
240  template <class U, class V>
241  KOKKOS_FORCEINLINE_FUNCTION pair<first_type, second_type>& operator=(
242  const pair<U, V>& p) {
243  first = p.first;
244  second = p.second;
245  return *this;
246  }
247 
257  std::pair<T1, T2> to_std_pair() const {
258  return std::make_pair(first, second);
259  }
260 };
261 
262 template <class T1, class T2>
263 struct pair<T1&, T2> {
265  using first_type = T1&;
267  using second_type = T2;
268 
270  first_type first;
272  second_type second;
273 
278  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type f, second_type const& s)
279  : first(f), second(s) {}
280 
285  template <class U, class V>
286  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
287  : first(p.first), second(p.second) {}
288 
289  // from std::pair<U,V>
290  template <class U, class V>
291  pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
292 
297  template <class U, class V>
298  KOKKOS_FORCEINLINE_FUNCTION pair<first_type, second_type>& operator=(
299  const pair<U, V>& p) {
300  first = p.first;
301  second = p.second;
302  return *this;
303  }
304 
314  std::pair<T1, T2> to_std_pair() const {
315  return std::make_pair(first, second);
316  }
317 };
318 
320 template <class T1, class T2>
321 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator==(const pair<T1, T2>& lhs,
322  const pair<T1, T2>& rhs) {
323  return lhs.first == rhs.first && lhs.second == rhs.second;
324 }
325 
327 template <class T1, class T2>
328 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator!=(const pair<T1, T2>& lhs,
329  const pair<T1, T2>& rhs) {
330  return !(lhs == rhs);
331 }
332 
334 template <class T1, class T2>
335 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<(const pair<T1, T2>& lhs,
336  const pair<T1, T2>& rhs) {
337  return lhs.first < rhs.first ||
338  (!(rhs.first < lhs.first) && lhs.second < rhs.second);
339 }
340 
342 template <class T1, class T2>
343 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<=(const pair<T1, T2>& lhs,
344  const pair<T1, T2>& rhs) {
345  return !(rhs < lhs);
346 }
347 
349 template <class T1, class T2>
350 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>(const pair<T1, T2>& lhs,
351  const pair<T1, T2>& rhs) {
352  return rhs < lhs;
353 }
354 
356 template <class T1, class T2>
357 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(const pair<T1, T2>& lhs,
358  const pair<T1, T2>& rhs) {
359  return !(lhs < rhs);
360 }
361 
366 template <class T1, class T2>
367 KOKKOS_FORCEINLINE_FUNCTION constexpr pair<T1, T2> make_pair(T1 x, T2 y) {
368  return (pair<T1, T2>(x, y));
369 }
370 
410 template <class T1, class T2>
411 KOKKOS_FORCEINLINE_FUNCTION pair<T1&, T2&> tie(T1& x, T2& y) {
412  return (pair<T1&, T2&>(x, y));
413 }
414 
415 //
416 // Specialization of Kokkos::pair for a \c void second argument. This
417 // is not actually a "pair"; it only contains one element, the first.
418 //
419 template <class T1>
420 struct pair<T1, void> {
421  using first_type = T1;
422  using second_type = void;
423 
425  enum { second = 0 };
426 
427  KOKKOS_DEFAULTED_FUNCTION constexpr pair() = default;
428 
429  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const first_type& f) : first(f) {}
430 
431  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const first_type& f, int)
432  : first(f) {}
433 
434  template <class U>
435  KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, void>& p)
436  : first(p.first) {}
437 
438  template <class U>
439  KOKKOS_FORCEINLINE_FUNCTION pair<T1, void>& operator=(
440  const pair<U, void>& p) {
441  first = p.first;
442  return *this;
443  }
444 };
445 
446 //
447 // Specialization of relational operators for Kokkos::pair<T1,void>.
448 //
449 
450 template <class T1>
451 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator==(
452  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
453  return lhs.first == rhs.first;
454 }
455 
456 template <class T1>
457 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator!=(
458  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
459  return !(lhs == rhs);
460 }
461 
462 template <class T1>
463 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<(
464  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
465  return lhs.first < rhs.first;
466 }
467 
468 template <class T1>
469 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<=(
470  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
471  return !(rhs < lhs);
472 }
473 
474 template <class T1>
475 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>(
476  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
477  return rhs < lhs;
478 }
479 
480 template <class T1>
481 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(
482  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
483  return !(lhs < rhs);
484 }
485 
486 namespace Impl {
487 
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
first_type first
The first element of the pair.
Definition: Kokkos_Pair.hpp:50
std::pair< T1, T2 > to_std_pair() const
KOKKOS_DEFAULTED_FUNCTION constexpr pair()=default
second_type second
The second element of the pair.
Definition: Kokkos_Pair.hpp:52
KOKKOS_FORCEINLINE_FUNCTION pair< T1, T2 > & operator=(const pair< U, V > &p)