My Project
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | Friends
List< T > Class Template Reference

#include <ftmpl_list.h>

Public Member Functions

 List ()
 
 List (const List< T > &)
 
 List (const T &)
 
 ~List ()
 
List< T > & operator= (const List< T > &)
 
void insert (const T &)
 
void insert (const T &, int(*cmpf)(const T &, const T &))
 
void insert (const T &, int(*cmpf)(const T &, const T &), void(*insf)(T &, const T &))
 
void append (const T &)
 
int isEmpty () const
 
int length () const
 
T getFirst () const
 
void removeFirst ()
 
T getLast () const
 
void removeLast ()
 
void sort (int(*)(const T &, const T &))
 
void print (OSTREAM &) const
 

Private Attributes

ListItem< T > * first
 
ListItem< T > * last
 
int _length
 

Friends

class ListIterator< T >
 
OSTREAMoperator (OSTREAM &os, const List< T > &l)
 

Detailed Description

template<class T>
class List< T >

Definition at line 51 of file ftmpl_list.h.

Constructor & Destructor Documentation

◆ List() [1/3]

template<class T >
List< T >::List

Definition at line 86 of file ftmpl_list.cc.

87{
88 first = last = 0;
89 _length = 0;
90}
ListItem< T > * first
Definition: ftmpl_list.h:54
int _length
Definition: ftmpl_list.h:56
ListItem< T > * last
Definition: ftmpl_list.h:55

◆ List() [2/3]

template<class T >
List< T >::List ( const List< T > &  l)

Definition at line 94 of file ftmpl_list.cc.

95{
96 ListItem<T>* cur = l.last;
97 if ( cur )
98 {
99 first = new ListItem<T>( *(cur->item), 0, 0 );
100 last = first;
101 cur = cur->prev;
102 while ( cur )
103 {
104 first = new ListItem<T>( *(cur->item), first, 0 );
105 first->next->prev = first;
106 cur = cur->prev;
107 }
108 _length = l._length;
109 }
110 else
111 {
112 first = last = 0;
113 _length = 0;
114 }
115}
int l
Definition: cfEzgcd.cc:100
ListItem * prev
Definition: ftmpl_list.h:32
T * item
Definition: ftmpl_list.h:33

◆ List() [3/3]

template<class T >
List< T >::List ( const T t)

Definition at line 119 of file ftmpl_list.cc.

120{
121 first = last = new ListItem<T>( t, 0, 0 );
122 _length = 1;
123}

◆ ~List()

template<class T >
List< T >::~List

Definition at line 127 of file ftmpl_list.cc.

128{
129 ListItem<T> *dummy;
130 while ( first )
131 {
132 dummy = first;
133 first = first->next;
134 delete dummy;
135 }
136}

Member Function Documentation

◆ append()

template<class T >
void List< T >::append ( const T t)

Definition at line 256 of file ftmpl_list.cc.

257{
258 last = new ListItem<T>( t, 0, last );
259 if ( first )
260 last->prev->next = last;
261 first = ( first ) ? first : last;
262 _length++;
263}

◆ getFirst()

template<class T >
T List< T >::getFirst

Definition at line 279 of file ftmpl_list.cc.

280{
281 ASSERT( first, "List: no item available" );
282 return first->getItem();
283}
#define ASSERT(expression, message)
Definition: cf_assert.h:99

◆ getLast()

template<class T >
T List< T >::getLast

Definition at line 309 of file ftmpl_list.cc.

310{
311 ASSERT( first, "List: no item available" );
312 return last->getItem();
313}

◆ insert() [1/3]

template<class T >
void List< T >::insert ( const T t)

Definition at line 193 of file ftmpl_list.cc.

194{
195 first = new ListItem<T>( t, first, 0 );
196 if ( last )
197 first->next->prev = first;
198 last = ( last ) ? last : first;
199 _length++;
200}

◆ insert() [2/3]

template<class T >
void List< T >::insert ( const T t,
int(*)(const T &, const T &)  cmpf 
)

Definition at line 204 of file ftmpl_list.cc.

205{
206 if ( ! first || cmpf( *first->item, t ) > 0 )
207 insert( t );
208 else if ( cmpf( *last->item, t ) < 0 )
209 append( t );
210 else
211 {
212 ListItem<T> * cursor = first;
213 int c;
214 while ( (c = cmpf( *cursor->item, t )) < 0 )
215 cursor = cursor->next;
216 if ( c == 0 )
217 *cursor->item = t;
218 else
219 {
220 cursor = cursor->prev;
221 cursor->next = new ListItem<T>( t, cursor->next, cursor );
222 cursor->next->next->prev = cursor->next;
223 _length++;
224 }
225 }
226}
ListItem * next
Definition: ftmpl_list.h:31
void append(const T &)
Definition: ftmpl_list.cc:256
void insert(const T &)
Definition: ftmpl_list.cc:193

◆ insert() [3/3]

