remove repeated elements in TinySet

This commit is contained in:
Tiago Gomes 2012-05-29 17:09:45 +01:00
parent 6feb746412
commit 0315cd51a3
1 changed files with 18 additions and 5 deletions

View File

@ -12,7 +12,10 @@ class TinySet
{
public:
TinySet (const TinySet& s)
typedef typename vector<T>::iterator iterator;
typedef typename vector<T>::const_iterator const_iterator;
TinySet (const TinySet& s)
: vec_(s.vec_), cmp_(s.cmp_) { }
TinySet (const Compare& cmp = Compare())
@ -25,11 +28,10 @@ class TinySet
: vec_(elements), cmp_(cmp)
{
std::sort (begin(), end(), cmp_);
iterator it = unique_cmp (begin(), end());
vec_.resize (it - begin());
}
typedef typename vector<T>::iterator iterator;
typedef typename vector<T>::const_iterator const_iterator;
iterator insert (const T& t)
{
iterator it = std::lower_bound (begin(), end(), t, cmp_);
@ -224,11 +226,22 @@ class TinySet
}
private:
iterator unique_cmp (iterator first, iterator last)
{
iterator result = first;
while (++first != last) {
if ( cmp_(*result, *first)) {
*(++result) = *first;
}
}
return ++result;
}
bool consistent (void) const
{
typename vector<T>::size_type i;
for (i = 0; i < vec_.size() - 1; i++) {
if (cmp_(vec_[i], vec_[i + 1]) == false) {
if ( ! cmp_(vec_[i], vec_[i + 1])) {
return false;
}
}