[BACK]Return to garray.h CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/garray.h, Revision 1.3

1.1       espie       1: #ifndef GARRAY_H
                      2: #define GARRAY_H
                      3:
                      4: /* $OpenPackages$ */
1.2       espie       5: /* $OpenBSD: garray.h,v 1.1 2001/06/12 22:44:21 espie Exp $ */
1.1       espie       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)
1.2       espie      45:
1.1       espie      46: #ifdef STATS_GROW
                     47: #define MAY_INCREASE_STATS     STAT_GROWARRAY++
                     48: #else
                     49: #define MAY_INCREASE_STATS
                     50: #endif
                     51:
1.3     ! espie      52: #define Array_AtEnd(l, gn)                             \
        !            53: do {                                                   \
        !            54:        if ((l)->n >= (l)->size) {                      \
        !            55:                (l)->size *= 2;                         \
        !            56:                (l)->a = erealloc((l)->a,               \
        !            57:                    sizeof(struct GNode *) * (l)->size);\
        !            58:                MAY_INCREASE_STATS;                     \
        !            59:        }                                               \
        !            60:        (l)->a[(l)->n++] = (gn);                        \
1.1       espie      61: } while (0)
                     62:
                     63: #define Array_Find(l, func, v)                 \
                     64: do {                                           \
                     65:        unsigned int i;                         \
                     66:        for (i = 0; i < (l)->n; i++)            \
                     67:                if ((func)((l)->a[i], (v)) == 0)\
                     68:                    break;                      \
1.3     ! espie      69: } while (0)
        !            70:
        !            71: #define Array_FindP(l, func, v)                                \
        !            72: do {                                                   \
        !            73:        unsigned int i;                                 \
        !            74:        for (i = 0; i < (l)->n; i++)                    \
        !            75:                if ((func)(&((l)->a[i]), (v)) == 0)     \
        !            76:                    break;                              \
1.1       espie      77: } while (0)
                     78:
                     79: #define Array_ForEach(l, func, v)              \
                     80: do {                                           \
                     81:        unsigned int i;                         \
                     82:        for (i = 0; i < (l)->n; i++)            \
                     83:                (func)((l)->a[i], (v));         \
                     84: } while (0)
                     85:
                     86: #define Array_Every(l, func)                   \
                     87: do {                                           \
                     88:        unsigned int i;                         \
                     89:        for (i = 0; i < (l)->n; i++)            \
                     90:                (func)((l)->a[i]);              \
                     91: } while (0)
                     92:
                     93: #define Array_Init(l, sz)                              \
                     94: do {                                                   \
                     95:        (l)->size = (sz);                               \
                     96:        (l)->n = 0;                                     \
                     97:        (l)->a = emalloc(sizeof(GNode *) * (l)->size);  \
                     98: } while (0)
                     99:
                    100: #define Array_Reset(l)         \
                    101: do {                           \
                    102:        (l)->n = 0;             \
                    103: } while (0)
                    104:
                    105: #define Array_IsEmpty(l)       ((l)->n == 0)
                    106:
                    107: #endif