Wed Sep 2 23:33:13 2020 UTC ()
make(1): improve grouping of the Lst functions

Lst_IsEmpty does not belong in the "create and destroy" group, but in
"query information without modifying anything".

The functions named LstNode_* all belong together.  They do not provide
much abstraction, but still they restrict the API and hide a few struct
fields that are only used internally by Lst_Open/Lst_Close and
Lst_ForEach.

Use consistent wording in the documentation of the functions (list,
node, datum).


(rillig)
diff -r1.59 -r1.60 src/usr.bin/make/lst.h

cvs diff -r1.59 -r1.60 src/usr.bin/make/lst.h (expand / switch to unified diff)

--- src/usr.bin/make/lst.h 2020/08/30 11:15:05 1.59
+++ src/usr.bin/make/lst.h 2020/09/02 23:33:13 1.60
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lst.h,v 1.59 2020/08/30 11:15:05 rillig Exp $ */ 1/* $NetBSD: lst.h,v 1.60 2020/09/02 23:33:13 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor. 8 * Adam de Boor.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -63,115 +63,117 @@ @@ -63,115 +63,117 @@
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * SUCH DAMAGE. 71 * SUCH DAMAGE.
72 * 72 *
73 * from: @(#)lst.h 8.1 (Berkeley) 6/6/93 73 * from: @(#)lst.h 8.1 (Berkeley) 6/6/93
74 */ 74 */
75 75
76/*- 76/* Doubly-linked lists of arbitrary pointers. */
77 * lst.h -- 77
78 * Header for using the list library 
79 */ 
80#ifndef MAKE_LST_H 78#ifndef MAKE_LST_H
81#define MAKE_LST_H 79#define MAKE_LST_H
82 80
83#include <sys/param.h> 81#include <sys/param.h>
84#include <stdlib.h> 82#include <stdlib.h>
85 
86/* 
87 * basic typedef. This is what the Lst_ functions handle 
88 */ 
89 83
 84/* A doubly-linked list of pointers. */
90typedef struct List *Lst; 85typedef struct List *Lst;
 86/* A single node in the doubly-linked list. */
91typedef struct ListNode *LstNode; 87typedef struct ListNode *LstNode;
92 88
 89/* Copy a node, usually by allocating a copy of the given object.
 90 * For reference-counted objects, the original object may need to be
 91 * modified, therefore the parameter is not const. */
93typedef void *LstCopyProc(void *); 92typedef void *LstCopyProc(void *);
 93/* Free the datum of a node, called before freeing the node itself. */
