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/lists/list_view.cuh>
21 #include <cudf/types.hpp>
25 
26 #include <cuda/std/type_traits>
27 
28 namespace cudf {
29 
36 template <typename...>
37 using void_t = void;
38 
39 template <typename L, typename R, typename = void>
40 struct is_relationally_comparable_impl : std::false_type {
41 };
42 
43 template <typename L, typename R>
44 using less_comparable = decltype(std::declval<L>() < std::declval<R>());
45 
46 template <typename L, typename R>
47 using greater_comparable = decltype(std::declval<L>() > std::declval<R>());
48 
49 template <typename L, typename R>
51  R,
52  void_t<less_comparable<L, R>, greater_comparable<L, R>>>
53  : std::true_type {
54 };
55 
56 template <typename L, typename R, typename = void>
57 struct is_equality_comparable_impl : std::false_type {
58 };
59 
60 template <typename L, typename R>
61 using equality_comparable = decltype(std::declval<L>() == std::declval<R>());
62 
63 template <typename L, typename R>
64 struct is_equality_comparable_impl<L, R, void_t<equality_comparable<L, R>>> : std::true_type {
65 };
66 
67 template <typename T>
68 using is_timestamp_t = cuda::std::disjunction<std::is_same<cudf::timestamp_D, T>,
69  std::is_same<cudf::timestamp_s, T>,
70  std::is_same<cudf::timestamp_ms, T>,
71  std::is_same<cudf::timestamp_us, T>,
72  std::is_same<cudf::timestamp_ns, T>>;
73 
74 template <typename T>
75 using is_duration_t = cuda::std::disjunction<std::is_same<cudf::duration_D, T>,
76  std::is_same<cudf::duration_s, T>,
77  std::is_same<cudf::duration_ms, T>,
78  std::is_same<cudf::duration_us, T>,
79  std::is_same<cudf::duration_ns, T>>;
80 
93 template <typename L, typename R>
94 constexpr inline bool is_relationally_comparable()
95 {
97 }
98 
111 template <typename L, typename R>
112 constexpr inline bool is_equality_comparable()
113 {
115 }
116 
124 template <typename T>
125 constexpr inline bool is_numeric()
126 {
127  return std::is_integral<T>::value or std::is_floating_point<T>::value;
128 }
129 
131  template <typename T>
132  bool operator()()
133  {
134  return is_numeric<T>();
135  }
136 };
137 
149 constexpr inline bool is_numeric(data_type type)
150 {
151  return cudf::type_dispatcher(type, is_numeric_impl{});
152 }
153 
165 template <typename T>
166 constexpr inline bool is_index_type()
167 {
168  return std::is_integral<T>::value and not std::is_same<T, bool>::value;
169 }
170 
172  template <typename T>
173  bool operator()()
174  {
175  return is_index_type<T>();
176  }
177 };
178 
190 constexpr inline bool is_index_type(data_type type)
191 {
193 }
194 
202 template <typename T>
203 constexpr inline bool is_unsigned()
204 {
205  return std::is_unsigned<T>::value;
206 }
207 
209  template <typename T>
210  bool operator()()
211  {
212  return is_unsigned<T>();
213  }
214 };
224 constexpr inline bool is_unsigned(data_type type)
225 {
226  return cudf::type_dispatcher(type, is_unsigned_impl{});
227 }
228 
236 template <typename T>
237 constexpr inline bool is_floating_point()
238 {
239  return std::is_floating_point<T>::value;
240 }
241 
243  template <typename T>
244  bool operator()()
245  {
246  return is_floating_point<T>();
247  }
248 };
249 
259 constexpr inline bool is_floating_point(data_type type)
260 {
262 }
263 
271 template <typename T>
272 constexpr inline bool is_boolean()
273 {
274  return std::is_same<T, bool>::value;
275 }
276 
278  template <typename T>
279  constexpr bool operator()()
280  {
281  return is_boolean<T>();
282  }
283 };
284 
292 constexpr inline bool is_boolean(data_type type)
293 {
294  return cudf::type_dispatcher(type, is_boolean_impl{});
295 }
296 
304 template <typename T>
305 constexpr inline bool is_timestamp()
306 {
307  return is_timestamp_t<T>::value;
308 }
309 
311  template <typename T>
312  bool operator()()
313  {
314  return is_timestamp<T>();
315  }
316 };
317 
327 constexpr inline bool is_timestamp(data_type type)
328 {
330 }
331 
339 template <typename T>
340 constexpr inline bool is_fixed_point()
341 {
342  return std::is_same<numeric::decimal32, T>::value || std::is_same<numeric::decimal64, T>::value;
343 }
344 
346  template <typename T>
347  bool operator()()
348  {
349  return is_fixed_point<T>();
350  }
351 };
352 
360 constexpr inline bool is_fixed_point(data_type type)
361 {
363 }
364 
372 template <typename T>
373 constexpr inline bool is_duration()
374 {
375  return is_duration_t<T>::value;
376 }
377 
379  template <typename T>
380  bool operator()()
381  {
382  return is_duration<T>();
383  }
384 };
385 
395 constexpr inline bool is_duration(data_type type)
396 {
397  return cudf::type_dispatcher(type, is_duration_impl{});
398 }
399 
407 template <typename T>
408 constexpr inline bool is_chrono()
409 {
410  return is_duration<T>() || is_timestamp<T>();
411 }
412 
414  template <typename T>
415  bool operator()()
416  {
417  return is_chrono<T>();
418  }
419 };
420 
431 constexpr inline bool is_chrono(data_type type)
432 {
433  return cudf::type_dispatcher(type, is_chrono_impl{});
434 }
435 
443 template <typename T>
444 constexpr inline bool is_dictionary()
445 {
446  return std::is_same<dictionary32, T>::value;
447 }
448 
450  template <typename T>
451  bool operator()()
452  {
453  return is_dictionary<T>();
454  }
455 };
456 
464 constexpr inline bool is_dictionary(data_type type)
465 {
467 }
477 template <typename T>
478 constexpr inline bool is_fixed_width()
479 {
480  // TODO Add fixed width wrapper types
481  // Is a category fixed width?
482  return cudf::is_numeric<T>() || cudf::is_chrono<T>() || cudf::is_fixed_point<T>();
483 }
484 
486  template <typename T>
487  bool operator()()
488  {
489  return is_fixed_width<T>();
490  }
491 };
492 
502 constexpr inline bool is_fixed_width(data_type type)
503 {
505 }
506 
507 class string_view;
508 
521 template <typename T>
522 constexpr inline bool is_compound()
523 {
524  return std::is_same<T, cudf::string_view>::value or std::is_same<T, cudf::dictionary32>::value or
525  std::is_same<T, cudf::list_view>::value or std::is_same<T, cudf::struct_view>::value;
526 }
527 
529  template <typename T>
530  bool operator()()
531  {
532  return is_compound<T>();
533  }
534 };
535 
548 constexpr inline bool is_compound(data_type type)
549 {
550  return cudf::type_dispatcher(type, is_compound_impl{});
551 }
552 
564 template <typename T>
565 constexpr inline bool is_nested()
566 {
567  return std::is_same<T, cudf::list_view>::value || std::is_same<T, cudf::struct_view>::value;
568 }
569 
571  template <typename T>
572  bool operator()()
573  {
574  return is_nested<T>();
575  }
576 };
577 
589 constexpr inline bool is_nested(data_type type)
590 {
591  return cudf::type_dispatcher(type, is_nested_impl{});
592 }
593 
594 template <typename FromType, typename ToType>
595 struct is_logically_castable_impl : std::false_type {
596 };
597 
598 // Allow cast to same type
599 template <typename Type>
600 struct is_logically_castable_impl<Type, Type> : std::true_type {
601 };
602 
603 #ifndef MAP_CASTABLE_TYPES
604 #define MAP_CASTABLE_TYPES(Type1, Type2) \
605  template <> \
606  struct is_logically_castable_impl<Type1, Type2> : std::true_type { \
607  }; \
608  template <> \
609  struct is_logically_castable_impl<Type2, Type1> : std::true_type { \
610  };
611 #endif
612 
613 // Allow cast between timestamp and integer representation
614 MAP_CASTABLE_TYPES(cudf::timestamp_D, cudf::timestamp_D::duration::rep);
615 MAP_CASTABLE_TYPES(cudf::timestamp_s, cudf::timestamp_s::duration::rep);
616 MAP_CASTABLE_TYPES(cudf::timestamp_ms, cudf::timestamp_ms::duration::rep);
617 MAP_CASTABLE_TYPES(cudf::timestamp_us, cudf::timestamp_us::duration::rep);
618 MAP_CASTABLE_TYPES(cudf::timestamp_ns, cudf::timestamp_ns::duration::rep);
619 // Allow cast between durations and integer representation
620 MAP_CASTABLE_TYPES(cudf::duration_D, cudf::duration_D::rep);
621 MAP_CASTABLE_TYPES(cudf::duration_s, cudf::duration_s::rep);
622 MAP_CASTABLE_TYPES(cudf::duration_ms, cudf::duration_ms::rep);
623 MAP_CASTABLE_TYPES(cudf::duration_us, cudf::duration_us::rep);
624 MAP_CASTABLE_TYPES(cudf::duration_ns, cudf::duration_ns::rep);
625 // Allow cast between decimals and integer representation
626 MAP_CASTABLE_TYPES(numeric::decimal32, numeric::decimal32::rep);
627 MAP_CASTABLE_TYPES(numeric::decimal64, numeric::decimal64::rep);
628 
629 template <typename FromType>
631  template <typename ToType>
632  constexpr bool operator()()
633  {
635  }
636 };
637 
639  template <typename FromType>
640  constexpr bool operator()(data_type to)
641  {
643  }
644 };
645 
659 constexpr bool is_logically_castable(data_type from, data_type to)
660 {
662 }
663 
664 template <typename From, typename To>
665 struct is_convertible : std::is_convertible<From, To> {
666 };
667 
668 // This will ensure that timestamps can be promoted to a higher precision. Presently, they can't
669 // do that due to nvcc/gcc compiler issues
670 template <typename Duration1, typename Duration2>
672  : std::is_convertible<typename cudf::detail::time_point<Duration1>::duration,
673  typename cudf::detail::time_point<Duration2>::duration> {
674 };
675 
678 } // 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:401
cudf::is_unsigned_impl
Definition: traits.hpp:208
durations.hpp
Concrete type definitions for int32_t and int64_t durations in varying resolutions.
cudf::is_logically_castable_from_impl
Definition: traits.hpp:638
numeric::fixed_point
A type for representing a number with a fixed amount of precision.
Definition: fixed_point.hpp:212
cudf::is_index_type_impl
Definition: traits.hpp:171
cudf::is_logically_castable_impl
Definition: traits.hpp:595
cudf::duration_s
cuda::std::chrono::duration< int64_t, cuda::std::chrono::seconds::period > duration_s
Type alias representing an int64_t duration of seconds.
Definition: durations.hpp:39
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:522
cudf::is_boolean
constexpr bool is_boolean()
Indicates whether T is a Boolean type.
Definition: traits.hpp:272
cudf::is_nested_impl
Definition: traits.hpp:570
cudf::duration_D
cuda::std::chrono::duration< int32_t, cuda::std::chrono::days::period > duration_D
Type alias representing an int32_t duration of days.
Definition: durations.hpp:35
cudf::is_numeric
constexpr bool is_numeric()
Indicates whether the type T is a numeric type.
Definition: traits.hpp:125
cudf::detail::timestamp
Definition: timestamps.hpp:35
cudf::is_numeric_impl
Definition: traits.hpp:130
cudf::is_boolean_impl
Definition: traits.hpp:277
cudf::duration_ns
cuda::std::chrono::duration< int64_t, cuda::std::chrono::nanoseconds::period > duration_ns
Type alias representing an int64_t duration of nanoseconds.
Definition: durations.hpp:51
cudf::is_relationally_comparable_impl
Definition: traits.hpp:40
cudf::is_fixed_width_impl
Definition: traits.hpp:485
cudf::is_chrono_impl
Definition: traits.hpp:413
cudf::is_timestamp_impl
Definition: traits.hpp:310
cudf::is_unsigned
constexpr bool is_unsigned()
Indicates whether the type T is a unsigned numeric type.
Definition: traits.hpp:203
cudf::is_floating_point
constexpr bool is_floating_point()
Indicates whether the type T is a floating point type.
Definition: traits.hpp:237
cudf::is_logically_castable_to_impl
Definition: traits.hpp:630
cudf::is_dictionary
constexpr bool is_dictionary()
Indicates whether the type T is a dictionary type.
Definition: traits.hpp:444
cudf::is_floating_point_impl
Definition: traits.hpp:242
cudf::data_type
Indicator for the logical data type of an element in a column.
Definition: types.hpp:235
cudf::is_index_type
constexpr bool is_index_type()
Indicates whether the type T is a index type.
Definition: traits.hpp:166
cudf
cuDF interfaces
Definition: aggregation.hpp:34
cudf::is_logically_castable
constexpr bool is_logically_castable(data_type from, data_type to)
Indicates whether from is logically castable to to.
Definition: traits.hpp:659
cudf::is_chrono
constexpr bool is_chrono()
Indicates whether the type T is a chrono type.
Definition: traits.hpp:408
list_view.cuh
Class definition for cudf::list_view.
cudf::is_fixed_width
constexpr bool is_fixed_width()
Indicates whether elements of type T are fixed-width.
Definition: traits.hpp:478
cudf::is_nested
constexpr bool is_nested()
Indicates whether T is a nested type.
Definition: traits.hpp:565
cudf::is_equality_comparable_impl
Definition: traits.hpp:57
cudf::is_compound_impl
Definition: traits.hpp:528
cudf::is_dictionary_impl
Definition: traits.hpp:449
cudf::is_convertible
Definition: traits.hpp:665
cudf::is_duration_impl
Definition: traits.hpp:378
cudf::is_fixed_point
constexpr bool is_fixed_point()
Indicates whether the type T is a fixed-point type.
Definition: traits.hpp:340
cudf::is_duration
constexpr bool is_duration()
Indicates whether the type T is a duration type.
Definition: traits.hpp:373
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:112
cudf::is_timestamp
constexpr bool is_timestamp()
Indicates whether the type T is a timestamp type.
Definition: traits.hpp:305
type_dispatcher.hpp
Defines the mapping between cudf::type_id runtime type information and concrete C++ types.
cudf::is_fixed_point_impl
Definition: traits.hpp:345
cudf::is_relationally_comparable
constexpr bool is_relationally_comparable()
Indicates whether objects of types L and R can be relationally compared.
Definition: traits.hpp:94
struct_view.hpp
Class definition for cudf::struct_view.
cudf::duration_us
cuda::std::chrono::duration< int64_t, cuda::std::chrono::microseconds::period > duration_us
Type alias representing an int64_t duration of microseconds.
Definition: durations.hpp:47
cudf::duration_ms
cuda::std::chrono::duration< int64_t, cuda::std::chrono::milliseconds::period > duration_ms
Type alias representing an int64_t duration of milliseconds.
Definition: durations.hpp:43