add more api to sorted vector implementation

This commit is contained in:
Tiago Gomes 2012-04-17 19:02:56 +01:00
parent 5e88510a7d
commit f02d0fb798

View File

@ -196,14 +196,14 @@ class SortedVector
{ {
public: public:
SortedVector (const Compare& c = Compare()) : vec_(), cmp_(c) { } SortedVector (const Compare& c = Compare()) : vec_(), cmp_(c) { }
/*
template <class InputIterator> template <class InputIterator>
SortedVector (InputIterator first, InputIterator last, SortedVector (InputIterator first, InputIterator last,
const Compare& c = Compare()) : vec_(first, last), cmp_(c) const Compare& c = Compare()) : vec_(first, last), cmp_(c)
{ {
std::sort (begin(), end(), cmp_); std::sort (begin(), end(), cmp_);
} }
*/
typedef typename vector<T>::iterator iterator; typedef typename vector<T>::iterator iterator;
typedef typename vector<T>::const_iterator const_iterator; typedef typename vector<T>::const_iterator const_iterator;
iterator begin (void) { return vec_.begin(); } iterator begin (void) { return vec_.begin(); }
@ -219,6 +219,12 @@ class SortedVector
return i; return i;
} }
void push_back (const T& t)
{
vec_.push_back (t);
assert (consistent());
}
const_iterator find (const T& t) const const_iterator find (const T& t) const
{ {
const_iterator i = std::lower_bound (begin(), end(), t, cmp_); const_iterator i = std::lower_bound (begin(), end(), t, cmp_);
@ -231,6 +237,8 @@ class SortedVector
return i == end() || cmp_(t, *i) ? end() : i; return i == end() || cmp_(t, *i) ? end() : i;
} }
const vector<T>& elements (void) { return vec_; }
void reserve (unsigned space) { vec_.reserve (space); } void reserve (unsigned space) { vec_.reserve (space); }
unsigned size (void) const { return vec_.size(); } unsigned size (void) const { return vec_.size(); }
@ -242,6 +250,17 @@ class SortedVector
iterator erase (iterator it) { return vec_.erase (it); } iterator erase (iterator it) { return vec_.erase (it); }
private: private:
bool consistent (void) const
{
for (unsigned i = 0; i < vec_.size() - 1; i++) {
if (cmp_(vec_[i], vec_[i+1]) == false) {
return false;
}
}
return true;
}
std::vector<T> vec_; std::vector<T> vec_;
Compare cmp_; Compare cmp_;
}; };