|
| 1 | +/* Copyright (C) 2012 by Ben Martin */ |
| 2 | +/* |
| 3 | + * Redistribution and use in source and binary forms, with or without |
| 4 | + * modification, are permitted provided that the following conditions are met: |
| 5 | +
|
| 6 | + * Redistributions of source code must retain the above copyright notice, this |
| 7 | + * list of conditions and the following disclaimer. |
| 8 | +
|
| 9 | + * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | + * this list of conditions and the following disclaimer in the documentation |
| 11 | + * and/or other materials provided with the distribution. |
| 12 | +
|
| 13 | + * The name of the author may not be used to endorse or promote products |
| 14 | + * derived from this software without specific prior written permission. |
| 15 | +
|
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | +#include <stdio.h> /* for NULL */ |
| 28 | +#include "dlist.h" |
| 29 | + |
| 30 | +void dlist_pushfront( struct dlistnode** list, struct dlistnode* node ) { |
| 31 | + if( *list ) { |
| 32 | + node->next = *list; |
| 33 | + node->next->prev = node; |
| 34 | + } |
| 35 | + *list = node; |
| 36 | +} |
| 37 | + |
| 38 | +int dlist_size( struct dlistnode** list ) { |
| 39 | + struct dlistnode* node = *list; |
| 40 | + int ret = 0; |
| 41 | + for( ; node; node=node->next ) { |
| 42 | + ret++; |
| 43 | + } |
| 44 | + return ret; |
| 45 | +} |
| 46 | + |
| 47 | +int dlist_isempty( struct dlistnode** list ) { |
| 48 | + return *list == NULL; |
| 49 | +} |
| 50 | + |
| 51 | +void dlist_erase( struct dlistnode** list, struct dlistnode* node ) { |
| 52 | + if( !node ) |
| 53 | + return; |
| 54 | + if( *list = node ) { |
| 55 | + *list = node->next; |
| 56 | + if( node->next ) { |
| 57 | + node->next->prev = 0; |
| 58 | + } |
| 59 | + return 0; |
| 60 | + } |
| 61 | + if( node->prev ) { |
| 62 | + node->prev->next = node->next; |
| 63 | + } |
| 64 | + if( node->next ) { |
| 65 | + node->next->prev = node->prev; |
| 66 | + } |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +void dlist_foreach( struct dlistnode** list, dlist_foreach_func_type func ) |
| 71 | +{ |
| 72 | + struct dlistnode* node = *list; |
| 73 | + while( node ) { |
| 74 | + struct dlistnode* t = node; |
| 75 | + node = node->next; |
| 76 | + func( t ); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void dlist_foreach_udata( struct dlistnode** list, dlist_foreach_udata_func_type func, void* udata ) |
| 81 | +{ |
| 82 | + struct dlistnode* node = *list; |
| 83 | + while( node ) { |
| 84 | + struct dlistnode* t = node; |
| 85 | + node = node->next; |
| 86 | + func( t, udata ); |
| 87 | + } |
| 88 | +} |
| 89 | + |
0 commit comments