Boost logo

Boost :

Subject: Re: [boost] Offering sorted_vector for Boost, any interest?
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2012-02-15 18:05:18

> From: ramey_at_[hidden]
> #include <vector>
> #include <algorithm>
> // some comments here
> template<class T, class A>
> class fast_set : public std::vector<T, A> {
> const T & find(const T & t){
> static bool sorted = false;
> if(! sorted){
> std::sort(begin(), end());
> sorted = true;
> }
> return std::bsearch(begin(), end(), t);
> };
> };

I'm still not entirely sure whether you're being serious, but
in the event that you are:

1. A 'static' variable declared inside a method is shared
by all class instances. What you want is a class member variable.

2. Mutating operations like push_back() do not preserve the
invariant "'sorted' is true iff. the elements are sorted".
So, if you find() and then push_back() and then find() again,
the second find() may return an incorrect result.

To get this approach to work properly, you'd have to override
every mutating operation and have it clear the 'sorted' flag.


Boost list run by bdawes at, gregod at, cpdaniel at, john at