Debian Codegraph @

inc/dlist.h

raw symbols SCIP
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 #ifndef FONTFORGE_DLIST_H
29 #define FONTFORGE_DLIST_H
30
31 /**
32 * Doubly linked list abstraction. Putting a full member of this
33 * struct first in another struct means you can treat it as a
34 * dlinkedlist. You can have a struct in many lists simply by
35 * embedding another dlistnode member and handing a pointer to that
36 * member to the dlist() helper functions. Double linking has big
37 * advantages in removal of single elements where you do not need to
38 * rescan to find removeme->prev;
39 */
40 struct dlistnode {
41 struct dlistnode* next;
42 struct dlistnode* prev;
43 };
44
45 /**
46 * DEVELOPERS: make sure the start of this struct is compatible with
47 * dlistnode. While I could use the dlistnode as a first member, using
48 * a copy of the members in the same order as dlistnode has them
49 * allows callers using this struct a bit simpler access.
50 *
51 * While one can embed a dlistnode member into a struct to create
52 * linked lists, sometimes you want to return a splice of one of those
53 * lists. For example, if you have a double linked list of all your
54 * hotkeys, you might like to return only the ones that have a
55 * modifier of the Control key. You want to leave the hotkey structs
56 * in their original list, but create a new kust that references just
57 * a desired selection of objects.
58 *
59 * In other words, if you have some data you want to return in a
60 * double linked list, then use this node type. You can build one up
61 * using dlist_pushfront_external() and the caller can free that list
62 * using dlist_free_external(). Any of the foreach() functions will
63 * work to iterate a list of dlistnodeExternal as this list is
64 * identical to a dlistnode with an extra ptr payload.
65 */
66 struct dlistnodeExternal {
67 struct dlistnode* next;
68 struct dlistnode* prev;
69 void* ptr;
70 };
71
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75
76 /**
77 * Push the node onto the head of the list
78 */
79 extern void dlist_pushfront( struct dlistnode** list, struct dlistnode* node );
80
81 /**
82 * Take the last node off the list and return it. If the list is empty, return 0.
83 */
84 struct dlistnode* dlist_popback( struct dlistnode** list );
85
86 /**
87 * the number of nodes in the list
88 */
89 extern int dlist_size( struct dlistnode** list );
90
91 /**
92 * is the list empty
93 */
94 extern int dlist_isempty( struct dlistnode** list );
95
96 /**
97 * Remove the node from the list. The node itself is not free()ed.
98 * That is still up to the caller. All this function does is preserve
99 * the list structure without the node being in it.
100 */
101 extern void dlist_erase( struct dlistnode** list, struct dlistnode* node );
102 typedef void (*dlist_foreach_func_type)( struct dlistnode* );
103
104 /**
105 * Call func for every node in the list. This is a defensive
106 * implementation, if you want to remove a node from the list inside
107 * func() that is perfectly fine.
108 */
109 extern void dlist_foreach( struct dlistnode** list, dlist_foreach_func_type func );
110 typedef void (*dlist_foreach_udata_func_type)( struct dlistnode*, void* udata );
111
112 /**
113 * Like dlist_foreach(), defensive coding still, but the udata pointer
114 * is passed back to your visitor function.
115 */
116 extern void dlist_foreach_udata( struct dlistnode** list, dlist_foreach_udata_func_type func, void* udata );
117
118 /**
119 * Like dlist_foreach_udata() but nodes are visited in reverse order.
120 */
121 extern void dlist_foreach_reverse_udata( struct dlistnode** list, dlist_foreach_udata_func_type func, void* udata );
122
123 /**
124 * Assuming list is an externalNode list, push a newly allocated list node with
125 * a dlistnodeExternal.ptr = ptr passed.
126 */
127 extern void dlist_pushfront_external( struct dlistnode** list, void* ptr );
128
129 /**
130 * Free a list of externalNode type. The externalNode memory is
131 * free()ed, whatever externalNode.ptr is pointing to is not free()ed.
132 */
133 extern void dlist_free_external( struct dlistnode** list );
134
135
136 typedef void (*dlist_visitor_func_type)( struct dlistnode* );
137
138 /**
139 * To create a list of bounded length, use this function. Limit is the
140 * maximum length the list can reach. If list nodes have to be removed
141 * to be under this limit then "f" is used as a callback to free list
142 * nodes. This allows application specific freeing of a list node, and
143 * the ability to maintain a limit on the length of a list as a simple
144 * one line call.
145 *
146 * The current implementation expects you to only be trimming one or
147 * two entries at a time. It will still work for trimming 100 entries
148 * at a single time, but might not be quite as optimized for that case
149 * as it could be.
150 */
151 extern void dlist_trim_to_limit( struct dlistnode** list, int limit, dlist_visitor_func_type f );
152
153 #ifdef __cplusplus
154 }
155 #endif
156
157 #endif /* FONTFORGE_DLIST_H */
158