/*
 *  Copyright 2008-2013 NVIDIA Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#pragma once

#include <thrust/detail/config.h>

#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
#  pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
#  pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
#  pragma system_header
#endif // no system header

#include <thrust/detail/type_traits.h>
#include <thrust/iterator/detail/iterator_category_to_traversal.h>
#include <thrust/iterator/iterator_categories.h>

#if !_THRUST_HAS_DEVICE_SYSTEM_STD
#  include <type_traits>
#endif

THRUST_NAMESPACE_BEGIN

template <typename Iterator>
struct iterator_value
{
  using type = typename thrust::iterator_traits<Iterator>::value_type;
}; // end iterator_value

template <typename Iterator>
using iterator_value_t = typename iterator_value<Iterator>::type;

template <typename Iterator>
struct iterator_pointer
{
  using type = typename thrust::iterator_traits<Iterator>::pointer;
}; // end iterator_pointer

template <typename Iterator>
using iterator_pointer_t = typename iterator_pointer<Iterator>::type;

template <typename Iterator>
struct iterator_reference
{
  using type = typename iterator_traits<Iterator>::reference;
}; // end iterator_reference

template <typename Iterator>
using iterator_reference_t = typename iterator_reference<Iterator>::type;

template <typename Iterator>
struct iterator_difference
{
  using type = typename thrust::iterator_traits<Iterator>::difference_type;
}; // end iterator_difference

template <typename Iterator>
using iterator_difference_t = typename iterator_difference<Iterator>::type;

namespace detail
{

template <typename Iterator, typename = void>
struct iterator_system_impl
{};

template <typename Iterator>
struct iterator_system_impl<Iterator, _THRUST_STD::void_t<typename iterator_traits<Iterator>::iterator_category>>
    : detail::iterator_category_to_system<typename iterator_traits<Iterator>::iterator_category>
{};

} // namespace detail

template <typename Iterator>
struct iterator_system : detail::iterator_system_impl<Iterator>
{};

// specialize iterator_system for void *, which has no category
template <>
struct iterator_system<void*>
{
  using type = thrust::iterator_system<int*>::type;
}; // end iterator_system<void*>

template <>
struct iterator_system<const void*>
{
  using type = thrust::iterator_system<const int*>::type;
}; // end iterator_system<void*>

template <typename Iterator>
using iterator_system_t = typename iterator_system<Iterator>::type;

template <typename Iterator>
struct iterator_traversal
    : detail::iterator_category_to_traversal<typename thrust::iterator_traits<Iterator>::iterator_category>
{}; // end iterator_traversal

namespace detail
{

template <typename T>
struct is_iterator_traversal : _THRUST_STD::is_convertible<T, incrementable_traversal_tag>
{}; // end is_iterator_traversal

template <typename T>
struct is_iterator_system
    : _THRUST_STD::disjunction<_THRUST_STD::is_convertible<T, any_system_tag>,
                              _THRUST_STD::disjunction<_THRUST_STD::is_convertible<T, host_system_tag>,
                                                      _THRUST_STD::is_convertible<T, device_system_tag>>>
{}; // end is_iterator_system

} // end namespace detail
THRUST_NAMESPACE_END
