forked from boostorg/range
*** empty log message ***
[SVN r24368]
This commit is contained in:
125
doc/intro.html
125
doc/intro.html
@ -18,22 +18,32 @@
|
||||
|
||||
<h2>Introduction</h2>
|
||||
<p>
|
||||
When writing generic code that works with Standard Library containers, one often
|
||||
finds it desirable to extend that code to work with other types that offer
|
||||
enough functionality to satisfy the needs of the generic code, but in an altered
|
||||
form. For example, raw arrays are often suitable for use with generic code that
|
||||
works with containers, provided a suitable adapter is used. Likewise, null
|
||||
terminated strings can be treated as containers of characters, if suitably
|
||||
adapted. This library provides the means to adapt Standard Library containers,
|
||||
null terminated strings, <code>std::pairs</code> of iterators, and raw
|
||||
arrays, such that the same generic code can work with them all.
|
||||
</p>
|
||||
|
||||
<!-- <p>
|
||||
This library makes it possible to treat different types as if they have
|
||||
implemented a subset of the container requirements (see §23.1of the C++
|
||||
standard). Formally, that subset is defined by the <a href="Range.htm"
|
||||
target="_self" >Range</a> concept. The subset deals mostly with iterator
|
||||
returning functions and nested <code >typedef</code>s. The main goal is to treat
|
||||
built-in arrays, standard containers, pairs of iterators and some iterators
|
||||
uniformly.
|
||||
target="_self" >Range</a> concept. The subset deals mostly with iterator
|
||||
returning functions and nested <code >typedef</code>s. The main goal is to treat
|
||||
built-in arrays, standard containers, pairs of iterators and some iterators
|
||||
uniformly.
|
||||
</p>
|
||||
-->
|
||||
<p >
|
||||
The main advantages are
|
||||
<ul >
|
||||
<li >
|
||||
safe use of built-in arrays
|
||||
</li>
|
||||
<li >
|
||||
simpler implementation of generic container algorithms
|
||||
simpler implementation of generic range algorithms
|
||||
</li>
|
||||
<li >
|
||||
more flexible client code
|
||||
@ -41,58 +51,69 @@
|
||||
<li >
|
||||
correct handling of null-terminated strings
|
||||
</li>
|
||||
<li >
|
||||
safe use of built-in arrays (for legacy code; why else would you use
|
||||
arrays?) </li>
|
||||
|
||||
</ul>
|
||||
</p>
|
||||
<p >
|
||||
Below are given a small example (the complete example can be found <a href="../test/algorithm_example.cpp" target="_self" >here</a>
|
||||
):
|
||||
<pre >
|
||||
<blockquote>
|
||||
<pre >
|
||||
//
|
||||
// example: extracting bounds in a generic algorithm
|
||||
//
|
||||
template< typename ForwardRange, typename T >
|
||||
inline typename boost::iterator_of< ForwardRange >::type
|
||||
find( ForwardRange& c, const T& value )
|
||||
{
|
||||
return std::find( boost::begin( c ), boost::end( c ), value );
|
||||
}
|
||||
|
||||
template< typename ForwardRange, typename T >
|
||||
inline typename boost::const_iterator_of< ForwardRange >::type
|
||||
find( const ForwardRange& c, const T& value )
|
||||
{
|
||||
return std::find( boost::begin( c ), boost::end( c ), value );
|
||||
}
|
||||
|
||||
//
|
||||
// replace first value and return its index
|
||||
//
|
||||
template< class ForwardRange, class T >
|
||||
inline typename boost::size_type_of< ForwardRange >::type
|
||||
my_generic_replace( ForwardRange& c, const T& value, const T& replacement )
|
||||
{
|
||||
typename boost::iterator_of< ForwardRange >::type found = find( c, value );
|
||||
|
||||
//
|
||||
// Example: extracting bounds in generic algorithms
|
||||
//
|
||||
|
||||
template< typename XRange, typename T >
|
||||
inline typename boost::iterator_of<XRange>::type
|
||||
find( XRange& c, const T& value )
|
||||
{
|
||||
return std::find( boost::begin( c ), boost::end( c ), value );
|
||||
}
|
||||
|
||||
template< typename XRange, typename T >
|
||||
inline typename boost::const_iterator_of<XRange>::type
|
||||
find( const XRange& c, const T& value )
|
||||
{
|
||||
return std::find( boost::begin( c ), boost::end( c ), value );
|
||||
}
|
||||
|
||||
//
|
||||
// replace first value and return its index
|
||||
//
|
||||
template< typename EC, typename T >
|
||||
inline typename boost::size_type_of< EC >::type
|
||||
my_generic_replace( EC& c, const T& value, const T& replacement )
|
||||
{
|
||||
typename boost::const_iterator_of<EC>::type found = find( c, value );
|
||||
if( found != boost::end( c ) )
|
||||
*found = replacement;
|
||||
return std::distance( boost::begin( c ), found );
|
||||
}
|
||||
|
||||
//
|
||||
// usage
|
||||
//
|
||||
std::vector<int> my_vector;
|
||||
typedef vector<int>::iterator iterator;
|
||||
std::pair<iterator,iterator> my_view( my_vector.begin(), my_vector.begin(
|
||||
) + N );
|
||||
char str[] = "a string";
|
||||
// ...
|
||||
std::cout << my_generic_replace( my_vector, 4, 2 )
|
||||
<< my_generic_replace( my_view, 4, 2 )
|
||||
<< my_generic_replace( str, 'a', 'b' );
|
||||
</pre>
|
||||
return std::distance( boost::begin( c ), found );
|
||||
}
|
||||
|
||||
By using the free-standing functions and type-generators, the code automatically
|
||||
//
|
||||
// usage
|
||||
//
|
||||
const int N = 5;
|
||||
std::vector<int> my_vector;
|
||||
int values[] = { 1,2,3,4,5,6,7,8,9 };
|
||||
my_vector.assign( values, values + 9 );
|
||||
typedef std::vector<int>::iterator iterator;
|
||||
std::pair<iterator,iterator> my_view( boost::begin( my_vector ),
|
||||
boost::begin( my_vector ) + N );
|
||||
char str_val[] = "a string";
|
||||
char* str = str_val;
|
||||
|
||||
std::cout << my_generic_replace( my_vector, 4, 2 )
|
||||
<< my_generic_replace( my_view, 4, 2 )
|
||||
<< my_generic_replace( str, 'a', 'b' );
|
||||
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
By using the free-standing functions and metafunctions, the code automatically
|
||||
works for all the types supported by this library. Notice that we have to
|
||||
provide two version of <code >find()</code> since we cannot forward a non-const
|
||||
rvalue with reference arguments (see this article about <a href="http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2002/n1385.htm" target="_self" >The
|
||||
|
Reference in New Issue
Block a user