mes/src/hash.c

227 lines
5.4 KiB
C
Raw Normal View History

/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
2019-01-13 10:02:28 +00:00
* Copyright © 2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
* Copyright © 2019 Jeremiah Orians <jeremiah@pdp10.guru>
*
* This file is part of GNU Mes.
*
* GNU Mes is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* GNU Mes is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
build: Simplify. core: Make mes src/ c files separate compilation units. * include/mes/constants.h: New file. * include/mes/macros.h: New file. * include/mes/mes.h: New file. * src/gc.c: Update. * src/hash.c: Update. * src/lib.c: Update. * src/math.c: Update. * src/mes.c: Update. * src/module.c: Update. * src/posix.c: Update. * src/reader.c: Update. * src/string.c: Update. * src/struct.c: Update. * src/vector.c: Update. mes: Update datadir. * src/mes.c (g_datadir): New global. (open_boot): Rename from read_boot. (read_boot): New function. * mes/module/mes/boot-0.scm: Move from boot-0.scm.in * configure: Update. * configure.sh: Update. mescc: Create libraries from separate files. * .gitignore: Update. * build-aux/bootstrap-mes.sh: Remove. * build-aux/bootstrap.sh.in: Remove. * build-aux/build-guile.sh: Update. * build-aux/build-mes.sh: Update. * build-aux/build-scaffold.sh: New file. * build-aux/build.sh.in: Update. * build-aux/cc.sh: Update. * build-aux/check-tcc.sh: Remove. * build-aux/config.sh.in: New file. * build-aux/config.sh: Remove. * build-aux/install.sh.in: Update. * build-aux/test-boot.sh: New file. * build-aux/test-c.sh: New file. * build-aux/test-driver: New file. * build-aux/test-suite.sh: New file. * build-aux/trace.sh: Update. * build-aux/uninstall.sh.in: Update. * configure: Update. * configure.sh: Update. * lib/linux/x86-mes-mescc/crt1.c: Move from lib/linux/x86-mes. * lib/linux/x86-mes-mescc/mini.c: Likewise. * lib/linux/x86_64-mes-mescc/crt1.c: Move from lib/linux/x86_64-mes. * lib/linux/x86_64-mes-mescc/mini.c: Likewise. * lib/linux/x86-mes-gcc/syscall.c: Rename from mes.c. * lib/linux/x86-mes-mescc/syscall.c: Likewise. * lib/linux/x86_64-mes-gcc/syscall.c: Likewise. * lib/linux/x86_64-mes-mescc/syscall.c: Likewise. * lib/mes/mes_open.c: Include config.h. * lib/tests/stdio/70-printf-hello.c: Likewise. * lib/tests/stdio/70-printf-simple.c: Likewise. * scaffold/gc-test.sh: New file. * simple.sh: Update.
2019-06-08 14:36:22 +01:00
#include "mes/lib.h"
#include "mes/mes.h"
#include <string.h>
2019-01-13 10:02:28 +00:00
SCM
hash_cstring (char *s, SCM size)
{
2019-01-13 10:02:28 +00:00
int h = s[0] * 37;
if (s[0] && s[1])
2019-01-13 10:02:28 +00:00
{
h = h + s[1] * 43;
}
2019-01-13 10:02:28 +00:00
require (0 != size, "src/hash.c: hash_cstring must not be zero");
h = h % size;
return h;
}
SCM
2019-01-13 10:02:28 +00:00
hashq_ (struct scm *x, SCM size)
{
2019-01-13 10:02:28 +00:00
struct scm *y = x;
if (y->type == TSPECIAL || y->type == TSYMBOL)
{
char *p = y->cdr->string;
return hash_cstring (p, size); /* FIXME: hash x directly */
}
error (cell_symbol_system_error, cons (make_string_ ("hashq_: not a symbol"), x));
exit (EXIT_FAILURE);
}
SCM
2019-01-13 10:02:28 +00:00
hash_ (struct scm *x, SCM size)
{
2019-01-13 10:02:28 +00:00
struct scm *y = x;
if (y->type == TSTRING)
{
char *p = y->cdr->string;
return hash_cstring (p, size);
}
require (FALSE, "src/hash.c: hash_ impossible condition hit");
return hashq_ (x, size);
}
2019-01-13 10:02:28 +00:00
struct scm *
hashq (struct scm *x, struct scm *size)
{
2019-01-13 10:02:28 +00:00
require (FALSE, "src/hash.c: hashq impossible condition hit");
return make_number (hashq_ (x, size->value));
}
2019-01-13 10:02:28 +00:00
struct scm *
hash (struct scm *x, struct scm *size)
{
2019-01-13 10:02:28 +00:00
require (FALSE, "src/hash.c: hash impossible condition hit");
return make_number (hash_ (x, size->value));
}
2019-01-13 10:02:28 +00:00
struct scm *
hashq_get_handle (struct scm *table, struct scm *key, struct scm *dflt)
{
2019-01-13 10:02:28 +00:00
struct scm *ydflt = dflt;
if (ydflt->type == TPAIR)
{
2019-01-13 10:02:28 +00:00
return ydflt->car;
}
2019-01-13 10:02:28 +00:00
struct scm *ybucket = vector_ref_ (struct_ref_ (table, 4), hashq_ (key, struct_ref_ (table, 3)->value));
if (ybucket->type == TPAIR)
{
return assq (key, ybucket);
}
return cell_f;
}
2019-01-13 10:02:28 +00:00
struct scm *
hashq_ref (struct scm *table, struct scm *key, struct scm *dflt)
{
2019-01-13 10:02:28 +00:00
struct scm *x = hashq_get_handle (table, key, dflt);
if (x == cell_f)
{
return x;
}
return x->cdr;
}
2019-01-13 10:02:28 +00:00
struct scm *
hash_ref (struct scm *table, struct scm *key, struct scm *dflt) /* External */
{
2019-01-13 10:02:28 +00:00
struct scm *bucket = vector_ref_ (struct_ref_ (table, 4), hash_ (key, struct_ref_ (table, 3)->value));
if (bucket->type == TPAIR)
{
struct scm *y = assoc (key, bucket);
if (y != cell_f)
{
return y->cdr;
}
}
return cell_f;
}
2019-01-13 10:02:28 +00:00
struct scm *
hashq_set_x (struct scm *table, struct scm *key, struct scm *value)
{
2019-01-13 10:02:28 +00:00
SCM size = struct_ref_ (table, 3)->value;
struct scm *buckets = struct_ref_ (table, 4);
struct scm *ybucket = vector_ref_ (buckets, hashq_ (key, size));
if (ybucket->type != TPAIR)
{
vector_set_x_ (buckets, hashq_ (key, size), acons (key, value, cell_nil));
}
else
{
vector_set_x_ (buckets, hashq_ (key, size),
acons (key, value, vector_ref_ (buckets, hashq_ (key, size))));
}
return value;
}
2019-01-13 10:02:28 +00:00
struct scm *
hash_set_x (struct scm *table, struct scm *key, struct scm *value)
{
2019-01-13 10:02:28 +00:00
SCM size = struct_ref_ (table, 3)->value;
unsigned h = hash_ (key, size);
struct scm *buckets = struct_ref_ (table, 4);
struct scm *bucket = vector_ref_ (buckets, h);
struct scm *ybucket = bucket;
if (ybucket->type != TPAIR)
{
2019-01-13 10:02:28 +00:00
bucket = cell_nil;
}
2019-01-13 10:02:28 +00:00
bucket = acons (key, value, bucket);
vector_set_x_ (buckets, h, bucket);
return value;
}
2019-01-13 10:02:28 +00:00
struct scm *
make_hashq_type () /* ((internal)) */
{
2019-01-13 10:02:28 +00:00
struct scm *record_type = cell_symbol_record_type; /* FIXME */
struct scm *fields = cell_nil;
fields = cons (cell_symbol_buckets, fields);
fields = cons (cell_symbol_size, fields);
fields = cons (fields, cell_nil);
fields = cons (cell_symbol_hashq_table, fields);
return make_struct (record_type, fields, cell_unspecified);
}
2019-01-13 10:02:28 +00:00
struct scm *
make_hash_table_ (SCM size)
{
if (!size)
2019-01-13 10:02:28 +00:00
{
size = 100;
}
2019-01-13 10:02:28 +00:00
struct scm *hashq_type = make_hashq_type ();
struct scm *buckets = make_vector__ (size);
struct scm *values = cell_nil;
values = cons (buckets, values);
2019-01-13 10:02:28 +00:00
values = cons (make_number (size), values);
values = cons (cell_symbol_hashq_table, values);
2019-01-13 10:02:28 +00:00
/* FIXME: symbol/printer return make_struct (hashq_type, values, cstring_to_symbol ("hash-table-printer"); */
return make_struct (hashq_type, values, cell_unspecified);
}
2019-01-13 10:02:28 +00:00
struct scm *
make_hash_table (struct scm *x)
{
2019-01-13 10:02:28 +00:00
SCM size = 0;
struct scm *y = x;
if (y->type == TPAIR)
{
2019-01-13 10:02:28 +00:00
require (TNUMBER == y->type, "y->type must be TNUMBER\nsrc/hash.c: make_hash_table\n");
size = y->value;
}
2019-01-13 10:02:28 +00:00
return make_hash_table_ (size);
}
2019-01-13 10:02:28 +00:00
/* Externally exposed */
struct scm *
hashq_set_x_ (struct scm *table, struct scm *key, struct scm *value)
{
return hashq_set_x (table, key, value);
}
struct scm *
hash_ref_ (struct scm *table, struct scm *key, struct scm *dflt)
{
return hash_ref (table, key, dflt);
}