remove repeated elements in TinySet
This commit is contained in:
parent
6feb746412
commit
0315cd51a3
@ -12,7 +12,10 @@ class TinySet
|
|||||||
{
|
{
|
||||||
public:
|
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_) { }
|
: vec_(s.vec_), cmp_(s.cmp_) { }
|
||||||
|
|
||||||
TinySet (const Compare& cmp = Compare())
|
TinySet (const Compare& cmp = Compare())
|
||||||
@ -25,11 +28,10 @@ class TinySet
|
|||||||
: vec_(elements), cmp_(cmp)
|
: vec_(elements), cmp_(cmp)
|
||||||
{
|
{
|
||||||
std::sort (begin(), end(), 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 insert (const T& t)
|
||||||
{
|
{
|
||||||
iterator it = std::lower_bound (begin(), end(), t, cmp_);
|
iterator it = std::lower_bound (begin(), end(), t, cmp_);
|
||||||
@ -224,11 +226,22 @@ class TinySet
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
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
|
bool consistent (void) const
|
||||||
{
|
{
|
||||||
typename vector<T>::size_type i;
|
typename vector<T>::size_type i;
|
||||||
for (i = 0; i < vec_.size() - 1; 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;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user