RFC: where to put sorted vector template/class?
Noel Grandin
noel at peralex.com
Tue Jul 10 07:07:10 PDT 2012
On 2012-07-10 15:35, Caolán McNamara wrote:
> On Tue, 2012-07-10 at 14:53 +0200, Noel Grandin wrote:
>> Rather than re-implement such a class/template repeatedly, is there a
>> common place I could stash such a template, and then include it in the
>> places it is necessary?
> o3tl maybe ?
>
Since I have your attention, and I know very little about modern
template programming :-)
How does this look? It's the bare minimum I need in a sorted_vector class.
namespace o3tl
{
/** Represents a sorted vector of values.
@tpl Value
@tpl Compare comparison method
*/
template <class Value, class Compare = less<Value>>
class sorted_vector : vector<Value>
{
public:
// MODIFIERS
pair<iterator,bool> insert( const Value& x );
size_type erase( const Value& x );
// OPERATIONS
/* Searches the container for an element with a value of x
* and returns an iterator to it if found, otherwise it returns an
* iterator to sorted_vector::end (the element past the end of the
container).
*/
iterator find( const Value& x ) const;
};
// IMPLEMENTATION
template <class Value, class Compare>
pair<iterator,bool> sorted_vector<Value,Compare>::insert( const Value& x )
{
iterator it = std::lower_bound(begin(), end(), p, Compare);
if( !Compare(*it, x) )
{
return make_pair( it, false );
}
it = insert( it, p );
return make_pair( it, true );
}
template <class Value, class Compare>
size_type sorted_vector<Value,Compare>::erase( const Value& x )
{
iterator it = std::lower_bound(begin(), end(), p, Compare);
if( !Compare(*it, x) )
{
erase( it );
return 1;
}
return 0;
}
template <class Value, class Compare>
iterator sorted_vector<Value,Compare>::find( const Value& x )
{
iterator it = std::lower_bound(begin(), end(), p, Compare);
if( !Compare(*it, x) )
{
return it;
}
return end();
}
Disclaimer: http://www.peralex.com/disclaimer.html
More information about the LibreOffice
mailing list