⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 index_iterator.hpp

📁 新版本TR1的stl
💻 HPP
📖 第 1 页 / 共 2 页
字号:
        m_index +=  n;

        return *this;
    }

    /// Moves the iterator backward
    ///
    /// \param n The amount by which to decrement the iterator's current position
    class_type& operator -=(difference_type n)
    {
        m_it    -=  n;
        m_index -=  n;

        return *this;
    }

    /// Access the element at the given index
    ///
    /// \param index The required offset from the iterator's position

#ifdef STLSOFT_INDEX_ITERATOR_MUTABLE_OP_SUPPORT
    effective_reference         operator [](difference_type index)
    {
        return m_it[index];
    }
#endif /* STLSOFT_INDEX_ITERATOR_MUTABLE_OP_SUPPORT */

    /// Access the element at the given index
    ///
    /// \param index The required offset from the iterator's position
    effective_const_reference   operator [](difference_type index) const
    {
        return m_it[index];
    }

    /// Calculate the distance between \c this and \c rhs
    difference_type distance(class_type const& rhs) const
    {
        return m_it - rhs.m_it;
    }
/// @}

/// \name Index Iterator methods
/// @{
public:
    index_type index() const
    {
        return m_index;
    }
/// @}

/// \name Comparison
/// @{
public:
    ss_bool_t equal(class_type const& rhs) const
    {
        return m_it == rhs.m_it;
    }

    int compare(class_type const& rhs) const
    {
        return (m_it < rhs.m_it) ? -1 : (rhs.m_it < m_it) ? +1 : 0;
    }
/// @}

/// \name Implementation
/// @{
private:
#ifdef STLSOFT_INDEX_ITERATOR_MEM_SEL_OP_SUPPORT
    effective_pointer invoke_member_selection_operator_(yes_type)
    {
        return m_it;
    }
    effective_pointer invoke_member_selection_operator_(no_type)
    {
        return m_it.operator ->();
    }

    effective_const_pointer invoke_member_selection_operator_(yes_type) const
    {
        return m_it;
    }
    effective_const_pointer invoke_member_selection_operator_(no_type) const
    {
        return m_it.operator ->();
    }
#endif /* STLSOFT_INDEX_ITERATOR_MEM_SEL_OP_SUPPORT */
/// @}

/// \name Members
/// @{
private:
    base_iterator_type  m_it;
    index_type          m_index;
/// @}
};

/* /////////////////////////////////////////////////////////////////////////
 * Creator functions
 */

/** \brief Creator function for index_iterator
 *
 * \ingroup group__library__iterators
 *
 * \param it The iterator to index
 * \param index The initial index of the iterator. Defaults to 0
 *
 * \return An instance of the specialisation index_iterator&lt;I&gt;
 */
template<   ss_typename_param_k I
        >
inline index_iterator<I> make_index_iterator(I it, ss_ptrdiff_t index = 0)
{
    return index_iterator<I>(it, index);
}

/** \brief Creator function for index_iterator
 *
 * \ingroup group__library__iterators
 *
 * \param it The iterator to index
 * \param index The initial index of the iterator. Defaults to 0
 *
 * \return An instance of the specialisation index_iterator&lt;T&gt;
 *
 * \note Short-hand for make_index_iterator()
 */
template<   ss_typename_param_k I
        >
inline index_iterator<I> indexer(I it, ss_ptrdiff_t index = 0)
{
    return make_index_iterator(it, index);
}

/* /////////////////////////////////////////////////////////////////////////
 * Operators
 */

// operator ==

template<   ss_typename_param_k I
        ,   ss_typename_param_k T
        >
inline ss_bool_t operator ==(index_iterator<I, T> const& lhs, index_iterator<I, T> const& rhs)
{
    return lhs.equal(rhs);
}

// operator !=

template<   ss_typename_param_k I
        ,   ss_typename_param_k T
        >
inline ss_bool_t operator !=(index_iterator<I, T> const& lhs, index_iterator<I, T> const& rhs)
{
    return !lhs.equal(rhs);
}

// operator +

template<   ss_typename_param_k I
        ,   ss_typename_param_k T
        >
inline index_iterator<I, T> operator +(index_iterator<I, T> const& lhs, ss_typename_type_k index_iterator<I, T>::difference_type rhs)
{
    return index_iterator<I, T>(lhs) += rhs;
}

// operator -

template<   ss_typename_param_k I
        ,   ss_typename_param_k T
        >
