24 template<
typename T1,
typename T2>
26 const std::pair<T1,T2>& rhs )
28 os << rhs.first <<
", " << rhs.second;
108 template<
class T,
class _A = std::allocator<T> >
111 typedef std::vector<T,_A> inherited;
114 typedef typename std::vector<T>::const_iterator const_iterator;
115 typedef typename std::vector<T>::iterator iterator;
121 : std::vector<T, _A>( ), m_bSorted(
true) { }
124 ,
typename std::vector<T>::const_iterator end)
125 : std::vector<T, _A>(start,end), m_bSorted(0) { };
138 void SetSorted(
bool bSorted =
true ) { m_bSorted = bSorted; }
152 std::sort( inherited::begin(), inherited::end() );
157 bool bContains(
const T& t )
const
160 gsWarn<<
"gsSortedVector is not sorted, bContains("<<t<<
")"
161 <<
"is not guaranteed to be correct.\n";
163 return std::binary_search( inherited::begin(), inherited::end(), t );
166 typename std::vector<T>::iterator lower_bound_it(
const T& key )
171 typename std::vector<T>::iterator it = std::lower_bound( inherited::begin(), inherited::end(), key );
175 T* lower_bound_ptr(
const T& key )
177 typename std::vector<T>::iterator it = lower_bound_it( key );
179 if (it==inherited::end())
204 typename std::vector<T>::iterator find_it_or_fail(
const T& key )
206 typename std::vector<T>::iterator it = lower_bound_it( key );
208 if ( it != inherited::end() )
217 if( !((*it)<key) && !(key<(*it)) )
221 return inherited::end();
224 typename std::vector<T>::const_iterator find_it_or_fail(
const T& key )
const
226 typename std::vector<T>::const_iterator it =
227 std::lower_bound( inherited::begin(), inherited::end(), key );
229 if ( it != inherited::end() )
230 if( !((*it)<key) && !(key<(*it)) )
233 return inherited::end();
241 T* find_ptr_or_fail(
const T& key )
243 typename std::vector<T>::iterator it = find_it_or_fail( key );
244 if ( it != inherited::end() )
251 size_t getIndex(
const T& key )
const
253 return find_it_or_fail(key) - inherited::begin();
266 void push_sorted(
const T& t )
274 inherited::insert( std::lower_bound( inherited::begin(), inherited::end(), t ), t );
278 void push_sorted_unique(
const T& t )
287 iterator pos = std::lower_bound(inherited::begin(), inherited::end(), t );
289 if ( pos == inherited::end() || *pos != t )
290 inherited::insert(pos, t);
300 void push_unsorted(
const T& t )
306 size_t uniqueSize()
const
308 if ( inherited::begin() == inherited::end() )
312 gsWarn<<
"gsSortedVector is not sorted, uniqueSize()"
313 <<
"is not guaranteed to be correct.\n";
316 for (const_iterator it = inherited::begin()+1; it != inherited::end(); ++it)
317 if ( *(it-1) != *(it) ) ++cnt;
338 template<
class _Pr>
inline
343 std::sort( inherited::begin(), inherited::end(), pr );
347 template<
class _Pr>
inline
348 typename std::vector<T>::iterator lower_bound_it(
const T& key, _Pr pr )
352 std::sort( inherited::begin(), inherited::end(), pr );
355 typename std::vector<T>::iterator it = std::lower_bound( inherited::begin(), inherited::end(), key, pr );
359 template<
class _Pr>
inline
360 T* lower_bound_ptr(
const T& key, _Pr pr )
362 typename std::vector<T>::iterator it = lower_bound_it( key, pr );
364 if (it==inherited::end())
371 template<
class _Pr>
inline
372 void push_sorted(
const T& t, _Pr pr )
376 std::sort( inherited::begin(), inherited::end(), pr );
381 insert( std::lower_bound( inherited::begin(), inherited::end(), t, pr ), t );
384 template<
class _Pr>
inline
385 typename std::vector<T>::iterator find_it_or_fail(
const T& key, _Pr pr )
387 typename std::vector<T>::iterator it = lower_bound_it( key, pr );
389 if ( it != inherited::end() )
391 if (!(pr((*it), key)) && !(pr(key,(*it))))
394 return inherited::end();
397 template<
class _Pr>
inline
398 T* find_ptr_or_fail(
const T& key, _Pr pr )
400 typename std::vector<T>::iterator it = find_it_or_fail( key, pr );
401 if ( it != inherited::end() )
This class is derived from std::vector, and adds sort tracking.
Definition: gsSortedVector.h:109
std::ostream & operator<<(std::ostream &os, const _expr< E > &b)
Stream operator for expressions.
Definition: gsExpressions.h:382
#define gsWarn
Definition: gsDebug.h:50