AGX Dynamics 2.42.3.1
Loading...
Searching...
No Matches
List.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGX_LIST_H
18#define AGX_LIST_H
19
20#define AGX_USE_AGX_LIST_IMPL 1
21
22#if AGX_USE_AGX_LIST_IMPL
23
24#include <agx/Math.h>
25#include <iterator>
26
27#ifdef __linux__
28#include <cstddef>
29#endif
30
31namespace agx
32{
33#define AGX_LIST_ITERATOR_TYPEDEFS \
34 typedef std::bidirectional_iterator_tag iterator_category; \
35 typedef T& reference; \
36 typedef const T& const_reference; \
37 typedef T* pointer; \
38 typedef const T* const_pointer; \
39 typedef T value_type; \
40 typedef ptrdiff_t difference_type
41
42 template < typename T >
43 class LNode
44 {
45 public:
46 LNode() : m_element( T() ), m_next( nullptr ), m_prev( nullptr ) {}
47 LNode( T element ) : m_element( element ), m_next( nullptr ), m_prev( nullptr ) {}
48 T m_element;
49 LNode* m_next;
50 LNode* m_prev;
51 };
52
53 class ItOp
54 {
55 public:
56 typedef agx::UInt value_type;
57
58 public:
59 struct PPOp
60 {
61 template< typename T >
62 T& operator()( T& val ) const
63 {
64 return ++val;
65 }
66 };
67
68 struct MMOp
69 {
70 template< typename T >
71 T& operator()( T& val ) const
72 {
73 return --val;
74 }
75 };
76
77 public:
78 explicit ItOp( value_type num )
79 : m_num( num ) {}
80
81 template< typename ItT, typename Op >
82 ItT operator()( const ItT& org, Op op ) const
83 {
84 ItT ret = org;
85 for ( value_type i = value_type( 0 ); i < m_num; ++i )
86 op( ret );
87 return ret;
88 }
89
90 private:
91 value_type m_num;
92 };
93
94 template< typename ItT >
95 ItT operator + ( const ItT& it, const agx::ItOp& inc )
96 {
97 return inc( it, agx::ItOp::PPOp() );
98 }
99
100 template< typename ItT >
101 ItT operator - ( const ItT& it, const agx::ItOp& inc )
102 {
103 return inc( it, agx::ItOp::MMOp() );
104 }
105
106 template < typename T >
107 class List
108 {
109 public:
110 typedef List< T > my_type;
111 typedef T value_type;
112 typedef T& reference;
113 typedef const T& const_reference;
114 typedef T* pointer;
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;
120
121 public:
122 class const_iterator
123 {
124 public:
125 AGX_LIST_ITERATOR_TYPEDEFS;
126
127 public:
128 friend class List;
129
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 )
133 {
134 *this = rhs;
135 }
136 inline const_iterator& operator=( const const_iterator& rhs )
137 {
138 m_ptr = rhs.m_ptr;
139 return *this;
140 }
141 inline const_iterator& operator++()
142 {
143 m_ptr = m_ptr->m_next;
144 return *this;
145 }
146 inline const_iterator operator++(int)
147 {
148 const_iterator tmp = *this;
149 ++(*this);
150 return tmp;
151 }
152 inline const_iterator operator+( const agx::ItOp& inc ) const
153 {
154 return inc( *this, ItOp::PPOp() );
155 }
156 inline const_iterator& operator--()
157 {
158 m_ptr = m_ptr->m_prev;
159 return *this;
160 }
161 inline const_iterator operator--(int)
162 {
163 const_iterator tmp = *this;
164 --(*this);
165 return tmp;
166 }
167 inline const_iterator operator-( const agx::ItOp& inc ) const
168 {
169 return inc( *this, ItOp::MMOp() );
170 }
171 inline const_reference operator*() const
172 {
173 return m_ptr->m_element;
174 }
175 inline const_pointer operator->() const
176 {
177 return &(m_ptr->m_element);
178 }
179 inline bool operator==(const const_iterator& i) const
180 {
181 return m_ptr == i.m_ptr;
182 }
183 inline bool operator!=(const const_iterator& i) const
184 {
185 return m_ptr != i.m_ptr;
186 }
187 private:
188 node_ptr_type m_ptr;
189 };
190
191 class iterator
192 {
193 public:
194 AGX_LIST_ITERATOR_TYPEDEFS;
195
196 public:
197 friend class List;
198
199 inline iterator() : m_ptr( nullptr ) {}
200 inline explicit iterator( node_ptr_type ptr ) : m_ptr( ptr ) {}
201 inline iterator( const iterator& rhs )
202 {
203 *this = rhs;
204 }
205 inline iterator& operator=( const iterator& rhs )
206 {
207 m_ptr = rhs.m_ptr;
208 return *this;
209 }
210 inline iterator& operator++()
211 {
212 m_ptr = m_ptr->m_next;
213 return *this;
214 }
215 inline iterator operator++(int)
216 {
217 iterator tmp = *this;
218 ++(*this);
219 return tmp;
220 }
221 inline iterator operator+( const agx::ItOp& inc ) const
222 {
223 return inc( *this, ItOp::PPOp() );
224 }
225 inline iterator& operator--()
226 {
227 m_ptr = m_ptr->m_prev;
228 return *this;
229 }
230 inline iterator operator--(int)
231 {
232 iterator tmp = *this;
233 --(*this);
234 return tmp;
235 }
236 inline iterator operator-( const agx::ItOp& inc ) const
237 {
238 return inc( *this, ItOp::MMOp() );
239 }
240 inline reference operator*() const
241 {
242 return m_ptr->m_element;
243 }
244 inline pointer operator->() const
245 {
246 return &(m_ptr->m_element);
247 }
248 inline bool operator==(const iterator& i) const
249 {
250 return m_ptr == i.m_ptr;
251 }
252 inline bool operator==(const const_iterator& i) const
253 {
254 return m_ptr == i.m_ptr;
255 }
256 inline bool operator!=(const iterator& i) const
257 {
258 return m_ptr != i.m_ptr;
259 }
260 inline bool operator!=(const const_iterator& i) const
261 {
262 return m_ptr != i.m_ptr;
263 }
264 inline operator const_iterator() const
265 {
266 return const_iterator(m_ptr);
267 }
268 private:
269 node_ptr_type m_ptr;
270 };
271
272 class const_reverse_iterator
273 {
274 public:
275 AGX_LIST_ITERATOR_TYPEDEFS;
276
277 public:
278 friend class List;
279
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 )
283 {
284 *this = rhs;
285 }
286 inline const_reverse_iterator( const iterator& rhs )
287 {
288 *this = rhs;
289 }
290 inline const_reverse_iterator( const const_iterator& rhs )
291 {
292 *this = rhs;
293 }
294 inline const_reverse_iterator& operator=( const const_reverse_iterator& rhs )
295 {
296 m_ptr = rhs.m_ptr;
297 return *this;
298 }
299 inline const_reverse_iterator& operator=( const iterator& rhs )
300 {
301 m_ptr = rhs.m_ptr;
302 return *this;
303 }
304 inline const_reverse_iterator& operator=( const const_iterator& rhs )
305 {
306 m_ptr = rhs.m_ptr;
307 return *this;
308 }
309 inline const_reverse_iterator& operator++()
310 {
311 m_ptr = m_ptr->m_prev;
312 return *this;
313 }
314 inline const_reverse_iterator operator++(int)
315 {
316 const_reverse_iterator tmp = *this;
317 ++(*this);
318 return tmp;
319 }
320 inline const_reverse_iterator operator+( const agx::ItOp& inc ) const
321 {
322 return inc( *this, ItOp::PPOp() );
323 }
324 inline const_reverse_iterator& operator--()
325 {
326 m_ptr = m_ptr->m_next;
327 return *this;
328 }
329 inline const_reverse_iterator operator--(int)
330 {
331 const_iterator tmp = *this;
332 --(*this);
333 return tmp;
334 }
335 inline const_reverse_iterator operator-( const agx::ItOp& inc ) const
336 {
337 return inc( *this, ItOp::MMOp() );
338 }
339 inline const_reference operator*() const
340 {
341 return m_ptr->m_prev->m_element;
342 }
343 inline const_pointer operator->() const
344 {
345 return &(m_ptr->m_prev->m_element);
346 }
347 inline bool operator==(const const_reverse_iterator& i) const
348 {
349 return m_ptr == i.m_ptr;
350 }
351 inline bool operator!=(const const_reverse_iterator& i) const
352 {
353 return m_ptr != i.m_ptr;
354 }
355 private:
356 node_ptr_type m_ptr;
357 };
358
359 class reverse_iterator
360 {
361 public:
362 AGX_LIST_ITERATOR_TYPEDEFS;
363
364 public:
365 friend class List;
366
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 )
370 {
371 *this = rhs;
372 }
373 inline reverse_iterator( const iterator& rhs )
374 {
375 *this = rhs;
376 }
377 inline reverse_iterator& operator=( const iterator& rhs ) {
378 m_ptr = rhs.m_ptr;
379 return *this;
380 }
381 inline reverse_iterator& operator=( const const_reverse_iterator& rhs ) {
382 m_ptr = rhs.m_ptr;
383 return *this;
384 }
385 inline reverse_iterator& operator++()
386 {
387 m_ptr = m_ptr->m_prev;
388 return *this;
389 }
390 inline reverse_iterator operator++(int)
391 {
392 reverse_iterator tmp = *this;
393 ++(*this);
394 return tmp;
395 }
396 inline reverse_iterator operator+( const agx::ItOp& inc ) const
397 {
398 return inc( *this, ItOp::PPOp() );
399 }
400 inline reverse_iterator& operator--()
401 {
402 m_ptr = m_ptr->m_next;
403 return *this;
404 }
405 inline reverse_iterator operator--(int)
406 {
407 reverse_iterator tmp = *this;
408 --(*this);
409 return tmp;
410 }
411 inline reverse_iterator operator-( const agx::ItOp& inc ) const
412 {
413 return inc( *this, ItOp::MMOp() );
414 }
415 inline reference operator*() const
416 {
417 return m_ptr->m_prev->m_element;
418 }
419 inline pointer operator->() const
420 {
421 return &(m_ptr->m_prev->m_element);
422 }
423 inline bool operator==(const reverse_iterator& i) const
424 {
425 return m_ptr == i.m_ptr;
426 }
427 inline bool operator!=(const reverse_iterator& i) const
428 {
429 return m_ptr != i.m_ptr;
430 }
431 inline operator const_reverse_iterator()
432 {
433 return const_reverse_iterator(m_ptr);
434 }
435 private:
436 node_ptr_type m_ptr;
437 };
438
439 class EqualPred1
440 {
441 public:
442 EqualPred1( const_reference obj ) : m_obj( obj ) {}
443 bool operator()( const_reference obj ) const { return m_obj == obj; }
444 private:
445 value_type m_obj;
446 // Not defined.
447 private:
448 EqualPred1();
449 };
450
451 class EqualPred2
452 {
453 public:
454 bool operator()( const_reference left, const_reference right ) const { return left == right; }
455 };
456
457 class LessPred1
458 {
459 public:
460 LessPred1( const_reference obj ) : m_obj( obj ) {}
461 bool operator()( const_reference obj ) const { return m_obj == obj; }
462 private:
463 value_type m_obj;
464 // Not defined.
465 private:
466 LessPred1();
467 };
468
469 class LessPred2
470 {
471 public:
472 bool operator()( const_reference left, const_reference right ) const { return left < right; }
473 };
474
475 public:
479 List()
480 : m_size( 0 ), m_end( allocate() ), m_front( m_end )
481 {
482 }
483
487 List( const List& other )
488 : m_size( 0 ), m_end( allocate() ), m_front( m_end )
489 {
490 *this = other;
491 }
492
496 ~List()
497 {
498 clear();
499 if ( m_front == m_end && m_front != nullptr )
500 deallocate( m_front );
501 }
502
509 List& operator=( const my_type& other )
510 {
511 if ( this == &other )
512 return *this;
513
514 clear();
515 insert( end(), other.begin(), other.end() );
516 return *this;
517 }
518
524 bool operator==( const my_type& other ) const
525 {
526 if ( size() != other.size() )
527 return false;
528
529 for ( const_iterator myIt = begin(), otherIt = other.begin(), endIt = end(); myIt != endIt; ++myIt, ++otherIt )
530 if ( *myIt != *otherIt )
531 return false;
532
533 return true;
534 }
535
541 const_iterator find( const_reference _val ) const
542 {
543 iterator i(m_front);
544 iterator iend(m_end);
545 for ( ; i != iend; ++i ) {
546 if ( *i == _val ) return i;
547 }
548 return end();
549 }
550
556 iterator find( const_reference _val )
557 {
558 iterator i(m_front);
559 iterator iend(m_end);
560 for ( ; i != iend; ++i ) {
561 if ( *i == _val ) return i;
562 }
563 return end();
564 }
565
571 bool contains( const_reference _val )
572 {
573 return find( _val ) != end();
574 }
575
579 inline iterator begin() { return iterator( m_front ); }
580
584 inline const_iterator begin() const { return const_iterator( m_front ); }
585
589 inline iterator end() { return iterator( m_end ); }
590
594 inline const_iterator end() const { return const_iterator( m_end ); }
595
599 inline reverse_iterator rbegin() { return reverse_iterator( m_end ); }
600
604 inline const_reverse_iterator rbegin() const { return const_reverse_iterator( m_end ); }
605
609 inline reverse_iterator rend() { return reverse_iterator( m_front ); }
610
614 inline const_reverse_iterator rend() const { return const_reverse_iterator( m_front ); }
615
619 inline size_type size() const { return m_size; }
620
624 inline bool empty() const { return m_size == 0; }
625
630 inline reference front() { agxAssert( !empty() ); return m_front->m_element; }
631
636 inline const_reference front() const { agxAssert( !empty() ); return m_front->m_element; }
637
642 inline reference back() { agxAssert( !empty() ); return m_end->m_prev->m_element; }
643
648 inline const_reference back() const { agxAssert( !empty() ); return m_end->m_prev->m_element; }
649
656 inline iterator insert( const_iterator _where, const_reference c_ref )
657 {
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;
663 ++m_size;
664 if ( node->m_prev == nullptr ) {
665 m_front = node;
666 }
667 else {
668 node->m_prev->m_next = node;
669 }
670 return iterator(node);
671 }
672
679 template< typename InputIterator >
680 void insert( const_iterator _where, InputIterator _first, InputIterator _last )
681 {
682 for ( ; _first != _last; ++_first ) {
683 insert( _where, *_first );
684 }
685 }
686
691 inline void push_back( const_reference c_ref )
692 {
693 insert( end(), c_ref );
694 }
695
700 inline void push_front( const_reference c_ref )
701 {
702 insert( begin(), c_ref );
703 }
704
709 inline void pop_front()
710 {
711 agxAssert( !empty() );
712 erase( begin() );
713 }
714
719 inline void pop_back()
720 {
721 agxAssert( !empty() );
722 erase( --end() );
723 }
724
728 void clear()
729 {
730 while ( !empty() ) {
731 pop_back();
732 }
733 }
734
740 inline iterator erase( const_iterator _where )
741 {
742 if ( _where.m_ptr == m_front ) {
743 m_front = m_front->m_next;
744 m_front->m_prev = nullptr;
745
746 deallocate( _where.m_ptr );
747
748 agxAssert( m_size > 0 );
749 --m_size;
750 agxAssert( m_size > 0 || (m_size == 0 && m_front == m_end) );
751
752 return iterator(m_front);
753 }
754 else {
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;
757
758 node_ptr_type ret = _where.m_ptr->m_prev;
759
760 deallocate( _where.m_ptr );
761
762 agxAssert( m_size > 0 );
763 --m_size;
764
765 return iterator(ret);
766 }
767 }
768
773 template< typename Pred >
774 void remove_if( Pred pred )
775 {
776 node_ptr_type node = m_front;
777
778 while ( node != m_end ) {
779 if ( pred( node->m_element ) )
780 node = erase( const_iterator( node ) ).m_ptr;
781 else
782 node = node->m_next;
783 }
784 }
785
790 inline void remove( const_reference valArg )
791 {
792 remove_if( EqualPred1( valArg ) );
793 }
794
801 template< typename Pred >
802 void merge( my_type& other, Pred pred )
803 {
804 if ( this != &other ) {
805 iterator myIt = begin();
806 iterator myEnd = end();
807
808 const_iterator otherIt = other.begin();
809 const_iterator otherEnd = other.end();
810
811 while ( myIt != myEnd && otherIt != otherEnd ) {
812 if ( pred( *otherIt, *myIt ) ) {
813 const_iterator otherNext = otherIt;
814 splice( myIt, other, otherIt, ++otherNext, 1 );
815 otherIt = otherNext;
816 }
817 else ++myIt;
818 }
819
820 if ( otherIt != otherEnd )
821 splice( myEnd, other, otherIt, otherEnd, other.m_size );
822
823 agxVerify( other.empty() );
824 }
825 }
826
832 void merge( my_type& other )
833 {
834 merge( other, LessPred2() );
835 }
836
841 template< typename Pred >
842 void unique( Pred pred )
843 {
844 if ( empty() )
845 return;
846
847 const const_iterator endIt = end();
848 iterator curr = begin();
849 iterator prev = curr++;
850 while ( curr != endIt ) {
851 // Erase? Yes - let prev be what it was and increment curr.
852 // Note that erase wont return end since we always have prev.
853 if ( pred( *prev, *curr ) )
854 curr = ++erase( curr );
855 // Erase? No - continue.
856 else
857 prev = curr++;
858 }
859 }
860
864 void unique()
865 {
866 unique( EqualPred2() );
867 }
868
872 void reverse()
873 {
874 if ( m_size < 2 )
875 return;
876
877 node_ptr_type node = m_front;
878 for ( ; ; ) {
879 const node_ptr_type next = node->m_next;
880 node->m_next = node->m_prev;
881 node->m_prev = next;
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;
886 m_end->m_prev = tmp;
887 tmp->m_next = m_end;
888 break;
889 }
890 node = next;
891 }
892 }
893
897 void swap( my_type& other )
898 {
899 std::swap( m_front, other.m_front );
900 std::swap( m_end, other.m_end );
901 std::swap( m_size, other.m_size );
902 }
903
907 template< typename Pred >
908 void sort( Pred pred )
909 {
910 if ( m_size < 2 )
911 return;
912
913 const size_type MAXBINS = 25;
914 my_type tmpList;
915 my_type binList[ MAXBINS + 1 ];
916
917 size_type maxBin = 0;
918 while ( !empty() ) {
919 tmpList.splice( tmpList.begin(), *this, begin(), ++begin(), 1 );
920
921 size_type bin = 0;
922 for ( ; bin < maxBin && !binList[ bin ].empty(); ++bin ) {
923 binList[ bin ].merge( tmpList, pred );
924 binList[ bin ].swap( tmpList );
925 }
926
927 if ( bin == MAXBINS )
928 binList[ bin - 1 ].merge( tmpList, pred );
929 else {
930 binList[ bin ].swap( tmpList );
931 maxBin += size_type( bin == maxBin );
932 }
933 }
934
935 for ( size_type bin = 1; bin < maxBin; ++bin )
936 binList[ bin ].merge( binList[ bin - 1 ], pred );
937
938 splice( end(), binList[ maxBin - 1 ], binList[ maxBin - 1 ].begin(), binList[ maxBin - 1 ].end(), binList[ maxBin - 1 ].size() );
939 }
940
949 void splice( const_iterator _where, my_type& other, const_iterator first, const_iterator last, size_type count )
950 {
951 if ( count == 0 )
952 return;
953
954 if ( this != &other ) {
955 m_size += count;
956 other.m_size -= count;
957 }
958
959 // If first is other.begin(), other gets a new m_front.
960 if ( first.m_ptr->m_prev == nullptr )
961 other.m_front = last.m_ptr;
962 else
963 first.m_ptr->m_prev->m_next = last.m_ptr;
964
965 last.m_ptr->m_prev->m_next = _where.m_ptr;
966
967 if ( _where.m_ptr->m_prev == nullptr )
968 m_front = first.m_ptr;
969 else
970 _where.m_ptr->m_prev->m_next = first.m_ptr;
971
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;
976 }
977
983 void splice( const_iterator _where, my_type& other )
984 {
985 splice( _where, other, other.begin(), other.end(), other.size() );
986 }
987
996 void splice( const_iterator _where, my_type& other, const_iterator first, const_iterator last )
997 {
998 size_type count = 0;
999 if ( first == other.begin() && last == other.end() )
1000 count = other.size();
1001 else for ( const_iterator i = first; i != last; ++i, ++count )
1002 ;
1003 splice( _where, other, first, last, count );
1004 }
1005
1009 void sort()
1010 {
1011 sort( LessPred2() );
1012 }
1013
1014 private:
1015 node_ptr_type allocate()
1016 {
1017 return new node_type();
1018 }
1019
1020 void deallocate( node_ptr_type node )
1021 {
1022 delete node;
1023 }
1024
1025 private:
1026 size_type m_size;
1027 node_ptr_type m_end;
1028 node_ptr_type m_front;
1029 };
1030}
1031
1032
1033#else
1034
1035#include <list>
1036
1037namespace agx
1038{
1039 template< typename T >
1040 class List : public std::list< T > {};
1041}
1042
1043#endif
1044
1045#endif
AGXCORE_EXPORT agx::String operator+(const std::string &str, const agx::Name &name)
#define m_size
Definition: agx/Vector.h:429
#define agxVerify(expr)
Definition: debug.h:131
#define agxAssert(expr)
Definition: debug.h:143
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
uint64_t UInt
Definition: Integer.h:27
void swap(Vector< T > &x, Vector< T > &y)
Definition: agx/Vector.h:434
bool operator==(T val, InvalidIndexStruct)
Definition: agx.h:213
LinearProbingHashSetImplementation< KeyT, HashT >::iterator begin(LinearProbingHashSetImplementation< KeyT, HashT > &set)
AgXString< T > operator+(const Indentation &indentation, const AgXString< T > &str)
Definition: Indentation.h:66
void swap(agx::Name &lhs, agx::Name &rhs)
Definition: Name.h:323