This commit is contained in:
2026-05-19 10:51:22 -06:00
parent c1851fe242
commit 78a4fb402d
5 changed files with 48 additions and 23 deletions
+2
View File
@@ -40,6 +40,8 @@
pkg-config
guile
clang-tools # clangd
gdb
gdbgui
];
};
};
BIN
View File
Binary file not shown.
+33 -22
View File
@@ -1,11 +1,38 @@
#include <stdio.h>
#include <string.h>
#include "../runtime/gyehoek.h"
#include "../runtime/weak-set.h"
static int
symbol_lookup_predicate_fn (SCM sym, void *closure) {
SCM other = SCM_PACK_POINTER (closure);
return 1;
symbol_lookup_predicate_fn (SCM sym1, void *closure) {
const SCM sym2 = *((SCM*)closure);
const int symp1 = SCM_SYMBOLP (sym1);
const int symp2 = SCM_SYMBOLP (sym2);
// both symbols?
if (SCM_SYMBOLP (sym1) && SCM_SYMBOLP (sym2)) {
const SCM str1 = SCM_CELL_OBJECT (sym1, 2);
const SCM str2 = SCM_CELL_OBJECT (sym2, 2);
const size_t len1 = scm_c_string_length (str1);
const size_t len2 = scm_c_string_length (str2);
// same length?
if (len1 == len2) {
// same name?
const char * const s1 = scm_c_string_chars (str1);
const char * const s2 = scm_c_string_chars (str2);
return strncmp (s1, s2, len1);
}
}
return 0;
}
SCM test (SCM set, const char *sym_name, SCM value) {
SCM str = scm_from_cstring (sym_name);
SCM sym = scm_make_symbol (str, scm_c_hash (str));
SCM r = scm_c_weak_set_insert (set, scm_c_hash (str), value,
symbol_lookup_predicate_fn,
&sym);
printf ("%ld\n", weak_set_count (set));
return r;
}
int main () {
@@ -15,23 +42,7 @@ int main () {
/* printf ("%ld\n", hash); */
SCM set = scm_c_make_weak_set (31);
SCM str = scm_from_cstring ("my-symbol");
SCM sym = scm_make_symbol (str, scm_c_hash (str));
scm_c_weak_set_insert (set, scm_c_hash (str),
SCM_PACK (SCM_MAKE_SMALL_INT (123)),
symbol_lookup_predicate_fn,
SCM_UNPACK_POINTER (sym));
printf ("%ld\n", weak_set_count (set));
scm_c_weak_set_insert (set, scm_c_hash (str),
SCM_PACK (SCM_MAKE_SMALL_INT (123)),
symbol_lookup_predicate_fn,
SCM_UNPACK_POINTER (sym));
printf ("%ld\n", weak_set_count (set));
SCM str2 = scm_from_cstring ("my-symbol2");
SCM sym2 = scm_make_symbol (str2, scm_c_hash (str2));
scm_c_weak_set_insert (set, scm_c_hash (str2),
SCM_PACK (SCM_MAKE_SMALL_INT (456)),
symbol_lookup_predicate_fn,
SCM_UNPACK_POINTER (sym2));
printf ("%ld\n", weak_set_count (set));
test (set, "my-symbol", SCM_PACK (SCM_MAKE_SMALL_INT (123)));
test (set, "my-symbol", SCM_PACK (SCM_MAKE_SMALL_INT (123)));
test (set, "my-symbol2", SCM_PACK (SCM_MAKE_SMALL_INT (456)));
}
+11
View File
@@ -80,7 +80,18 @@ extern const long scm_tc7_weak_set;
extern const long scm_tc7_symbol;
extern const long scm_tc7_string;
/// Strings
#define SCM_STRINGP(x) (SCM_TYP7 (x) == scm_tc7_string)
size_t scm_c_string_length (SCM str);
const char *scm_c_string_chars (SCM str);
/// Symbols
#define SCM_SYMBOLP(x) (SCM_TYP7 (x) == scm_tc7_symbol)
+2 -1
View File
@@ -92,6 +92,7 @@ find_bucket (scm_t_weak_set *set, unsigned long hash,
&& pred (SCM_PACK (set->entries[p].key), closure)) {
return SCM_PACK (set->entries[p].key);
} else {
/* i have faith in gcc's TCO. */
return find_bucket (set, hash, key, pred, closure,
(p + 1) % set->size);
}
@@ -101,7 +102,7 @@ find_bucket (scm_t_weak_set *set, unsigned long hash,
SCM scm_c_weak_set_insert (SCM set, unsigned long hash,
SCM key, scm_t_set_predicate_fn pred,
void* closure) {
void *closure) {
scm_t_weak_set *s = SCM_WEAK_SET (set);
unsigned long size = s->size;