diff options
Diffstat (limited to 'int_array.c')
-rw-r--r-- | int_array.c | 65 |
1 files changed, 35 insertions, 30 deletions
diff --git a/int_array.c b/int_array.c index a2c9e41e..02e682f3 100644 --- a/int_array.c +++ b/int_array.c @@ -45,9 +45,10 @@ static inline NODE **int_find(NODE *symbol, long k, uint32_t hash1); static NODE **int_insert(NODE *symbol, long k, uint32_t hash1); static void grow_int_table(NODE *symbol); -array_ptr int_array_func[] = { +afunc_t int_array_func[] = { int_array_init, is_integer, + null_length, int_lookup, int_exists, int_clear, @@ -55,22 +56,27 @@ array_ptr int_array_func[] = { int_list, int_copy, int_dump, + (afunc_t) 0, }; -/* int_array_init --- check relevant environment variables */ +/* int_array_init --- array initialization routine */ static NODE ** -int_array_init(NODE *symbol ATTRIBUTE_UNUSED, NODE *subs ATTRIBUTE_UNUSED) +int_array_init(NODE *symbol, NODE *subs ATTRIBUTE_UNUSED) { - long newval; + if (symbol == NULL) { /* first time */ + long newval; + + /* check relevant environment variables */ + if ((newval = getenv_long("INT_CHAIN_MAX")) > 0) + INT_CHAIN_MAX = newval; + } else + null_array(symbol); - if ((newval = getenv_long("INT_CHAIN_MAX")) > 0) - INT_CHAIN_MAX = newval; return (NODE **) ! NULL; } - /* is_integer --- check if subscript is an integer */ NODE ** @@ -147,7 +153,8 @@ is_integer(NODE *symbol, NODE *subs) } -/* int_lookup --- Find SYMBOL[SUBS] in the assoc array. Install it with value "" +/* + * int_lookup --- Find SYMBOL[SUBS] in the assoc array. Install it with value "" * if it isn't there. Returns a pointer ala get_lhs to where its value is stored. */ @@ -160,7 +167,8 @@ int_lookup(NODE *symbol, NODE *subs) NODE **lhs; NODE *xn; - /* N.B: symbol->table_size is the total # of non-integers (symbol->xarray) + /* + * N.B: symbol->table_size is the total # of non-integers (symbol->xarray) * and integer elements. Also, symbol->xarray must have at least one * item in it, and can not exist if there are no integer elements. * In that case, symbol->xarray is promoted to 'symbol' (See int_remove). @@ -207,7 +215,8 @@ int_lookup(NODE *symbol, NODE *subs) } -/* int_exists --- test whether the array element symbol[subs] exists or not, +/* + * int_exists --- test whether the array element symbol[subs] exists or not, * return pointer to value if it does. */ @@ -266,8 +275,7 @@ int_clear(NODE *symbol, NODE *subs ATTRIBUTE_UNUSED) } if (symbol->buckets != NULL) efree(symbol->buckets); - init_array(symbol); /* re-initialize symbol */ - symbol->flags &= ~ARRAYMAXED; + symbol->ainit(symbol, NULL); /* re-initialize symbol */ return NULL; } @@ -338,9 +346,7 @@ removed: BUCKET *head = symbol->buckets[hash1]; assert(b->aicount == 1); - /* move the last element from head - * to bucket to make it full. - */ + /* move the last element from head to bucket to make it full. */ i = --head->aicount; /* head has one less element */ b->ainum[1] = head->ainum[i]; b->aivalue[1] = head->aivalue[i]; @@ -356,8 +362,7 @@ removed: symbol->table_size--; if (xn == NULL && symbol->table_size == 0) { efree(symbol->buckets); - init_array(symbol); /* re-initialize array 'symbol' */ - symbol->flags &= ~ARRAYMAXED; + symbol->ainit(symbol, NULL); /* re-initialize array 'symbol' */ } else if (xn != NULL && symbol->table_size == xn->table_size) { /* promote xn (str_array) to symbol */ xn->flags &= ~XARRAY; @@ -421,6 +426,7 @@ int_copy(NODE *symbol, NODE *newsymb) } *pnew = newchain; + newchain->ainext = NULL; pnew = & newchain->ainext; } } @@ -456,15 +462,17 @@ int_list(NODE *symbol, NODE *t) int j, elem_size = 1; long num; static char buf[100]; + assoc_kind_t assoc_kind; if (symbol->table_size == 0) return NULL; + assoc_kind = (assoc_kind_t) t->flags; num_elems = symbol->table_size; - if ((t->flags & (AINDEX|AVALUE|ADELETE)) == (AINDEX|ADELETE)) + if ((assoc_kind & (AINDEX|AVALUE|ADELETE)) == (AINDEX|ADELETE)) num_elems = 1; - if ((t->flags & (AINDEX|AVALUE)) == (AINDEX|AVALUE)) + if ((assoc_kind & (AINDEX|AVALUE)) == (AINDEX|AVALUE)) elem_size = 2; list_size = elem_size * num_elems; @@ -486,7 +494,7 @@ int_list(NODE *symbol, NODE *t) for (j = 0; j < b->aicount; j++) { /* index */ num = b->ainum[j]; - if (t->flags & AISTR) { + if (assoc_kind & AISTR) { sprintf(buf, "%ld", num); subs = make_string(buf, strlen(buf)); subs->numbr = num; @@ -498,12 +506,12 @@ int_list(NODE *symbol, NODE *t) list[k++] = subs; /* value */ - if (t->flags & AVALUE) { + if (assoc_kind & AVALUE) { r = b->aivalue[j]; if (r->type == Node_val) { - if ((t->flags & AVNUM) != 0) + if ((assoc_kind & AVNUM) != 0) (void) force_number(r); - else if ((t->flags & AVSTR) != 0) + else if ((assoc_kind & AVSTR) != 0) r = force_string(r); } list[k++] = r; @@ -659,14 +667,13 @@ static uint32_t int_hash(uint32_t k, uint32_t hsize) { -/* Code snippet copied from: +/* + * Code snippet copied from: * Hash functions (http://www.azillionmonkeys.com/qed/hash.html). * Copyright 2004-2008 by Paul Hsieh. Licenced under LGPL 2.1. */ - /* This is the final mixing function used by Paul Hsieh - * in SuperFastHash. - */ + /* This is the final mixing function used by Paul Hsieh in SuperFastHash. */ k ^= k << 3; k += k >> 5; @@ -709,9 +716,7 @@ int_insert(NODE *symbol, long k, uint32_t hash1) b = symbol->buckets[hash1]; - /* Only the first bucket in the chain can be partially full, - * but is never empty. - */ + /* Only the first bucket in the chain can be partially full, but is never empty. */ if (b == NULL || (i = b->aicount) == 2) { getbucket(b); |