xref: /openbsd-src/usr.bin/make/garray.h (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 #ifndef GARRAY_H
2 #define GARRAY_H
3 
4 /* $OpenPackages$ */
5 /* $OpenBSD: garray.h,v 1.1 2001/06/12 22:44:21 espie Exp $ */
6 /* Growable array implementation */
7 
8 /*
9  * Copyright (c) 2001 Marc Espie.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
24  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 struct growableArray {
34 	GNode 	     **a;	/* Only used for gnodes right now */
35 	unsigned int size;	/* Total max size */
36 	unsigned int n;		/* Current number of members */
37 };
38 
39 #define AppendList2Array(l1, l2)				\
40 do {								\
41     	LstNode ln;						\
42 	for (ln = Lst_First((l1)); ln != NULL; ln = Lst_Adv(ln))\
43 		Array_AtEnd((l2), Lst_Datum(ln));		\
44 } while (0)
45 
46 #ifdef STATS_GROW
47 #define MAY_INCREASE_STATS	STAT_GROWARRAY++
48 #else
49 #define MAY_INCREASE_STATS
50 #endif
51 
52 #define Array_AtEnd(l, gn) 						    \
53 do { 									    \
54 	if ((l)->n >= (l)->size) { 					    \
55 	    (l)->size *= 2; 					    	    \
56 	    (l)->a = erealloc((l)->a, sizeof(struct GNode *) * (l)->size);  \
57 	    MAY_INCREASE_STATS;						    \
58 	} 								    \
59 	(l)->a[(l)->n++] = (gn); 					    \
60 } while (0)
61 
62 #define Array_Find(l, func, v)			\
63 do {						\
64 	unsigned int i;				\
65 	for (i = 0; i < (l)->n; i++)		\
66 		if ((func)((l)->a[i], (v)) == 0)\
67 		    break;			\
68 } while (0)
69 
70 #define Array_ForEach(l, func, v)		\
71 do {						\
72 	unsigned int i;				\
73 	for (i = 0; i < (l)->n; i++)		\
74 		(func)((l)->a[i], (v));		\
75 } while (0)
76 
77 #define Array_Every(l, func)			\
78 do {						\
79 	unsigned int i;				\
80 	for (i = 0; i < (l)->n; i++)		\
81 		(func)((l)->a[i]);		\
82 } while (0)
83 
84 #define Array_Init(l, sz)				\
85 do {							\
86 	(l)->size = (sz);				\
87 	(l)->n = 0;					\
88 	(l)->a = emalloc(sizeof(GNode *) * (l)->size);	\
89 } while (0)
90 
91 #define Array_Reset(l)		\
92 do {				\
93 	(l)->n = 0;		\
94 } while (0)
95 
96 #define Array_IsEmpty(l)	((l)->n == 0)
97 
98 #endif
99