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. 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 
55 namespace Kokkos {
64 template <class T1, class T2>
65 struct pair {
67  typedef T1 first_type;
69  typedef T2 second_type;
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 
156 template <class T1, class T2>
157 struct pair<T1&, T2&> {
159  typedef T1& first_type;
161  typedef T2& second_type;
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 
213 template <class T1, class T2>
214 struct pair<T1, T2&> {
216  typedef T1 first_type;
218  typedef T2& second_type;
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 
270 template <class T1, class T2>
271 struct pair<T1&, T2> {
273  typedef T1& first_type;
275  typedef T2 second_type;
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 
328 template <class T1, class T2>
329 KOKKOS_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 
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 == rhs);
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 lhs.first < rhs.first ||
346  (!(rhs.first < lhs.first) && lhs.second < rhs.second);
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 rhs < lhs;
361 }
362 
364 template <class T1, class T2>
365 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(const pair<T1, T2>& lhs,
366  const pair<T1, T2>& rhs) {
367  return !(lhs < rhs);
368 }
369 
374 template <class T1, class T2>
375 KOKKOS_FORCEINLINE_FUNCTION constexpr pair<T1, T2> make_pair(T1 x, T2 y) {
376  return (pair<T1, T2>(x, y));
377 }
378 
418 template <class T1, class T2>
419 KOKKOS_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 //
427 template <class T1>
428 struct pair<T1, void> {
429  typedef T1 first_type;
430  typedef void second_type;
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 
458 template <class T1>
459 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator==(
460  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
461  return lhs.first == rhs.first;
462 }
463 
464 template <class T1>
465 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator!=(
466  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
467  return !(lhs == rhs);
468 }
469 
470 template <class T1>
471 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<(
472  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
473  return lhs.first < rhs.first;
474 }
475 
476 template <class T1>
477 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<=(
478  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
479  return !(rhs < lhs);
480 }
481 
482 template <class T1>
483 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>(
484  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
485  return rhs < lhs;
486 }
487 
488 template <class T1>
489 KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>=(
490  const pair<T1, void>& lhs, const pair<T1, void>& rhs) {
491  return !(lhs < rhs);
492 }
493 
494 namespace Impl {
495 
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 #endif // KOKKOS_PAIR_HPP
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 pair< T1, T2 > make_pair(T1 x, T2 y)
Return a new pair.
KOKKOS_INLINE_FUNCTION bool operator==(complex< RealType1 > const &x, complex< RealType2 > const &y) noexcept
Binary == operator for complex complex.
KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const volatile pair< U, V > &p)
Copy constructor.
T2 second_type
The second template parameter of this class.
Definition: Kokkos_Pair.hpp:69
Replacement for std::pair that works on CUDA devices.
Definition: Kokkos_Pair.hpp:65
KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator<(const pair< T1, T2 > &lhs, const pair< T1, T2 > &rhs)
Less-than operator for Kokkos::pair.
first_type first
The first element of the pair.
Definition: Kokkos_Pair.hpp:72
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
T1 first_type
The first template parameter of this class.
Definition: Kokkos_Pair.hpp:67
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 void operator=(const volatile pair< U, V > &p) volatile
Assignment operator, for volatile *this.
KOKKOS_FORCEINLINE_FUNCTION pair< T1 &, T2 & > tie(T1 &x, T2 &y)
Return a pair of references to the input arguments.
KOKKOS_FORCEINLINE_FUNCTION constexpr bool operator>(const pair< T1, T2 > &lhs, const pair< T1, T2 > &rhs)
Greater-than operator for Kokkos::pair.
std::pair< T1, T2 > to_std_pair() const
Return the std::pair version of this object.
KOKKOS_INLINE_FUNCTION bool operator!=(complex< RealType1 > const &x, complex< RealType2 > const &y) noexcept
Binary != operator for complex complex.
KOKKOS_DEFAULTED_FUNCTION constexpr pair()=default
Default constructor.
KOKKOS_FORCEINLINE_FUNCTION constexpr pair(const pair< U, V > &p)
Copy constructor.
Definition: Kokkos_Pair.hpp:96
second_type second
The second element of the pair.
Definition: Kokkos_Pair.hpp:74
KOKKOS_FORCEINLINE_FUNCTION pair< T1, T2 > & operator=(const pair< U, V > &p)
Assignment operator.