source: trunk/libtransmission/list.c @ 6425

Last change on this file since 6425 was 6425, checked in by charles, 13 years ago

minor text cleanup

  • Property svn:keywords set to Date Rev Author Id
File size: 3.6 KB
Line 
1/*
2 * This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com>
3 *
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.
9 *
10 * $Id: list.c 6425 2008-08-01 16:43:22Z charles $
11 */
12
13#include "transmission.h"
14#include "list.h"
15#include "utils.h"
16
17static tr_list*
18node_alloc( void )
19{
20    return tr_new0( tr_list, 1 );
21}
22
23static void
24node_free( tr_list* node )
25{
26    tr_free( node );
27}
28
29/***
30****
31***/
32
33void
34tr_list_free( tr_list** list, TrListForeachFunc data_free_func )
35{
36    while( *list )
37    {
38        tr_list *node = *list;
39        *list = (*list)->next;
40        if( data_free_func )
41            data_free_func( node->data );
42        node_free( node );
43    }
44}
45
46void
47tr_list_prepend( tr_list ** list, void * data )
48{
49    tr_list * node = node_alloc ();
50    node->data = data;
51    node->next = *list;
52    if( *list )
53        (*list)->prev = node;
54    *list = node;
55}
56
57void
58tr_list_append( tr_list ** list, void * data )
59{
60    tr_list * node = node_alloc( );
61    node->data = data;
62    if( !*list )
63        *list = node;
64    else {
65        tr_list * l = *list;
66        while( l->next )
67            l = l->next;
68        l->next = node;
69        node->prev = l;
70    }
71}
72
73void
74tr_list_insert_sorted( tr_list ** list,
75                       void       * data,
76                       int          compare(const void*,const void*) )
77{
78    /* find l, the node that we'll insert this data before */
79    tr_list * l;
80    for( l=*list; l!=NULL; l=l->next ) {
81        const int c = (compare)( data, l->data );
82        if( c <= 0 )
83            break;
84    }
85
86    if( l == NULL)
87        tr_list_append( list, data );
88    else if( l == *list )
89        tr_list_prepend( list, data );
90    else {
91        tr_list * node = node_alloc( );
92        node->data = data;
93        if( l->prev ) { node->prev = l->prev; node->prev->next = node; }
94        node->next = l;
95        l->prev = node;
96    }
97}
98
99
100tr_list*
101tr_list_find_data ( tr_list * list, const void * data )
102{
103    for(; list; list=list->next )
104        if( list->data == data )
105            return list;
106
107    return NULL;
108}
109
110static void*
111tr_list_remove_node ( tr_list ** list, tr_list * node )
112{
113    void * data;
114    tr_list * prev = node ? node->prev : NULL;
115    tr_list * next = node ? node->next : NULL;
116    if( prev ) prev->next = next;
117    if( next ) next->prev = prev;
118    if( *list == node ) *list = next;
119    data = node ? node->data : NULL;
120    node_free( node );
121    return data;
122}
123
124void*
125tr_list_pop_front( tr_list ** list )
126{
127    void * ret = NULL;
128    if( *list )
129    {
130        ret = (*list)->data;
131        tr_list_remove_node( list, *list );
132    }
133    return ret;
134}
135
136void*
137tr_list_remove_data ( tr_list ** list, const void * data )
138{
139    return tr_list_remove_node( list, tr_list_find_data( *list, data ) );
140}
141
142void*
143tr_list_remove( tr_list         ** list,
144                const void       * b,
145                TrListCompareFunc  compare_func )
146{
147    return tr_list_remove_node( list, tr_list_find( *list, b, compare_func ) );
148}
149
150
151tr_list*
152tr_list_find ( tr_list * list , const void * b, TrListCompareFunc func )
153{
154    for( ; list; list=list->next )
155        if( !func( list->data, b ) )
156            return list;
157
158    return NULL;
159}
160
161void
162tr_list_foreach( tr_list * list, TrListForeachFunc func )
163{
164    while( list ) {
165        func( list->data );
166        list = list->next;
167    }
168}
169
170int
171tr_list_size( const tr_list * list )
172{
173    int size = 0;
174    while( list ) {
175        ++size;
176        list = list->next;
177    }
178    return size;
179}
Note: See TracBrowser for help on using the repository browser.