#if 0
inline index_iterator<I, T> operator -(index_iterator<I, T> const& lhs, ss_typename_type_k index_iterator<I, T>::difference_type rhs)
#else /* ? 0 */
inline index_iterator<I, T> operator -(index_iterator<I, T> const& lhs, ss_ptrdiff_t rhs)
#endif /* 0 */
{
    return index_iterator<I, T>(lhs) -= rhs;
}

template<   ss_typename_param_k I
        ,   ss_typename_param_k T
        >
inline ss_typename_type_k index_iterator<I, T>::difference_type operator -(index_iterator<I, T> const& lhs, index_iterator<I, T> const& rhs)
{
    return lhs.distance(rhs);
}

// operator <

template<   ss_typename_param_k I
        ,   ss_typename_param_k T
        >
inline ss_bool_t operator <(index_iterator<I, T> const& lhs, index_iterator<I, T> const& rhs)
{
    return lhs.compare(rhs) < 0;
}

// operator <=

template<   ss_typename_param_k I
        ,   ss_typename_param_k T
        >
inline ss_bool_t operator <=(index_iterator<I, T> const& lhs, index_iterator<I, T> const& rhs)
{
    return lhs.compare(rhs) <= 0;
}

// operator >

template<   ss_typename_param_k I
        ,   ss_typename_param_k T
        >
inline ss_bool_t operator >(index_iterator<I, T> const& lhs, index_iterator<I, T> const& rhs)
{
    return lhs.compare(rhs) > 0;
}

// operator >=

template<   ss_typename_param_k I
        ,   ss_typename_param_k T
        >
inline ss_bool_t operator >=(index_iterator<I, T> const& lhs, index_iterator<I, T> const& rhs)
{
    return lhs.compare(rhs) >= 0;
}

////////////////////////////////////////////////////////////////////////////
// Unit-testing

#ifdef STLSOFT_UNITTEST
# include "./unittest/index_iterator_unittest_.h"
#endif /* STLSOFT_UNITTEST */

/* ////////////////////////////////////////////////////////////////////// */

#ifndef _STLSOFT_NO_NAMESPACE
} // namespace stlsoft
#endif /* _STLSOFT_NO_NAMESPACE */

#if defined(STLSOFT_CF_STD_LIBRARY_IS_DINKUMWARE_VC) && \
    STLSOFT_CF_STD_LIBRARY_DINKUMWARE_VC_VERSION < STLSOFT_CF_DINKUMWARE_VC_VERSION_7_1

namespace std
{
    template<   ss_typename_param_k I
            ,   ss_typename_param_k T
            >
    inline ss_typename_type_k stlsoft_ns_qual(index_iterator)<I, T>::iterator_category _Iter_cat(stlsoft_ns_qual(index_iterator)<I, T> const&)
    {
        return ss_typename_type_k stlsoft_ns_qual(index_iterator)<I, T>::iterator_category();
    }
    template<   ss_typename_param_k I
            ,   ss_typename_param_k T
            >
    inline ss_typename_type_k stlsoft_ns_qual(index_iterator)<I, T>::value_type *_Val_type(stlsoft_ns_qual(index_iterator)<I, T> const&)
    {
        return static_cast</* ss_typename_type_k  */stlsoft_ns_qual(index_iterator)<I, T>::value_type*>(0);
    }

# if STLSOFT_CF_STD_LIBRARY_DINKUMWARE_VC_VERSION == STLSOFT_CF_DINKUMWARE_VC_VERSION_7_0
    template<   ss_typename_param_k I
            ,   ss_typename_param_k T
            >
    inline ss_typename_type_k stlsoft_ns_qual(index_iterator)<I, T>::difference_type *_Dist_type(stlsoft_ns_qual(index_iterator)<I, T> const&)
    {
        return static_cast</* ss_typename_type_k  */stlsoft_ns_qual(index_iterator)<I, T>::difference_type*>(0);
    }
# elif STLSOFT_CF_STD_LIBRARY_DINKUMWARE_VC_VERSION < STLSOFT_CF_DINKUMWARE_VC_VERSION_7_1
# else
#  error Error in discrimination
# endif

} // namespace std


#endif /* old-dinkumware */

/* ////////////////////////////////////////////////////////////////////// */

#endif /* !STLSOFT_INCL_STLSOFT_ITERATORS_HPP_INDEX_ITERATOR */

/* ////////////////////////////////////////////////////////////////////// */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -