Home | History | Annotate | Line # | Download | only in make
lst.c revision 1.85
      1 /* $NetBSD: lst.c,v 1.85 2020/10/24 09:03:54 rillig Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Adam de Boor.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include "make.h"
     36 
     37 MAKE_RCSID("$NetBSD: lst.c,v 1.85 2020/10/24 09:03:54 rillig Exp $");
     38 
     39 static ListNode *
     40 LstNodeNew(ListNode *prev, ListNode *next, void *datum)
     41 {
     42     ListNode *node = bmake_malloc(sizeof *node);
     43     node->prev = prev;
     44     node->next = next;
     45     node->datum = datum;
     46     return node;
     47 }
     48 
     49 /* Create and initialize a new, empty list. */
     50 List *
     51 Lst_New(void)
     52 {
     53     List *list = bmake_malloc(sizeof *list);
     54 
     55     list->first = NULL;
     56     list->last = NULL;
     57 
     58     return list;
     59 }
     60 
     61 /* Duplicate an entire list, usually by copying the datum pointers.
     62  * If copyProc is given, that function is used to create the new datum from the
     63  * old datum, usually by creating a copy of it. */
     64 List *
     65 Lst_Copy(List *list, LstCopyProc copyProc)
     66 {
     67     List *newList;
     68     ListNode *node;
     69 
     70     newList = Lst_New();
     71 
     72     for (node = list->first; node != NULL; node = node->next) {
     73 	void *datum = copyProc != NULL ? copyProc(node->datum) : node->datum;
     74 	Lst_Append(newList, datum);
     75     }
     76 
     77     return newList;
     78 }
     79 
     80 /* Free a list and all its nodes. The list data itself are not freed though. */
     81 void
     82 Lst_Free(List *list)
     83 {
     84     ListNode *node;
     85     ListNode *next;
     86 
     87     for (node = list->first; node != NULL; node = next) {
     88 	next = node->next;
     89 	free(node);
     90     }
     91 
     92     free(list);
     93 }
     94 
     95 /* Destroy a list and free all its resources. The freeProc is called with the
     96  * datum from each node in turn before the node is freed. */
     97 void
     98 Lst_Destroy(List *list, LstFreeProc freeProc)
     99 {
    100     ListNode *node;
    101     ListNode *next;
    102 
    103     for (node = list->first; node != NULL; node = next) {
    104 	next = node->next;
    105 	freeProc(node->datum);
    106 	free(node);
    107     }
    108 
    109     free(list);
    110 }
    111 
    112 /*
    113  * Functions to modify a list
    114  */
    115 
    116 /* Insert a new node with the given piece of data before the given node in the
    117  * given list. */
    118 void
    119 Lst_InsertBefore(List *list, ListNode *node, void *datum)
    120 {
    121     ListNode *newNode;
    122 
    123     assert(datum != NULL);
    124 
    125     newNode = LstNodeNew(node->prev, node, datum);
    126 
    127     if (node->prev != NULL)
    128 	node->prev->next = newNode;
    129     node->prev = newNode;
    130 
    131     if (node == list->first)
    132 	list->first = newNode;
    133 }
    134 
    135 /* Add a piece of data at the start of the given list. */
    136 void
    137 Lst_Prepend(List *list, void *datum)
    138 {
    139     ListNode *node;
    140 
    141     assert(datum != NULL);
    142 
    143     node = LstNodeNew(NULL, list->first, datum);
    144 
    145     if (list->first == NULL) {
    146 	list->first = node;
    147 	list->last = node;
    148     } else {
    149 	list->first->prev = node;
    150 	list->first = node;
    151     }
    152 }
    153 
    154 /* Add a piece of data at the end of the given list. */
    155 void
    156 Lst_Append(List *list, void *datum)
    157 {
    158     ListNode *node;
    159 
    160     assert(datum != NULL);
    161 
    162     node = LstNodeNew(list->last, NULL, datum);
    163 
    164     if (list->last == NULL) {
    165 	list->first = node;
    166 	list->last = node;
    167     } else {
    168 	list->last->next = node;
    169 	list->last = node;
    170     }
    171 }
    172 
    173 /* Remove the given node from the given list.
    174  * The datum stored in the node must be freed by the caller, if necessary. */
    175 void
    176 Lst_Remove(List *list, ListNode *node)
    177 {
    178     /* unlink it from its neighbors */
    179     if (node->next != NULL)
    180 	node->next->prev = node->prev;
    181     if (node->prev != NULL)
    182 	node->prev->next = node->next;
    183 
    184     /* unlink it from the list */
    185     if (list->first == node)
    186 	list->first = node->next;
    187     if (list->last == node)
    188 	list->last = node->prev;
    189 }
    190 
    191 /* Replace the datum in the given node with the new datum. */
    192 void
    193 LstNode_Set(ListNode *node, void *datum)
    194 {
    195     assert(datum != NULL);
    196 
    197     node->datum = datum;
    198 }
    199 
    200 /* Replace the datum in the given node to NULL.
    201  * Having NULL values in a list is unusual though. */
    202 void
    203 LstNode_SetNull(ListNode *node)
    204 {
    205     node->datum = NULL;
    206 }
    207 
    208 
    209 /*
    210  * Functions for entire lists
    211  */
    212 
    213 /* Return the first node from the list for which the match function returns
    214  * TRUE, or NULL if none of the nodes matched. */
    215 ListNode *
    216 Lst_Find(List *list, LstFindProc match, const void *matchArgs)
    217 {
    218     return Lst_FindFrom(list, list->first, match, matchArgs);
    219 }
    220 
    221 /* Return the first node from the list, starting at the given node, for which
    222  * the match function returns TRUE, or NULL if none of the nodes matches.
    223  *
    224  * The start node may be NULL, in which case nothing is found. */
    225 ListNode *
    226 Lst_FindFrom(List *list, ListNode *node,
    227 	     LstFindProc match, const void *matchArgs)
    228 {
    229     ListNode *tln;
    230 
    231     assert(list != NULL);
    232     assert(match != NULL);
    233 
    234     for (tln = node; tln != NULL; tln = tln->next)
    235 	if (match(tln->datum, matchArgs))
    236 	    return tln;
    237 
    238     return NULL;
    239 }
    240 
    241 /* Return the first node that contains the given datum, or NULL. */
    242 ListNode *
    243 Lst_FindDatum(List *list, const void *datum)
    244 {
    245     ListNode *node;
    246 
    247     assert(datum != NULL);
    248 
    249     for (node = list->first; node != NULL; node = node->next)
    250 	if (node->datum == datum)
    251 	    return node;
    252 
    253     return NULL;
    254 }
    255 
    256 int
    257 Lst_ForEachUntil(List *list, LstActionUntilProc proc, void *procData)
    258 {
    259     ListNode *node;
    260     int result = 0;
    261 
    262     for (node = list->first; node != NULL; node = node->next) {
    263 	result = proc(node->datum, procData);
    264 	if (result != 0)
    265 	    break;
    266     }
    267     return result;
    268 }
    269 
    270 /* Move all nodes from list2 to the end of list1.
    271  * List2 is destroyed and freed. */
    272 void
    273 Lst_MoveAll(List *list1, List *list2)
    274 {
    275     if (list2->first != NULL) {
    276 	list2->first->prev = list1->last;
    277 	if (list1->last != NULL)
    278 	    list1->last->next = list2->first;
    279 	else
    280 	    list1->first = list2->first;
    281 
    282 	list1->last = list2->last;
    283     }
    284     free(list2);
    285 }
    286 
    287 /* Copy the element data from src to the start of dst. */
    288 void
    289 Lst_PrependAll(List *dst, List *src)
    290 {
    291     ListNode *node;
    292     for (node = src->last; node != NULL; node = node->prev)
    293 	Lst_Prepend(dst, node->datum);
    294 }
    295 
    296 /* Copy the element data from src to the end of dst. */
    297 void
    298 Lst_AppendAll(List *dst, List *src)
    299 {
    300     ListNode *node;
    301     for (node = src->first; node != NULL; node = node->next)
    302 	Lst_Append(dst, node->datum);
    303 }
    304 
    305 /*
    306  * for using the list as a queue
    307  */
    308 
    309 /* Add the datum to the tail of the given list. */
    310 void
    311 Lst_Enqueue(List *list, void *datum)
    312 {
    313     Lst_Append(list, datum);
    314 }
    315 
    316 /* Remove and return the datum at the head of the given list. */
    317 void *
    318 Lst_Dequeue(List *list)
    319 {
    320     void *datum = list->first->datum;
    321     Lst_Remove(list, list->first);
    322     assert(datum != NULL);	/* since NULL would mean end of the list */
    323     return datum;
    324 }
    325 
    326 void
    327 Vector_Init(Vector *v)
    328 {
    329     v->len = 0;
    330     v->cap = 10;
    331     v->items = bmake_malloc(v->cap * sizeof v->items[0]);
    332 }
    333 
    334 Boolean Vector_IsEmpty(Vector *v)
    335 {
    336     return v->len == 0;
    337 }
    338 
    339 void Vector_Push(Vector *v, void *datum)
    340 {
    341     if (v->len >= v->cap) {
    342 	v->cap *= 2;
    343 	v->items = bmake_realloc(v->items,
    344 				 v->cap * sizeof v->items[0]);
    345     }
    346     v->items[v->len] = datum;
    347     v->len++;
    348 }
    349 
    350 void *Vector_Pop(Vector *v)
    351 {
    352     void *datum;
    353 
    354     assert(v->len > 0);
    355     v->len--;
    356     datum = v->items[v->len];
    357 #ifdef CLEANUP
    358     v->items[v->len] = NULL;
    359 #endif
    360     return datum;
    361 }
    362 
    363 void Vector_Done(Vector *v)
    364 {
    365     free(v->items);
    366 }
    367