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 
75 
81  KOKKOS_FORCEINLINE_FUNCTION
82  pair()
83  : first(), second()
84  {}
85 
90  KOKKOS_FORCEINLINE_FUNCTION
91  pair(first_type const& f, second_type const& s)
92  : first(f), second(s)
93  {}
94 
99  template <class U, class V>
100  KOKKOS_FORCEINLINE_FUNCTION
101  pair( const pair<U,V> &p)
102  : first(p.first), second(p.second)
103  {}
104 
109  template <class U, class V>
110  KOKKOS_FORCEINLINE_FUNCTION
111  pair( const volatile pair<U,V> &p)
112  : first(p.first), second(p.second)
113  {}
114 
119  template <class U, class V>
120  KOKKOS_FORCEINLINE_FUNCTION
122  {
123  first = p.first;
124  second = p.second;
125  return *this;
126  }
127 
128 
140  template <class U, class V>
141  KOKKOS_FORCEINLINE_FUNCTION
142  void operator=(const volatile pair<U,V> &p) volatile
143  {
144  first = p.first;
145  second = p.second;
146  // We deliberately do not return anything here. See explanation
147  // in public documentation above.
148  }
149 
150  // from std::pair<U,V>
151  template <class U, class V>
152  pair( const std::pair<U,V> &p)
153  : first(p.first), second(p.second)
154  {}
155 
165  std::pair<T1,T2> to_std_pair() const
166  { return std::make_pair(first,second); }
167 };
168 
169 template <class T1, class T2>
170 struct pair<T1&, T2&>
171 {
173  typedef T1& first_type;
175  typedef T2& second_type;
176 
178  first_type first;
180  second_type second;
181 
186  KOKKOS_FORCEINLINE_FUNCTION
187  pair(first_type f, second_type s)
188  : first(f), second(s)
189  {}
190 
195  template <class U, class V>
196  KOKKOS_FORCEINLINE_FUNCTION
197  pair( const pair<U,V> &p)
198  : first(p.first), second(p.second)
199  {}
200 
201  // from std::pair<U,V>
202  template <class U, class V>
203  pair( const std::pair<U,V> &p)
204  : first(p.first), second(p.second)
205  {}
206 
211  template <class U, class V>
212  KOKKOS_FORCEINLINE_FUNCTION
213  pair<first_type, second_type> & operator=(const pair<U,V> &p)
214  {
215  first = p.first;
216  second = p.second;
217  return *this;
218  }
219 
229  std::pair<T1,T2> to_std_pair() const
230  { return std::make_pair(first,second); }
231 };
232 
233 template <class T1, class T2>
234 struct pair<T1, T2&>
235 {
237  typedef T1 first_type;
239  typedef T2& second_type;
240 
242  first_type first;
244  second_type second;
245 
250  KOKKOS_FORCEINLINE_FUNCTION
251  pair(first_type const& f, second_type s)
252  : first(f), second(s)
253  {}
254 
259  template <class U, class V>
260  KOKKOS_FORCEINLINE_FUNCTION
261  pair( const pair<U,V> &p)
262  : first(p.first), second(p.second)
263  {}
264 
265  // from std::pair<U,V>
266  template <class U, class V>
267  pair( const std::pair<U,V> &p)
268  : first(p.first), second(p.second)
269  {}
270 
275  template <class U, class V>
276  KOKKOS_FORCEINLINE_FUNCTION
277  pair<first_type, second_type> & operator=(const pair<U,V> &p)
278  {
279  first = p.first;
280  second = p.second;
281  return *this;
282  }
283 
293  std::pair<T1,T2> to_std_pair() const
294  { return std::make_pair(first,second); }
295 };
296 
297 template <class T1, class T2>
298 struct pair<T1&, T2>
299 {
301  typedef T1& first_type;
303  typedef T2 second_type;
304 
306  first_type first;
308  second_type second;
309 
314  KOKKOS_FORCEINLINE_FUNCTION
315  pair(first_type f, second_type const& s)
316  : first(f), second(s)
317  {}
318 
323  template <class U, class V>
324  KOKKOS_FORCEINLINE_FUNCTION
325  pair( const pair<U,V> &p)
326  : first(p.first), second(p.second)
327  {}
328 
329  // from std::pair<U,V>
330  template <class U, class V>
331  pair( const std::pair<U,V> &p)
332  : first(p.first), second(p.second)
333  {}
334 
339  template <class U, class V>
340  KOKKOS_FORCEINLINE_FUNCTION
341  pair<first_type, second_type> & operator=(const pair<U,V> &p)
342  {
343  first = p.first;
344  second = p.second;
345  return *this;
346  }
347 
357  std::pair<T1,T2> to_std_pair() const
358  { return std::make_pair(first,second); }
359 };
360 
362 template <class T1, class T2>
363 KOKKOS_FORCEINLINE_FUNCTION
364 bool operator== (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
365 { return lhs.first==rhs.first && lhs.second==rhs.second; }
366 
368 template <class T1, class T2>
369 KOKKOS_FORCEINLINE_FUNCTION
370 bool operator!= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
371 { return !(lhs==rhs); }
372 
374 template <class T1, class T2>
375 KOKKOS_FORCEINLINE_FUNCTION
376 bool operator< (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
377 { return lhs.first<rhs.first || (!(rhs.first<lhs.first) && lhs.second<rhs.second); }
378 
380 template <class T1, class T2>
381 KOKKOS_FORCEINLINE_FUNCTION
382 bool operator<= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
383 { return !(rhs<lhs); }
384 
386 template <class T1, class T2>
387 KOKKOS_FORCEINLINE_FUNCTION
388 bool operator> (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
389 { return rhs<lhs; }
390 
392 template <class T1, class T2>
393 KOKKOS_FORCEINLINE_FUNCTION
394 bool operator>= (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
395 { return !(lhs<rhs); }
396 
401 template <class T1,class T2>
402 KOKKOS_FORCEINLINE_FUNCTION
403 pair<T1,T2> make_pair (T1 x, T2 y)
404 { return ( pair<T1,T2>(x,y) ); }
405 
445 template <class T1,class T2>
446 KOKKOS_FORCEINLINE_FUNCTION
447 pair<T1 &,T2 &> tie (T1 & x, T2 & y)
448 { return ( pair<T1 &,T2 &>(x,y) ); }
449 
450 //
451 // Specialization of Kokkos::pair for a \c void second argument. This
452 // is not actually a "pair"; it only contains one element, the first.
453 //
454 template <class T1>
455 struct pair<T1,void>
456 {
457  typedef T1 first_type;
458  typedef void second_type;
459 
461  enum { second = 0 };
462 
463  KOKKOS_FORCEINLINE_FUNCTION
464  pair()
465  : first()
466  {}
467 
468  KOKKOS_FORCEINLINE_FUNCTION
469  pair(const first_type & f)
470  : first(f)
471  {}
472 
473  KOKKOS_FORCEINLINE_FUNCTION
474  pair(const first_type & f, int)
475  : first(f)
476  {}
477 
478  template <class U>
479  KOKKOS_FORCEINLINE_FUNCTION
480  pair( const pair<U,void> &p)
481  : first(p.first)
482  {}
483 
484  template <class U>
485  KOKKOS_FORCEINLINE_FUNCTION
486  pair<T1, void> & operator=(const pair<U,void> &p)
487  {
488  first = p.first;
489  return *this;
490  }
491 };
492 
493 //
494 // Specialization of relational operators for Kokkos::pair<T1,void>.
495 //
496 
497 template <class T1>
498 KOKKOS_FORCEINLINE_FUNCTION
499 bool operator== (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
500 { return lhs.first==rhs.first; }
501 
502 template <class T1>
503 KOKKOS_FORCEINLINE_FUNCTION
504 bool operator!= (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
505 { return !(lhs==rhs); }
506 
507 template <class T1>
508 KOKKOS_FORCEINLINE_FUNCTION
509 bool operator< (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
510 { return lhs.first<rhs.first; }
511 
512 template <class T1>
513 KOKKOS_FORCEINLINE_FUNCTION
514 bool operator<= (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
515 { return !(rhs<lhs); }
516 
517 template <class T1>
518 KOKKOS_FORCEINLINE_FUNCTION
519 bool operator> (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
520 { return rhs<lhs; }
521 
522 template <class T1>
523 KOKKOS_FORCEINLINE_FUNCTION
524 bool operator>= (const pair<T1,void>& lhs, const pair<T1,void>& rhs)
525 { return !(lhs<rhs); }
526 
527 } // namespace Kokkos
528 
529 
530 #endif //KOKKOS_PAIR_HPP
KOKKOS_FORCEINLINE_FUNCTION pair()
Default constructor.
Definition: Kokkos_Pair.hpp:82
KOKKOS_INLINE_FUNCTION bool operator!=(const complex< RealType > &x, const complex< RealType > &y)
Inequality operator for two complex numbers.
KOKKOS_FORCEINLINE_FUNCTION 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
KOKKOS_FORCEINLINE_FUNCTION pair(first_type const &f, second_type const &s)
Constructor that takes both elements of the pair.
Definition: Kokkos_Pair.hpp:91
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 bool operator>(const pair< T1, T2 > &lhs, const pair< T1, T2 > &rhs)
Greater-than operator for Kokkos::pair.
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 bool operator>=(const pair< T1, T2 > &lhs, const pair< T1, T2 > &rhs)
Greater-than-or-equal-to 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 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 > make_pair(T1 x, T2 y)
Return a new pair.
KOKKOS_FORCEINLINE_FUNCTION pair< T1, T2 > & operator=(const pair< U, V > &p)
Assignment operator.