94typedef void LstFreeProc(void *); 94typedef void LstFreeProc(void *);
95typedef Boolean LstFindProc(const void *, const void *); 95/* Return TRUE if the datum matches the args, for Lst_Find. */
96typedef int LstActionProc(void *, void *); 96typedef Boolean LstFindProc(const void *datum, const void *args);
97 97/* An action for Lst_ForEach. */
98/* 98typedef int LstActionProc(void *datum, void *args);
99 * Creation/destruction functions 99
100 */ 100/* Create or destroy a list */
101/* Create a new list */ 101
102Lst Lst_Init(void); 102/* Create a new list. */
103/* Duplicate an existing list */ 103Lst Lst_Init(void);
104Lst Lst_Copy(Lst, LstCopyProc); 104/* Duplicate an existing list. */
105/* Destroy an old one */ 105Lst Lst_Copy(Lst, LstCopyProc);
106void Lst_Free(Lst); 106/* Free the list, leaving the node data unmodified. */
107void Lst_Destroy(Lst, LstFreeProc); 107void Lst_Free(Lst);
108/* True if list is empty */ 108/* Free the list, freeing the node data using the given function. */
109Boolean Lst_IsEmpty(Lst); 109void Lst_Destroy(Lst, LstFreeProc);
110 110
111/* 111/* Get information about a list */
112 * Functions to modify a list 112
113 */ 113Boolean Lst_IsEmpty(Lst);
114/* Insert an element before another */ 114/* Return the first node of the list, or NULL. */
115void Lst_InsertBefore(Lst, LstNode, void *); 115LstNode Lst_First(Lst);
116/* Place an element at the front of a lst. */ 116/* Return the last node of the list, or NULL. */
117void Lst_Prepend(Lst, void *); 117LstNode Lst_Last(Lst);
118/* Place an element at the end of a lst. */ 118/* Find the first node for which the function returns TRUE, or NULL. */
119void Lst_Append(Lst, void *); 119LstNode Lst_Find(Lst, LstFindProc, const void *);
120/* Remove an element */ 120/* Find the first node for which the function returns TRUE, or NULL.
121void Lst_Remove(Lst, LstNode); 121 * The search starts at the given node, towards the end of the list. */
122/* Replace a node with a new value */ 122LstNode Lst_FindFrom(Lst, LstNode, LstFindProc, const void *);
123void LstNode_Set(LstNode, void *); 123/* Find the first node that contains the given datum, or NULL. */
124void LstNode_SetNull(LstNode); 124LstNode Lst_FindDatum(Lst, const void *);
125 125
126void Lst_PrependAll(Lst, Lst); 126/* Modify a list */
127void Lst_AppendAll(Lst, Lst); 127
128void Lst_MoveAll(Lst, Lst); 128/* Insert a datum before the given node. */
129 129void Lst_InsertBefore(Lst, LstNode, void *);
130/* 130/* Place a datum at the front of the list. */
131 * Node-specific functions 131void Lst_Prepend(Lst, void *);
132 */ 132/* Place a datum at the end of the list. */
133/* Return first element in list */ 133void Lst_Append(Lst, void *);
134LstNode Lst_First(Lst); 134/* Remove the node from the list. */
135/* Return last element in list */ 135void Lst_Remove(Lst, LstNode);
136LstNode Lst_Last(Lst); 136void Lst_PrependAll(Lst, Lst);
137/* Return successor to given element */ 137void Lst_AppendAll(Lst, Lst);
138LstNode LstNode_Next(LstNode); 138void Lst_MoveAll(Lst, Lst);
139/* Return predecessor to given element */ 139
140LstNode LstNode_Prev(LstNode); 140/* Node-specific functions */
141/* Get datum from LstNode */ 141
142void *LstNode_Datum(LstNode); 142/* Return the successor of the node, or NULL. */
143 143LstNode LstNode_Next(LstNode);
144/* 144/* Return the predecessor of the node, or NULL. */
145 * Functions for entire lists 145LstNode LstNode_Prev(LstNode);
146 */ 146/* Return the datum of the node. Usually not NULL. */
147/* Find an element in a list */ 147void *LstNode_Datum(LstNode);
148LstNode Lst_Find(Lst, LstFindProc, const void *); 148/* Replace the value of the node. */
149/* Find an element starting from somewhere */ 149void LstNode_Set(LstNode, void *);
150LstNode Lst_FindFrom(Lst, LstNode, LstFindProc, const void *); 150/* Set the value of the node to NULL. Having NULL in a list is unusual. */
151/* Return the first node that contains the given datum, or NULL. */ 151void LstNode_SetNull(LstNode);
152LstNode Lst_FindDatum(Lst, const void *); 152
153/* Apply a function to all elements of a lst */ 153/* Iterating over a list, using a callback function */
154int Lst_ForEach(Lst, LstActionProc, void *); 154
155/* Apply a function to all elements of a lst starting from a certain point. */ 155/* Apply a function to each datum of the list, until the callback function
156int Lst_ForEachFrom(Lst, LstNode, LstActionProc, void *); 156 * returns non-zero. */
157/* 157int Lst_ForEach(Lst, LstActionProc, void *);
158 * these functions are for dealing with a list as a table, of sorts. 158/* Apply a function to each datum of the list, starting at the node,
159 * An idea of the "current element" is kept and used by all the functions 159 * until the callback function returns non-zero. */
160 * between Lst_Open() and Lst_Close(). 160int Lst_ForEachFrom(Lst, LstNode, LstActionProc, void *);
161 */ 161
162/* Open the list */ 162/* Iterating over a list while keeping track of the current node and possible
163void Lst_Open(Lst); 163 * concurrent modifications */
164/* Next element please, or NULL */ 164
165LstNode Lst_Next(Lst); 165/* Start iterating the list. */
166/* Finish table access */ 166void Lst_Open(Lst);
167void Lst_Close(Lst); 167/* Return the next node, or NULL. */
168 168LstNode Lst_Next(Lst);
169/* 169/* Finish iterating the list. */
170 * for using the list as a queue 170void Lst_Close(Lst);
171 */ 171
172/* Place an element at tail of queue */ 172/* Using the list as a queue */
173void Lst_Enqueue(Lst, void *); 173
174/* Remove an element from head of queue */ 174/* Add a datum at the tail of the queue. */
175void *Lst_Dequeue(Lst); 175void Lst_Enqueue(Lst, void *);
 176/* Remove the head node of the queue and return its datum. */
 177void *Lst_Dequeue(Lst);
176 178
177#endif /* MAKE_LST_H */ 179#endif /* MAKE_LST_H */