📄 associative_range.hpp
字号:
typedef ss_typename_type_k associative_type::const_reference reference;
typedef ss_typename_type_k associative_type::const_reference const_reference;
typedef ss_typename_type_k associative_type::difference_type difference_type;
typedef ss_typename_type_k associative_type::size_type size_type;
};
#endif /* STLSOFT_CF_HAS_MEMBER_TYPE_SUPPORTED */
/// \brief This class adapts an STL associative instance into a Range
///
/// \param S The associative class
/// \param T The associative range traits, used to deduce the Range's iterator, const_iterator, reference, const_reference and value_type
///
/// It is categoried as an Iterable Range
///
/// It could be used as follows
/// \htmlonly
/// <code>
/// <pre>
/// void dump_elements(std::vector<int> const &numbers)
/// {
/// for(associative_range<std::vector<int> > r(numbers); r; ++r)
/// {
/// std::cout << &r; // Dump the current value to stdout
/// }
/// }
/// </pre>
/// </code>
/// \endhtmlonly
template< ss_typename_param_k S
#if defined(STLSOFT_CF_HAS_MEMBER_TYPE_SUPPORTED)
, ss_typename_param_k T = associative_range_traits<S, is_const<S>::value> // Determines whether the associative is const
#else /* ? STLSOFT_CF_HAS_MEMBER_TYPE_SUPPORTED */
, ss_typename_param_k T = const_associative_range_traits<S> // Determines whether the associative is const
#endif /* STLSOFT_CF_HAS_MEMBER_TYPE_SUPPORTED */
>
class associative_range
: public iterable_range_tag
{
public:
/// The associative type
typedef S associative_type;
/// The traits type
typedef T traits_type;
/// The range category tag type
typedef iterable_range_tag range_tag_type;
/// The current instantiation of the type
typedef associative_range<S, T> class_type;
/// The associative reference type
typedef ss_typename_type_k traits_type::associative_reference_type associative_reference_type;
/// The key type
typedef ss_typename_type_k traits_type::key_type key_type;
/// The referent type
typedef ss_typename_type_k traits_type::mapped_type mapped_type;
/// The value type
typedef ss_typename_type_k traits_type::value_type value_type;
/// The mutating (non-const) iterator type
typedef ss_typename_type_k traits_type::iterator iterator;
/// The non-mutating (const) iterator type
typedef ss_typename_type_k traits_type::const_iterator const_iterator;
/// The mutating (non-const) reference type
typedef ss_typename_type_k traits_type::reference reference;
/// The non-mutating (const) reference type
typedef ss_typename_type_k traits_type::const_reference const_reference;
/// The difference type
typedef ss_typename_type_k traits_type::difference_type difference_type;
/// The size type
typedef ss_typename_type_k traits_type::size_type size_type;
public:
#if !defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT) || \
defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_OVERLOAD_DISCRIMINATED) || \
( defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER == 1200)
/// Constructor
///
/// \param seq The associative which will be adapted to a range
associative_range(associative_reference_type seq) // The constness of this will require some thinking about. Maybe need associative_range and const_associative_range??
: m_position(seq.begin())
, m_last(seq.end())
{}
#endif /* !STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT || STLSOFT_CF_MEMBER_TEMPLATE_CTOR_OVERLOAD_DISCRIMINATED */
#if defined(STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT) && \
( !defined(STLSOFT_COMPILER_IS_MSVC) || \
_MSC_VER != 1200)
/// Constructor
///
/// \param seq The associative which will be adapted to a range
template <ss_typename_param_k S2>
associative_range(S2 &seq)
: m_position(seq.begin())
, m_last(seq.end())
{}
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT */
/// Copy constructor
///
/// \note This has to be provided, to avoid precipitating C4217 with Visual C++
associative_range(class_type const &rhs)
: m_position(rhs.m_position)
, m_last(rhs.m_last)
{}
/// Copy assignment operator
///
/// \note This has to be provided, to avoid precipitating C4217 with Visual C++
class_type &operator =(class_type const &rhs)
{
m_position = rhs.m_position;
m_last = rhs.m_last;
return *this;
}
/// \name Notional Range methods
/// @{
private:
STLSOFT_DEFINE_OPERATOR_BOOL_TYPES_T(class_type, boolean_generator_type, boolean_type);
public:
/// Indicates whether the range is open
ss_bool_t is_open() const
{
return m_position != m_last;
}
/// Returns the current key+value pair in the range
reference current()
{
STLSOFT_ASSERT(is_open());
return *m_position;
}
/// Returns the current key+value pair in the range
const_reference current() const
{
STLSOFT_ASSERT(is_open());
return *m_position;
}
/// Returns the key of the current item in the range
key_type current_key()
{
return current().first;
}
/// Returns the value of the current item in the range
key_type current_key() const
{
return current().first;
}
/// Returns the value of the current item in the range
mapped_type current_value()
{
return current().second;
}
/// Returns the value of the current item in the range
mapped_type current_value() const
{
return current().second;
}
/// Advances the current position in the range
class_type &advance()
{
STLSOFT_MESSAGE_ASSERT("Attempting to increment the range past its end point", is_open());
++m_position;
return *this;
}
/// Indicates whether the range is open
operator boolean_type() const
{
return boolean_generator_type::translate(is_open());
}
/// Returns the current key+value pair in the range
reference operator *()
{
return current();
}
/// Returns the current key+value pair in the range
const_reference operator *() const
{
return current();
}
/// Advances the current position in the range
class_type &operator ++()
{
return advance();
}
/// Advances the current position in the range, returning a copy of the
/// range prior to its being advanced
class_type operator ++(int)
{
class_type ret(*this);
operator ++();
return ret;
}
/// @}
/// \name Iterable Range methods
/// @{
public:
/// Returns an iterator to the current position of the range
iterator begin()
{
return m_position;
}
/// Returns an iterator to the end of the range
iterator end()
{
return m_last;
}
/// Returns an iterator to the current position of the range
const_iterator begin() const
{
return m_position;
}
/// Returns an iterator to the end of the range
const_iterator end() const
{
return m_last;
}
/// @}
// Members
private:
iterator m_position;
iterator m_last;
};
////////////////////////////////////////////////////////////////////////////
// Unit-testing
#ifdef STLSOFT_UNITTEST
# include "./unittest/associative_range_unittest_.h"
#endif /* STLSOFT_UNITTEST */
/* ////////////////////////////////////////////////////////////////////// */
#ifndef RANGELIB_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
} // namespace rangelib
# else
} // namespace rangelib_project
} // namespace stlsoft
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !RANGELIB_NO_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* !RANGELIB_INCL_RANGELIB_HPP_ASSOCIATIVE_RANGE */
/* ////////////////////////////////////////////////////////////////////// */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -