1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/* array.c: routines to deal with arrays */
/* Copyright (C) 1999 by the Massachusetts Institute of Technology.
*
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright
* notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting
* documentation, and that the name of M.I.T. not be used in
* advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
* M.I.T. makes no representations about the suitability of
* this software for any purpose. It is provided "as is"
* without express or implied warranty.
*/
#include "nawm.h"
#include "lang.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
int hashstr(char *str);
void array_free_element(nawmval data, dtype stype);
int hashstr(char *str)
{
int i, val;
for (i = val = 0; str; str++)
val += *str * ++i;
return val;
}
#define hash(type, val) ((type == T_STR) ? hashstr((char *)val) : val)
typedef struct _achain {
nawmval index;
nawmval data;
struct _achain *next;
} achain;
typedef struct _arraytype {
int tag;
dtype basetype;
dtype subtype;
} arraytype;
struct _array {
int size, nelts;
arraytype *type;
achain **elts;
};
static arraytype *atypes = NULL;
static int natypes = 0;
dtype array_type(dtype base, dtype sub)
{
int i;
for (i = 0; i < natypes; i++)
{
if (atypes[i].basetype == base && atypes[i].subtype == sub)
return (dtype)&atypes[i];
}
atypes = xrealloc(atypes, (i + 1) * sizeof(arraytype));
atypes[i].tag = T_ARRAY;
atypes[i].basetype = base;
atypes[i].subtype = sub;
return (dtype)&atypes[i];
}
dtype array_basetype(dtype arrtype)
{
arraytype *type = (arraytype *)arrtype;
assert(type->tag == T_ARRAY);
return type->basetype;
}
dtype array_subtype(dtype arrtype)
{
arraytype *type = (arraytype *)arrtype;
assert(type->tag == T_ARRAY);
return type->subtype;
}
char *array_typename(dtype arrtype)
{
char *base, *sub, *ans;
arraytype *type = (arraytype *)arrtype;
assert(type->tag == T_ARRAY);
base = typename(type->basetype);
sub = typename(type->subtype);
ans = gcmalloc(strlen(base) + strlen(sub) + 3, free);
sprintf(ans, "%s[%s]", base, sub);
return ans;
}
array *create_array(dtype type, int nbuckets)
{
array *ans;
ans = gcmalloc(sizeof(array), (void (*)(void *))free_array);
ans->type = (arraytype *)type;
ans->size = nbuckets;
ans->nelts = 0;
ans->elts = xmalloc(nbuckets * sizeof(achain *));
memset(ans->elts, 0, nbuckets * sizeof(achain *));
return ans;
}
int array_size(array *arr)
{
return arr->nelts;
}
nawmval array_lookup(array *arr, nawmval subscript)
{
int ind = hash(arr->type->subtype, subscript) % arr->size;
achain *a;
for (a = arr->elts[ind]; a; a = a->next)
{
if (a->index == subscript)
return a->data;
}
return 0;
}
int array_contains(array *arr, nawmval value)
{
int ind;
achain *a;
for (ind = 0; ind < arr->size; ind++)
{
for (a = arr->elts[ind]; a; a = a->next)
{
if (arr->type->basetype == T_STR)
{
if (!strcmp((char *)a->data, (char *)value))
return 1;
}
else
{
if (a->data == value)
return 1;
}
}
}
return 0;
}
void array_insert(array *arr, nawmval subscript, nawmval value)
{
int ind = hash(arr->type->subtype, subscript) % arr->size;
achain *a, *atmp;
int len;
if (!is_atomic_type(arr->type->basetype))
ref((void *)value);
for (a = arr->elts[ind], len = 0; a; a = a->next, len++)
{
if (a->index == subscript)
{
array_free_element(a->data, arr->type->subtype);
a->data = value;
return;
}
}
if (len > 10)
{
int nsize, i;
achain **nelts;
/* Need to reorganize */
nsize = (arr->size + 1) * 2 + 1;
nelts = xmalloc(nsize * sizeof(achain *));
memset(nelts, 0, nsize * sizeof(achain *));
for (i = 0; i < arr->size; i++)
{
for (a = arr->elts[i]; a; a = atmp)
{
atmp = a->next;
ind = hash(arr->type->subtype, a->index) % nsize;
a->next = nelts[ind];
nelts[ind] = a;
}
}
free(arr->elts);
arr->elts = nelts;
arr->size = nsize;
ind = hash(arr->type->subtype, subscript) % nsize;
}
a = xmalloc(sizeof(achain));
a->index = subscript;
a->data = value;
a->next = arr->elts[ind];
arr->elts[ind] = a;
arr->nelts++;
}
void array_delete(array *arr, nawmval subscript)
{
int ind = hash(arr->type->subtype, subscript) % arr->size;
achain *a, *aprev;
for (a = arr->elts[ind], aprev = NULL; a; aprev = a, a = a->next)
{
if (a->index == subscript)
{
array_free_element(a->data, arr->type->subtype);
if (aprev)
aprev->next = a->next;
else
arr->elts[ind] = a->next;
free(a);
arr->nelts--;
return;
}
}
}
void free_array(array *arr)
{
int i;
achain *a, *atmp;
for (i = 0; i < arr->size; i++)
{
for (a = arr->elts[i]; a; a = atmp)
{
array_free_element(a->data, arr->type->basetype);
atmp = a->next;
free(a);
}
free(arr->elts[i]);
}
free(arr->elts);
free(arr);
}
void array_free_element(nawmval data, dtype type)
{
if (!is_atomic_type(type))
unref((void *)data);
}
nawmval array_first(array *arr, arrayiter *ai)
{
ai->ind = 0;
ai->chain = arr->elts[ai->ind];
return ai->chain->data;
}
nawmval array_next(array *arr, arrayiter *ai)
{
ai->chain = ai->chain->next;
if (!ai->chain)
{
ai->ind++;
if (ai->ind >= arr->size)
return 0;
ai->chain = arr->elts[ai->ind];
}
return ai->chain->data;
}
|