diff options
author | Tiago Gomes <tiago.gomes@codethink.co.uk> | 2015-01-22 11:39:46 +0000 |
---|---|---|
committer | Tiago Gomes <tiago.gomes@codethink.co.uk> | 2015-01-22 11:39:46 +0000 |
commit | 70efb9c3529a4c0cee5f4672704f0341d05fb359 (patch) | |
tree | 30739c93a70d922d6710a148427042f29f130f24 /int_array.c | |
parent | 6a0eb04c3d2ac24d80d505b42a8f67799487d133 (diff) | |
parent | 902fcb22d611b7f9e99369ecab223c00c877b82c (diff) | |
download | gawk-70efb9c3529a4c0cee5f4672704f0341d05fb359.tar.gz |
Merge branch 'master' into baserock/tiagogomes/armv8l64baserock/tiagogomes/armv8l64
Diffstat (limited to 'int_array.c')
-rw-r--r-- | int_array.c | 115 |
1 files changed, 47 insertions, 68 deletions
diff --git a/int_array.c b/int_array.c index fd58de26..c2bf37b5 100644 --- a/int_array.c +++ b/int_array.c @@ -3,7 +3,7 @@ */ /* - * Copyright (C) 1986, 1988, 1989, 1991-2011 the Free Software Foundation, Inc. + * Copyright (C) 1986, 1988, 1989, 1991-2013 the Free Software Foundation, Inc. * * This file is part of GAWK, the GNU implementation of the * AWK Programming Language. @@ -40,18 +40,15 @@ static NODE **int_list(NODE *symbol, NODE *t); static NODE **int_copy(NODE *symbol, NODE *newsymb); static NODE **int_dump(NODE *symbol, NODE *ndump); -#ifdef ARRAYDEBUG -static NODE **int_option(NODE *opt, NODE *val); -#endif - static uint32_t int_hash(uint32_t k, uint32_t hsize); 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, @@ -59,25 +56,27 @@ array_ptr int_array_func[] = { int_list, int_copy, int_dump, -#ifdef ARRAYDEBUG - int_option, -#endif + (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 ** @@ -86,7 +85,7 @@ is_integer(NODE *symbol, NODE *subs) long l; AWKNUM d; - if (subs == Nnull_string) + if (subs == Nnull_string || do_mpfr) return NULL; if ((subs->flags & NUMINT) != 0) @@ -101,10 +100,10 @@ is_integer(NODE *symbol, NODE *subs) return NULL; } - /* a[3]=1; print "3" in a -- TRUE - * a[3]=1; print "+3" in a -- FALSE - * a[3]=1; print "03" in a -- FALSE - * a[-3]=1; print "-3" in a -- TRUE + /* a[3]=1; print "3" in a -- true + * a[3]=1; print "+3" in a -- false + * a[3]=1; print "03" in a -- false + * a[-3]=1; print "-3" in a -- true */ if ((subs->flags & (STRING|STRCUR)) != 0) { @@ -154,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. */ @@ -167,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). @@ -214,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. */ @@ -273,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; } @@ -290,7 +291,8 @@ int_remove(NODE *symbol, NODE *subs) int i; NODE *xn = symbol->xarray; - assert(symbol->buckets != NULL); + if (symbol->table_size == 0 || symbol->buckets == NULL) + return NULL; if (! is_integer(symbol, subs)) { if (xn == NULL || xn->aremove(xn, subs) == NULL) @@ -344,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]; @@ -362,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; @@ -404,6 +403,7 @@ int_copy(NODE *symbol, NODE *newsymb) ) { getbucket(newchain); newchain->aicount = chain->aicount; + newchain->ainext = NULL; for (j = 0; j < chain->aicount; j++) { NODE *oldval; @@ -426,6 +426,7 @@ int_copy(NODE *symbol, NODE *newsymb) } *pnew = newchain; + newchain->ainext = NULL; pnew = & newchain->ainext; } } @@ -461,14 +462,17 @@ int_list(NODE *symbol, NODE *t) int j, elem_size = 1; long num; static char buf[100]; + assoc_kind_t assoc_kind; - assert(symbol->table_size > 0); + 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; @@ -490,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) != 0) { sprintf(buf, "%ld", num); subs = make_string(buf, strlen(buf)); subs->numbr = num; @@ -502,12 +506,12 @@ int_list(NODE *symbol, NODE *t) list[k++] = subs; /* value */ - if (t->flags & AVALUE) { + if ((assoc_kind & AVALUE) != 0) { 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; @@ -582,7 +586,7 @@ int_dump(NODE *symbol, NODE *ndump) fprintf(output_fp, "flags: %s\n", flags2str(symbol->flags)); } indent(indent_level); - fprintf(output_fp, "INT_CHAIN_MAX: %lu\n", INT_CHAIN_MAX); + fprintf(output_fp, "INT_CHAIN_MAX: %lu\n", (unsigned long) INT_CHAIN_MAX); indent(indent_level); fprintf(output_fp, "array_size: %lu (int)\n", (unsigned long) symbol->array_size); indent(indent_level); @@ -663,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; @@ -713,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); @@ -802,25 +803,3 @@ grow_int_table(NODE *symbol) } efree(old); } - - -#ifdef ARRAYDEBUG - -static NODE ** -int_option(NODE *opt, NODE *val) -{ - int newval; - NODE *tmp; - NODE **ret = (NODE **) ! NULL; - - tmp = force_string(opt); - (void) force_number(val); - if (STREQ(tmp->stptr, "INT_CHAIN_MAX")) { - newval = (int) val->numbr; - if (newval > 0) - INT_CHAIN_MAX = newval; - } else - ret = NULL; - return ret; -} -#endif |