| 1 | greg | 2.6 | /* RCSid: $Id$ */ | 
| 2 | greg | 2.1 | /* | 
| 3 |  |  | * Header file for general associative table lookup routines | 
| 4 |  |  | */ | 
| 5 |  |  |  | 
| 6 | greg | 2.6 | /* ==================================================================== | 
| 7 |  |  | * The Radiance Software License, Version 1.0 | 
| 8 |  |  | * | 
| 9 |  |  | * Copyright (c) 1990 - 2002 The Regents of the University of California, | 
| 10 |  |  | * through Lawrence Berkeley National Laboratory.   All rights reserved. | 
| 11 |  |  | * | 
| 12 |  |  | * Redistribution and use in source and binary forms, with or without | 
| 13 |  |  | * modification, are permitted provided that the following conditions | 
| 14 |  |  | * are met: | 
| 15 |  |  | * | 
| 16 |  |  | * 1. Redistributions of source code must retain the above copyright | 
| 17 |  |  | *         notice, this list of conditions and the following disclaimer. | 
| 18 |  |  | * | 
| 19 |  |  | * 2. Redistributions in binary form must reproduce the above copyright | 
| 20 |  |  | *       notice, this list of conditions and the following disclaimer in | 
| 21 |  |  | *       the documentation and/or other materials provided with the | 
| 22 |  |  | *       distribution. | 
| 23 |  |  | * | 
| 24 |  |  | * 3. The end-user documentation included with the redistribution, | 
| 25 |  |  | *           if any, must include the following acknowledgment: | 
| 26 |  |  | *             "This product includes Radiance software | 
| 27 |  |  | *                 (http://radsite.lbl.gov/) | 
| 28 |  |  | *                 developed by the Lawrence Berkeley National Laboratory | 
| 29 |  |  | *               (http://www.lbl.gov/)." | 
| 30 |  |  | *       Alternately, this acknowledgment may appear in the software itself, | 
| 31 |  |  | *       if and wherever such third-party acknowledgments normally appear. | 
| 32 |  |  | * | 
| 33 |  |  | * 4. The names "Radiance," "Lawrence Berkeley National Laboratory" | 
| 34 |  |  | *       and "The Regents of the University of California" must | 
| 35 |  |  | *       not be used to endorse or promote products derived from this | 
| 36 |  |  | *       software without prior written permission. For written | 
| 37 |  |  | *       permission, please contact [email protected]. | 
| 38 |  |  | * | 
| 39 |  |  | * 5. Products derived from this software may not be called "Radiance", | 
| 40 |  |  | *       nor may "Radiance" appear in their name, without prior written | 
| 41 |  |  | *       permission of Lawrence Berkeley National Laboratory. | 
| 42 |  |  | * | 
| 43 |  |  | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | 
| 44 |  |  | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
| 45 |  |  | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
| 46 |  |  | * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR | 
| 47 |  |  | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
| 48 |  |  | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
| 49 |  |  | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | 
| 50 |  |  | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | 
| 51 |  |  | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
| 52 |  |  | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
| 53 |  |  | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
| 54 |  |  | * SUCH DAMAGE. | 
| 55 |  |  | * ==================================================================== | 
| 56 |  |  | * | 
| 57 |  |  | * This software consists of voluntary contributions made by many | 
| 58 |  |  | * individuals on behalf of Lawrence Berkeley National Laboratory.   For more | 
| 59 |  |  | * information on Lawrence Berkeley National Laboratory, please see | 
| 60 |  |  | * <http://www.lbl.gov/>. | 
| 61 |  |  | */ | 
| 62 |  |  |  | 
| 63 | greg | 2.1 | typedef struct { | 
| 64 | gwlarson | 2.5 | char    *key;                   /* key name */ | 
| 65 |  |  | unsigned long   hval;           /* key hash value (for efficiency) */ | 
| 66 |  |  | char    *data;                  /* pointer to client data */ | 
| 67 | greg | 2.1 | } LUENT; | 
| 68 |  |  |  | 
| 69 |  |  | typedef struct { | 
| 70 | gwlarson | 2.5 | unsigned long   (*hashf)();     /* key hash function */ | 
| 71 |  |  | int     (*keycmp)();            /* key comparison function */ | 
| 72 |  |  | void    (*freek)();             /* free a key */ | 
| 73 |  |  | void    (*freed)();             /* free the data */ | 
| 74 |  |  | int     tsiz;                   /* current table size */ | 
| 75 |  |  | LUENT   *tabl;                  /* table, if allocated */ | 
| 76 |  |  | int     ndel;                   /* number of deleted entries */ | 
| 77 | greg | 2.1 | } LUTAB; | 
| 78 |  |  |  | 
| 79 | gregl | 2.3 | #undef strcmp | 
| 80 | greg | 2.1 | #define LU_SINIT(fk,fd)         {lu_shash,strcmp,(void (*)())(fk),\ | 
| 81 |  |  | (void (*)())(fd),0,NULL,0} | 
| 82 |  |  |  | 
| 83 |  |  | /* | 
| 84 |  |  | * The lu_init routine is called to initialize a table.  The number of | 
| 85 |  |  | * elements passed is not a limiting factor, as a table can grow to | 
| 86 |  |  | * any size permitted by memory.  However, access will be more efficient | 
| 87 |  |  | * if this number strikes a reasonable balance between default memory use | 
| 88 |  |  | * and the expected (minimum) table size.  The value returned is the | 
| 89 |  |  | * actual allocated table size (or zero if there was insufficient memory). | 
| 90 |  |  | * | 
| 91 |  |  | * The hashf, keycmp, freek and freed member functions must be assigned | 
| 92 |  |  | * separately.  If the hash value is sufficient to guarantee equality | 
| 93 |  |  | * between keys, then the keycmp pointer may be NULL.  Otherwise, it | 
| 94 |  |  | * should return 0 if the two passed keys match.  If it is not necessary | 
| 95 |  |  | * (or possible) to free the key and/or data values, then the freek and/or | 
| 96 |  |  | * freed member functions may be NULL. | 
| 97 |  |  | * | 
| 98 |  |  | * It isn't fully necessary to call lu_init to initialize the LUTAB structure. | 
| 99 |  |  | * If tsiz is 0, then the first call to lu_find will allocate a minimal table. | 
| 100 |  |  | * The LU_SINIT macro provides a convenient static declaration for character | 
| 101 |  |  | * string keys. | 
| 102 |  |  | * | 
| 103 |  |  | * The lu_find routine returns the entry corresponding to the given | 
| 104 |  |  | * key.  If the entry does not exist, the corresponding key field will | 
| 105 |  |  | * be NULL.  If the entry has been previously deleted but not yet freed, | 
| 106 |  |  | * then only the data field will be NULL.  It is the caller's | 
| 107 |  |  | * responsibility to (allocate and) assign the key and data fields when | 
| 108 |  |  | * creating a new entry.  The only case where lu_find returns NULL is when | 
| 109 |  |  | * the system has run out of memory. | 
| 110 |  |  | * | 
| 111 |  |  | * The lu_delete routine frees an entry's data (if any) by calling | 
| 112 |  |  | * the freed member function, but does not free the key field.  This | 
| 113 |  |  | * will be freed later during (or instead of) table reallocation. | 
| 114 | greg | 2.2 | * It is therefore an error to reuse or do anything with the key | 
| 115 |  |  | * field after calling lu_delete. | 
| 116 | greg | 2.1 | * | 
| 117 | gwlarson | 2.4 | * The lu_doall routine loops through every filled table entry, calling | 
| 118 |  |  | * the given function once on each entry.  If a NULL pointer is passed | 
| 119 |  |  | * for this function, then lu_doall simply returns the total number of | 
| 120 |  |  | * active entries.  Otherwise, it returns the sum of all the function | 
| 121 |  |  | * evaluations. | 
| 122 |  |  | * | 
| 123 | greg | 2.1 | * The lu_done routine calls the given free function once for each | 
| 124 |  |  | * assigned table entry (i.e. each entry with an assigned key value). | 
| 125 |  |  | * The user must define these routines to free the key and the data | 
| 126 |  |  | * in the LU_TAB structure.  The final action of lu_done is to free the | 
| 127 |  |  | * allocated table itself. | 
| 128 |  |  | */ | 
| 129 |  |  |  | 
| 130 | greg | 2.6 | extern int      strcmp(); | 
| 131 |  |  |  | 
| 132 |  |  | #ifdef NOPROTO | 
| 133 |  |  |  | 
| 134 | greg | 2.1 | extern int      lu_init(); | 
| 135 |  |  | extern LUENT    *lu_find(); | 
| 136 |  |  | extern void     lu_delete(); | 
| 137 | gwlarson | 2.4 | extern int      lu_doall(); | 
| 138 | greg | 2.1 | extern void     lu_done(); | 
| 139 | gwlarson | 2.5 | extern unsigned long    lu_shash(); | 
| 140 | greg | 2.1 |  | 
| 141 | greg | 2.6 | #else | 
| 142 |  |  |  | 
| 143 |  |  | extern int      lu_init(LUTAB *tbl, int nel); | 
| 144 |  |  | extern unsigned long    lu_shash(char *s); | 
| 145 |  |  | extern LUENT    *lu_find(LUTAB *tbl, char *key); | 
| 146 |  |  | extern void     lu_delete(LUTAB *tbl, char *key); | 
| 147 |  |  | extern int      lu_doall(LUTAB *tbl, int (*f)()); | 
| 148 |  |  | extern void     lu_done(LUTAB *tbl); | 
| 149 |  |  |  | 
| 150 |  |  | #endif |