G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsSortedVector< T, _A > Class Template Reference

Detailed Description

template<class T, class _A = std::allocator<T>>
class gismo::gsSortedVector< T, _A >

This class is derived from std::vector, and adds sort tracking.

There are two basic ways to use a sorted vector:

METHOD 1 Always maintain sort order by inserting with push_sorted() - the location of new items is determined before inserting; since the vector remains sorted, this doesn't take too long (although for large batch insertions METHOD 2 is definitely faster);

METHOD 2 Allow batch insertion without sorting with push_unsorted(); then provide an additional call to sort the vector; before searching for an item, the vector is always sorted if needed;

Of course you need to provide an operator()< for the type of object you're sorting, if it doesn't have one. Example:

class MyClass
{
public:
bool operator< (const MyClass left) const
{
if ( left.m_nMostImportant == m_nMostImportant )
return left.m_nLeastImportant < m_nLeastImportant;
return left.m_nMostImportant < m_nMostImportant;
}
int m_nMostImportant;
int m_nLeastImportant;
}

NOTE: C++ doesn't let you use an operator()< for POINTERS. This breaks down when creating the template code, as you end up with a ref to a ref which is not allowed. So if you have a vector of pointers, here's what you have to do:

FOR NOW, with straight C++, create a less-than functor, then pass that in to the functor versions of the class methods below. Create a functor, aka function object, as follows:

struct my_class_lessthan
{
bool operator()(const MyClass* left, const MyClass* right)
{
return left->get_timestamp() < right->get_timestamp();
}
};

Usage example:

gsSortedVector<MyClass*> svpMC;
svpMC.push_unsorted(new MyClass(blah, blah);
svpMC.push_unsorted(new MyClass(blah, blah);
vpMC.sort( my_class_lessthan() );

Once C++0x is available, I need to update this class to use a function object wrapper, and allow the user to set it in the constructor, then always use it automatically. http:en.wikipedia.org/wiki/C%2B%2B0x#Polymorphic_wrappers_for_function_objects

Warning
if you change the key value of any object in the vector, you have unsorted the array without marking it as such. Make sure you call SetSorted(false) where appropriate.

Inherits vector< T, _A >.