1 /** 2 * Interface to the C linked list type. 3 * 4 * List is a complete package of functions to deal with singly linked 5 * lists of pointers or integers. 6 * Features: 7 * 1. Uses mem package. 8 * 2. Has loop-back tests. 9 * 3. Each item in the list can have multiple predecessors, enabling 10 * different lists to 'share' a common tail. 11 * 12 * Copyright: Copyright (C) 1986-1990 by Northwest Software 13 * Copyright (c) 1999-2016 by Digital Mars, All Rights Reserved 14 * Authors: $(LINK2 http://www.digitalmars.com, Walter Bright) 15 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) 16 * Source: $(DMDSRC backend/tk/_dlist.d) 17 */ 18 19 module tk.dlist; 20 21 extern (C++): 22 nothrow: 23 @nogc: 24 25 /* **************** TYPEDEFS AND DEFINES ****************** */ 26 27 struct LIST 28 { 29 /* Do not access items in this struct directly, use the */ 30 /* functions designed for that purpose. */ 31 LIST* next; /* next element in list */ 32 int count; /* when 0, element may be deleted */ 33 union 34 { void* ptr; /* data pointer */ 35 int data; 36 } 37 } 38 39 alias LIST* list_t; /* pointer to a list entry */ 40 41 /* FPNULL is a null function pointer designed to be an argument to 42 * list_free(). 43 */ 44 45 alias void function(void*) list_free_fp; 46 47 enum FPNULL = cast(list_free_fp)null; 48 49 /* **************** PUBLIC VARIABLES ********************* */ 50 51 extern int list_inited; /* != 0 if list package is initialized */ 52 53 /* **************** PUBLIC FUNCTIONS ********************* */ 54 55 /******************************** 56 * Create link to existing list, that is, share the list with 57 * somebody else. 58 * 59 * Returns: 60 * pointer to that list entry. 61 */ 62 63 list_t list_link(list_t list) 64 { 65 if (list) 66 ++list.count; 67 return list; 68 } 69 70 /******************************** 71 * Returns: 72 * pointer to next entry in list. 73 */ 74 75 list_t list_next(list_t list) { return list.next; } 76 77 /******************************** 78 * Returns: 79 * pointer to previous item in list. 80 */ 81 82 list_t list_prev(list_t start, list_t list); 83 84 /******************************** 85 * Returns: 86 * ptr from list entry. 87 */ 88 89 void* list_ptr(list_t list) { return list.ptr; } 90 91 /******************************** 92 * Returns: 93 * integer item from list entry. 94 */ 95 96 int list_data(list_t list) { return list.data; } 97 98 /******************************** 99 * Append integer item to list. 100 */ 101 102 void list_appenddata(list_t* plist, int d) 103 { 104 list_append(plist, null).data = d; 105 } 106 107 /******************************** 108 * Prepend integer item to list. 109 */ 110 111 void list_prependdata(list_t *plist,int d) 112 { 113 list_prepend(plist, null).data = d; 114 } 115 116 /********************** 117 * Initialize list package. 118 * Output: 119 * list_inited = 1 120 */ 121 122 void list_init(); 123 124 /******************* 125 * Terminate list package. 126 * Output: 127 * list_inited = 0 128 */ 129 130 void list_term(); 131 132 /******************** 133 * Free list. 134 * Params: 135 * plist = Pointer to list to free 136 * freeptr = Pointer to freeing function for the data pointer 137 * (use FPNULL if none) 138 * Output: 139 * *plist is null 140 */ 141 142 void list_free(list_t* plist, list_free_fp freeptr); 143 144 extern (C++) void list_free(list_t *l); 145 146 /*************************** 147 * Remove ptr from the list pointed to by *plist. 148 * Output: 149 * *plist is updated to be the start of the new list 150 * Returns: 151 * null if *plist is null 152 * otherwise ptr 153 */ 154 155 void* list_subtract(list_t* plist, void* ptr); 156 157 /*************************** 158 * Remove first element in list pointed to by *plist. 159 * Returns: 160 * First element, null if *plist is null 161 */ 162 163 void* list_pop(list_t* plist) 164 { 165 return list_subtract(plist, list_ptr(*plist)); 166 } 167 168 /************************* 169 * Append ptr to *plist. 170 * Returns: 171 * pointer to list item created. 172 * null if out of memory 173 */ 174 175 list_t list_append(list_t* plist, void* ptr); 176 list_t list_append_debug(list_t* plist, void* ptr, const(char)* file, int line); 177 178 /************************* 179 * Prepend ptr to *plist. 180 * Returns: 181 * pointer to list item created (which is also the start of the list). 182 * null if out of memory 183 */ 184 185 list_t list_prepend(list_t* plist, void* ptr); 186 187 /************************* 188 * Count up and return number of items in list. 189 * Returns: 190 * # of entries in list 191 */ 192 193 int list_nitems(list_t list); 194 195 /************************* 196 * Returns: 197 * nth list entry in list. 198 */ 199 200 list_t list_nth(list_t list, int n); 201 202 /*********************** 203 * Returns: 204 * last list entry in list. 205 */ 206 207 list_t list_last(list_t list); 208 209 /*********************** 210 * Copy a list and return it. 211 */ 212 213 list_t list_copy(list_t list); 214 215 /************************ 216 * Compare two lists. 217 * Returns: 218 * If they have the same ptrs, return 1 else 0. 219 */ 220 221 int list_equal(list_t list1, list_t list2); 222 223 /************************ 224 * Compare two lists using the comparison function fp. 225 * The comparison function is the same as used for qsort(). 226 * Returns: 227 * If they compare equal, return 0 else value returned by fp. 228 */ 229 230 int list_cmp(list_t list1, list_t list2, int function(void*, void*) fp); 231 232 /************************* 233 * Search for ptr in list. 234 * Returns: 235 * If found, return list entry that it is, else null. 236 */ 237 238 list_t list_inlist(list_t list, void* ptr); 239 240 /************************* 241 * Concatenate two lists (l2 appended to l1). 242 * Output: 243 * *pl1 updated to be start of concatenated list. 244 * Returns: 245 * *pl1 246 */ 247 248 list_t list_cat(list_t *pl1, list_t l2); 249 250 /************************* 251 * Build a list out of the null-terminated argument list. 252 * Returns: 253 * generated list 254 */ 255 256 list_t list_build(void* p, ...); 257 258 /*************************************** 259 * Apply a function fp to each member of a list. 260 */ 261 262 void list_apply(list_t* plist, void function(void*) fp); 263 264 /******************************************** 265 * Reverse a list. 266 */ 267 268 list_t list_reverse(list_t); 269 270 /********************************************** 271 * Copy list of pointers into an array of pointers. 272 */ 273 274 void list_copyinto(list_t, void*); 275 276 /********************************************** 277 * Insert item into list at nth position. 278 */ 279 280 list_t list_insert(list_t*, void*, int n); 281