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. 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 H. Carter Edwards (hcedwar@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 
72  first_type first;
74  second_type second;
75 
81  KOKKOS_FORCEINLINE_FUNCTION constexpr
82  pair() = default ;
83 
88  KOKKOS_FORCEINLINE_FUNCTION constexpr
89  pair(first_type const& f, second_type const& s)
90  : first(f), second(s)
91  {}
92 
97  template <class U, class V>
98  KOKKOS_FORCEINLINE_FUNCTION constexpr
99  pair( const pair<U,V> &p)
100  : first(p.first), second(p.second)
101  {}
102 
107  template <class U, class V>
108  KOKKOS_FORCEINLINE_FUNCTION constexpr
109  pair( const volatile pair<U,V> &p)
110  : first(p.first), second(p.second)
111  {}
112 
117  template <class U, class V>
118  KOKKOS_FORCEINLINE_FUNCTION
120  {
121  first = p.first;
122  second = p.second;
123  return *this;
124  }
125 
126 
138  template <class U, class V>
139  KOKKOS_FORCEINLINE_FUNCTION
140  void operator=(const volatile pair<U,V> &p) volatile
141  {
142  first = p.first;
143  second = p.second;
144  // We deliberately do not return anything here. See explanation
145  // in public documentation above.
146  }
147 
148  // from std::pair<U,V>
149  template <class U, class V>
150  pair( const std::pair<U,V> &p)
151  : first(p.first), second(p.second)
152  {}
153 
163  std::pair<T1,T2> to_std_pair() const
164  { return std::make_pair(first,second); }
165 };
166 
167 template <class T1, class T2>
168 struct pair<T1&, T2&>
169 {
171  typedef T1& first_type;
173  typedef T2& second_type;
174 
176  first_type first;
178  second_type second;
179 
184  KOKKOS_FORCEINLINE_FUNCTION constexpr
185  pair(first_type f, second_type s)
186  : first(f), second(s)
187  {}
188 
193  template <class U, class V>
194  KOKKOS_FORCEINLINE_FUNCTION constexpr
195  pair( const pair<U,V> &p)
196  : first(p.first), second(p.second)
197  {}
198 
199  // from std::pair<U,V>
200  template <class U, class V>
201  pair( const std::pair<U,V> &p)
202  : first(p.first), second(p.second)
203  {}
204 
209  template <class U, class V>
210  KOKKOS_FORCEINLINE_FUNCTION
212  {
213  first = p.first;
214  second = p.second;
215  return *this;
216  }
217 
227  std::pair<T1,T2> to_std_pair() const
228  { return std::make_pair(first,second); }
229 };
230 
231 template <class T1, class T2>
232 struct pair<T1, T2&>
233 {
235  typedef T1 first_type;
237  typedef T2& second_type;
238 
240  first_type first;
242  second_type second;
243 
248  KOKKOS_FORCEINLINE_FUNCTION constexpr
249  pair(first_type const& f, second_type s)
250  : first(f), second(s)
251  {}
252 
257  template <class U, class V>
258  KOKKOS_FORCEINLINE_FUNCTION constexpr
259  pair( const pair<U,V> &p)
260  : first(p.first), second(p.second)
261  {}
262 
263  // from std::pair<U,V>
264  template <class U, class V>
265  pair( const std::pair<U,V> &p)
266  : first(p.first), second(p.second)
267  {}
268 
273  template <class U, class V>
274  KOKKOS_FORCEINLINE_FUNCTION
276  {
277  first = p.first;
278  second = p.second;
279  return *this;
280  }
281 
291  std::pair<T1,T2> to_std_pair() const
292  { return std::make_pair(first,second); }
293 };
294 
295 template <class T1, class T2>
296 struct pair<T1&, T2>
297 {
299  typedef T1& first_type;
301  typedef T2 second_type;
302 
304  first_type first;
306  second_type second;
307 
312  KOKKOS_FORCEINLINE_FUNCTION constexpr
313  pair(first_type f, second_type const& s)
314  : first(f), second(s)
315  {}
316 
321  template <class U, class V>
322  KOKKOS_FORCEINLINE_FUNCTION constexpr
323  pair( const pair<U,V> &p)
324  : first(p.first), second(p.second)
325  {}
326 
327  // from std::pair<U,V>
328  template <class U, class V>
329  pair( const std::pair<U,V> &p)
330  : first(p.first), second(p.second)
331  {}
332 
337  template <class U, class V>
338  KOKKOS_FORCEINLINE_FUNCTION
340  {
341  first = p.first;
342  second = p.second;
343  return *this;
344  }
345 
355  std::pair<T1,T2> to_std_pair() const
356  { return std::make_pair(first,second); }
357 };
358 
360 template <class T1, class T2>
361 KOKKOS_FORCEINLINE_FUNCTION
362 bool operator== (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
363 { return lhs.first==rhs.first && lhs.second==rhs.second; }
364 
366 template <class T1, class T2>
367 KOKKOS_FORCEINLINE_FUNCTION constexpr
368 bool operator!= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
369 { return !(lhs==rhs); }
370 
372 template <class T1, class T2>
373 KOKKOS_FORCEINLINE_FUNCTION constexpr
374 bool operator< (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
375 { return lhs.first<rhs.first || (!(rhs.first<lhs.first) && lhs.second<rhs.second); }
376 
378 template <class T1, class T2>
379 KOKKOS_FORCEINLINE_FUNCTION constexpr
380 bool operator<= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
381 { return !(rhs<lhs); }
382 
384 template <class T1, class T2>
385 KOKKOS_FORCEINLINE_FUNCTION constexpr
386 bool operator> (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
387 { return rhs<lhs; }
388 
390 template <class T1, class T2>
391 KOKKOS_FORCEINLINE_FUNCTION constexpr
392 bool operator>= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
393 { return !(lhs<rhs); }
394 
399 template <class T1,class T2>
400 KOKKOS_FORCEINLINE_FUNCTION constexpr
401 pair<T1,T2> make_pair (T1 x, T2 y)
402 { return ( pair<T1,T2>(x,y) ); }
403 
443 template <class T1,class T2>
444 KOKKOS_FORCEINLINE_FUNCTION
445 pair<T1 &,T2 &> tie (T1 & x, T2 & y)
446 { return ( pair<T1 &,T2 &>(x,y) ); }
447 
448 //
449 // Specialization of Kokkos::pair for a \c void second argument. This
450 // is not actually a "pair"; it only contains one element, the first.
451 //
452 template <class T1>
453 struct pair<T1,void>
454 {
455  typedef T1 first_type;
456  typedef void second_type;
457 
458  first_type first;
459  enum { second = 0 };
460 
461  KOKKOS_FORCEINLINE_FUNCTION constexpr
462  pair() = default ;
463 
464  KOKKOS_FORCEINLINE_FUNCTION constexpr
465  pair(const first_type & f)
466  : first(f)
467  {}
468 
469  KOKKOS_FORCEINLINE_FUNCTION constexpr
470  pair(const first_type & f, int)
471  : first(f)
472  {}
473 
474  template <class U>
475  KOKKOS_FORCEINLINE_FUNCTION constexpr
476  pair( const pair<U,void> &p)
477  : first(p.first)
478  {}
479 
480  template <class U>
481  KOKKOS_FORCEINLINE_FUNCTION
483  {
484  first = p.first;
485  return *this;
486  }
487 };
488 
489 //
490 // Specialization of relational operators for Kokkos::pair<T1,void>.
491 //
492 
493 template <class T1>
494 KOKKOS_FORCEINLINE_FUNCTION constexpr
495 bool operator== (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
496 { return lhs.first==rhs.first; }
497 
498 template <class T1>
499 KOKKOS_FORCEINLINE_FUNCTION constexpr
500 bool operator!= (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
501 { return !(lhs==rhs); }
502 
503 template <class T1>
504 KOKKOS_FORCEINLINE_FUNCTION constexpr
505 bool operator< (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
506 { return lhs.first<rhs.first; }
507 
508 template <class T1>
509 KOKKOS_FORCEINLINE_FUNCTION constexpr
510 bool operator<= (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
511 { return !(rhs<lhs); }
512 
513 template <class T1>
514 KOKKOS_FORCEINLINE_FUNCTION constexpr
515 bool operator> (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
516 { return rhs<lhs; }
517 
518 template <class T1>
519 KOKKOS_FORCEINLINE_FUNCTION constexpr
520 bool operator>= (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
521 { return !(lhs<rhs); }
522 
523 } // namespace Kokkos
524 
525 
526 #endif //KOKKOS_PAIR_HPP
527 
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.
KOKKOS_INLINE_FUNCTION bool operator!=(const complex< RealType > &x, const complex< RealType > &y)
Inequality operator for two complex numbers.
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:89
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_INLINE_FUNCTION bool operator==(const complex< RealType > &x, const complex< RealType > &y)
Equality operator for two complex numbers.
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.
Definition: Kokkos_Pair.hpp:99
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.