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. 2.0
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Christian R. Trott (crtrott@sandia.gov)
38 //
39 // ************************************************************************
40 //@HEADER
41 
47 
48 #ifndef KOKKOS_PAIR_HPP
49 #define KOKKOS_PAIR_HPP
50 
51 #include <Kokkos_Macros.hpp>
52 #include <utility>
53 
54 namespace Kokkos {
63 template <class T1, class T2>
64 struct pair
65 {
67  typedef T1 first_type;
69  typedef T2 second_type;
70 
75 
81  KOKKOS_FORCEINLINE_FUNCTION constexpr
82 #ifdef KOKKOS_CUDA_9_DEFAULTED_BUG_WORKAROUND
83  pair() : first(), second() {}
84 #else
85  pair() = default;
86 #endif
87 
92  KOKKOS_FORCEINLINE_FUNCTION constexpr
93  pair(first_type const& f, second_type const& s)
94  : first(f), second(s)
95  {}
96 
101  template <class U, class V>
102  KOKKOS_FORCEINLINE_FUNCTION constexpr
103  pair( const pair<U,V> &p)
104  : first(p.first), second(p.second)
105  {}
106 
111  template <class U, class V>
112  KOKKOS_FORCEINLINE_FUNCTION constexpr
113  pair( const volatile pair<U,V> &p)
114  : first(p.first), second(p.second)
115  {}
116 
121  template <class U, class V>
122  KOKKOS_FORCEINLINE_FUNCTION
124  {
125  first = p.first;
126  second = p.second;
127  return *this;
128  }
129 
130 
142  template <class U, class V>
143  KOKKOS_FORCEINLINE_FUNCTION
144  void operator=(const volatile pair<U,V> &p) volatile
145  {
146  first = p.first;
147  second = p.second;
148  // We deliberately do not return anything here. See explanation
149  // in public documentation above.
150  }
151 
152  // from std::pair<U,V>
153  template <class U, class V>
154  pair( const std::pair<U,V> &p)
155  : first(p.first), second(p.second)
156  {}
157 
167  std::pair<T1,T2> to_std_pair() const
168  { return std::make_pair(first,second); }
169 };
170 
171 template <class T1, class T2>
172 struct pair<T1&, T2&>
173 {
175  typedef T1& first_type;
177  typedef T2& second_type;
178 
180  first_type first;
182  second_type second;
183 
188  KOKKOS_FORCEINLINE_FUNCTION constexpr
189  pair(first_type f, second_type s)
190  : first(f), second(s)
191  {}
192 
197  template <class U, class V>
198  KOKKOS_FORCEINLINE_FUNCTION constexpr
199  pair( const pair<U,V> &p)
200  : first(p.first), second(p.second)
201  {}
202 
203  // from std::pair<U,V>
204  template <class U, class V>
205  pair( const std::pair<U,V> &p)
206  : first(p.first), second(p.second)
207  {}
208 
213  template <class U, class V>
214  KOKKOS_FORCEINLINE_FUNCTION
215  pair<first_type, second_type> & operator=(const pair<U,V> &p)
216  {
217  first = p.first;
218  second = p.second;
219  return *this;
220  }
221 
231  std::pair<T1,T2> to_std_pair() const
232  { return std::make_pair(first,second); }
233 };
234 
235 template <class T1, class T2>
236 struct pair<T1, T2&>
237 {
239  typedef T1 first_type;
241  typedef T2& second_type;
242 
244  first_type first;
246  second_type second;
247 
252  KOKKOS_FORCEINLINE_FUNCTION constexpr
253  pair(first_type const& f, second_type s)
254  : first(f), second(s)
255  {}
256 
261  template <class U, class V>
262  KOKKOS_FORCEINLINE_FUNCTION constexpr
263  pair( const pair<U,V> &p)
264  : first(p.first), second(p.second)
265  {}
266 
267  // from std::pair<U,V>
268  template <class U, class V>
269  pair( const std::pair<U,V> &p)
270  : first(p.first), second(p.second)
271  {}
272 
277  template <class U, class V>
278  KOKKOS_FORCEINLINE_FUNCTION
279  pair<first_type, second_type> & operator=(const pair<U,V> &p)
280  {
281  first = p.first;
282  second = p.second;
283  return *this;
284  }
285 
295  std::pair<T1,T2> to_std_pair() const
296  { return std::make_pair(first,second); }
297 };
298 
299 template <class T1, class T2>
300 struct pair<T1&, T2>
301 {
303  typedef T1& first_type;
305  typedef T2 second_type;
306 
308  first_type first;
310  second_type second;
311 
316  KOKKOS_FORCEINLINE_FUNCTION constexpr
317  pair(first_type f, second_type const& s)
318  : first(f), second(s)
319  {}
320 
325  template <class U, class V>
326  KOKKOS_FORCEINLINE_FUNCTION constexpr
327  pair( const pair<U,V> &p)
328  : first(p.first), second(p.second)
329  {}
330 
331  // from std::pair<U,V>
332  template <class U, class V>
333  pair( const std::pair<U,V> &p)
334  : first(p.first), second(p.second)
335  {}
336 
341  template <class U, class V>
342  KOKKOS_FORCEINLINE_FUNCTION
343  pair<first_type, second_type> & operator=(const pair<U,V> &p)
344  {
345  first = p.first;
346  second = p.second;
347  return *this;
348  }
349 
359  std::pair<T1,T2> to_std_pair() const
360  { return std::make_pair(first,second); }
361 };
362 
364 template <class T1, class T2>
365 KOKKOS_FORCEINLINE_FUNCTION
366 bool operator== (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
367 { return lhs.first==rhs.first && lhs.second==rhs.second; }
368 
370 template <class T1, class T2>
371 KOKKOS_FORCEINLINE_FUNCTION constexpr
372 bool operator!= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
373 { return !(lhs==rhs); }
374 
376 template <class T1, class T2>
377 KOKKOS_FORCEINLINE_FUNCTION constexpr
378 bool operator< (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
379 { return lhs.first<rhs.first || (!(rhs.first<lhs.first) && lhs.second<rhs.second); }
380 
382 template <class T1, class T2>
383 KOKKOS_FORCEINLINE_FUNCTION constexpr
384 bool operator<= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
385 { return !(rhs<lhs); }
386 
388 template <class T1, class T2>
389 KOKKOS_FORCEINLINE_FUNCTION constexpr
390 bool operator> (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
391 { return rhs<lhs; }
392 
394 template <class T1, class T2>
395 KOKKOS_FORCEINLINE_FUNCTION constexpr
396 bool operator>= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
397 { return !(lhs<rhs); }
398 
403 template <class T1,class T2>
404 KOKKOS_FORCEINLINE_FUNCTION constexpr
405 pair<T1,T2> make_pair (T1 x, T2 y)
406 { return ( pair<T1,T2>(x,y) ); }
407 
447 template <class T1,class T2>
448 KOKKOS_FORCEINLINE_FUNCTION
449 pair<T1 &,T2 &> tie (T1 & x, T2 & y)
450 { return ( pair<T1 &,T2 &>(x,y) ); }
451 
452 //
453 // Specialization of Kokkos::pair for a \c void second argument. This
454 // is not actually a "pair"; it only contains one element, the first.
455 //
456 template <class T1>
457 struct pair<T1,void>
458 {
459  typedef T1 first_type;
460  typedef void second_type;
461 
463  enum { second = 0 };
464 
465  KOKKOS_FORCEINLINE_FUNCTION constexpr
466 #ifdef KOKKOS_CUDA_9_DEFAULTED_BUG_WORKAROUND
467  pair() : first() {}
468 #else
469  pair() = default;
470 #endif
471 
472  KOKKOS_FORCEINLINE_FUNCTION constexpr
473  pair(const first_type & f)
474  : first(f)
475  {}
476 
477  KOKKOS_FORCEINLINE_FUNCTION constexpr
478  pair(const first_type & f, int)
479  : first(f)
480  {}
481 
482  template <class U>
483  KOKKOS_FORCEINLINE_FUNCTION constexpr
484  pair( const pair<U,void> &p)
485  : first(p.first)
486  {}
487 
488  template <class U>
489  KOKKOS_FORCEINLINE_FUNCTION
490  pair<T1, void> & operator=(const pair<U,void> &p)
491  {
492  first = p.first;
493  return *this;
494  }
495 };
496 
497 //
498 // Specialization of relational operators for Kokkos::pair<T1,void>.
499 //
500 
501 template <class T1>
502 KOKKOS_FORCEINLINE_FUNCTION constexpr
503 bool operator== (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
504 { return lhs.first==rhs.first; }
505 
506 template <class T1>
507 KOKKOS_FORCEINLINE_FUNCTION constexpr
508 bool operator!= (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
509 { return !(lhs==rhs); }
510 
511 template <class T1>
512 KOKKOS_FORCEINLINE_FUNCTION constexpr
513 bool operator< (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
514 { return lhs.first<rhs.first; }
515 
516 template <class T1>
517 KOKKOS_FORCEINLINE_FUNCTION constexpr
518 bool operator<= (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
519 { return !(rhs<lhs); }
520 
521 template <class T1>
522 KOKKOS_FORCEINLINE_FUNCTION constexpr
523 bool operator> (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
524 { return rhs<lhs; }
525 
526 template <class T1>
527 KOKKOS_FORCEINLINE_FUNCTION constexpr
528 bool operator>= (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
529 { return !(lhs<rhs); }
530 
531 
532 namespace Impl {
533 
534 template <class T> struct is_pair_like : std::false_type { };
535 template <class T, class U> struct is_pair_like<Kokkos::pair<T, U>> : std::true_type { };
536 template <class T, class U> struct is_pair_like<std::pair<T, U>> : std::true_type { };
537 
538 } // end namespace Impl
539 
540 } // namespace Kokkos
541 
542 
543 #endif //KOKKOS_PAIR_HPP
544 
KOKKOS_INLINE_FUNCTION bool operator==(const complex< RealType1 > &x, const complex< RealType2 > &y)
Equality operator for two complex numbers.
KOKKOS_INLINE_FUNCTION bool operator!=(const complex< RealType1 > &x, const complex< RealType2 > &y)
Inequality operator for two complex numbers.
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(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:64
std::pair< T1, T2 > to_std_pair() const
Return the std::pair version of this object.
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:93
T1 first_type
The first template parameter of this class.
Definition: Kokkos_Pair.hpp:67
KOKKOS_FORCEINLINE_FUNCTION void operator=(const volatile pair< U, V > &p) volatile
Assignment operator, for volatile *this.
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)
Greater-than operator for Kokkos::pair.
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(const pair< U, V > &p)
Copy constructor.
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.
KOKKOS_FORCEINLINE_FUNCTION constexpr pair()=default
Default constructor.