1 /* -*-comment-start: "//";comment-end:""-*-
2 * GNU Mes --- Maxwell Equations of Software
3 * Copyright © 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
5 * This file is part of GNU Mes.
7 * GNU Mes is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or (at
10 * your option) any later version.
12 * GNU Mes is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
21 SCM make_vector__ (long k);
22 SCM vector_ref_ (SCM x, long i);
23 SCM vector_set_x_ (SCM x, long i, SCM e);
28 if (c >= 'a' && c <= 'z')
34 hashq_ (SCM x, long size)
36 int hash = char_hash (VALUE (CAR (STRING (x)))) * 27;
37 if (TYPE (CDR (STRING (x))) == TPAIR)
38 hash = hash + char_hash (VALUE (CADR (STRING (x))));
40 hash = hash + char_hash (0);
46 hashq (SCM x, SCM size)
48 return hashq_ (x, VALUE (size));
52 hashq_ref (SCM table, SCM key, SCM dflt)
54 unsigned hash = hashq_ (key, 0);
55 SCM buckets = struct_ref_ (table, 4);
56 SCM bucket = vector_ref_ (buckets, hash);
58 if (TYPE (dflt) == TPAIR)
60 if (TYPE (bucket) == TPAIR)
61 x = assq (key, bucket);
66 hashq_set_x (SCM table, SCM key, SCM value)
68 unsigned hash = hashq_ (key, 0);
69 SCM buckets = struct_ref_ (table, 4);
70 SCM bucket = vector_ref_ (buckets, hash);
71 if (TYPE (bucket) != TPAIR)
73 bucket = acons (key, value, bucket);
74 vector_set_x_ (buckets, hash, bucket);
79 hash_table_printer (SCM table)
81 fdputs ("#<", g_stdout); display_ (struct_ref_ (table, 2)); fdputc (' ', g_stdout);
82 fdputs ("size: ", g_stdout); display_ (struct_ref_ (table, 3)); fdputc (' ', g_stdout);
83 SCM buckets = struct_ref_ (table, 4);
84 fdputs ("buckets: ", g_stdout);
85 for (int i=0; i<LENGTH (buckets); i++)
87 SCM e = vector_ref_ (buckets, i);
88 if (e != cell_unspecified)
90 fdputc ('[', g_stdout);
91 while (TYPE (e) == TPAIR)
95 if (TYPE (e) == TPAIR)
96 fdputc (' ', g_stdout);
98 fdputs ("]\n ", g_stdout);
101 fdputc ('>', g_stdout);
105 make_hashq_type () ///((internal))
107 SCM record_type_name = cstring_to_symbol ("<record-type>");
108 SCM record_type = record_type_name; // FIXME
109 SCM hashq_type_name = cstring_to_symbol ("<hashq-table>");
110 SCM fields = cell_nil;
111 fields = cons (cstring_to_symbol ("buckets"), fields);
112 fields = cons (cstring_to_symbol ("size"), fields);
113 fields = cons (fields, cell_nil);
114 fields = cons (hashq_type_name, fields);
115 return make_struct (record_type, fields, cell_unspecified);
119 make_hash_table_ (long size)
123 SCM hashq_type_name = cstring_to_symbol ("<hashq-table>");
124 SCM record_type_name = cstring_to_symbol ("<record-type>");
125 //SCM hashq_type = hashq_type_name; // FIXME
126 SCM hashq_type = make_hashq_type ();
128 SCM buckets = make_vector__ (size);
129 SCM values = cell_nil;
130 values = cons (buckets, values);
131 values = cons (MAKE_NUMBER (size), values);
132 values = cons (hashq_type_name, values);
133 return make_struct (hashq_type, values, cell_hash_table_printer);
137 make_hash_table (SCM x)
140 if (TYPE (x) == TPAIR)
142 assert (TYPE (x) == TNUMBER);
145 return make_hash_table_ (size);