| 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 |
|
| 28 |
#include <fontforge-config.h> |
| 29 |
|
| 30 |
#include "dlist.h" |
| 31 |
|
| 32 |
#include <stdio.h> /* for NULL */ |
| 33 |
#include <stdlib.h> |
| 34 |
|
| 35 |
void dlist_pushfront( struct dlistnode** list, struct dlistnode* node ) { |
| 36 |
if( *list ) { |
| 37 |
node->next = *list; |
| 38 |
node->next->prev = node; |
| 39 |
} |
| 40 |
*list = node; |
| 41 |
} |
| 42 |
|
| 43 |
int dlist_size( struct dlistnode** list ) { |
| 44 |
struct dlistnode* node = *list; |
| 45 |
int ret = 0; |
| 46 |
for( ; node; node=node->next ) { |
| 47 |
ret++; |
| 48 |
} |
| 49 |
return ret; |
| 50 |
} |
| 51 |
|
| 52 |
int dlist_isempty( struct dlistnode** list ) { |
| 53 |
return *list == NULL; |
| 54 |
} |
| 55 |
|
| 56 |
void dlist_erase( struct dlistnode** list, struct dlistnode* node ) { |
| 57 |
if( !node ) |
| 58 |
return; |
| 59 |
if( *list == node ) { |
| 60 |
*list = node->next; |
| 61 |
if( node->next ) { |
| 62 |
node->next->prev = 0; |
| 63 |
} |
| 64 |
return; |
| 65 |
} |
| 66 |
if( node->prev ) { |
| 67 |
node->prev->next = node->next; |
| 68 |
} |
| 69 |
if( node->next ) { |
| 70 |
node->next->prev = node->prev; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
void dlist_foreach( struct dlistnode** list, dlist_foreach_func_type func ) |
| 75 |
{ |
| 76 |
struct dlistnode* node = *list; |
| 77 |
while( node ) { |
| 78 |
struct dlistnode* t = node; |
| 79 |
node = node->next; |
| 80 |
func( t ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
void dlist_foreach_udata( struct dlistnode** list, dlist_foreach_udata_func_type func, void* udata ) |
| 85 |
{ |
| 86 |
struct dlistnode* node = *list; |
| 87 |
while( node ) { |
| 88 |
struct dlistnode* t = node; |
| 89 |
node = node->next; |
| 90 |
func( t, udata ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
static struct dlistnode* dlist_last( struct dlistnode* node ) |
| 95 |
{ |
| 96 |
if( !node ) |
| 97 |
return node; |
| 98 |
|
| 99 |
while( node->next ) { |
| 100 |
node = node->next; |
| 101 |
} |
| 102 |
return node; |
| 103 |
} |
| 104 |
|
| 105 |
struct dlistnode* dlist_popback( struct dlistnode** list ) { |
| 106 |
struct dlistnode* node = dlist_last(*list); |
| 107 |
if( node ) |
| 108 |
dlist_erase( list, node ); |
| 109 |
return node; |
| 110 |
} |
| 111 |
|
| 112 |
void dlist_foreach_reverse_udata( struct dlistnode** list, dlist_foreach_udata_func_type func, void* udata ) |
| 113 |
{ |
| 114 |
struct dlistnode* node = dlist_last(*list); |
| 115 |
while( node ) { |
| 116 |
struct dlistnode* t = node; |
| 117 |
node = node->prev; |
| 118 |
func( t, udata ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
void dlist_pushfront_external( struct dlistnode** list, void* ptr ) |
| 123 |
{ |
| 124 |
struct dlistnodeExternal* n = (struct dlistnodeExternal*) calloc(1,sizeof(struct dlistnodeExternal)); |
| 125 |
n->ptr = ptr; |
| 126 |
dlist_pushfront( list, (struct dlistnode*)n ); |
| 127 |
} |
| 128 |
|
| 129 |
static void freenode(struct dlistnode* node ) |
| 130 |
{ |
| 131 |
free(node); |
| 132 |
} |
| 133 |
|
| 134 |
void dlist_free_external( struct dlistnode** list ) |
| 135 |
{ |
| 136 |
if( !list || !(*list) ) |
| 137 |
return; |
| 138 |
dlist_foreach( list, freenode ); |
| 139 |
} |
| 140 |
|
| 141 |
void dlist_trim_to_limit( struct dlistnode** list, int limit, dlist_visitor_func_type f ) |
| 142 |
{ |
| 143 |
int sz = dlist_size( list ); |
| 144 |
while( sz >= limit ) { |
| 145 |
struct dlistnode* node = dlist_popback( list ); |
| 146 |
f(node); |
| 147 |
freenode(node); |
| 148 |
sz = dlist_size( list ); |
| 149 |
} |
| 150 |
} |
| 151 |
|