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

📄 array_view.hpp

📁 新版本TR1的stl
💻 HPP
📖 第 1 页 / 共 2 页
字号:
    /// Indicates whether the search sequence is empty
    ss_bool_t empty() const
    {
        STLSOFT_ASSERT(is_valid());

        return 0 == m_size;
    }
    /// Returns the maximum number of elements in the sequence
    static size_type max_size()
    {
        return static_cast<size_type>(-1) / sizeof(value_type);
    }
///  @}

/// \name Subscripting
/// @{
public:
    /// Returns the element at the given index
    ///
    /// \param index The offset of the requested element
    /// \note No runtime checking of the validity of the index is provided in release builds, only a debug-time assert
    reference operator [](size_type index)
    {
        STLSOFT_MESSAGE_ASSERT("index out of bounds, in array_view", !(size() < index));

        STLSOFT_ASSERT(is_valid());

        return m_base[index];
    }
    /// Returns the element at the given index
    ///
    /// \param index The offset of the requested element
    /// \note No runtime checking of the validity of the index is provided in release builds, only a debug-time assert
    const_reference operator [](size_type index) const
    {
        STLSOFT_MESSAGE_ASSERT("index out of bounds, in array_view", !(size() < index));

        STLSOFT_ASSERT(is_valid());

        return const_cast<pointer>(m_base)[index];
    }

# ifdef STLSOFT_CF_EXCEPTION_SUPPORT
    /// Returns the element at the given index
    ///
    /// \param index The offset of the requested element
    ///
    /// \note Throws an instance of std::out_of_range if the index is not < size()
    reference at(size_type index)
    {
        STLSOFT_ASSERT(is_valid());

        range_check_(index);

        return m_base[index];
    }
    /// Returns the element at the given index
    ///
    /// \param index The offset of the requested element
    ///
    /// \note Throws an instance of std::out_of_range if the index is not < size()
    const_reference at(size_type index) const
    {
        STLSOFT_ASSERT(is_valid());

        range_check_(index);

        return const_cast<pointer>(m_base)[index];
    }
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
///  @}

/// \name Iteration
/// @{
public:
    /// Begins the iteration
    ///
    /// \return An iterator representing the start of the sequence
    iterator begin()
    {
        STLSOFT_ASSERT(is_valid());

        return m_base;
    }
    /// Ends the iteration
    ///
    /// \return An iterator representing the end of the sequence
    iterator end()
    {
        STLSOFT_ASSERT(is_valid());

        return begin() + size();
    }
    /// Begins the iteration
    ///
    /// \return An iterator representing the start of the sequence
    const_iterator begin() const
    {
        STLSOFT_ASSERT(is_valid());

        return m_base;
    }
    /// Ends the iteration
    ///
    /// \return An iterator representing the end of the sequence
    const_iterator end() const
    {
        STLSOFT_ASSERT(is_valid());

        return begin() + size();
    }

#if defined(STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT)
    /// Begins the reverse iteration
    ///
    /// \return An iterator representing the start of the reverse sequence
    const_reverse_iterator rbegin() const
    {
        return const_reverse_iterator(end());
    }
    /// Ends the reverse iteration
    ///
    /// \return An iterator representing the end of the reverse sequence
    const_reverse_iterator rend() const
    {
        return const_reverse_iterator(begin());
    }
    /// Begins the reverse iteration
    ///
    /// \return An iterator representing the start of the reverse sequence
    reverse_iterator  rbegin()
    {
        return reverse_iterator(end());
    }
    /// Ends the reverse iteration
    ///
    /// \return An iterator representing the end of the reverse sequence
    reverse_iterator  rend()
    {
        return reverse_iterator(begin());
    }
#endif /* STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT */
///  @}

/// \name Invariant
/// @{
#ifdef STLSOFT_UNITTEST
public:
#else /* ? STLSOFT_UNITTEST */
private:
#endif /* STLSOFT_UNITTEST */
    ss_bool_t is_valid() const
    {
        if( 0 != m_size &&
            NULL == m_base)
        {
#ifdef STLSOFT_UNITTEST
            fprintf(err, "Cannot have non-empty array view with NULL base pointer\n");
#endif /* STLSOFT_UNITTEST */

            return false;
        }

        return true;
    }
///  @}

/// \name Implementation
/// @{
private:
# ifdef STLSOFT_CF_EXCEPTION_SUPPORT
    void range_check_(size_type index) const
    {
        if(!(index < size()))
        {
            STLSOFT_THROW_X(stlsoft_ns_qual_std(out_of_range)("array view index out of range"));
        }
    }
#endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
///  @}

/// \name Members
/// @{
private:
    size_type   m_size;
    pointer     m_base;
///  @}
};

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

#ifdef STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT
template <ss_typename_param_k T, ss_size_t N>
inline array_view<T> make_array_view(T (&t)[N])
{
    return array_view<T>(&t[0], &t[N]);
//    return array_view<T>(t); // This one not used, because CodeWarrior gets confused
}
#endif /* STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT */

template <ss_typename_param_k T>
inline array_view<T> make_array_view(T *begin, T *end)
{
    return array_view<T>(begin, end);
}

template <ss_typename_param_k T>
inline array_view<const T> make_array_view(T const* begin, T const* end)
{
    return array_view<const T>(begin, end);
}

template <ss_typename_param_k T>
inline array_view<T> make_array_view(T *p, ss_size_t n)
{
    return array_view<T>(p, n);
}

#if 0
template <ss_typename_param_k T>
inline array_view<const T> make_array_view(T const* p, ss_size_t n)
{
    return array_view<const T>(p, n);
}
#endif /* 0 */

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

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

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

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

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

#endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_ARRAY_VIEW */

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

⌨️ 快捷键说明

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