thrust/iterator/constant_iterator.h

File members: thrust/iterator/constant_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.
 */

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

THRUST_NAMESPACE_BEGIN

template <typename Value, typename Incrementable = use_default, typename System = use_default>
class constant_iterator : public detail::constant_iterator_base<Value, Incrementable, System>::type
{
  friend class thrust::iterator_core_access;
  using super_t       = typename detail::constant_iterator_base<Value, Incrementable, System>::type;
  using incrementable = typename detail::constant_iterator_base<Value, Incrementable, System>::incrementable;
  using base_iterator = typename detail::constant_iterator_base<Value, Incrementable, System>::base_iterator;

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

  _CCCL_HOST_DEVICE constant_iterator()
      : super_t()
      , m_value()
  {}

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

  _CCCL_HOST_DEVICE constant_iterator(value_type const& v, incrementable const& i = incrementable())
      : super_t(base_iterator(i))
      , m_value(v)
  {}

  template <typename OtherValue, typename OtherIncrementable>
  _CCCL_HOST_DEVICE constant_iterator(OtherValue const& v, OtherIncrementable const& i = incrementable())
      : super_t(base_iterator(i))
      , m_value(v)
  {}

  _CCCL_HOST_DEVICE Value const& value() const
  {
    return m_value;
  }

protected:
  _CCCL_HOST_DEVICE Value const& value_reference() const
  {
    return m_value;
  }

  _CCCL_HOST_DEVICE Value& value_reference()
  {
    return m_value;
  }

private: // Core iterator interface
  _CCCL_HOST_DEVICE reference dereference() const
  {
    return m_value;
  }

private:
  Value m_value;

}; // end constant_iterator

template <typename ValueT, typename IndexT>
inline _CCCL_HOST_DEVICE constant_iterator<ValueT, IndexT> make_constant_iterator(ValueT x, IndexT i = int())
{
  return constant_iterator<ValueT, IndexT>(x, i);
} // end make_constant_iterator()

template <typename V>
inline _CCCL_HOST_DEVICE constant_iterator<V> make_constant_iterator(V x)
{
  return constant_iterator<V>(x, 0);
} // end make_constant_iterator()

THRUST_NAMESPACE_END