thrust/iterator/counting_iterator.h

File members: thrust/iterator/counting_iterator.h

/*
 *  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.
 */

/*
 * Copyright David Abrahams 2003.
 *
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying NOTICE file for the complete license)
 *
 * For more information, see http://www.boost.org
 */

#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/iterator/iterator_adaptor.h>
#include <thrust/iterator/iterator_categories.h>
#include <thrust/iterator/iterator_facade.h>

// #include the details first
#include <thrust/iterator/detail/counting_iterator.inl>

THRUST_NAMESPACE_BEGIN

template <typename Incrementable,
          typename System     = use_default,
          typename Traversal  = use_default,
          typename Difference = use_default>
class counting_iterator : public detail::counting_iterator_base<Incrementable, System, Traversal, Difference>::type
{
  using super_t = typename detail::counting_iterator_base<Incrementable, System, Traversal, Difference>::type;

  friend class thrust::iterator_core_access;

public:
  using reference       = typename super_t::reference;
  using difference_type = typename super_t::difference_type;

  _CCCL_HOST_DEVICE counting_iterator()
      : super_t(Incrementable{})
  {}

  template <
    class OtherSystem,
    detail::enable_if_convertible_t<
      typename thrust::iterator_system<counting_iterator<Incrementable, OtherSystem, Traversal, Difference>>::type,
      typename thrust::iterator_system<super_t>::type,
      int> = 0>
  _CCCL_HOST_DEVICE counting_iterator(counting_iterator<Incrementable, OtherSystem, Traversal, Difference> const& rhs)
      : super_t(rhs.base())
  {}

  _CCCL_HOST_DEVICE explicit counting_iterator(Incrementable x)
      : super_t(x)
  {}

private:
  _CCCL_HOST_DEVICE reference dereference() const
  {
    return this->base_reference();
  }

  // note that we implement equal specially for floating point counting_iterator
  template <typename OtherIncrementable, typename OtherSystem, typename OtherTraversal, typename OtherDifference>
  _CCCL_HOST_DEVICE bool
  equal(counting_iterator<OtherIncrementable, OtherSystem, OtherTraversal, OtherDifference> const& y) const
  {
    using e = thrust::detail::counting_iterator_equal<difference_type, Incrementable, OtherIncrementable>;
    return e::equal(this->base(), y.base());
  }

  template <class OtherIncrementable>
  _CCCL_HOST_DEVICE difference_type
  distance_to(counting_iterator<OtherIncrementable, System, Traversal, Difference> const& y) const
  {
    using d = typename thrust::detail::eval_if<
      thrust::detail::is_numeric<Incrementable>::value,
      thrust::detail::identity_<thrust::detail::number_distance<difference_type, Incrementable, OtherIncrementable>>,
      thrust::detail::identity_<
        thrust::detail::iterator_distance<difference_type, Incrementable, OtherIncrementable>>>::type;

    return d::distance(this->base(), y.base());
  }

}; // end counting_iterator

template <typename Incrementable>
inline _CCCL_HOST_DEVICE counting_iterator<Incrementable> make_counting_iterator(Incrementable x)
{
  return counting_iterator<Incrementable>(x);
}

THRUST_NAMESPACE_END