1 #ifndef __cxxtest__LinkedList_cpp__
2 #define __cxxtest__LinkedList_cpp__
4 #include <cxxtest/LinkedList.h>
6 namespace CxxTest
7 {
8 List GlobalFixture::_list = { 0, 0 };
9 List RealSuiteDescription::_suites = { 0, 0 };
11 void List::initialize()
12 {
13 _head = _tail = 0;
14 }
16 Link *List::head()
17 {
18 Link *l = _head;
19 while ( l && !l->active() )
20 l = l->next();
21 return l;
22 }
24 const Link *List::head() const
25 {
26 Link *l = _head;
27 while ( l && !l->active() )
28 l = l->next();
29 return l;
30 }
32 Link *List::tail()
33 {
34 Link *l = _tail;
35 while ( l && !l->active() )
36 l = l->prev();
37 return l;
38 }
40 const Link *List::tail() const
41 {
42 Link *l = _tail;
43 while ( l && !l->active() )
44 l = l->prev();
45 return l;
46 }
48 bool List::empty() const
49 {
50 return (_head == 0);
51 }
53 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 }
61 Link *List::nth( unsigned n )
62 {
63 Link *l = head();
64 while ( n -- )
65 l = l->next();
66 return l;
67 }
69 void List::activateAll()
70 {
71 for ( Link *l = _head; l != 0; l = l->justNext() )
72 l->setActive( true );
73 }
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 }
82 Link::Link() :
83 _next( 0 ),
84 _prev( 0 ),
85 _active( true )
86 {
87 }
89 Link::~Link()
90 {
91 }
93 bool Link::active() const
94 {
95 return _active;
96 }
98 void Link::setActive( bool value )
99 {
100 _active = value;
101 }
103 Link * Link::justNext()
104 {
105 return _next;
106 }
108 Link * Link::justPrev()
109 {
110 return _prev;
111 }
113 Link * Link::next()
114 {
115 Link *l = _next;
116 while ( l && !l->_active )
117 l = l->_next;
118 return l;
119 }
121 Link * Link::prev()
122 {
123 Link *l = _prev;
124 while ( l && !l->_active )
125 l = l->_prev;
126 return l;
127 }
129 const Link * Link::next() const
130 {
131 Link *l = _next;
132 while ( l && !l->_active )
133 l = l->_next;
134 return l;
135 }
137 const Link * Link::prev() const
138 {
139 Link *l = _prev;
140 while ( l && !l->_active )
141 l = l->_prev;
142 return l;
143 }
145 void Link::attach( List &l )
146 {
147 if ( l._tail )
148 l._tail->_next = this;
150 _prev = l._tail;
151 _next = 0;
153 if ( l._head == 0 )
154 l._head = this;
155 l._tail = this;
156 }
158 void Link::detach( List &l )
159 {
160 if ( _prev )
161 _prev->_next = _next;
162 else
163 l._head = _next;
165 if ( _next )
166 _next->_prev = _prev;
167 else
168 l._tail = _prev;
169 }
170 };
172 #endif // __cxxtest__LinkedList_cpp__