traits.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
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 
21 namespace CUDF_EXPORT cudf {
22 
29 template <typename...>
30 using void_t = void;
31 
43 #define CUDF_ENABLE_IF(...) cuda::std::enable_if_t<(__VA_ARGS__)>* = nullptr
44 
46 template <typename L, typename R>
47 using less_comparable = decltype(cuda::std::declval<L>() < cuda::std::declval<R>());
48 
50 template <typename L, typename R>
51 using greater_comparable = decltype(cuda::std::declval<L>() > cuda::std::declval<R>());
52 
54 template <typename L, typename R>
55 using equality_comparable = decltype(cuda::std::declval<L>() == cuda::std::declval<R>());
56 
57 namespace detail {
58 template <typename L, typename R, typename = void>
59 struct is_relationally_comparable_impl : cuda::std::false_type {};
60 
61 template <typename L, typename R>
62 struct is_relationally_comparable_impl<L,
63  R,
64  void_t<less_comparable<L, R>, greater_comparable<L, R>>>
65  : cuda::std::true_type {};
66 
67 template <typename L, typename R, typename = void>
68 struct is_equality_comparable_impl : cuda::std::false_type {};
69 
70 template <typename L, typename R>
71 struct is_equality_comparable_impl<L, R, void_t<equality_comparable<L, R>>> : cuda::std::true_type {
72 };
73 
74 // has common type
75 template <typename AlwaysVoid, typename... Ts>
76 struct has_common_type_impl : cuda::std::false_type {};
77 
78 template <typename... Ts>
79 struct has_common_type_impl<void_t<cuda::std::common_type_t<Ts...>>, Ts...> : cuda::std::true_type {
80 };
81 } // namespace detail
82 
84 template <typename... Ts>
85 using has_common_type = typename detail::has_common_type_impl<void, Ts...>::type;
86 
88 template <typename... Ts>
89 constexpr inline bool has_common_type_v = detail::has_common_type_impl<void, Ts...>::value;
90 
92 template <typename T>
93 using is_timestamp_t = cuda::std::disjunction<cuda::std::is_same<cudf::timestamp_D, T>,
94  cuda::std::is_same<cudf::timestamp_h, T>,
95  cuda::std::is_same<cudf::timestamp_m, T>,
96  cuda::std::is_same<cudf::timestamp_s, T>,
97  cuda::std::is_same<cudf::timestamp_ms, T>,
98  cuda::std::is_same<cudf::timestamp_us, T>,
99  cuda::std::is_same<cudf::timestamp_ns, T>>;
100 
102 template <typename T>
103 using is_duration_t = cuda::std::disjunction<cuda::std::is_same<cudf::duration_D, T>,
104  cuda::std::is_same<cudf::duration_h, T>,
105  cuda::std::is_same<cudf::duration_m, T>,
106  cuda::std::is_same<cudf::duration_s, T>,
107  cuda::std::is_same<cudf::duration_ms, T>,
108  cuda::std::is_same<cudf::duration_us, T>,
109  cuda::std::is_same<cudf::duration_ns, T>>;
110 
123 template <typename L, typename R>
124 constexpr inline bool is_relationally_comparable()
125 {
126  return detail::is_relationally_comparable_impl<L, R>::value;
127 }
128 
137 
150 template <typename L, typename R>
151 constexpr inline bool is_equality_comparable()
152 {
153  return detail::is_equality_comparable_impl<L, R>::value;
154 }
155 
164 
172 template <typename T>
173 CUDF_HOST_DEVICE constexpr inline bool is_numeric()
174 {
175  return cuda::std::is_arithmetic<T>();
176 }
177 
190 
202 template <typename T>
203 constexpr inline bool is_index_type()
204 {
205  return cuda::std::is_integral_v<T> and not cuda::std::is_same_v<T, bool>;
206 }
207 
220 
227 template <typename T>
228 CUDF_HOST_DEVICE constexpr inline bool is_signed()
229 {
230  return cuda::std::is_signed_v<T>;
231 }
232 
242 bool is_signed(data_type type);
243 
251 template <typename T>
252 constexpr inline bool is_unsigned()
253 {
254  return cuda::std::is_unsigned_v<T>;
255 }
256 
267 
274 template <typename Iterator>
275 CUDF_HOST_DEVICE constexpr inline bool is_signed_iterator()
276 {
277  return cuda::std::is_signed_v<typename cuda::std::iterator_traits<Iterator>::value_type>;
278 }
279 
287 template <typename T>
288 CUDF_HOST_DEVICE constexpr inline bool is_integral()
289 {
290  return cuda::std::is_integral_v<T>;
291 }
292 
303 
311 template <typename T>
312 constexpr inline bool is_integral_not_bool()
313 {
314  return cuda::std::is_integral_v<T> and not cuda::std::is_same_v<T, bool>;
315 }
316 
327 
335 template <typename T>
336 constexpr inline bool is_numeric_not_bool()
337 {
338  return cudf::is_numeric<T>() and not cuda::std::is_same_v<T, bool>;
339 }
340 
351 
359 template <typename T>
360 CUDF_HOST_DEVICE constexpr inline bool is_floating_point()
361 {
362  return cuda::std::is_floating_point_v<T>;
363 }
364 
375 
383 template <typename T>
384 constexpr inline bool is_byte()
385 {
386  return cuda::std::is_same_v<cuda::std::remove_cv_t<T>, std::byte>;
387 }
388 
396 template <typename T>
397 constexpr inline bool is_boolean()
398 {
399  return cuda::std::is_same_v<T, bool>;
400 }
401 
410 
418 template <typename T>
419 CUDF_HOST_DEVICE constexpr inline bool is_timestamp()
420 {
422 }
423 
434 
442 template <typename T>
443 CUDF_HOST_DEVICE constexpr inline bool is_fixed_point()
444 {
445  return cuda::std::is_same_v<numeric::decimal32, T> ||
446  cuda::std::is_same_v<numeric::decimal64, T> ||
447  cuda::std::is_same_v<numeric::decimal128, T> ||
448  cuda::std::is_same_v<numeric::fixed_point<int32_t, numeric::Radix::BASE_2>, T> ||
449  cuda::std::is_same_v<numeric::fixed_point<int64_t, numeric::Radix::BASE_2>, T> ||
450  cuda::std::is_same_v<numeric::fixed_point<__int128_t, numeric::Radix::BASE_2>, T>;
451 }
452 
461 
469 template <typename T>
470 CUDF_HOST_DEVICE constexpr inline bool is_duration()
471 {
473 }
474 
485 
493 template <typename T>
494 CUDF_HOST_DEVICE constexpr inline bool is_chrono()
495 {
496  return is_duration<T>() || is_timestamp<T>();
497 }
498 
509 bool is_chrono(data_type type);
510 
523 template <typename T>
524 constexpr bool is_rep_layout_compatible()
525 {
526  return cudf::is_numeric<T>() or cudf::is_chrono<T>() or cudf::is_boolean<T>() or
527  cudf::is_byte<T>();
528 }
529 
537 template <typename T>
538 CUDF_HOST_DEVICE constexpr inline bool is_dictionary()
539 {
540  return cuda::std::is_same_v<dictionary32, T>;
541 }
542 
551 
559 template <typename T>
560 constexpr inline bool is_dictionary_key()
561 {
562  return !is_dictionary<T>() && is_relationally_comparable<T, T>();
563 }
564 
573 
583 template <typename T>
584 CUDF_HOST_DEVICE constexpr inline bool is_fixed_width()
585 {
586  // TODO Add fixed width wrapper types
587  // Is a category fixed width?
588  return cudf::is_numeric<T>() || cudf::is_chrono<T>() || cudf::is_fixed_point<T>();
589 }
590 
601 
602 class string_view;
603 
616 template <typename T>
617 CUDF_HOST_DEVICE constexpr inline bool is_compound()
618 {
619  return cuda::std::is_same_v<T, cudf::string_view> or
620  cuda::std::is_same_v<T, cudf::dictionary32> or cuda::std::is_same_v<T, cudf::list_view> or
621  cuda::std::is_same_v<T, cudf::struct_view>;
622 }
623 
637 
649 template <typename T>
650 CUDF_HOST_DEVICE constexpr inline bool is_nested()
651 {
652  return cuda::std::is_same_v<T, cudf::list_view> || cuda::std::is_same_v<T, cudf::struct_view>;
653 }
654 
666 bool is_nested(data_type type);
667 
682 
683 template <typename From, typename To>
684 struct is_convertible : cuda::std::is_convertible<From, To> {};
685 
686 // This will ensure that timestamps can be promoted to a higher precision. Presently, they can't
687 // do that due to nvcc/gcc compiler issues
688 template <typename Duration1, typename Duration2>
690  : cuda::std::is_convertible<typename cudf::detail::time_point<Duration1>::duration,
691  typename cudf::detail::time_point<Duration2>::duration> {};
692 
695 } // namespace CUDF_EXPORT cudf
Indicator for the logical data type of an element in a column.
Definition: types.hpp:286
A non-owning, immutable view of device data that is a variable length char array representing a UTF-8...
Definition: string_view.hpp:35
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:384
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:85
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:51
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:55
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:109
bool is_dictionary_key(data_type type)
Indicates whether type is a valid dictionary type.
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:524
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:47
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:99
void void_t
Utility metafunction that maps a sequence of any types to the type void.
Definition: traits.hpp:30
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:89
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:275
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