template<class T >
void List< T >::insert ( const T t,
int(*)(const T &, const T &)  cmpf,
void(*)(T &, const T &)  insf 
)

Definition at line 230 of file ftmpl_list.cc.

231{
232 if ( ! first || cmpf( *first->item, t ) > 0 )
233 insert( t );
234 else if ( cmpf( *last->item, t ) < 0 )
235 append( t );
236 else
237 {
238 ListItem<T> * cursor = first;
239 int c;
240 while ( (c = cmpf( *cursor->item, t )) < 0 )
241 cursor = cursor->next;
242 if ( c == 0 )
243 insf( *cursor->item, t );
244 else
245 {
246 cursor = cursor->prev;
247 cursor->next = new ListItem<T>( t, cursor->next, cursor );
248 cursor->next->next->prev = cursor->next;
249 _length++;
250 }
251 }
252}

◆ isEmpty()

template<class T >
int List< T >::isEmpty

Definition at line 267 of file ftmpl_list.cc.

268{
269 return ( first == 0 );
270}

◆ length()

template<class T >
int List< T >::length

Definition at line 273 of file ftmpl_list.cc.

274{
275 return _length;
276}

◆ operator=()

template<class T >
List< T > & List< T >::operator= ( const List< T > &  l)

Definition at line 140 of file ftmpl_list.cc.

141{
142 if ( this != &l )
143 {
144 ListItem<T> *dummy;
145 while ( first )
146 {
147 dummy = first;
148 first = first->next;
149 delete dummy;
150 }
151 ListItem<T>* cur = l.last;
152 if ( cur )
153 {
154 first = new ListItem<T>( *(cur->item), 0, 0 );
155 last = first;
156 cur = cur->prev;
157 while ( cur )
158 {
159 first = new ListItem<T>( *(cur->item), first, 0 );
160 first->next->prev = first;
161 cur = cur->prev;
162 }
163 _length = l._length;
164 }
165 else
166 {
167 first = last = 0;
168 _length = 0;
169 }
170 _length = l._length;
171 }
172 return *this;
173}

◆ print()

template<class T >
void List< T >::print ( OSTREAM os) const

Definition at line 366 of file ftmpl_list.cc.

367{
368 ListItem<T> *cur = first;
369 os << "( ";
370 while ( cur )
371 {
372 cur->print( os );
373 if ( (cur = cur->getNext()) )
374 os << ", ";
375 }
376 os << " )";
377}
ListItem< T > * getNext()
Definition: ftmpl_list.cc:53
void print(OSTREAM &)
Definition: ftmpl_list.cc:74

◆ removeFirst()

template<class T >
void List< T >::removeFirst

Definition at line 287 of file ftmpl_list.cc.

288{
289 if ( first )
290 {
291 _length--;
292 if ( first == last )
293 {
294 delete first;
295 first = last = 0;
296 }
297 else
298 {
299 ListItem<T> *dummy = first;
300 first->next->prev = 0;
301 first = first->next;
302 delete dummy;
303 }
304 }
305}

◆ removeLast()

template<class T >
void List< T >::removeLast

Definition at line 317 of file ftmpl_list.cc.

318{
319 if ( last )
320 {
321 _length--;
322 if ( first == last )
323 {
324 delete last;
325 first = last = 0;
326 }
327 else
328 {
329 ListItem<T> *dummy = last;
330 last->prev->next = 0;
331 last = last->prev;
332 delete dummy;
333 }
334 }
335}

◆ sort()

template<class T >
void List< T >::sort ( int(*)(const T &, const T &)  swapit)

Definition at line 339 of file ftmpl_list.cc.

340{
341 if ( first != last )
342 {
343 int swap;
344 do
345 {
346 swap = 0;
347 ListItem<T> *cur = first;
348 while ( cur->next )
349 {
350 if ( swapit( *(cur->item), *(cur->next->item) ) )
351 {
352 T* dummy = cur->item;
353 cur->item = cur->next->item;
354 cur->next->item = dummy;
355 swap = 1;
356 }
357 cur = cur->next;
358 }
359 } while (swap);
360 }
361}
#define swap(_i, _j)
STATIC_VAR jList * T
Definition: janet.cc:30

Friends And Related Function Documentation

◆ ListIterator< T >

template<class T >
friend class ListIterator< T >
friend

Definition at line 76 of file ftmpl_list.h.

◆ operator

template<class T >
OSTREAM & operator ( OSTREAM os,
const List< T > &  l 
)
friend

Field Documentation

◆ _length

template<class T >
int List< T >::_length
private

Definition at line 56 of file ftmpl_list.h.

◆ first

template<class T >
ListItem<T>* List< T >::first
private

Definition at line 54 of file ftmpl_list.h.

◆ last

template<class T >
ListItem<T>* List< T >::last
private

Definition at line 55 of file ftmpl_list.h.


The documentation for this class was generated from the following files: