1 #ifndef __cxxtest__LinkedList_cpp__ 2 #define __cxxtest__LinkedList_cpp__ 3 4 #include <cxxtest/LinkedList.h> 5 6 namespace CxxTest 7 { 8 List GlobalFixture::_list = { 0, 0 }; 9 List RealSuiteDescription::_suites = { 0, 0 }; 10 initialize()11 void List::initialize() 12 { 13 _head = _tail = 0; 14 } 15 head()16 Link *List::head() 17 { 18 Link *l = _head; 19 while ( l && !l->active() ) 20 l = l->next(); 21 return l; 22 } 23 head() const24 const Link *List::head() const 25 { 26 Link *l = _head; 27 while ( l && !l->active() ) 28 l = l->next(); 29 return l; 30 } 31 tail()32 Link *List::tail() 33 { 34 Link *l = _tail; 35 while ( l && !l->active() ) 36 l = l->prev(); 37 return l; 38 } 39 tail() const40 const Link *List::tail() const 41 { 42 Link *l = _tail; 43 while ( l && !l->active() ) 44 l = l->prev(); 45 return l; 46 } 47 empty() const48 bool List::empty() const 49 { 50 return (_head == 0); 51 } 52 size() const53 unsigned List::size() const 54 { 55 unsigned count = 0; 56 for ( const Link *l = head(); l != 0; l = l->next() ) 57 ++ count; 58 return count; 59 } 60 nth(unsigned n)61 Link *List::nth( unsigned n ) 62 { 63 Link *l = head(); 64 while ( n -- ) 65 l = l->next(); 66 return l; 67 } 68 activateAll()69 void List::activateAll() 70 { 71 for ( Link *l = _head; l != 0; l = l->justNext() ) 72 l->setActive( true ); 73 } 74 leaveOnly(const Link & link)75 void List::leaveOnly( const Link &link ) 76 { 77 for ( Link *l = head(); l != 0; l = l->next() ) 78 if ( l != &link ) 79 l->setActive( false ); 80 } 81 Link()82 Link::Link() : 83 _next( 0 ), 84 _prev( 0 ), 85 _active( true ) 86 { 87 } 88 ~Link()89 Link::~Link() 90 { 91 } 92 active() const93 bool Link::active() const 94 { 95 return _active; 96 } 97 setActive(bool value)98 void Link::setActive( bool value ) 99 { 100 _active = value; 101 } 102 justNext()103 Link * Link::justNext() 104 { 105 return _next; 106 } 107 justPrev()108 Link * Link::justPrev() 109 { 110 return _prev; 111 } 112 next()113 Link * Link::next() 114 { 115 Link *l = _next; 116 while ( l && !l->_active ) 117 l = l->_next; 118 return l; 119 } 120 prev()121 Link * Link::prev() 122 { 123 Link *l = _prev; 124 while ( l && !l->_active ) 125 l = l->_prev; 126 return l; 127 } 128 next() const129 const Link * Link::next() const 130 { 131 Link *l = _next; 132 while ( l && !l->_active ) 133 l = l->_next; 134 return l; 135 } 136 prev() const137 const Link * Link::prev() const 138 { 139 Link *l = _prev; 140 while ( l && !l->_active ) 141 l = l->_prev; 142 return l; 143 } 144 attach(List & l)145 void Link::attach( List &l ) 146 { 147 if ( l._tail ) 148 l._tail->_next = this; 149 150 _prev = l._tail; 151 _next = 0; 152 153 if ( l._head == 0 ) 154 l._head = this; 155 l._tail = this; 156 } 157 detach(List & l)158 void Link::detach( List &l ) 159 { 160 if ( _prev ) 161 _prev->_next = _next; 162 else 163 l._head = _next; 164 165 if ( _next ) 166 _next->_prev = _prev; 167 else 168 l._tail = _prev; 169 } 170 }; 171 172 #endif // __cxxtest__LinkedList_cpp__ 173