traits.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
9 #include <cudf/types.hpp>
13 
14 #include <cuda/std/type_traits>
15 
16 namespace CUDF_EXPORT cudf {
17 
25 template <typename...>
26 using void_t = void;
27 
39 #define CUDF_ENABLE_IF(...) cuda::std::enable_if_t<(__VA_ARGS__)>* = nullptr
40 
42 template <typename L, typename R>
43 using less_comparable = decltype(cuda::std::declval<L>() < cuda::std::declval<R>());
44 
46 template <typename L, typename R>
47 using greater_comparable = decltype(cuda::std::declval<L>() > cuda::std::declval<R>());
48 
50 template <typename L, typename R>
51 using equality_comparable = decltype(cuda::std::declval<L>() == cuda::std::declval<R>());
52 
53 namespace detail {
54 template <typename L, typename R, typename = void>
55 struct is_relationally_comparable_impl : cuda::std::false_type {};
56 
57 template <typename L, typename R>
58 struct is_relationally_comparable_impl<L,
59  R,
60  void_t<less_comparable<L, R>, greater_comparable<L, R>>>
61  : cuda::std::true_type {};
62 
63 template <typename L, typename R, typename = void>
64 struct is_equality_comparable_impl : cuda::std::false_type {};
65 
66 template <typename L, typename R>
67 struct is_equality_comparable_impl<L, R, void_t<equality_comparable<L, R>>> : cuda::std::true_type {
68 };
69 
70 // has common type
71 template <typename AlwaysVoid, typename... Ts>
72 struct has_common_type_impl : cuda::std::false_type {};
73 
74 template <typename... Ts>
75 struct has_common_type_impl<void_t<cuda::std::common_type_t<Ts...>>, Ts...> : cuda::std::true_type {
76 };
77 } // namespace detail
78 
80 template <typename... Ts>
81 using has_common_type = typename detail::has_common_type_impl<void, Ts...>::type;
82 
84 template <typename... Ts>
85 constexpr inline bool has_common_type_v = detail::has_common_type_impl<void, Ts...>::value;
86 
88 template <typename T>
89 using is_timestamp_t = cuda::std::disjunction<cuda::std::is_same<cudf::timestamp_D, T>,
90  cuda::std::is_same<cudf::timestamp_h, T>,
91  cuda::std::is_same<cudf::timestamp_m, T>,
92  cuda::std::is_same<cudf::timestamp_s, T>,
93  cuda::std::is_same<cudf::timestamp_ms, T>,
94  cuda::std::is_same<cudf::timestamp_us, T>,
95  cuda::std::is_same<cudf::timestamp_ns, T>>;
96 
98 template <typename T>
99 using is_duration_t = cuda::std::disjunction<cuda::std::is_same<cudf::duration_D, T>,
100  cuda::std::is_same<cudf::duration_h, T>,
101  cuda::std::is_same<cudf::duration_m, T>,
102  cuda::std::is_same<cudf::duration_s, T>,
103  cuda::std::is_same<cudf::duration_ms, T>,
104  cuda::std::is_same<cudf::duration_us, T>,
105  cuda::std::is_same<cudf::duration_ns, T>>;
106 
119 template <typename L, typename R>
120 constexpr inline bool is_relationally_comparable()
121 {
122  return detail::is_relationally_comparable_impl<L, R>::value;
123 }
124 
133 
146 template <typename L, typename R>
147 constexpr inline bool is_equality_comparable()
148 {
149  return detail::is_equality_comparable_impl<L, R>::value;
150 }
151 
160 
168 template <typename T>
169 CUDF_HOST_DEVICE constexpr inline bool is_numeric()
170 {
171  return cuda::std::is_arithmetic<T>();
172 }
173 
186 
198 template <typename T>
199 constexpr inline bool is_index_type()
200 {
201  return cuda::std::is_integral_v<T> and not cuda::std::is_same_v<T, bool>;
202 }
203 
216 
223 template <typename T>
224 constexpr inline bool is_signed()
225 {
226  return cuda::std::is_signed_v<T>;
227 }
228 
238 bool is_signed(data_type type);
239 
247 template <typename T>
248 constexpr inline bool is_unsigned()
249 {
250  return cuda::std::is_unsigned_v<T>;
251 }
252 
263 
270 template <typename Iterator>
271 CUDF_HOST_DEVICE constexpr inline bool is_signed_iterator()
272 {
273  return cuda::std::is_signed_v<typename cuda::std::iterator_traits<Iterator>::value_type>;
274 }
275 
283 template <typename T>
284 constexpr inline bool is_integral()
285 {
286  return cuda::std::is_integral_v<T>;
287 }
288 
299 
307 template <typename T>
308 constexpr inline bool is_integral_not_bool()
309 {
310  return cuda::std::is_integral_v<T> and not cuda::std::is_same_v<T, bool>;
311 }
312 
323 
331 template <typename T>
332 constexpr inline bool is_numeric_not_bool()
333 {
334  return cudf::is_numeric<T>() and not cuda::std::is_same_v<T, bool>;
335 }
336 
347 
355 template <typename T>
356 CUDF_HOST_DEVICE constexpr inline bool is_floating_point()
357 {
358  return cuda::std::is_floating_point_v<T>;
359 }
360 
371 
379 template <typename T>
380 constexpr inline bool is_byte()
381 {
382  return cuda::std::is_same_v<cuda::std::remove_cv_t<T>, std::byte>;
383 }
384 
392 template <typename T>
393 constexpr inline bool is_boolean()
394 {
395  return cuda::std::is_same_v<T, bool>;
396 }
397 
406 
414 template <typename T>
415 CUDF_HOST_DEVICE constexpr inline bool is_timestamp()
416 {
418 }
419 
430 
438 template <typename T>
439 CUDF_HOST_DEVICE constexpr inline bool is_fixed_point()
440 {
441  return cuda::std::is_same_v<numeric::decimal32, T> ||
442  cuda::std::is_same_v<numeric::decimal64, T> ||
443  cuda::std::is_same_v<numeric::decimal128, T> ||
444  cuda::std::is_same_v<numeric::fixed_point<int32_t, numeric::Radix::BASE_2>, T> ||
445  cuda::std::is_same_v<numeric::fixed_point<int64_t, numeric::Radix::BASE_2>, T> ||
446  cuda::std::is_same_v<numeric::fixed_point<__int128_t, numeric::Radix::BASE_2>, T>;
447 }
448 
457 
465 template <typename T>
466 CUDF_HOST_DEVICE constexpr inline bool is_duration()
467 {
469 }
470 
481 
489 template <typename T>
490 CUDF_HOST_DEVICE constexpr inline bool is_chrono()
491 {
492  return is_duration<T>() || is_timestamp<T>();
493 }
494 
505 bool is_chrono(data_type type);
506 
519 template <typename T>
520 constexpr bool is_rep_layout_compatible()
521 {
522  return cudf::is_numeric<T>() or cudf::is_chrono<T>() or cudf::is_boolean<T>() or
523  cudf::is_byte<T>();
524 }
525 
533 template <typename T>
534 constexpr inline bool is_dictionary()
535 {
536  return cuda::std::is_same_v<dictionary32, T>;
537 }
538 
547 
557 template <typename T>
558 CUDF_HOST_DEVICE constexpr inline bool is_fixed_width()
559 {
560  // TODO Add fixed width wrapper types
561  // Is a category fixed width?
562  return cudf::is_numeric<T>() || cudf::is_chrono<T>() || cudf::is_fixed_point<T>();
563 }
564 
575 
576 class string_view;
577 
590 template <typename T>
591 CUDF_HOST_DEVICE constexpr inline bool is_compound()
592 {
593  return cuda::std::is_same_v<T, cudf::string_view> or
594  cuda::std::is_same_v<T, cudf::dictionary32> or cuda::std::is_same_v<T, cudf::list_view> or
595  cuda::std::is_same_v<T, cudf::struct_view>;
596 }
597 
611 
623 template <typename T>
624 CUDF_HOST_DEVICE constexpr inline bool is_nested()
625 {
626  return cuda::std::is_same_v<T, cudf::list_view> || cuda::std::is_same_v<T, cudf::struct_view>;
627 }
628 
640 bool is_nested(data_type type);
641 
656 
657 template <typename From, typename To>
658 struct is_convertible : cuda::std::is_convertible<From, To> {};
659 
660 // This will ensure that timestamps can be promoted to a higher precision. Presently, they can't
661 // do that due to nvcc/gcc compiler issues
662 template <typename Duration1, typename Duration2>
664  : cuda::std::is_convertible<typename cudf::detail::time_point<Duration1>::duration,
665  typename cudf::detail::time_point<Duration2>::duration> {};
666 
669 } // namespace CUDF_EXPORT cudf
Indicator for the logical data type of an element in a column.
Definition: types.hpp:238
A non-owning, immutable view of device data that is a variable length char array representing a UTF-8...
Definition: string_view.hpp:33
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:380
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:81
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:47
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:51
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:105
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:520
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:43
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:95
void void_t
Utility metafunction that maps a sequence of any types to the type void.
Definition: traits.hpp:26
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:85
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:271
cuDF interfaces
Definition: host_udf.hpp:26
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:28
Type declarations for libcudf.
#define CUDF_HOST_DEVICE
Indicates that the function or method is usable on host and device.
Definition: types.hpp:21