2 * This file Copyright (C) Mnemosyne LLC
4 * This file is licensed by the GPL version 2. Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
10 * $Id: list.c 11709 2011-01-19 13:48:47Z jordan $
13 #include "transmission.h"
20 return tr_new0( tr_list
, 1 );
24 node_free( tr_list
* node
)
34 tr_list_free( tr_list
** list
,
35 TrListForeachFunc data_free_func
)
39 tr_list
*node
= *list
;
40 *list
= ( *list
)->next
;
42 data_free_func( node
->data
);
48 tr_list_prepend( tr_list
** list
,
51 tr_list
* node
= node_alloc ( );
56 ( *list
)->prev
= node
;
61 tr_list_append( tr_list
** list
,
64 tr_list
* node
= node_alloc( );
81 tr_list_find_data( tr_list
* list
,
84 for( ; list
; list
= list
->next
)
85 if( list
->data
== data
)
92 tr_list_remove_node( tr_list
** list
,
96 tr_list
* prev
= node
? node
->prev
: NULL
;
97 tr_list
* next
= node
? node
->next
: NULL
;
99 if( prev
) prev
->next
= next
;
100 if( next
) next
->prev
= prev
;
101 if( *list
== node
) *list
= next
;
102 data
= node
? node
->data
: NULL
;
108 tr_list_pop_front( tr_list
** list
)
114 ret
= ( *list
)->data
;
115 tr_list_remove_node( list
, *list
);
121 tr_list_remove_data( tr_list
** list
,
124 return tr_list_remove_node( list
, tr_list_find_data( *list
, data
) );
128 tr_list_remove( tr_list
** list
,
130 TrListCompareFunc compare_func
)
132 return tr_list_remove_node( list
, tr_list_find( *list
, b
, compare_func
) );
136 tr_list_find( tr_list
* list
,
138 TrListCompareFunc func
)
140 for( ; list
; list
= list
->next
)
141 if( !func( list
->data
, b
) )
148 tr_list_insert_sorted( tr_list
** list
,
150 TrListCompareFunc compare
)
152 /* find l, the node that we'll insert this data before */
155 for( l
= *list
; l
!= NULL
; l
= l
->next
)
157 const int c
= (compare
)( data
, l
->data
);
163 tr_list_append( list
, data
);
164 else if( l
== *list
)
165 tr_list_prepend( list
, data
);
167 tr_list
* node
= node_alloc( );
169 node
->prev
= l
->prev
;
171 node
->prev
->next
= node
;
172 node
->next
->prev
= node
;
177 tr_list_size( const tr_list
* list
)