traits.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021, 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 
19 #include <cudf/types.hpp>
23 
24 #include <cuda/std/type_traits>
25 
26 namespace cudf {
27 
34 template <typename...>
35 using void_t = void;
36 
48 #define CUDF_ENABLE_IF(...) std::enable_if_t<(__VA_ARGS__)>* = nullptr
49 
50 template <typename L, typename R>
51 using less_comparable = decltype(std::declval<L>() < std::declval<R>());
52 
53 template <typename L, typename R>
54 using greater_comparable = decltype(std::declval<L>() > std::declval<R>());
55 
56 template <typename L, typename R>
57 using equality_comparable = decltype(std::declval<L>() == std::declval<R>());
58 
59 namespace detail {
60 template <typename L, typename R, typename = void>
61 struct is_relationally_comparable_impl : std::false_type {
62 };
63 
64 template <typename L, typename R>
66  R,
67  void_t<less_comparable<L, R>, greater_comparable<L, R>>>
68  : std::true_type {
69 };
70 
71 template <typename L, typename R, typename = void>
72 struct is_equality_comparable_impl : std::false_type {
73 };
74 
75 template <typename L, typename R>
76 struct is_equality_comparable_impl<L, R, void_t<equality_comparable<L, R>>> : std::true_type {
77 };
78 
79 // has common type
80 template <typename AlwaysVoid, typename... Ts>
81 struct has_common_type_impl : std::false_type {
82 };
83 
84 template <typename... Ts>
85 struct has_common_type_impl<void_t<std::common_type_t<Ts...>>, Ts...> : std::true_type {
86 };
87 } // namespace detail
88 
89 template <typename... Ts>
90 using has_common_type = typename detail::has_common_type_impl<void, Ts...>::type;
91 
92 template <typename... Ts>
93 constexpr inline bool has_common_type_v = detail::has_common_type_impl<void, Ts...>::value;
94 
95 template <typename T>
96 using is_timestamp_t = cuda::std::disjunction<std::is_same<cudf::timestamp_D, T>,
97  std::is_same<cudf::timestamp_s, T>,
98  std::is_same<cudf::timestamp_ms, T>,
99  std::is_same<cudf::timestamp_us, T>,
100  std::is_same<cudf::timestamp_ns, T>>;
101 
102 template <typename T>
103 using is_duration_t = cuda::std::disjunction<std::is_same<cudf::duration_D, T>,
104  std::is_same<cudf::duration_s, T>,
105  std::is_same<cudf::duration_ms, T>,
106  std::is_same<cudf::duration_us, T>,
107  std::is_same<cudf::duration_ns, T>>;
108 
121 template <typename L, typename R>
122 constexpr inline bool is_relationally_comparable()
123 {
125 }
126 
139 template <typename L, typename R>
140 constexpr inline bool is_equality_comparable()
141 {
143 }
144 
145 namespace detail {
150  template <typename T>
151  bool operator()() const
152  {
153  return cudf::is_equality_comparable<T, T>();
154  }
155 };
156 } // namespace detail
157 
166 {
168 }
169 
177 template <typename T>
178 constexpr inline bool is_numeric()
179 {
180  return std::is_integral<T>::value or std::is_floating_point<T>::value;
181 }
182 
184  template <typename T>
185  constexpr bool operator()()
186  {
187  return is_numeric<T>();
188  }
189 };
190 
202 constexpr inline bool is_numeric(data_type type)
203 {
204  return cudf::type_dispatcher(type, is_numeric_impl{});
205 }
206 
218 template <typename T>
219 constexpr inline bool is_index_type()
220 {
221  return std::is_integral<T>::value and not std::is_same_v<T, bool>;
222 }
223 
225  template <typename T>
226  constexpr bool operator()()
227  {
228  return is_index_type<T>();
229  }
230 };
231 
243 constexpr inline bool is_index_type(data_type type)
244 {
246 }
247 
255 template <typename T>
256 constexpr inline bool is_unsigned()
257 {
258  return std::is_unsigned<T>::value;
259 }
260 
262  template <typename T>
263  constexpr bool operator()()
264  {
265  return is_unsigned<T>();
266  }
267 };
277 constexpr inline bool is_unsigned(data_type type)
278 {
279  return cudf::type_dispatcher(type, is_unsigned_impl{});
280 }
281 
288 template <typename Iterator>
289 constexpr inline bool is_signed_iterator()
290 {
291  return std::is_signed<typename std::iterator_traits<Iterator>::value_type>::value;
292 }
293 
301 template <typename T>
302 constexpr inline bool is_floating_point()
303 {
304  return std::is_floating_point<T>::value;
305 }
306 
308  template <typename T>
309  constexpr bool operator()()
310  {
311  return is_floating_point<T>();
312  }
313 };
314 
324 constexpr inline bool is_floating_point(data_type type)
325 {
327 }
328 
336 template <typename T>
337 constexpr inline bool is_boolean()
338 {
339  return std::is_same_v<T, bool>;
340 }
341 
343  template <typename T>
344  constexpr bool operator()()
345  {
346  return is_boolean<T>();
347  }
348 };
349 
357 constexpr inline bool is_boolean(data_type type)
358 {
359  return cudf::type_dispatcher(type, is_boolean_impl{});
360 }
361 
369 template <typename T>
370 constexpr inline bool is_timestamp()
371 {
372  return is_timestamp_t<T>::value;
373 }
374 
376  template <typename T>
377  constexpr bool operator()()
378  {
379  return is_timestamp<T>();
380  }
381 };
382 
392 constexpr inline bool is_timestamp(data_type type)
393 {
395 }
396 
404 template <typename T>
405 constexpr inline bool is_fixed_point()
406 {
407  return std::is_same_v<numeric::decimal32, T> || std::is_same_v<numeric::decimal64, T>;
408 }
409 
411  template <typename T>
412  constexpr bool operator()()
413  {
414  return is_fixed_point<T>();
415  }
416 };
417 
425 constexpr inline bool is_fixed_point(data_type type)
426 {
428 }
429 
437 template <typename T>
438 constexpr inline bool is_duration()
439 {
440  return is_duration_t<T>::value;
441 }
442 
444  template <typename T>
445  constexpr bool operator()()
446  {
447  return is_duration<T>();
448  }
449 };
450 
460 constexpr inline bool is_duration(data_type type)
461 {
462  return cudf::type_dispatcher(type, is_duration_impl{});
463 }
464 
472 template <typename T>
473 constexpr inline bool is_chrono()
474 {
475  return is_duration<T>() || is_timestamp<T>();
476 }
477 
479  template <typename T>
480  constexpr bool operator()()
481  {
482  return is_chrono<T>();
483  }
484 };
485 
496 constexpr inline bool is_chrono(data_type type)
497 {
498  return cudf::type_dispatcher(type, is_chrono_impl{});
499 }
500 
512 template <typename T>
513 constexpr bool is_rep_layout_compatible()
514 {
515  return cudf::is_numeric<T>() or cudf::is_chrono<T>() or cudf::is_boolean<T>();
516 }
517 
525 template <typename T>
526 constexpr inline bool is_dictionary()
527 {
528  return std::is_same_v<dictionary32, T>;
529 }
530 
532  template <typename T>
533  constexpr bool operator()()
534  {
535  return is_dictionary<T>();
536  }
537 };
538 
546 constexpr inline bool is_dictionary(data_type type)
547 {
549 }
559 template <typename T>
560 constexpr inline bool is_fixed_width()
561 {
562  // TODO Add fixed width wrapper types
563  // Is a category fixed width?
564  return cudf::is_numeric<T>() || cudf::is_chrono<T>() || cudf::is_fixed_point<T>();
565 }
566 
568  template <typename T>
569  constexpr bool operator()()
570  {
571  return is_fixed_width<T>();
572  }
573 };
574 
584 constexpr inline bool is_fixed_width(data_type type)
585 {
587 }
588 
589 class string_view;
590 
603 template <typename T>
604 constexpr inline bool is_compound()
605 {
606  return std::is_same_v<T, cudf::string_view> or std::is_same_v<T, cudf::dictionary32> or
607  std::is_same_v<T, cudf::list_view> or std::is_same_v<T, cudf::struct_view>;
608 }
609 
611  template <typename T>
612  constexpr bool operator()()
613  {
614  return is_compound<T>();
615  }
616 };
617 
630 constexpr inline bool is_compound(data_type type)
631 {
632  return cudf::type_dispatcher(type, is_compound_impl{});
633 }
634 
646 template <typename T>
647 constexpr inline bool is_nested()
648 {
649  return std::is_same_v<T, cudf::list_view> || std::is_same_v<T, cudf::struct_view>;
650 }
651 
653  template <typename T>
654  constexpr bool operator()()
655  {
656  return is_nested<T>();
657  }
658 };
659 
671 constexpr inline bool is_nested(data_type type)
672 {
673  return cudf::type_dispatcher(type, is_nested_impl{});
674 }
675 
676 template <typename FromType>
678  template <typename ToType, typename std::enable_if_t<is_compound<ToType>()>* = nullptr>
679  constexpr bool operator()()
680  {
681  return false;
682  }
683 
684  template <typename ToType, typename std::enable_if_t<not is_compound<ToType>()>* = nullptr>
685  constexpr bool operator()()
686  {
687  if (not cuda::std::is_trivially_copyable_v<FromType> ||
688  not cuda::std::is_trivially_copyable_v<ToType>) {
689  return false;
690  }
691  constexpr auto from_size = sizeof(cudf::device_storage_type_t<FromType>);
692  constexpr auto to_size = sizeof(cudf::device_storage_type_t<ToType>);
693  return from_size == to_size;
694  }
695 };
696 
698  template <typename FromType, typename std::enable_if_t<is_compound<FromType>()>* = nullptr>
699  constexpr bool operator()(data_type)
700  {
701  return false;
702  }
703 
704  template <typename FromType, typename std::enable_if_t<not is_compound<FromType>()>* = nullptr>
705  constexpr bool operator()(data_type to)
706  {
708  }
709 };
710 
724 constexpr bool is_bit_castable(data_type from, data_type to)
725 {
726  return type_dispatcher(from, is_bit_castable_from_impl{}, to);
727 }
728 
729 template <typename From, typename To>
730 struct is_convertible : std::is_convertible<From, To> {
731 };
732 
733 // This will ensure that timestamps can be promoted to a higher precision. Presently, they can't
734 // do that due to nvcc/gcc compiler issues
735 template <typename Duration1, typename Duration2>
736 struct is_convertible<cudf::detail::timestamp<Duration1>, cudf::detail::timestamp<Duration2>>
737  : std::is_convertible<typename cudf::detail::time_point<Duration1>::duration,
738  typename cudf::detail::time_point<Duration2>::duration> {
739 };
740 
743 } // namespace cudf
cudf::type_dispatcher
constexpr decltype(auto) CUDA_HOST_DEVICE_CALLABLE type_dispatcher(cudf::data_type dtype, Functor f, Ts &&... args)
Invokes an operator() template with the type instantiation based on the specified cudf::data_type's i...
Definition: type_dispatcher.hpp:414
cudf::is_unsigned_impl
Definition: traits.hpp:261
cudf::is_bit_castable_to_impl
Definition: traits.hpp:677
durations.hpp
Concrete type definitions for int32_t and int64_t durations in varying resolutions.
cudf::detail::is_equality_comparable_impl
Definition: traits.hpp:72
cudf::is_index_type_impl
Definition: traits.hpp:224
cudf::is_bit_castable
constexpr bool is_bit_castable(data_type from, data_type to)
Indicates whether from is bit-castable to to.
Definition: traits.hpp:724
cudf::is_signed_iterator
constexpr bool is_signed_iterator()
Indicates whether the Iterator value type is unsigned.
Definition: traits.hpp:289
types.hpp
Type declarations for libcudf.
cudf::is_compound
constexpr bool is_compound()
Indicates whether the type T is a compound type.
Definition: traits.hpp:604
cudf::is_boolean
constexpr bool is_boolean()
Indicates whether T is a Boolean type.
Definition: traits.hpp:337
cudf::is_rep_layout_compatible
constexpr bool is_rep_layout_compatible()
Indicates whether T is layout compatible with its "representation" type.
Definition: traits.hpp:513
cudf::is_nested_impl
Definition: traits.hpp:652
cudf::is_numeric
constexpr bool is_numeric()
Indicates whether the type T is a numeric type.
Definition: traits.hpp:178
cudf::detail::has_common_type_impl
Definition: traits.hpp:81
cudf::is_numeric_impl
Definition: traits.hpp:183
cudf::is_boolean_impl
Definition: traits.hpp:342
cudf::is_fixed_width_impl
Definition: traits.hpp:567
cudf::is_chrono_impl
Definition: traits.hpp:478
cudf::is_timestamp_impl
Definition: traits.hpp:375
cudf::is_unsigned
constexpr bool is_unsigned()
Indicates whether the type T is a unsigned numeric type.
Definition: traits.hpp:256
cudf::is_floating_point
constexpr bool is_floating_point()
Indicates whether the type T is a floating point type.
Definition: traits.hpp:302
cudf::detail::unary_equality_comparable_functor
Helper functor to check if a specified type T supports equality comparisons.
Definition: traits.hpp:149
cudf::is_dictionary
constexpr bool is_dictionary()
Indicates whether the type T is a dictionary type.
Definition: traits.hpp:526
cudf::is_floating_point_impl
Definition: traits.hpp:307
cudf::data_type
Indicator for the logical data type of an element in a column.
Definition: types.hpp:242
cudf::is_index_type
constexpr bool is_index_type()
Indicates whether the type T is a index type.
Definition: traits.hpp:219
cudf
cuDF interfaces
Definition: aggregation.hpp:34
cudf::is_chrono
constexpr bool is_chrono()
Indicates whether the type T is a chrono type.
Definition: traits.hpp:473
cudf::is_fixed_width
constexpr bool is_fixed_width()
Indicates whether elements of type T are fixed-width.
Definition: traits.hpp:560
cudf::is_nested
constexpr bool is_nested()
Indicates whether T is a nested type.
Definition: traits.hpp:647
cudf::is_compound_impl
Definition: traits.hpp:610
cudf::is_dictionary_impl
Definition: traits.hpp:531
cudf::is_convertible
Definition: traits.hpp:730
cudf::is_duration_impl
Definition: traits.hpp:443
cudf::is_fixed_point
constexpr bool is_fixed_point()
Indicates whether the type T is a fixed-point type.
Definition: traits.hpp:405
cudf::is_duration
constexpr bool is_duration()
Indicates whether the type T is a duration type.
Definition: traits.hpp:438
timestamps.hpp
Concrete type definitions for int32_t and int64_t timestamps in varying resolutions as durations sinc...
cudf::is_equality_comparable
constexpr bool is_equality_comparable()
Indicates whether objects of types L and R can be compared for equality.
Definition: traits.hpp:140
cudf::is_timestamp
constexpr bool is_timestamp()
Indicates whether the type T is a timestamp type.
Definition: traits.hpp:370
cudf::is_bit_castable_from_impl
Definition: traits.hpp:697
cudf::device_storage_type_t
std::conditional_t< std::is_same_v< numeric::decimal32, T >, int32_t, std::conditional_t< std::is_same_v< numeric::decimal64, T >, int64_t, T > > device_storage_type_t
"Returns" the corresponding type that is stored on the device when using cudf::column
Definition: type_dispatcher.hpp:102
type_dispatcher.hpp
Defines the mapping between cudf::type_id runtime type information and concrete C++ types.
cudf::detail::is_relationally_comparable_impl
Definition: traits.hpp:61
cudf::is_fixed_point_impl
Definition: traits.hpp:410
cudf::is_relationally_comparable
constexpr bool is_relationally_comparable()
Indicates whether objects of types L and R can be relationally compared.
Definition: traits.hpp:122