traits.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2023, 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 {
28 
36 template <typename...>
37 using void_t = void;
38 
50 #define CUDF_ENABLE_IF(...) std::enable_if_t<(__VA_ARGS__)>* = nullptr
51 
53 template <typename L, typename R>
54 using less_comparable = decltype(std::declval<L>() < std::declval<R>());
55 
57 template <typename L, typename R>
58 using greater_comparable = decltype(std::declval<L>() > std::declval<R>());
59 
61 template <typename L, typename R>
62 using equality_comparable = decltype(std::declval<L>() == std::declval<R>());
63 
64 namespace detail {
65 template <typename L, typename R, typename = void>
66 struct is_relationally_comparable_impl : 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  : std::true_type {};
73 
74 template <typename L, typename R, typename = void>
75 struct is_equality_comparable_impl : std::false_type {};
76 
77 template <typename L, typename R>
78 struct is_equality_comparable_impl<L, R, void_t<equality_comparable<L, R>>> : std::true_type {};
79 
80 // has common type
81 template <typename AlwaysVoid, typename... Ts>
82 struct has_common_type_impl : std::false_type {};
83 
84 template <typename... Ts>
85 struct has_common_type_impl<void_t<std::common_type_t<Ts...>>, Ts...> : std::true_type {};
86 } // namespace detail
87 
89 template <typename... Ts>
90 using has_common_type = typename detail::has_common_type_impl<void, Ts...>::type;
91 
93 template <typename... Ts>
94 constexpr inline bool has_common_type_v = detail::has_common_type_impl<void, Ts...>::value;
95 
97 template <typename T>
98 using is_timestamp_t = cuda::std::disjunction<std::is_same<cudf::timestamp_D, T>,
99  std::is_same<cudf::timestamp_s, T>,
100  std::is_same<cudf::timestamp_ms, T>,
101  std::is_same<cudf::timestamp_us, T>,
102  std::is_same<cudf::timestamp_ns, T>>;
103 
105 template <typename T>
106 using is_duration_t = cuda::std::disjunction<std::is_same<cudf::duration_D, T>,
107  std::is_same<cudf::duration_s, T>,
108  std::is_same<cudf::duration_ms, T>,
109  std::is_same<cudf::duration_us, T>,
110  std::is_same<cudf::duration_ns, T>>;
111 
124 template <typename L, typename R>
125 constexpr inline bool is_relationally_comparable()
126 {
127  return detail::is_relationally_comparable_impl<L, R>::value;
128 }
129 
138 
151 template <typename L, typename R>
152 constexpr inline bool is_equality_comparable()
153 {
154  return detail::is_equality_comparable_impl<L, R>::value;
155 }
156 
165 
173 template <typename T>
174 constexpr inline bool is_numeric()
175 {
176  return cuda::std::is_arithmetic<T>();
177 }
178 
191 
203 template <typename T>
204 constexpr inline bool is_index_type()
205 {
206  return std::is_integral_v<T> and not std::is_same_v<T, bool>;
207 }
208 
221 
229 template <typename T>
230 constexpr inline bool is_unsigned()
231 {
232  return std::is_unsigned_v<T>;
233 }
234 
245 
252 template <typename Iterator>
253 constexpr inline bool is_signed_iterator()
254 {
255  return std::is_signed_v<typename std::iterator_traits<Iterator>::value_type>;
256 }
257 
265 template <typename T>
266 constexpr inline bool is_integral()
267 {
268  return cuda::std::is_integral_v<T>;
269 }
270 
281 
289 template <typename T>
290 constexpr inline bool is_integral_not_bool()
291 {
292  return cuda::std::is_integral_v<T> and not std::is_same_v<T, bool>;
293 }
294 
305 
313 template <typename T>
314 constexpr inline bool is_floating_point()
315 {
316  return std::is_floating_point_v<T>;
317 }
318 
329 
337 template <typename T>
338 constexpr inline bool is_byte()
339 {
340  return std::is_same_v<std::remove_cv_t<T>, std::byte>;
341 }
342 
350 template <typename T>
351 constexpr inline bool is_boolean()
352 {
353  return std::is_same_v<T, bool>;
354 }
355 
364 
372 template <typename T>
373 constexpr inline bool is_timestamp()
374 {
376 }
377 
388 
396 template <typename T>
397 constexpr inline bool is_fixed_point()
398 {
399  return std::is_same_v<numeric::decimal32, T> || std::is_same_v<numeric::decimal64, T> ||
400  std::is_same_v<numeric::decimal128, T>;
401 }
402 
411 
419 template <typename T>
420 constexpr inline bool is_duration()
421 {
423 }
424 
435 
443 template <typename T>
444 constexpr inline bool is_chrono()
445 {
446  return is_duration<T>() || is_timestamp<T>();
447 }
448 
459 bool is_chrono(data_type type);
460 
473 template <typename T>
474 constexpr bool is_rep_layout_compatible()
475 {
476  return cudf::is_numeric<T>() or cudf::is_chrono<T>() or cudf::is_boolean<T>() or
477  cudf::is_byte<T>();
478 }
479 
487 template <typename T>
488 constexpr inline bool is_dictionary()
489 {
490  return std::is_same_v<dictionary32, T>;
491 }
492 
501 
511 template <typename T>
512 constexpr inline bool is_fixed_width()
513 {
514  // TODO Add fixed width wrapper types
515  // Is a category fixed width?
516  return cudf::is_numeric<T>() || cudf::is_chrono<T>() || cudf::is_fixed_point<T>();
517 }
518 
529 
530 class string_view;
531 
544 template <typename T>
545 constexpr inline bool is_compound()
546 {
547  return std::is_same_v<T, cudf::string_view> or std::is_same_v<T, cudf::dictionary32> or
548  std::is_same_v<T, cudf::list_view> or std::is_same_v<T, cudf::struct_view>;
549 }
550 
564 
576 template <typename T>
577 constexpr inline bool is_nested()
578 {
579  return std::is_same_v<T, cudf::list_view> || std::is_same_v<T, cudf::struct_view>;
580 }
581 
593 bool is_nested(data_type type);
594 
609 
610 template <typename From, typename To>
611 struct is_convertible : std::is_convertible<From, To> {};
612 
613 // This will ensure that timestamps can be promoted to a higher precision. Presently, they can't
614 // do that due to nvcc/gcc compiler issues
615 template <typename Duration1, typename Duration2>
617  : std::is_convertible<typename cudf::detail::time_point<Duration1>::duration,
618  typename cudf::detail::time_point<Duration2>::duration> {};
619 
622 } // namespace cudf
Indicator for the logical data type of an element in a column.
Definition: types.hpp:241
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.
constexpr bool is_fixed_point()
Indicates whether the type T is a fixed-point type.
Definition: traits.hpp:397
constexpr bool is_integral_not_bool()
Indicates whether the type T is an integral type but not bool type.
Definition: traits.hpp:290
constexpr bool is_compound()
Indicates whether the type T is a compound type.
Definition: traits.hpp:545
decltype(std::declval< L >() > std::declval< R >()) greater_comparable
Checks if two types are comparable using greater operator (i.e. >).
Definition: traits.hpp:58
constexpr bool is_dictionary()
Indicates whether the type T is a dictionary type.
Definition: traits.hpp:488
constexpr bool is_byte()
Indicates whether T is a std::byte type.
Definition: traits.hpp:338
decltype(std::declval< L >()==std::declval< R >()) equality_comparable
Checks if two types are comparable using equality operator (i.e. ==).
Definition: traits.hpp:62
typename detail::has_common_type_impl< void, Ts... >::type has_common_type
Checks if types have a common type.
Definition: traits.hpp:90
constexpr bool is_duration()
Indicates whether the type T is a duration type.
Definition: traits.hpp:420
decltype(std::declval< L >()< std::declval< R >()) less_comparable
Checks if two types are comparable using less operator (i.e. <).
Definition: traits.hpp:54
constexpr bool is_chrono()
Indicates whether the type T is a chrono type.
Definition: traits.hpp:444
constexpr bool is_numeric()
Indicates whether the type T is a numeric type.
Definition: traits.hpp:174
bool is_bit_castable(data_type from, data_type to)
Indicates whether from is bit-castable to to.
constexpr bool is_index_type()
Indicates whether the type T is a index type.
Definition: traits.hpp:204
constexpr bool is_rep_layout_compatible()
Indicates whether T is layout compatible with its "representation" type.
Definition: traits.hpp:474
constexpr bool is_equality_comparable()
Indicates whether objects of types L and R can be compared for equality.
Definition: traits.hpp:152
constexpr bool is_timestamp()
Indicates whether the type T is a timestamp type.
Definition: traits.hpp:373
void void_t
Utility metafunction that maps a sequence of any types to the type void.
Definition: traits.hpp:37
constexpr bool is_signed_iterator()
Indicates whether the Iterator value type is unsigned.
Definition: traits.hpp:253
constexpr bool has_common_type_v
Helper variable template for has_common_type<>::value.
Definition: traits.hpp:94
constexpr bool is_boolean()
Indicates whether T is a Boolean type.
Definition: traits.hpp:351
constexpr bool is_integral()
Indicates whether the type T is an integral type.
Definition: traits.hpp:266
constexpr bool is_fixed_width()
Indicates whether elements of type T are fixed-width.
Definition: traits.hpp:512
constexpr bool is_floating_point()
Indicates whether the type T is a floating point type.
Definition: traits.hpp:314
constexpr bool is_nested()
Indicates whether T is a nested type.
Definition: traits.hpp:577
constexpr bool is_relationally_comparable()
Indicates whether objects of types L and R can be relationally compared.
Definition: traits.hpp:125
constexpr bool is_unsigned()
Indicates whether the type T is a unsigned numeric type.
Definition: traits.hpp:230
cuda::std::disjunction< std::is_same< cudf::timestamp_D, T >, std::is_same< cudf::timestamp_s, T >, std::is_same< cudf::timestamp_ms, T >, std::is_same< cudf::timestamp_us, T >, std::is_same< cudf::timestamp_ns, T > > is_timestamp_t
Checks if a type is a timestamp type.
Definition: traits.hpp:102
cuda::std::disjunction< std::is_same< cudf::duration_D, T >, std::is_same< cudf::duration_s, T >, std::is_same< cudf::duration_ms, T >, std::is_same< cudf::duration_us, T >, std::is_same< cudf::duration_ns, T > > is_duration_t
Checks if a type is a duration type.
Definition: traits.hpp:110
cuDF interfaces
Definition: aggregation.hpp:34
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:38
Type declarations for libcudf.