2 * array.c - routines for associative arrays.
6 * Copyright (C) 1986, 1988, 1989, 1991-1999 the Free Software Foundation, Inc.
8 * This file is part of GAWK, the GNU implementation of the
9 * AWK Programming Language.
11 * GAWK is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * GAWK is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
27 * Tree walks (``for (iggy in foo)'') and array deletions use expensive
28 * linear searching. So what we do is start out with small arrays and
29 * grow them as needed, so that our arrays are hopefully small enough,
30 * most of the time, that they're pretty full and we're not looking at
33 * The decision is made to grow the array if the average chain length is
34 * ``too big''. This is defined as the total number of entries in the table
35 * divided by the size of the array being greater than some constant.
38 #define AVG_CHAIN_MAX 10 /* don't want to linear search more than this */
42 static NODE *assoc_find P((NODE *symbol, NODE *subs, int hash1));
43 static void grow_table P((NODE *symbol));
45 /* concat_exp --- concatenate expression list into a single string */
59 if (tree->type != Node_expression_list)
60 return force_string(tree_eval(tree));
61 r = force_string(tree_eval(tree->lnode));
62 if (tree->rnode == NULL)
64 subseplen = SUBSEP_node->lnode->stlen;
65 subsep = SUBSEP_node->lnode->stptr;
66 len = r->stlen + subseplen + 2;
67 emalloc(str, char *, len, "concat_exp");
68 memcpy(str, r->stptr, r->stlen+1);
71 for (tree = tree->rnode; tree != NULL; tree = tree->rnode) {
75 memcpy(s, subsep, subseplen+1);
78 r = force_string(tree_eval(tree->lnode));
79 len += r->stlen + subseplen;
81 erealloc(str, char *, len, "concat_exp");
83 memcpy(s, r->stptr, r->stlen+1);
87 r = make_str_node(str, s - str, ALREADY_MALLOCED);
92 /* assoc_clear --- flush all the values in symbol[] before doing a split() */
101 if (symbol->var_array == NULL)
103 for (i = 0; i < symbol->array_size; i++) {
104 for (bucket = symbol->var_array[i]; bucket != NULL; bucket = next) {
105 next = bucket->ahnext;
106 unref(bucket->ahname);
107 unref(bucket->ahvalue);
110 symbol->var_array[i] = NULL;
112 free(symbol->var_array);
113 symbol->var_array = NULL;
114 symbol->array_size = symbol->table_size = 0;
115 symbol->flags &= ~ARRAYMAXED;
118 /* hash --- calculate the hash function of the string in subs */
122 register const char *s;
126 register unsigned long h = 0;
129 * This is INCREDIBLY ugly, but fast. We break the string up into
130 * 8 byte units. On the first time through the loop we get the
131 * "leftover bytes" (strlen % 8). On every other iteration, we
132 * perform 8 HASHC's so we handle all 8 bytes. Essentially, this
133 * saves us 7 cmp & branch instructions. If this routine is
134 * heavily used enough, it's worth the ugly coding.
136 * OZ's original sdbm hash, copied from Margo Seltzers db package.
141 * #define HASHC h = *s++ + 65599 * h
142 * Because 65599 = pow(2, 6) + pow(2, 16) - 1 we multiply by shifts
144 #define HASHC htmp = (h << 6); \
145 h = *s++ + htmp + (htmp << 10) - h
153 * This was an implementation of "Duff's Device", but it has been
154 * redone, separating the switch for extra iterations from the
155 * loop. This is necessary because the DEC VAX-C compiler is
158 switch (len & (8 - 1)) {
170 register size_t loop = len >> 3;
183 /* "Duff's Device" for those who can handle it */
185 register size_t loop = (len + 8 - 1) >> 3;
187 switch (len & (8 - 1)) {
189 do { /* All fall throughs */
208 /* assoc_find --- locate symbol[subs] */
210 static NODE * /* NULL if not found */
211 assoc_find(symbol, subs, hash1)
216 register NODE *bucket;
218 for (bucket = symbol->var_array[hash1]; bucket != NULL;
219 bucket = bucket->ahnext) {
220 if (cmp_nodes(bucket->ahname, subs) == 0)
226 /* in_array --- test whether the array element symbol[subs] exists or not */
229 in_array(symbol, subs)
235 if (symbol->type == Node_param_list)
236 symbol = stack_ptr[symbol->param_cnt];
237 if ((symbol->flags & SCALAR) != 0)
238 fatal("attempt to use scalar as array");
240 * evaluate subscript first, it could have side effects
242 subs = concat_exp(subs); /* concat_exp returns a string node */
243 if (symbol->var_array == NULL) {
247 hash1 = hash(subs->stptr, subs->stlen, (unsigned long) symbol->array_size);
248 ret = (assoc_find(symbol, subs, hash1) != NULL);
255 * Find SYMBOL[SUBS] in the assoc array. Install it with value "" if it
256 * isn't there. Returns a pointer ala get_lhs to where its value is stored.
258 * SYMBOL is the address of the node (or other pointer) being dereferenced.
259 * SUBS is a number or string used as the subscript.
263 assoc_lookup(symbol, subs)
267 register NODE *bucket;
269 (void) force_string(subs);
271 if ((symbol->flags & SCALAR) != 0)
272 fatal("attempt to use scalar as array");
274 if (symbol->var_array == NULL) {
275 if (symbol->type != Node_var_array) {
276 unref(symbol->var_value);
277 symbol->type = Node_var_array;
279 symbol->array_size = symbol->table_size = 0; /* sanity */
280 symbol->flags &= ~ARRAYMAXED;
282 hash1 = hash(subs->stptr, subs->stlen,
283 (unsigned long) symbol->array_size);
285 hash1 = hash(subs->stptr, subs->stlen,
286 (unsigned long) symbol->array_size);
287 bucket = assoc_find(symbol, subs, hash1);
288 if (bucket != NULL) {
290 return &(bucket->ahvalue);
294 /* It's not there, install it. */
295 if (do_lint && subs->stlen == 0)
296 warning("subscript of array `%s' is null string",
299 /* first see if we would need to grow the array, before installing */
300 symbol->table_size++;
301 if ((symbol->flags & ARRAYMAXED) == 0
302 && symbol->table_size/symbol->array_size > AVG_CHAIN_MAX) {
304 /* have to recompute hash value for new size */
305 hash1 = hash(subs->stptr, subs->stlen,
306 (unsigned long) symbol->array_size);
310 bucket->type = Node_ahash;
311 if (subs->flags & TEMP)
312 bucket->ahname = dupnode(subs);
314 unsigned int saveflags = subs->flags;
316 subs->flags &= ~MALLOC;
317 bucket->ahname = dupnode(subs);
318 subs->flags = saveflags;
322 /* array subscripts are strings */
323 bucket->ahname->flags &= ~NUMBER;
324 bucket->ahname->flags |= STRING;
325 bucket->ahvalue = Nnull_string;
326 bucket->ahnext = symbol->var_array[hash1];
327 symbol->var_array[hash1] = bucket;
328 return &(bucket->ahvalue);
331 /* do_delete --- perform `delete array[s]' */
334 do_delete(symbol, tree)
338 register NODE *bucket, *last;
341 if (symbol->type == Node_param_list) {
342 symbol = stack_ptr[symbol->param_cnt];
343 if (symbol->type == Node_var)
346 if (symbol->type == Node_var_array) {
347 if (symbol->var_array == NULL)
350 fatal("delete: illegal use of variable `%s' as array",
353 if (tree == NULL) { /* delete array */
358 subs = concat_exp(tree); /* concat_exp returns string node */
359 hash1 = hash(subs->stptr, subs->stlen, (unsigned long) symbol->array_size);
362 for (bucket = symbol->var_array[hash1]; bucket != NULL;
363 last = bucket, bucket = bucket->ahnext)
364 if (cmp_nodes(bucket->ahname, subs) == 0)
366 if (bucket == NULL) {
368 warning("delete: index `%s' not in array `%s'",
369 subs->stptr, symbol->vname);
375 last->ahnext = bucket->ahnext;
377 symbol->var_array[hash1] = bucket->ahnext;
378 unref(bucket->ahname);
379 unref(bucket->ahvalue);
381 symbol->table_size--;
382 if (symbol->table_size <= 0) {
383 memset(symbol->var_array, '\0',
384 sizeof(NODE *) * symbol->array_size);
385 symbol->table_size = symbol->array_size = 0;
386 symbol->flags &= ~ARRAYMAXED;
387 free((char *) symbol->var_array);
388 symbol->var_array = NULL;
392 /* assoc_scan --- start a ``for (iggy in foo)'' loop */
395 assoc_scan(symbol, lookat)
397 struct search *lookat;
399 lookat->sym = symbol;
401 lookat->bucket = NULL;
402 lookat->retval = NULL;
403 if (symbol->var_array != NULL)
407 /* assoc_next --- actually find the next element in array */
411 struct search *lookat;
413 register NODE *symbol = lookat->sym;
416 fatal("null symbol in assoc_next");
417 if (symbol->var_array == NULL || lookat->idx > symbol->array_size) {
418 lookat->retval = NULL;
422 * This is theoretically unsafe. The element bucket might have
423 * been freed if the body of the scan did a delete on the next
424 * element of the bucket. The only way to do that is by array
425 * reference, which is unlikely. Basically, if the user is doing
426 * anything other than an operation on the current element of an
427 * assoc array while walking through it sequentially, all bets are
428 * off. (The safe way is to register all search structs on an
429 * array with the array, and update all of them on a delete or
432 if (lookat->bucket != NULL) {
433 lookat->retval = lookat->bucket->ahname;
434 lookat->bucket = lookat->bucket->ahnext;
437 for (; lookat->idx < symbol->array_size; lookat->idx++) {
440 if ((bucket = symbol->var_array[lookat->idx]) != NULL) {
441 lookat->retval = bucket->ahname;
442 lookat->bucket = bucket->ahnext;
447 lookat->retval = NULL;
448 lookat->bucket = NULL;
452 /* grow_table --- grow a hash table */
458 NODE **old, **new, *chain, *next;
461 unsigned long oldsize, newsize;
463 * This is an array of primes. We grow the table by an order of
464 * magnitude each time (not just doubling) so that growing is a
465 * rare operation. We expect, on average, that it won't happen
466 * more than twice. The final size is also chosen to be small
467 * enough so that MS-DOG mallocs can handle it. When things are
468 * very large (> 8K), we just double more or less, instead of
469 * just jumping from 8K to 64K.
471 static long sizes[] = { 13, 127, 1021, 8191, 16381, 32749, 65497,
472 #if ! defined(MSDOS) && ! defined(OS2) && ! defined(atarist)
473 131101, 262147, 524309, 1048583, 2097169,
474 4194319, 8388617, 16777259, 33554467,
475 67108879, 134217757, 268435459, 536870923,
480 /* find next biggest hash size */
481 newsize = oldsize = symbol->array_size;
482 for (i = 0, j = sizeof(sizes)/sizeof(sizes[0]); i < j; i++) {
483 if (oldsize < sizes[i]) {
489 if (newsize == oldsize) { /* table already at max (!) */
490 symbol->flags |= ARRAYMAXED;
494 /* allocate new table */
495 emalloc(new, NODE **, newsize * sizeof(NODE *), "grow_table");
496 memset(new, '\0', newsize * sizeof(NODE *));
498 /* brand new hash table, set things up and return */
499 if (symbol->var_array == NULL) {
500 symbol->table_size = 0;
504 /* old hash table there, move stuff to new, free old */
505 old = symbol->var_array;
506 for (i = 0; i < oldsize; i++) {
510 for (chain = old[i]; chain != NULL; chain = next) {
511 next = chain->ahnext;
512 hash1 = hash(chain->ahname->stptr,
513 chain->ahname->stlen, newsize);
515 /* remove from old list, add to new */
516 chain->ahnext = new[hash1];
525 * note that symbol->table_size does not change if an old array,
526 * and is explicitly set to 0 if a new one.
528 symbol->var_array = new;
529 symbol->array_size = newsize;