traits.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2025, 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 #pragma once
18 
20 #include <cudf/types.hpp>
24 
25 #include <cuda/std/type_traits>
26 
27 namespace CUDF_EXPORT cudf {
28 
36 template <typename...>
37 using void_t = void;
38 
50 #define CUDF_ENABLE_IF(...) cuda::std::enable_if_t<(__VA_ARGS__)>* = nullptr
51 
53 template <typename L, typename R>
54 using less_comparable = decltype(cuda::std::declval<L>() < cuda::std::declval<R>());
55 
57 template <typename L, typename R>
58 using greater_comparable = decltype(cuda::std::declval<L>() > cuda::std::declval<R>());
59 
61 template <typename L, typename R>
62 using equality_comparable = decltype(cuda::std::declval<L>() == cuda::std::declval<R>());
63 
64 namespace detail {
65 template <typename L, typename R, typename = void>
66 struct is_relationally_comparable_impl : cuda::std::false_type {};
67 
68 template <typename L, typename R>
69 struct is_relationally_comparable_impl<L,
70  R,
71  void_t<less_comparable<L, R>, greater_comparable<L, R>>>
72  : cuda::std::true_type {};
73 
74 template <typename L, typename R, typename = void>
75 struct is_equality_comparable_impl : cuda::std::false_type {};
76 
77 template <typename L, typename R>
78 struct is_equality_comparable_impl<L, R, void_t<equality_comparable<L, R>>> : cuda::std::true_type {
79 };
80 
81 // has common type
82 template <typename AlwaysVoid, typename... Ts>
83 struct has_common_type_impl : cuda::std::false_type {};
84 
85 template <typename... Ts>
86 struct has_common_type_impl<void_t<cuda::std::common_type_t<Ts...>>, Ts...> : cuda::std::true_type {
87 };
88 } // namespace detail
89 
91 template <typename... Ts>
92 using has_common_type = typename detail::has_common_type_impl<void, Ts...>::type;
93 
95 template <typename... Ts>
96 constexpr inline bool has_common_type_v = detail::has_common_type_impl<void, Ts...>::value;
97 
99 template <typename T>
100 using is_timestamp_t = cuda::std::disjunction<cuda::std::is_same<cudf::timestamp_D, T>,
101  cuda::std::is_same<cudf::timestamp_h, T>,
102  cuda::std::is_same<cudf::timestamp_m, T>,
103  cuda::std::is_same<cudf::timestamp_s, T>,
104  cuda::std::is_same<cudf::timestamp_ms, T>,
105  cuda::std::is_same<cudf::timestamp_us, T>,
106  cuda::std::is_same<cudf::timestamp_ns, T>>;
107 
109 template <typename T>
110 using is_duration_t = cuda::std::disjunction<cuda::std::is_same<cudf::duration_D, T>,
111  cuda::std::is_same<cudf::duration_h, T>,
112  cuda::std::is_same<cudf::duration_m, T>,
113  cuda::std::is_same<cudf::duration_s, T>,
114  cuda::std::is_same<cudf::duration_ms, T>,
115  cuda::std::is_same<cudf::duration_us, T>,
116  cuda::std::is_same<cudf::duration_ns, T>>;
117 
130 template <typename L, typename R>
131 constexpr inline bool is_relationally_comparable()
132 {
133  return detail::is_relationally_comparable_impl<L, R>::value;
134 }
135 
144 
157 template <typename L, typename R>
158 constexpr inline bool is_equality_comparable()
159 {
160  return detail::is_equality_comparable_impl<L, R>::value;
161 }
162 
171 
179 template <typename T>
180 CUDF_HOST_DEVICE constexpr inline bool is_numeric()
181 {
182  return cuda::std::is_arithmetic<T>();
183 }
184 
197 
209 template <typename T>
210 constexpr inline bool is_index_type()
211 {
212  return cuda::std::is_integral_v<T> and not cuda::std::is_same_v<T, bool>;
213 }
214 
227 
234 template <typename T>
235 constexpr inline bool is_signed()
236 {
237  return cuda::std::is_signed_v<T>;
238 }
239 
249 bool is_signed(data_type type);
250 
258 template <typename T>
259 constexpr inline bool is_unsigned()
260 {
261  return cuda::std::is_unsigned_v<T>;
262 }
263 
274 
281 template <typename Iterator>
282 CUDF_HOST_DEVICE constexpr inline bool is_signed_iterator()
283 {
284  return cuda::std::is_signed_v<typename cuda::std::iterator_traits<Iterator>::value_type>;
285 }
286 
294 template <typename T>
295 constexpr inline bool is_integral()
296 {
297  return cuda::std::is_integral_v<T>;
298 }
299 
310 
318 template <typename T>
319 constexpr inline bool is_integral_not_bool()
320 {
321  return cuda::std::is_integral_v<T> and not cuda::std::is_same_v<T, bool>;
322 }
323 
334 
342 template <typename T>
343 constexpr inline bool is_numeric_not_bool()
344 {
345  return cudf::is_numeric<T>() and not cuda::std::is_same_v<T, bool>;
346 }
347 
358 
366 template <typename T>
367 CUDF_HOST_DEVICE constexpr inline bool is_floating_point()
368 {
369  return cuda::std::is_floating_point_v<T>;
370 }
371 
382 
390 template <typename T>
391 constexpr inline bool is_byte()
392 {
393  return cuda::std::is_same_v<cuda::std::remove_cv_t<T>, std::byte>;
394 }
395 
403 template <typename T>
404 constexpr inline bool is_boolean()
405 {
406  return cuda::std::is_same_v<T, bool>;
407 }
408 
417 
425 template <typename T>
426 CUDF_HOST_DEVICE constexpr inline bool is_timestamp()
427 {
429 }
430 
441 
449 template <typename T>
450 CUDF_HOST_DEVICE constexpr inline bool is_fixed_point()
451 {
452  return cuda::std::is_same_v<numeric::decimal32, T> ||
453  cuda::std::is_same_v<numeric::decimal64, T> ||
454  cuda::std::is_same_v<numeric::decimal128, T> ||
455  cuda::std::is_same_v<numeric::fixed_point<int32_t, numeric::Radix::BASE_2>, T> ||
456  cuda::std::is_same_v<numeric::fixed_point<int64_t, numeric::Radix::BASE_2>, T> ||
457  cuda::std::is_same_v<numeric::fixed_point<__int128_t, numeric::Radix::BASE_2>, T>;
458 }
459 
468 
476 template <typename T>
477 CUDF_HOST_DEVICE constexpr inline bool is_duration()
478 {
480 }
481 
492 
500 template <typename T>
501 CUDF_HOST_DEVICE constexpr inline bool is_chrono()
502 {
503  return is_duration<T>() || is_timestamp<T>();
504 }
505 
516 bool is_chrono(data_type type);
517 
530 template <typename T>
531 constexpr bool is_rep_layout_compatible()
532 {
533  return cudf::is_numeric<T>() or cudf::is_chrono<T>() or cudf::is_boolean<T>() or
534  cudf::is_byte<T>();
535 }
536 
544 template <typename T>
545 constexpr inline bool is_dictionary()
546 {
547  return cuda::std::is_same_v<dictionary32, T>;
548 }
549 
558 
568 template <typename T>
569 CUDF_HOST_DEVICE constexpr inline bool is_fixed_width()
570 {
571  // TODO Add fixed width wrapper types
572  // Is a category fixed width?
573  return cudf::is_numeric<T>() || cudf::is_chrono<T>() || cudf::is_fixed_point<T>();
574 }
575 
586 
587 class string_view;
588 
601 template <typename T>
602 CUDF_HOST_DEVICE constexpr inline bool is_compound()
603 {
604  return cuda::std::is_same_v<T, cudf::string_view> or
605  cuda::std::is_same_v<T, cudf::dictionary32> or cuda::std::is_same_v<T, cudf::list_view> or
606  cuda::std::is_same_v<T, cudf::struct_view>;
607 }
608 
622 
634 template <typename T>
635 CUDF_HOST_DEVICE constexpr inline bool is_nested()
636 {
637  return cuda::std::is_same_v<T, cudf::list_view> || cuda::std::is_same_v<T, cudf::struct_view>;
638 }
639 
651 bool is_nested(data_type type);
652 
667 
668 template <typename From, typename To>
669 struct is_convertible : cuda::std::is_convertible<From, To> {};
670 
671 // This will ensure that timestamps can be promoted to a higher precision. Presently, they can't
672 // do that due to nvcc/gcc compiler issues
673 template <typename Duration1, typename Duration2>
675  : cuda::std::is_convertible<typename cudf::detail::time_point<Duration1>::duration,
676  typename cudf::detail::time_point<Duration2>::duration> {};
677 
680 } // namespace CUDF_EXPORT cudf
Indicator for the logical data type of an element in a column.
Definition: types.hpp:243
A non-owning, immutable view of device data that is a variable length char array representing a UTF-8...
Definition: string_view.hpp:44
Concrete type definition for dictionary columns.
Concrete type definitions for int32_t and int64_t durations in varying resolutions.
Class definition for fixed point data type.
bool is_dictionary(data_type type)
Indicates whether type is a dictionary data_type.
constexpr bool is_byte()
Indicates whether T is a std::byte type.
Definition: traits.hpp:391
bool is_duration(data_type type)
Indicates whether type is a duration data_type.
bool is_numeric(data_type type)
Indicates whether type is a numeric data_type.
bool is_integral(data_type type)
Indicates whether type is a integral data_type.
typename detail::has_common_type_impl< void, Ts... >::type has_common_type
Checks if types have a common type.
Definition: traits.hpp:92
bool is_equality_comparable(data_type type)
Checks whether data_type type supports equality comparisons.
bool is_index_type(data_type type)
Indicates whether the type type is a index type.
decltype(cuda::std::declval< L >() > cuda::std::declval< R >()) greater_comparable
Checks if two types are comparable using greater operator (i.e. >).
Definition: traits.hpp:58
bool is_numeric_not_bool(data_type type)
Indicates whether type is a numeric data_type but not BOOL8.
bool is_relationally_comparable(data_type type)
Checks whether data_type type supports relational comparisons.
decltype(cuda::std::declval< L >()==cuda::std::declval< R >()) equality_comparable
Checks if two types are comparable using equality operator (i.e. ==).
Definition: traits.hpp:62
cuda::std::disjunction< cuda::std::is_same< cudf::duration_D, T >, cuda::std::is_same< cudf::duration_h, T >, cuda::std::is_same< cudf::duration_m, T >, cuda::std::is_same< cudf::duration_s, T >, cuda::std::is_same< cudf::duration_ms, T >, cuda::std::is_same< cudf::duration_us, T >, cuda::std::is_same< cudf::duration_ns, T > > is_duration_t
Checks if a type is a duration type.
Definition: traits.hpp:116
bool is_boolean(data_type type)
Indicates whether type is a Boolean data_type.
bool is_bit_castable(data_type from, data_type to)
Indicates whether from is bit-castable to to.
bool is_compound(data_type type)
Indicates whether elements of type are compound.
constexpr bool is_rep_layout_compatible()
Indicates whether T is layout compatible with its "representation" type.
Definition: traits.hpp:531
bool is_signed(data_type type)
Indicates whether type is a signed numeric data_type.
bool is_nested(data_type type)
Indicates whether type is a nested type.
decltype(cuda::std::declval< L >()< cuda::std::declval< R >()) less_comparable
Checks if two types are comparable using less operator (i.e. <).
Definition: traits.hpp:54
cuda::std::disjunction< cuda::std::is_same< cudf::timestamp_D, T >, cuda::std::is_same< cudf::timestamp_h, T >, cuda::std::is_same< cudf::timestamp_m, T >, cuda::std::is_same< cudf::timestamp_s, T >, cuda::std::is_same< cudf::timestamp_ms, T >, cuda::std::is_same< cudf::timestamp_us, T >, cuda::std::is_same< cudf::timestamp_ns, T > > is_timestamp_t
Checks if a type is a timestamp type.
Definition: traits.hpp:106
void void_t
Utility metafunction that maps a sequence of any types to the type void.
Definition: traits.hpp:37
bool is_timestamp(data_type type)
Indicates whether type is a timestamp data_type.
bool is_floating_point(data_type type)
Indicates whether type is a floating point data_type.
bool is_unsigned(data_type type)
Indicates whether type is a unsigned numeric data_type.
constexpr bool has_common_type_v
Helper variable template for has_common_type<>::value.
Definition: traits.hpp:96
bool is_integral_not_bool(data_type type)
Indicates whether type is a integral data_type and not BOOL8.
bool is_chrono(data_type type)
Indicates whether type is a chrono data_type.
bool is_fixed_point(data_type type)
Indicates whether type is a fixed point data_type.
bool is_fixed_width(data_type type)
Indicates whether elements of type are fixed-width.
constexpr CUDF_HOST_DEVICE bool is_signed_iterator()
Indicates whether the Iterator value type is unsigned.
Definition: traits.hpp:282
cuDF interfaces
Definition: host_udf.hpp:37
Concrete type definitions for int32_t and int64_t timestamps in varying resolutions as durations sinc...
time_point< Duration > timestamp
A wrapper around a column of time_point in varying resolutions.
Definition: timestamps.hpp:39
Type declarations for libcudf.
#define CUDF_HOST_DEVICE
Indicates that the function or method is usable on host and device.
Definition: types.hpp:32