thrust
functional.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2018 NVIDIA Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
22 #pragma once
23 
24 #include <thrust/detail/config.h>
25 #include <functional>
26 #include <thrust/detail/functional/placeholder.h>
27 
28 namespace thrust
29 {
30 
34 template<typename Operation> struct unary_traits;
35 
36 template<typename Operation> struct binary_traits;
37 
68 template<typename Argument,
69  typename Result>
71 {
75  typedef Argument argument_type;
76 
80  typedef Result result_type;
81 }; // end unary_function
82 
108 template<typename Argument1,
109  typename Argument2,
110  typename Result>
112 {
116  typedef Argument1 first_argument_type;
117 
121  typedef Argument2 second_argument_type;
122 
126  typedef Result result_type;
127 }; // end binary_function
128 
175 template<typename T>
176 struct plus
177 {
181  typedef T first_argument_type;
182 
186  typedef T second_argument_type;
187 
191  typedef T result_type;
192 
195  __thrust_exec_check_disable__
196  __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs + rhs;}
197 }; // end plus
198 
232 template<typename T>
233 struct minus
234 {
238  typedef T first_argument_type;
239 
243  typedef T second_argument_type;
244 
248  typedef T result_type;
249 
252  __thrust_exec_check_disable__
253  __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs - rhs;}
254 }; // end minus
255 
289 template<typename T>
290 struct multiplies
291 {
295  typedef T first_argument_type;
296 
300  typedef T second_argument_type;
301 
305  typedef T result_type;
306 
309  __thrust_exec_check_disable__
310  __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs * rhs;}
311 }; // end multiplies
312 
346 template<typename T>
347 struct divides
348 {
352  typedef T first_argument_type;
353 
357  typedef T second_argument_type;
358 
362  typedef T result_type;
363 
366  __thrust_exec_check_disable__
367  __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs / rhs;}
368 }; // end divides
369 
403 template<typename T>
404 struct modulus
405 {
409  typedef T first_argument_type;
410 
414  typedef T second_argument_type;
415 
419  typedef T result_type;
420 
423  __thrust_exec_check_disable__
424  __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs % rhs;}
425 }; // end modulus
426 
457 template<typename T>
458 struct negate
459 {
463  typedef T argument_type;
464 
468  typedef T result_type;
469 
472  __thrust_exec_check_disable__
473  __host__ __device__ T operator()(const T &x) const {return -x;}
474 }; // end negate
475 
505 template<typename T>
506 struct square
507 {
511  typedef T argument_type;
512 
516  typedef T result_type;
517 
520  __thrust_exec_check_disable__
521  __host__ __device__ T operator()(const T &x) const {return x*x;}
522 }; // end square
523 
543 template<typename T>
544 struct equal_to
545 {
549  typedef T first_argument_type;
550 
554  typedef T second_argument_type;
555 
559  typedef bool result_type;
560 
563  __thrust_exec_check_disable__
564  __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs == rhs;}
565 }; // end equal_to
566 
578 template<typename T>
579 struct not_equal_to
580 {
584  typedef T first_argument_type;
585 
589  typedef T second_argument_type;
590 
594  typedef bool result_type;
595 
598  __thrust_exec_check_disable__
599  __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs != rhs;}
600 }; // end not_equal_to
601 
613 template<typename T>
614 struct greater
615 {
619  typedef T first_argument_type;
620 
624  typedef T second_argument_type;
625 
629  typedef bool result_type;
630 
633  __thrust_exec_check_disable__
634  __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs > rhs;}
635 }; // end greater
636 
648 template<typename T>
649 struct less
650 {
654  typedef T first_argument_type;
655 
659  typedef T second_argument_type;
660 
664  typedef bool result_type;
665 
668  __thrust_exec_check_disable__
669  __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs < rhs;}
670 }; // end less
671 
683 template<typename T>
684 struct greater_equal
685 {
689  typedef T first_argument_type;
690 
694  typedef T second_argument_type;
695 
699  typedef bool result_type;
700 
703  __thrust_exec_check_disable__
704  __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs >= rhs;}
705 }; // end greater_equal
706 
718 template<typename T>
719 struct less_equal
720 {
724  typedef T first_argument_type;
725 
729  typedef T second_argument_type;
730 
734  typedef bool result_type;
735 
738  __thrust_exec_check_disable__
739  __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs <= rhs;}
740 }; // end less_equal
741 
762 template<typename T>
763 struct logical_and
764 {
768  typedef T first_argument_type;
769 
773  typedef T second_argument_type;
774 
778  typedef bool result_type;
779 
782  __thrust_exec_check_disable__
783  __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs && rhs;}
784 }; // end logical_and
785 
797 template<typename T>
798 struct logical_or
799 {
803  typedef T first_argument_type;
804 
808  typedef T second_argument_type;
809 
813  typedef bool result_type;
814 
817  __thrust_exec_check_disable__
818  __host__ __device__ bool operator()(const T &lhs, const T &rhs) const {return lhs || rhs;}
819 }; // end logical_or
820 
846 template<typename T>
847 struct logical_not
848 {
852  typedef T first_argument_type;
853 
857  typedef T second_argument_type;
858 
862  typedef bool result_type;
863 
866  __thrust_exec_check_disable__
867  __host__ __device__ bool operator()(const T &x) const {return !x;}
868 }; // end logical_not
869 
910 template<typename T>
911 struct bit_and
912 {
916  typedef T first_argument_type;
921  typedef T second_argument_type;
926  typedef T result_type;
930  __thrust_exec_check_disable__
931  __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs & rhs;}
932 }; // end bit_and
933 
966 template<typename T>
967 struct bit_or
968 {
972  typedef T first_argument_type;
977  typedef T second_argument_type;
982  typedef T result_type;
986  __thrust_exec_check_disable__
987  __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs | rhs;}
988 }; // end bit_or
989 
1022 template<typename T>
1023 struct bit_xor
1028  typedef T first_argument_type;
1033  typedef T second_argument_type;
1038  typedef T result_type;
1042  __thrust_exec_check_disable__
1043  __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs ^ rhs;}
1044 }; // end bit_xor
1045 
1074 template<typename T>
1075 struct identity
1076 {
1080  typedef T argument_type;
1081 
1085  typedef T result_type;
1086 
1089  __thrust_exec_check_disable__
1090  __host__ __device__ const T &operator()(const T &x) const {return x;}
1091 }; // end identity
1092 
1117 template<typename T>
1118 struct maximum
1119 {
1124 
1129 
1133  typedef T result_type;
1134 
1137  __thrust_exec_check_disable__
1138  __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs < rhs ? rhs : lhs;}
1139 }; // end maximum
1140 
1165 template<typename T>
1166 struct minimum
1167 {
1172 
1177 
1181  typedef T result_type;
1182 
1185  __thrust_exec_check_disable__
1186  __host__ __device__ T operator()(const T &lhs, const T &rhs) const {return lhs < rhs ? lhs : rhs;}
1187 }; // end minimum
1188 
1207 template<typename T1, typename T2>
1209 {
1214 
1219 
1223  typedef T1 result_type;
1224 
1227  __host__ __device__ const T1 &operator()(const T1 &lhs, const T2 & /*rhs*/) const {return lhs;}
1228 }; // end project1st
1229 
1248 template<typename T1, typename T2>
1250 {
1255 
1260 
1264  typedef T2 result_type;
1265 
1268  __host__ __device__ const T2 &operator()(const T1 &/*lhs*/, const T2 &rhs) const {return rhs;}
1269 }; // end project2nd
1270 
1275 // odds and ends
1276 
1292 template<typename Predicate>
1293 struct unary_negate
1294  : public thrust::unary_function<typename Predicate::argument_type, bool>
1295 {
1299  __host__ __device__
1300  explicit unary_negate(Predicate p) : pred(p){}
1301 
1304  __thrust_exec_check_disable__
1305  __host__ __device__
1306  bool operator()(const typename Predicate::argument_type& x) { return !pred(x); }
1307 
1310  Predicate pred;
1313 }; // end unary_negate
1314 
1331 template<typename Predicate>
1332  __host__ __device__
1333  unary_negate<Predicate> not1(const Predicate &pred);
1334 
1345 template<typename Predicate>
1346 struct binary_negate
1347  : public thrust::binary_function<typename Predicate::first_argument_type,
1348  typename Predicate::second_argument_type,
1349  bool>
1350 {
1354  __host__ __device__
1355  explicit binary_negate(Predicate p) : pred(p){}
1356 
1359  __thrust_exec_check_disable__
1360  __host__ __device__
1361  bool operator()(const typename Predicate::first_argument_type& x, const typename Predicate::second_argument_type& y)
1362  {
1363  return !pred(x,y);
1364  }
1365 
1368  Predicate pred;
1371 }; // end binary_negate
1372 
1389 template<typename BinaryPredicate>
1390  __host__ __device__
1391  binary_negate<BinaryPredicate> not2(const BinaryPredicate &pred);
1392 
1445 namespace placeholders
1446 {
1448 
1451 #ifdef __CUDA_ARCH__
1452 static const __device__ thrust::detail::functional::placeholder<0>::type _1;
1453 #else
1454 static const thrust::detail::functional::placeholder<0>::type _1;
1455 #endif
1457 
1460 #ifdef __CUDA_ARCH__
1461 static const __device__ thrust::detail::functional::placeholder<1>::type _2;
1462 #else
1463 static const thrust::detail::functional::placeholder<1>::type _2;
1464 #endif
1466 
1469 #ifdef __CUDA_ARCH__
1470 static const __device__ thrust::detail::functional::placeholder<2>::type _3;
1471 #else
1472 static const thrust::detail::functional::placeholder<2>::type _3;
1473 #endif
1475 
1478 #ifdef __CUDA_ARCH__
1479 static const __device__ thrust::detail::functional::placeholder<3>::type _4;
1480 #else
1481 static const thrust::detail::functional::placeholder<3>::type _4;
1482 #endif
1484 
1487 #ifdef __CUDA_ARCH__
1488 static const __device__ thrust::detail::functional::placeholder<4>::type _5;
1489 #else
1490 static const thrust::detail::functional::placeholder<4>::type _5;
1491 #endif
1493 
1496 #ifdef __CUDA_ARCH__
1497 static const __device__ thrust::detail::functional::placeholder<5>::type _6;
1498 #else
1499 static const thrust::detail::functional::placeholder<5>::type _6;
1500 #endif
1502 
1505 #ifdef __CUDA_ARCH__
1506 static const __device__ thrust::detail::functional::placeholder<6>::type _7;
1507 #else
1508 static const thrust::detail::functional::placeholder<6>::type _7;
1509 #endif
1511 
1514 #ifdef __CUDA_ARCH__
1515 static const __device__ thrust::detail::functional::placeholder<7>::type _8;
1516 #else
1517 static const thrust::detail::functional::placeholder<7>::type _8;
1518 #endif
1520 
1523 #ifdef __CUDA_ARCH__
1524 static const __device__ thrust::detail::functional::placeholder<8>::type _9;
1525 #else
1526 static const thrust::detail::functional::placeholder<8>::type _9;
1527 #endif
1529 
1532 #ifdef __CUDA_ARCH__
1533 static const __device__ thrust::detail::functional::placeholder<9>::type _10;
1534 #else
1535 static const thrust::detail::functional::placeholder<9>::type _10;
1536 #endif
1537 
1538 
1539 } // end placeholders
1540 
1541 
1546 } // end thrust
1547 
1548 #include <thrust/detail/functional.inl>
1549 #include <thrust/detail/functional/operators.h>
1550 
T second_argument_type
The type of the function object&#39;s second argument.
Definition: functional.h:973
Definition: functional.h:963
T first_argument_type
The type of the function object&#39;s first argument.
Definition: functional.h:1024
Definition: functional.h:1113
T second_argument_type
The type of the function object&#39;s second argument.
Definition: functional.h:242
T second_argument_type
The type of the function object&#39;s second argument.
Definition: functional.h:917
Definition: functional.h:844
static const thrust::detail::functional::placeholder< 3 >::type _4
Definition: functional.h:1474
T second_argument_type
The type of the function object&#39;s second argument.
Definition: functional.h:299
Definition: functional.h:1340
Definition: functional.h:403
Definition: functional.h:1244
T first_argument_type
The type of the function object&#39;s first argument.
Definition: functional.h:1118
Definition: functional.h:760
T first_argument_type
The type of the function object&#39;s first argument.
Definition: functional.h:237
Definition: functional.h:70
Definition: functional.h:717
Definition: functional.h:175
Definition: functional.h:577
Definition: functional.h:457
thrust is the top-level namespace which contains all Thrust functions and types.
Definition: addressof.h:14
T1 first_argument_type
The type of the function object&#39;s first argument.
Definition: functional.h:1208
static const thrust::detail::functional::placeholder< 8 >::type _9
Definition: functional.h:1519
static const thrust::detail::functional::placeholder< 2 >::type _3
Definition: functional.h:1465
Argument1 first_argument_type
The type of the function object&#39;s first argument.
Definition: functional.h:116
T first_argument_type
The type of the function object&#39;s first argument.
Definition: functional.h:180
static const thrust::detail::functional::placeholder< 6 >::type _7
Definition: functional.h:1501
T first_argument_type
The type of the function object&#39;s first argument.
Definition: functional.h:1166
static const thrust::detail::functional::placeholder< 0 >::type _1
Definition: functional.h:1447
Definition: functional.h:111
Definition: functional.h:289
static const thrust::detail::functional::placeholder< 4 >::type _5
Definition: functional.h:1483
T1 first_argument_type
The type of the function object&#39;s first argument.
Definition: functional.h:1249
Definition: functional.h:1070
static const thrust::detail::functional::placeholder< 5 >::type _6
Definition: functional.h:1492
T first_argument_type
The type of the function object&#39;s first argument.
Definition: functional.h:294
T first_argument_type
The type of the function object&#39;s first argument.
Definition: functional.h:912
Definition: functional.h:647
__host__ __device__ unary_negate< Predicate > not1(const Predicate &pred)
Definition: functional.h:542
T second_argument_type
The type of the function object&#39;s second argument.
Definition: functional.h:356
T second_argument_type
The type of the function object&#39;s second argument.
Definition: functional.h:1029
Argument argument_type
The type of the function object&#39;s argument.
Definition: functional.h:75
static const thrust::detail::functional::placeholder< 9 >::type _10
Definition: functional.h:1528
T second_argument_type
The type of the function object&#39;s second argument.
Definition: functional.h:1171
Definition: functional.h:1203
Result result_type
The type of the function object&#39;s result;.
Definition: functional.h:126
Definition: functional.h:1161
Result result_type
The type of the function object&#39;s result.
Definition: functional.h:80
T first_argument_type
The type of the function object&#39;s first argument.
Definition: functional.h:351
Definition: functional.h:795
__host__ __device__ binary_negate< BinaryPredicate > not2(const BinaryPredicate &pred)
Definition: functional.h:682
Definition: functional.h:1287
Definition: functional.h:907
T second_argument_type
The type of the function object&#39;s second argument.
Definition: functional.h:185
T first_argument_type
The type of the function object&#39;s first argument.
Definition: functional.h:968
T2 second_argument_type
The type of the function object&#39;s second argument.
Definition: functional.h:1254
static const thrust::detail::functional::placeholder< 7 >::type _8
Definition: functional.h:1510
T second_argument_type
The type of the function object&#39;s second argument.
Definition: functional.h:1123
Definition: functional.h:505
T2 second_argument_type
The type of the function object&#39;s second argument.
Definition: functional.h:1213
T second_argument_type
The type of the function object&#39;s second argument.
Definition: functional.h:413
Definition: functional.h:346
Definition: functional.h:612
Argument2 second_argument_type
The type of the function object&#39;s second argument.
Definition: functional.h:121
Definition: functional.h:232
static const thrust::detail::functional::placeholder< 1 >::type _2
Definition: functional.h:1456
T first_argument_type
The type of the function object&#39;s first argument.
Definition: functional.h:408
Definition: functional.h:1019