Kokkos Core Kernels Package Version of the Day
Loading...
Searching...
No Matches
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 <utility>
32
33namespace Kokkos {
42template <class T1, class T2>
43struct pair {
45 using first_type = T1;
47 using second_type = T2;
48
53
59 KOKKOS_DEFAULTED_FUNCTION constexpr pair() = default;
60
65#ifdef KOKKOS_COMPILER_NVHPC // FIXME_NVHPC bug in NVHPC regarding constexpr
66 // constructors used in device code
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#ifdef KOKKOS_COMPILER_NVHPC // FIXME_NVHPC bug in NVHPC regarding constexpr
79 // constructors used in device code
80 KOKKOS_FORCEINLINE_FUNCTION
81#else
82 KOKKOS_FORCEINLINE_FUNCTION constexpr
83#endif
85 : first(p.first), second(p.second) {
86 }
87
88#ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
93 template <class U, class V>
94 KOKKOS_DEPRECATED KOKKOS_FORCEINLINE_FUNCTION constexpr pair(
95 const volatile pair<U, V>& p)
96 : first(p.first), second(p.second) {}
97#endif
98
103 template <class U, class V>
104 KOKKOS_FORCEINLINE_FUNCTION pair<T1, T2>& operator=(const pair<U, V>& p) {
105 first = p.first;
106 second = p.second;
107 return *this;
108 }
109
110#ifdef KOKKOS_ENABLE_DEPRECATED_CODE_4
122 template <class U, class V>
123 KOKKOS_DEPRECATED KOKKOS_FORCEINLINE_FUNCTION void operator=(
124 const volatile pair<U, V>& p) volatile {
125 first = p.first;
126 second = p.second;
127 // We deliberately do not return anything here. See explanation
128 // in public documentation above.
129 }
130#endif
131
132 // from std::pair<U,V>
133 template <class U, class V>
134 pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
135
145 std::pair<T1, T2> to_std_pair() const {
146 return std::make_pair(first, second);
147 }
148};
149
150template <class T1, class T2>
151struct pair<T1&, T2&> {
153 using first_type = T1&;
155 using second_type = T2&;
156
158 first_type first;
160 second_type second;
161
166 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type f, second_type s)
167 : first(f), second(s) {}
168
173 template <class U, class V>
174 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
175 : first(p.first), second(p.second) {}
176
177 // from std::pair<U,V>
178 template <class U, class V>
179 pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
180
185 template <class U, class V>
186 KOKKOS_FORCEINLINE_FUNCTION pair<first_type, second_type>& operator=(
187 const pair<U, V>& p) {
188 first = p.first;
189 second = p.second;
190 return *this;
191 }
192
202 std::pair<T1, T2> to_std_pair() const {
203 return std::make_pair(first, second);
204 }
205};
206
207template <class T1, class T2>
208struct pair<T1, T2&> {
210 using first_type = T1;
212 using second_type = T2&;
213
215 first_type first;
217 second_type second;
218
223 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type const& f, second_type s)
224 : first(f), second(s) {}
225
230 template <class U, class V>
231 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
232 : first(p.first), second(p.second) {}
233
234 // from std::pair<U,V>
235 template <class U, class V>
236 pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
237
242 template <class U, class V>
243 KOKKOS_FORCEINLINE_FUNCTION pair<first_type, second_type>& operator=(
244 const pair<U, V>& p) {
245 first = p.first;
246 second = p.second;
247 return *this;
248 }
249
259 std::pair<T1, T2> to_std_pair() const {
260 return std::make_pair(first, second);
261 }
262};
263
264template <class T1, class T2>
265struct pair<T1&, T2> {
267 using first_type = T1&;
269 using second_type = T2;
270
272 first_type first;
274 second_type second;
275
280 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type f, second_type const& s)
281 : first(f), second(s) {}
282
287 template <class U, class V>
288 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
289 : first(p.first), second(p.second) {}
290
291 // from std::pair<U,V>
292 template <class U, class V>
293 pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
294
299 template <class U, class V>
300 KOKKOS_FORCEINLINE_FUNCTION pair<first_type, second_type>& operator=(
301 const pair<U, V>& p) {
302 first = p.first;
303 second = p.second;
304 return *this;
305 }
306
316 std::pair<T1, T2> to_std_pair() const {
317 return std::make_pair(first, second);
318 }
319};
320
322template <class T1, class T2>
323KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator==(const pair<T1, T2>& lhs,
324 const pair<T1, T2>& rhs) {
325 return lhs.first == rhs.first && lhs.second == rhs.second;
326}
327
329template <class T1, class T2>
330KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator!=(const pair<T1, T2>& lhs,
331 const pair<T1, T2>& rhs) {
332 return !(lhs == rhs);
333}
334
336template <class T1, class T2>
337KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<(const pair<T1, T2>& lhs,
338 const pair<T1, T2>& rhs) {
339 return lhs.first < rhs.first ||
340 (!(rhs.first < lhs.first) && lhs.second < rhs.second);
341}
342
344template <class T1, class T2>
345KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<=(const pair<T1, T2>& lhs,
346 const pair<T1, T2>& rhs) {
347 return !(rhs < lhs);
348}
349
351template <class T1, class T2>
352KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>(const pair<T1, T2>& lhs,
353 const pair<T1, T2>& rhs) {
354 return rhs < lhs;
355}
356
358template <class T1, class T2>
359KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(const pair<T1, T2>& lhs,
360 const pair<T1, T2>& rhs) {
361 return !(lhs < rhs);
362}
363
368template <class T1, class T2>
369KOKKOS_FORCEINLINE_FUNCTION constexpr pair<T1, T2> make_pair(T1 x, T2 y) {
370 return (pair<T1, T2>(x, y));
371}
372
412template <class T1, class T2>
413KOKKOS_FORCEINLINE_FUNCTION pair<T1&, T2&> tie(T1& x, T2& y) {
414 return (pair<T1&, T2&>(x, y));
415}
416
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//
421template <class T1>
422struct pair<T1, void> {
423 using first_type = T1;
424 using second_type = void;
425
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
452template <class T1>
453KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator==(
454 const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
455 return lhs.first == rhs.first;
456}
457
458template <class T1>
459KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator!=(
460 const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
461 return !(lhs == rhs);
462}
463
464template <class T1>
465KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<(
466 const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
467 return lhs.first < rhs.first;
468}
469
470template <class T1>
471KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<=(
472 const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
473 return !(rhs < lhs);
474}
475
476template <class T1>
477KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>(
478 const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
479 return rhs < lhs;
480}
481
482template <class T1>
483KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(
484 const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
485 return !(lhs < rhs);
486}
487
488namespace Impl {
489
490template <class T>
491struct is_pair_like : std::false_type {};
492template <class T, class U>
493struct is_pair_like<Kokkos::pair<T, U>> : std::true_type {};
494template <class T, class U>
495struct is_pair_like<std::pair<T, U>> : std::true_type {};
496
497} // end namespace Impl
498
499} // namespace Kokkos
500
501#ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_PAIR
502#undef KOKKOS_IMPL_PUBLIC_INCLUDE
503#undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_PAIR
504#endif
505#endif // KOKKOS_PAIR_HPP
KOKKOS_FORCEINLINE_FUNCTION pair< T1 &, T2 & > tie(T1 &x, T2 &y)
Return a pair of references to the input arguments.
KOKKOS_FORCEINLINE_FUNCTION constexpr pair< T1, T2 > make_pair(T1 x, T2 y)
Return a new pair.
KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<(const pair< T1, T2 > &lhs, const pair< T1, T2 > &rhs)
Less-than operator for Kokkos::pair.
KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<=(const pair< T1, T2 > &lhs, const pair< T1, T2 > &rhs)
Less-than-or-equal-to operator for Kokkos::pair.
KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(const pair< T1, T2 > &lhs, const pair< T1, T2 > &rhs)
Greater-than-or-equal-to operator for Kokkos::pair.
KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>(const pair< T1, T2 > &lhs, const pair< T1, T2 > &rhs)
Greater-than operator for Kokkos::pair.
A thread safe view to a bitset.
Replacement for std::pair that works on CUDA devices.
T2 second_type
The second template parameter of this class.
first_type first
The first element of the pair.
KOKKOS_FORCEINLINE_FUNCTION pair< T1, T2 > & operator=(const pair< U, V > &p)
Assignment operator.
KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type const &f, second_type const &s)
Constructor that takes both elements of the pair.
KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair< U, V > &p)
Copy constructor.
std::pair< T1, T2 > to_std_pair() const
Return the std::pair version of this object.
second_type second
The second element of the pair.
KOKKOS_DEFAULTED_FUNCTION constexpr pair()=default
Default constructor.
T1 first_type
The first template parameter of this class.