20#define AGX_USE_AGX_LIST_IMPL 1
22#if AGX_USE_AGX_LIST_IMPL
33#define AGX_LIST_ITERATOR_TYPEDEFS \
34 typedef std::bidirectional_iterator_tag iterator_category; \
35 typedef T& reference; \
36 typedef const T& const_reference; \
38 typedef const T* const_pointer; \
39 typedef T value_type; \
40 typedef ptrdiff_t difference_type
42 template <
typename T >
46 LNode() : m_element( T() ), m_next(
nullptr ), m_prev(
nullptr ) {}
47 LNode( T element ) : m_element( element ), m_next(
nullptr ), m_prev(
nullptr ) {}
61 template<
typename T >
62 T& operator()( T& val )
const
70 template<
typename T >
71 T& operator()( T& val )
const
78 explicit ItOp( value_type num )
81 template<
typename ItT,
typename Op >
82 ItT operator()(
const ItT& org, Op op )
const
85 for ( value_type i = value_type( 0 ); i < m_num; ++i )
94 template<
typename ItT >
95 ItT
operator + (
const ItT& it,
const agx::ItOp& inc )
97 return inc( it, agx::ItOp::PPOp() );
100 template<
typename ItT >
101 ItT operator - (
const ItT& it,
const agx::ItOp& inc )
103 return inc( it, agx::ItOp::MMOp() );
106 template <
typename T >
110 typedef List< T > my_type;
111 typedef T value_type;
112 typedef T& reference;
113 typedef const T& const_reference;
115 typedef const T* const_pointer;
116 typedef size_t size_type;
117 typedef LNode<T> node_type;
118 typedef LNode<T>* node_ptr_type;
119 typedef LNode<T>& node_ref_type;
125 AGX_LIST_ITERATOR_TYPEDEFS;
130 inline const_iterator() : m_ptr(
nullptr ) {}
131 inline explicit const_iterator( node_ptr_type ptr ) : m_ptr( ptr ) {}
132 inline const_iterator(
const const_iterator& rhs )
136 inline const_iterator& operator=(
const const_iterator& rhs )
141 inline const_iterator& operator++()
143 m_ptr = m_ptr->m_next;
146 inline const_iterator operator++(
int)
148 const_iterator tmp = *
this;
152 inline const_iterator
operator+(
const agx::ItOp& inc )
const
154 return inc( *
this, ItOp::PPOp() );
156 inline const_iterator& operator--()
158 m_ptr = m_ptr->m_prev;
161 inline const_iterator operator--(
int)
163 const_iterator tmp = *
this;
167 inline const_iterator operator-(
const agx::ItOp& inc )
const
169 return inc( *
this, ItOp::MMOp() );
171 inline const_reference operator*()
const
173 return m_ptr->m_element;
175 inline const_pointer operator->()
const
177 return &(m_ptr->m_element);
179 inline bool operator==(
const const_iterator& i)
const
181 return m_ptr == i.m_ptr;
183 inline bool operator!=(
const const_iterator& i)
const
185 return m_ptr != i.m_ptr;
194 AGX_LIST_ITERATOR_TYPEDEFS;
199 inline iterator() : m_ptr(
nullptr ) {}
200 inline explicit iterator( node_ptr_type ptr ) : m_ptr( ptr ) {}
201 inline iterator(
const iterator& rhs )
205 inline iterator& operator=(
const iterator& rhs )
210 inline iterator& operator++()
212 m_ptr = m_ptr->m_next;
215 inline iterator operator++(
int)
217 iterator tmp = *
this;
221 inline iterator
operator+(
const agx::ItOp& inc )
const
223 return inc( *
this, ItOp::PPOp() );
225 inline iterator& operator--()
227 m_ptr = m_ptr->m_prev;
230 inline iterator operator--(
int)
232 iterator tmp = *
this;
236 inline iterator operator-(
const agx::ItOp& inc )
const
238 return inc( *
this, ItOp::MMOp() );
240 inline reference operator*()
const
242 return m_ptr->m_element;
244 inline pointer operator->()
const
246 return &(m_ptr->m_element);
248 inline bool operator==(
const iterator& i)
const
250 return m_ptr == i.m_ptr;
252 inline bool operator==(
const const_iterator& i)
const
254 return m_ptr == i.m_ptr;
256 inline bool operator!=(
const iterator& i)
const
258 return m_ptr != i.m_ptr;
260 inline bool operator!=(
const const_iterator& i)
const
262 return m_ptr != i.m_ptr;
264 inline operator const_iterator()
const
266 return const_iterator(m_ptr);
272 class const_reverse_iterator
275 AGX_LIST_ITERATOR_TYPEDEFS;
280 inline const_reverse_iterator() : m_ptr(
nullptr ) {}
281 inline explicit const_reverse_iterator( node_ptr_type ptr ) : m_ptr( ptr ) {}
282 inline const_reverse_iterator(
const const_reverse_iterator& rhs )
286 inline const_reverse_iterator(
const iterator& rhs )
290 inline const_reverse_iterator(
const const_iterator& rhs )
294 inline const_reverse_iterator& operator=(
const const_reverse_iterator& rhs )
299 inline const_reverse_iterator& operator=(
const iterator& rhs )
304 inline const_reverse_iterator& operator=(
const const_iterator& rhs )
309 inline const_reverse_iterator& operator++()
311 m_ptr = m_ptr->m_prev;
314 inline const_reverse_iterator operator++(
int)
316 const_reverse_iterator tmp = *
this;
320 inline const_reverse_iterator
operator+(
const agx::ItOp& inc )
const
322 return inc( *
this, ItOp::PPOp() );
324 inline const_reverse_iterator& operator--()
326 m_ptr = m_ptr->m_next;
329 inline const_reverse_iterator operator--(
int)
331 const_iterator tmp = *
this;
335 inline const_reverse_iterator operator-(
const agx::ItOp& inc )
const
337 return inc( *
this, ItOp::MMOp() );
339 inline const_reference operator*()
const
341 return m_ptr->m_prev->m_element;
343 inline const_pointer operator->()
const
345 return &(m_ptr->m_prev->m_element);
347 inline bool operator==(
const const_reverse_iterator& i)
const
349 return m_ptr == i.m_ptr;
351 inline bool operator!=(
const const_reverse_iterator& i)
const
353 return m_ptr != i.m_ptr;
359 class reverse_iterator
362 AGX_LIST_ITERATOR_TYPEDEFS;
367 inline reverse_iterator() : m_ptr(
nullptr ) {}
368 inline explicit reverse_iterator( node_ptr_type ptr ) : m_ptr( ptr ) {}
369 inline reverse_iterator(
const reverse_iterator& rhs )
373 inline reverse_iterator(
const iterator& rhs )
377 inline reverse_iterator& operator=(
const iterator& rhs ) {
381 inline reverse_iterator& operator=(
const const_reverse_iterator& rhs ) {
385 inline reverse_iterator& operator++()
387 m_ptr = m_ptr->m_prev;
390 inline reverse_iterator operator++(
int)
392 reverse_iterator tmp = *
this;
396 inline reverse_iterator
operator+(
const agx::ItOp& inc )
const
398 return inc( *
this, ItOp::PPOp() );
400 inline reverse_iterator& operator--()
402 m_ptr = m_ptr->m_next;
405 inline reverse_iterator operator--(
int)
407 reverse_iterator tmp = *
this;
411 inline reverse_iterator operator-(
const agx::ItOp& inc )
const
413 return inc( *
this, ItOp::MMOp() );
415 inline reference operator*()
const
417 return m_ptr->m_prev->m_element;
419 inline pointer operator->()
const
421 return &(m_ptr->m_prev->m_element);
423 inline bool operator==(
const reverse_iterator& i)
const
425 return m_ptr == i.m_ptr;
427 inline bool operator!=(
const reverse_iterator& i)
const
429 return m_ptr != i.m_ptr;
431 inline operator const_reverse_iterator()
433 return const_reverse_iterator(m_ptr);
442 EqualPred1( const_reference obj ) : m_obj( obj ) {}
443 bool operator()( const_reference obj )
const {
return m_obj == obj; }
454 bool operator()( const_reference left, const_reference right )
const {
return left == right; }
460 LessPred1( const_reference obj ) : m_obj( obj ) {}
461 bool operator()( const_reference obj )
const {
return m_obj == obj; }
472 bool operator()( const_reference left, const_reference right )
const {
return left < right; }
480 :
m_size( 0 ), m_end( allocate() ), m_front( m_end )
487 List(
const List& other )
488 :
m_size( 0 ), m_end( allocate() ), m_front( m_end )
499 if ( m_front == m_end && m_front !=
nullptr )
500 deallocate( m_front );
509 List& operator=(
const my_type& other )
511 if (
this == &other )
515 insert(
end(), other.begin(), other.end() );
526 if ( size() != other.size() )
529 for ( const_iterator myIt =
begin(), otherIt = other.begin(), endIt =
end(); myIt != endIt; ++myIt, ++otherIt )
530 if ( *myIt != *otherIt )
541 const_iterator find( const_reference _val )
const
544 iterator iend(m_end);
545 for ( ; i != iend; ++i ) {
546 if ( *i == _val )
return i;
556 iterator find( const_reference _val )
559 iterator iend(m_end);
560 for ( ; i != iend; ++i ) {
561 if ( *i == _val )
return i;
571 bool contains( const_reference _val )
573 return find( _val ) !=
end();
579 inline iterator
begin() {
return iterator( m_front ); }
584 inline const_iterator
begin()
const {
return const_iterator( m_front ); }
589 inline iterator
end() {
return iterator( m_end ); }
594 inline const_iterator
end()
const {
return const_iterator( m_end ); }
599 inline reverse_iterator rbegin() {
return reverse_iterator( m_end ); }
604 inline const_reverse_iterator rbegin()
const {
return const_reverse_iterator( m_end ); }
609 inline reverse_iterator rend() {
return reverse_iterator( m_front ); }
614 inline const_reverse_iterator rend()
const {
return const_reverse_iterator( m_front ); }
619 inline size_type size()
const {
return m_size; }
624 inline bool empty()
const {
return m_size == 0; }
630 inline reference front() {
agxAssert( !empty() );
return m_front->m_element; }
636 inline const_reference front()
const {
agxAssert( !empty() );
return m_front->m_element; }
642 inline reference back() {
agxAssert( !empty() );
return m_end->m_prev->m_element; }
648 inline const_reference back()
const {
agxAssert( !empty() );
return m_end->m_prev->m_element; }
656 inline iterator insert( const_iterator _where, const_reference c_ref )
658 node_ptr_type node = allocate();
659 node->m_element = c_ref;
660 node->m_prev = _where.m_ptr->m_prev;
661 node->m_next = _where.m_ptr;
662 _where.m_ptr->m_prev = node;
664 if ( node->m_prev ==
nullptr ) {
668 node->m_prev->m_next = node;
670 return iterator(node);
679 template<
typename InputIterator >
680 void insert( const_iterator _where, InputIterator _first, InputIterator _last )
682 for ( ; _first != _last; ++_first ) {
683 insert( _where, *_first );
691 inline void push_back( const_reference c_ref )
693 insert(
end(), c_ref );
700 inline void push_front( const_reference c_ref )
702 insert(
begin(), c_ref );
709 inline void pop_front()
719 inline void pop_back()
740 inline iterator erase( const_iterator _where )
742 if ( _where.m_ptr == m_front ) {
743 m_front = m_front->m_next;
744 m_front->m_prev =
nullptr;
746 deallocate( _where.m_ptr );
752 return iterator(m_front);
755 _where.m_ptr->m_prev->m_next = _where.m_ptr->m_next;
756 _where.m_ptr->m_next->m_prev = _where.m_ptr->m_prev;
758 node_ptr_type ret = _where.m_ptr->m_prev;
760 deallocate( _where.m_ptr );
765 return iterator(ret);
773 template<
typename Pred >
774 void remove_if( Pred pred )
776 node_ptr_type node = m_front;
778 while ( node != m_end ) {
779 if ( pred( node->m_element ) )
780 node = erase( const_iterator( node ) ).m_ptr;
790 inline void remove( const_reference valArg )
792 remove_if( EqualPred1( valArg ) );
801 template<
typename Pred >
802 void merge( my_type& other, Pred pred )
804 if (
this != &other ) {
805 iterator myIt =
begin();
806 iterator myEnd =
end();
808 const_iterator otherIt = other.begin();
809 const_iterator otherEnd = other.end();
811 while ( myIt != myEnd && otherIt != otherEnd ) {
812 if ( pred( *otherIt, *myIt ) ) {
813 const_iterator otherNext = otherIt;
814 splice( myIt, other, otherIt, ++otherNext, 1 );
820 if ( otherIt != otherEnd )
821 splice( myEnd, other, otherIt, otherEnd, other.m_size );
832 void merge( my_type& other )
834 merge( other, LessPred2() );
841 template<
typename Pred >
842 void unique( Pred pred )
847 const const_iterator endIt =
end();
848 iterator curr =
begin();
849 iterator prev = curr++;
850 while ( curr != endIt ) {
853 if ( pred( *prev, *curr ) )
854 curr = ++erase( curr );
866 unique( EqualPred2() );
877 node_ptr_type node = m_front;
879 const node_ptr_type next = node->m_next;
880 node->m_next = node->m_prev;
882 if ( node == m_end ) {
883 node_ptr_type tmp = m_front;
884 m_front = m_end->m_next;
885 m_front->m_prev =
nullptr;
897 void swap( my_type& other )
907 template<
typename Pred >
908 void sort( Pred pred )
913 const size_type MAXBINS = 25;
915 my_type binList[ MAXBINS + 1 ];
917 size_type maxBin = 0;
919 tmpList.splice( tmpList.begin(), *
this,
begin(), ++
begin(), 1 );
922 for ( ; bin < maxBin && !binList[ bin ].empty(); ++bin ) {
923 binList[ bin ].merge( tmpList, pred );
924 binList[ bin ].swap( tmpList );
927 if ( bin == MAXBINS )
928 binList[ bin - 1 ].merge( tmpList, pred );
930 binList[ bin ].swap( tmpList );
931 maxBin += size_type( bin == maxBin );
935 for ( size_type bin = 1; bin < maxBin; ++bin )
936 binList[ bin ].merge( binList[ bin - 1 ], pred );
938 splice(
end(), binList[ maxBin - 1 ], binList[ maxBin - 1 ].
begin(), binList[ maxBin - 1 ].
end(), binList[ maxBin - 1 ].size() );
949 void splice( const_iterator _where, my_type& other, const_iterator first, const_iterator last, size_type count )
954 if (
this != &other ) {
956 other.m_size -= count;
960 if ( first.m_ptr->m_prev ==
nullptr )
961 other.m_front = last.m_ptr;
963 first.m_ptr->m_prev->m_next = last.m_ptr;
965 last.m_ptr->m_prev->m_next = _where.m_ptr;
967 if ( _where.m_ptr->m_prev ==
nullptr )
968 m_front = first.m_ptr;
970 _where.m_ptr->m_prev->m_next = first.m_ptr;
972 node_ptr_type node = _where.m_ptr->m_prev;
973 _where.m_ptr->m_prev = last.m_ptr->m_prev;
974 last.m_ptr->m_prev = first.m_ptr->m_prev;
975 first.m_ptr->m_prev = node;
983 void splice( const_iterator _where, my_type& other )
985 splice( _where, other, other.begin(), other.end(), other.size() );
996 void splice( const_iterator _where, my_type& other, const_iterator first, const_iterator last )
999 if ( first == other.begin() && last == other.end() )
1000 count = other.size();
1001 else for ( const_iterator i = first; i != last; ++i, ++count )
1003 splice( _where, other, first, last, count );
1011 sort( LessPred2() );
1015 node_ptr_type allocate()
1017 return new node_type();
1020 void deallocate( node_ptr_type node )
1027 node_ptr_type m_end;
1028 node_ptr_type m_front;
1039 template<
typename T >
1040 class List :
public std::list< T > {};
AGXCORE_EXPORT agx::String operator+(const std::string &str, const agx::Name &name)
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
void swap(Vector< T > &x, Vector< T > &y)
bool operator==(T val, InvalidIndexStruct)
LinearProbingHashSetImplementation< KeyT, HashT >::iterator begin(LinearProbingHashSetImplementation< KeyT, HashT > &set)
AgXString< T > operator+(const Indentation &indentation, const AgXString< T > &str)
void swap(agx::Name &lhs, agx::Name &rhs)