2021-01-14 01:24:34 +00:00
|
|
|
/* $NetBSD: lst.c,v 1.102 2020/12/30 10:03:16 rillig Exp $ */
|
2020-09-05 16:11:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1988, 1989, 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Adam de Boor.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
#include "make.h"
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
MAKE_RCSID("$NetBSD: lst.c,v 1.102 2020/12/30 10:03:16 rillig Exp $");
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
static ListNode *
|
|
|
|
LstNodeNew(ListNode *prev, ListNode *next, void *datum)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
ListNode *ln = bmake_malloc(sizeof *ln);
|
|
|
|
|
|
|
|
ln->prev = prev;
|
|
|
|
ln->next = next;
|
|
|
|
ln->datum = datum;
|
|
|
|
|
|
|
|
return ln;
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create and initialize a new, empty list. */
|
2020-11-07 19:39:21 +00:00
|
|
|
List *
|
|
|
|
Lst_New(void)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
List *list = bmake_malloc(sizeof *list);
|
|
|
|
Lst_Init(list);
|
|
|
|
return list;
|
|
|
|
}
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
void
|
|
|
|
Lst_Done(List *list)
|
|
|
|
{
|
|
|
|
ListNode *ln, *next;
|
|
|
|
|
|
|
|
for (ln = list->first; ln != NULL; ln = next) {
|
|
|
|
next = ln->next;
|
|
|
|
free(ln);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Lst_DoneCall(List *list, LstFreeProc freeProc)
|
|
|
|
{
|
|
|
|
ListNode *ln, *next;
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
for (ln = list->first; ln != NULL; ln = next) {
|
|
|
|
next = ln->next;
|
|
|
|
freeProc(ln->datum);
|
|
|
|
free(ln);
|
|
|
|
}
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Free a list and all its nodes. The node data are not freed though. */
|
2020-09-05 16:11:04 +00:00
|
|
|
void
|
2020-11-07 19:39:21 +00:00
|
|
|
Lst_Free(List *list)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
Lst_Done(list);
|
|
|
|
free(list);
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Destroy a list and free all its resources. The freeProc is called with the
|
|
|
|
* datum from each node in turn before the node is freed.
|
|
|
|
*/
|
2020-09-05 16:11:04 +00:00
|
|
|
void
|
2020-11-07 19:39:21 +00:00
|
|
|
Lst_Destroy(List *list, LstFreeProc freeProc)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
Lst_DoneCall(list, freeProc);
|
|
|
|
free(list);
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Insert a new node with the datum before the given node. */
|
2020-09-05 16:11:04 +00:00
|
|
|
void
|
2020-11-20 03:54:37 +00:00
|
|
|
Lst_InsertBefore(List *list, ListNode *ln, void *datum)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
ListNode *newNode;
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
assert(datum != NULL);
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
newNode = LstNodeNew(ln->prev, ln, datum);
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (ln->prev != NULL)
|
|
|
|
ln->prev->next = newNode;
|
|
|
|
ln->prev = newNode;
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (ln == list->first)
|
|
|
|
list->first = newNode;
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a piece of data at the start of the given list. */
|
|
|
|
void
|
2020-11-07 19:39:21 +00:00
|
|
|
Lst_Prepend(List *list, void *datum)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
ListNode *ln;
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
assert(datum != NULL);
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
ln = LstNodeNew(NULL, list->first, datum);
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (list->first == NULL) {
|
|
|
|
list->first = ln;
|
|
|
|
list->last = ln;
|
|
|
|
} else {
|
|
|
|
list->first->prev = ln;
|
|
|
|
list->first = ln;
|
|
|
|
}
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a piece of data at the end of the given list. */
|
|
|
|
void
|
2020-11-07 19:39:21 +00:00
|
|
|
Lst_Append(List *list, void *datum)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
ListNode *ln;
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
assert(datum != NULL);
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
ln = LstNodeNew(list->last, NULL, datum);
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
if (list->last == NULL) {
|
|
|
|
list->first = ln;
|
|
|
|
list->last = ln;
|
|
|
|
} else {
|
|
|
|
list->last->next = ln;
|
|
|
|
list->last = ln;
|
|
|
|
}
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Remove the given node from the given list.
|
|
|
|
* The datum stored in the node must be freed by the caller, if necessary.
|
|
|
|
*/
|
2020-09-05 16:11:04 +00:00
|
|
|
void
|
2020-11-20 03:54:37 +00:00
|
|
|
Lst_Remove(List *list, ListNode *ln)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
/* unlink it from its neighbors */
|
|
|
|
if (ln->next != NULL)
|
|
|
|
ln->next->prev = ln->prev;
|
|
|
|
if (ln->prev != NULL)
|
|
|
|
ln->prev->next = ln->next;
|
|
|
|
|
|
|
|
/* unlink it from the list */
|
|
|
|
if (list->first == ln)
|
|
|
|
list->first = ln->next;
|
|
|
|
if (list->last == ln)
|
|
|
|
list->last = ln->prev;
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Replace the datum in the given node with the new datum. */
|
|
|
|
void
|
2020-11-20 03:54:37 +00:00
|
|
|
LstNode_Set(ListNode *ln, void *datum)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
assert(datum != NULL);
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
ln->datum = datum;
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Replace the datum in the given node with NULL.
|
|
|
|
* Having NULL values in a list is unusual though.
|
|
|
|
*/
|
2020-09-05 16:11:04 +00:00
|
|
|
void
|
2020-11-20 03:54:37 +00:00
|
|
|
LstNode_SetNull(ListNode *ln)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
ln->datum = NULL;
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Return the first node that contains the given datum, or NULL.
|
2020-11-20 03:54:37 +00:00
|
|
|
*
|
2021-01-14 01:24:34 +00:00
|
|
|
* Time complexity: O(length(list))
|
|
|
|
*/
|
2020-11-07 19:39:21 +00:00
|
|
|
ListNode *
|
|
|
|
Lst_FindDatum(List *list, const void *datum)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
ListNode *ln;
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
assert(datum != NULL);
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
for (ln = list->first; ln != NULL; ln = ln->next)
|
|
|
|
if (ln->datum == datum)
|
|
|
|
return ln;
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
return NULL;
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Move all nodes from src to the end of dst.
|
|
|
|
* The source list becomes empty but is not freed.
|
|
|
|
*/
|
2020-09-05 16:11:04 +00:00
|
|
|
void
|
2020-11-20 03:54:37 +00:00
|
|
|
Lst_MoveAll(List *dst, List *src)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
if (src->first != NULL) {
|
|
|
|
src->first->prev = dst->last;
|
|
|
|
if (dst->last != NULL)
|
|
|
|
dst->last->next = src->first;
|
|
|
|
else
|
|
|
|
dst->first = src->first;
|
|
|
|
|
|
|
|
dst->last = src->last;
|
|
|
|
}
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the element data from src to the start of dst. */
|
|
|
|
void
|
2020-11-07 19:39:21 +00:00
|
|
|
Lst_PrependAll(List *dst, List *src)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
ListNode *ln;
|
|
|
|
|
|
|
|
for (ln = src->last; ln != NULL; ln = ln->prev)
|
|
|
|
Lst_Prepend(dst, ln->datum);
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the element data from src to the end of dst. */
|
|
|
|
void
|
2020-11-07 19:39:21 +00:00
|
|
|
Lst_AppendAll(List *dst, List *src)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
ListNode *ln;
|
2020-09-05 16:11:04 +00:00
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
for (ln = src->first; ln != NULL; ln = ln->next)
|
|
|
|
Lst_Append(dst, ln->datum);
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 19:39:21 +00:00
|
|
|
/* Remove and return the datum at the head of the given list. */
|
|
|
|
void *
|
|
|
|
Lst_Dequeue(List *list)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
void *datum = list->first->datum;
|
|
|
|
Lst_Remove(list, list->first);
|
|
|
|
assert(datum != NULL); /* since NULL would mean end of the list */
|
|
|
|
return datum;
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-11-07 19:39:21 +00:00
|
|
|
Vector_Init(Vector *v, size_t itemSize)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
v->len = 0;
|
|
|
|
v->cap = 10;
|
|
|
|
v->itemSize = itemSize;
|
|
|
|
v->items = bmake_malloc(v->cap * v->itemSize);
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Add space for a new item to the vector and return a pointer to that space.
|
|
|
|
* The returned data is valid until the next modifying operation.
|
|
|
|
*/
|
2020-11-07 19:39:21 +00:00
|
|
|
void *
|
|
|
|
Vector_Push(Vector *v)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
if (v->len >= v->cap) {
|
|
|
|
v->cap *= 2;
|
|
|
|
v->items = bmake_realloc(v->items, v->cap * v->itemSize);
|
|
|
|
}
|
|
|
|
v->len++;
|
|
|
|
return Vector_Get(v, v->len - 1);
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 01:24:34 +00:00
|
|
|
/*
|
|
|
|
* Return the pointer to the last item in the vector.
|
|
|
|
* The returned data is valid until the next modifying operation.
|
|
|
|
*/
|
2020-09-05 16:11:04 +00:00
|
|
|
void *
|
2020-11-07 19:39:21 +00:00
|
|
|
Vector_Pop(Vector *v)
|
2020-09-05 16:11:04 +00:00
|
|
|
{
|
2021-01-14 01:24:34 +00:00
|
|
|
assert(v->len > 0);
|
|
|
|
v->len--;
|
|
|
|
return Vector_Get(v, v->len);
|
2020-09-05 16:11:04 +00:00
|
|
|
}
|