Kokkos Core Kernels Package Version of the Day
Kokkos_Pair.hpp
Go to the documentation of this file.
1//@HEADER
2// ************************************************************************
3//
4// Kokkos v. 3.0
5// Copyright (2020) 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// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17//
18// 2. Redistributions in binary form must reproduce the above copyright
19// notice, this list of conditions and the following disclaimer in the
20// documentation and/or other materials provided with the distribution.
21//
22// 3. Neither the name of the Corporation nor the names of the
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
27// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
30// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact Christian R. Trott (crtrott@sandia.gov)
39//
40// ************************************************************************
41//@HEADER
42
48
49#ifndef KOKKOS_PAIR_HPP
50#define KOKKOS_PAIR_HPP
51
52#include <Kokkos_Macros.hpp>
53#include <utility>
54
55namespace Kokkos {
64template <class T1, class T2>
65struct pair {
67 using first_type = T1;
69 using second_type = T2;
70
75
81 KOKKOS_DEFAULTED_FUNCTION constexpr pair() = default;
82
87 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type const& f,
88 second_type const& s)
89 : first(f), second(s) {}
90
95 template <class U, class V>
96 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
97 : first(p.first), second(p.second) {}
98
103 template <class U, class V>
104 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const volatile pair<U, V>& p)
105 : first(p.first), second(p.second) {}
106
111 template <class U, class V>
112 KOKKOS_FORCEINLINE_FUNCTION pair<T1, T2>& operator=(const pair<U, V>& p) {
113 first = p.first;
114 second = p.second;
115 return *this;
116 }
117
129 template <class U, class V>
130 KOKKOS_FORCEINLINE_FUNCTION void operator=(
131 const volatile pair<U, V>& p) volatile {
132 first = p.first;
133 second = p.second;
134 // We deliberately do not return anything here. See explanation
135 // in public documentation above.
136 }
137
138 // from std::pair<U,V>
139 template <class U, class V>
140 pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
141
151 std::pair<T1, T2> to_std_pair() const {
152 return std::make_pair(first, second);
153 }
154};
155
156template <class T1, class T2>
157struct pair<T1&, T2&> {
159 using first_type = T1&;
161 using second_type = T2&;
162
164 first_type first;
166 second_type second;
167
172 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type f, second_type s)
173 : first(f), second(s) {}
174
179 template <class U, class V>
180 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
181 : first(p.first), second(p.second) {}
182
183 // from std::pair<U,V>
184 template <class U, class V>
185 pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
186
191 template <class U, class V>
192 KOKKOS_FORCEINLINE_FUNCTION pair<first_type, second_type>& operator=(
193 const pair<U, V>& p) {
194 first = p.first;
195 second = p.second;
196 return *this;
197 }
198
208 std::pair<T1, T2> to_std_pair() const {
209 return std::make_pair(first, second);
210 }
211};
212
213template <class T1, class T2>
214struct pair<T1, T2&> {
216 using first_type = T1;
218 using second_type = T2&;
219
221 first_type first;
223 second_type second;
224
229 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type const& f, second_type s)
230 : first(f), second(s) {}
231
236 template <class U, class V>
237 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
238 : first(p.first), second(p.second) {}
239
240 // from std::pair<U,V>
241 template <class U, class V>
242 pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
243
248 template <class U, class V>
249 KOKKOS_FORCEINLINE_FUNCTION pair<first_type, second_type>& operator=(
250 const pair<U, V>& p) {
251 first = p.first;
252 second = p.second;
253 return *this;
254 }
255
265 std::pair<T1, T2> to_std_pair() const {
266 return std::make_pair(first, second);
267 }
268};
269
270template <class T1, class T2>
271struct pair<T1&, T2> {
273 using first_type = T1&;
275 using second_type = T2;
276
278 first_type first;
280 second_type second;
281
286 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(first_type f, second_type const& s)
287 : first(f), second(s) {}
288
293 template <class U, class V>
294 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, V>& p)
295 : first(p.first), second(p.second) {}
296
297 // from std::pair<U,V>
298 template <class U, class V>
299 pair(const std::pair<U, V>& p) : first(p.first), second(p.second) {}
300
305 template <class U, class V>
306 KOKKOS_FORCEINLINE_FUNCTION pair<first_type, second_type>& operator=(
307 const pair<U, V>& p) {
308 first = p.first;
309 second = p.second;
310 return *this;
311 }
312
322 std::pair<T1, T2> to_std_pair() const {
323 return std::make_pair(first, second);
324 }
325};
326
328template <class T1, class T2>
329KOKKOS_FORCEINLINE_FUNCTION bool operator==(const pair<T1, T2>& lhs,
330 const pair<T1, T2>& rhs) {
331 return lhs.first == rhs.first && lhs.second == rhs.second;
332}
333
335template <class T1, class T2>
336KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator!=(const pair<T1, T2>& lhs,
337 const pair<T1, T2>& rhs) {
338 return !(lhs == rhs);
339}
340
342template <class T1, class T2>
343KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<(const pair<T1, T2>& lhs,
344 const pair<T1, T2>& rhs) {
345 return lhs.first < rhs.first ||
346 (!(rhs.first < lhs.first) && lhs.second < rhs.second);
347}
348
350template <class T1, class T2>
351KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<=(const pair<T1, T2>& lhs,
352 const pair<T1, T2>& rhs) {
353 return !(rhs < lhs);
354}
355
357template <class T1, class T2>
358KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>(const pair<T1, T2>& lhs,
359 const pair<T1, T2>& rhs) {
360 return rhs < lhs;
361}
362
364template <class T1, class T2>
365KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(const pair<T1, T2>& lhs,
366 const pair<T1, T2>& rhs) {
367 return !(lhs < rhs);
368}
369
374template <class T1, class T2>
375KOKKOS_FORCEINLINE_FUNCTION constexpr pair<T1, T2> make_pair(T1 x, T2 y) {
376 return (pair<T1, T2>(x, y));
377}
378
418template <class T1, class T2>
419KOKKOS_FORCEINLINE_FUNCTION pair<T1&, T2&> tie(T1& x, T2& y) {
420 return (pair<T1&, T2&>(x, y));
421}
422
423//
424// Specialization of Kokkos::pair for a \c void second argument. This
425// is not actually a "pair"; it only contains one element, the first.
426//
427template <class T1>
428struct pair<T1, void> {
429 using first_type = T1;
430 using second_type = void;
431
433 enum { second = 0 };
434
435 KOKKOS_DEFAULTED_FUNCTION constexpr pair() = default;
436
437 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const first_type& f) : first(f) {}
438
439 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const first_type& f, int)
440 : first(f) {}
441
442 template <class U>
443 KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair<U, void>& p)
444 : first(p.first) {}
445
446 template <class U>
447 KOKKOS_FORCEINLINE_FUNCTION pair<T1, void>& operator=(
448 const pair<U, void>& p) {
449 first = p.first;
450 return *this;
451 }
452};
453
454//
455// Specialization of relational operators for Kokkos::pair<T1,void>.
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.first == rhs.first;
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 == rhs);
468}
469
470template <class T1>
471KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<(
472 const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
473 return lhs.first < rhs.first;
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 rhs < lhs;
486}
487
488template <class T1>
489KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(
490 const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
491 return !(lhs < rhs);
492}
493
494namespace Impl {
495
496template <class T>
497struct is_pair_like : std::false_type {};
498template <class T, class U>
499struct is_pair_like<Kokkos::pair<T, U>> : std::true_type {};
500template <class T, class U>
501struct is_pair_like<std::pair<T, U>> : std::true_type {};
502
503} // end namespace Impl
504
505} // namespace Kokkos
506
507#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.
Replacement for std::pair that works on CUDA devices.
Definition: Kokkos_Pair.hpp:65
T2 second_type
The second template parameter of this class.
Definition: Kokkos_Pair.hpp:69
first_type first
The first element of the pair.
Definition: Kokkos_Pair.hpp:72
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.
Definition: Kokkos_Pair.hpp:87
KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair< U, V > &p)
Copy constructor.
Definition: Kokkos_Pair.hpp:96
std::pair< T1, T2 > to_std_pair() const
Return the std::pair version of this object.
KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const volatile pair< U, V > &p)
Copy constructor.
KOKKOS_FORCEINLINE_FUNCTION void operator=(const volatile pair< U, V > &p) volatile
Assignment operator, for volatile *this.
second_type second
The second element of the pair.
Definition: Kokkos_Pair.hpp:74
KOKKOS_DEFAULTED_FUNCTION constexpr pair()=default
Default constructor.
T1 first_type
The first template parameter of this class.
Definition: Kokkos_Pair.hpp:67