// `type_traits` for NVRTC, which ships no device headers (see README.md).
// The traits a dtype dispatch is written in: `is_same(_v)`, `conditional(_t)`,
// `enable_if(_t)`, `void_t`, `integral_constant` and its relatives.

#ifndef PIE_NVRTC_TYPE_TRAITS_
#define PIE_NVRTC_TYPE_TRAITS_

namespace std {

template <class T, T v>
struct integral_constant {
  static constexpr T value = v;
  using value_type = T;
  using type = integral_constant;
  __host__ __device__ constexpr operator value_type() const noexcept { return value; }
  __host__ __device__ constexpr value_type operator()() const noexcept { return value; }
};

using true_type = integral_constant<bool, true>;
using false_type = integral_constant<bool, false>;

template <class T, class U>
struct is_same : false_type {};
template <class T>
struct is_same<T, T> : true_type {};
template <class T, class U>
inline constexpr bool is_same_v = is_same<T, U>::value;

template <bool B, class T, class F>
struct conditional {
  using type = T;
};
template <class T, class F>
struct conditional<false, T, F> {
  using type = F;
};
template <bool B, class T, class F>
using conditional_t = typename conditional<B, T, F>::type;

template <bool B, class T = void>
struct enable_if {};
template <class T>
struct enable_if<true, T> {
  using type = T;
};
template <bool B, class T = void>
using enable_if_t = typename enable_if<B, T>::type;

template <class T>
struct is_integral : false_type {};
template <> struct is_integral<bool> : true_type {};
template <> struct is_integral<char> : true_type {};
template <> struct is_integral<signed char> : true_type {};
template <> struct is_integral<unsigned char> : true_type {};
template <> struct is_integral<short> : true_type {};
template <> struct is_integral<unsigned short> : true_type {};
template <> struct is_integral<int> : true_type {};
template <> struct is_integral<unsigned int> : true_type {};
template <> struct is_integral<long> : true_type {};
template <> struct is_integral<unsigned long> : true_type {};
template <> struct is_integral<long long> : true_type {};
template <> struct is_integral<unsigned long long> : true_type {};
template <class T>
inline constexpr bool is_integral_v = is_integral<T>::value;

template <class...>
using void_t = void;

template <class T>
struct is_pointer : false_type {};
template <class T>
struct is_pointer<T*> : true_type {};
template <class T>
struct is_pointer<T* const> : true_type {};
template <class T>
struct is_pointer<T* volatile> : true_type {};
template <class T>
struct is_pointer<T* const volatile> : true_type {};
template <class T>
inline constexpr bool is_pointer_v = is_pointer<T>::value;

template <class T>
__host__ __device__ constexpr const T& max(const T& a, const T& b) {
  return a < b ? b : a;
}

template <class T>
__host__ __device__ constexpr const T& min(const T& a, const T& b) {
  return b < a ? b : a;
}

template <class T>
__host__ __device__ T&& declval() noexcept;

}  // namespace std

#endif  // PIE_NVRTC_TYPE_TRAITS_
