2 scm-hash.cc -- implement Scheme_hash_table
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
15 #include "ly-smobs.icc"
18 Return: number of objects.
21 copy_handle (void *closure
, SCM handle
)
23 SCM tab
= (SCM
) closure
;
24 scm_hashq_set_x (tab
, scm_car (handle
), scm_cdr (handle
));
29 copy_scm_hashes (SCM dest
, SCM src
)
31 scm_internal_hash_for_each_handle ( (SCM (*)(GUILE_ELLIPSIS
)) ©_handle
, dest
, src
);
34 Scheme_hash_table::Scheme_hash_table ()
38 hash_tab_
= scm_c_make_hash_table (119);
41 Scheme_hash_table::Scheme_hash_table (Scheme_hash_table
const &src
)
49 Scheme_hash_table::copy (Scheme_hash_table
const &src
)
54 hash_tab_
= scm_c_make_hash_table (SCM_HASHTABLE_N_ITEMS(src
.hash_tab_
));
55 copy_scm_hashes (hash_tab_
, src
.hash_tab_
);
58 Scheme_hash_table::~Scheme_hash_table ()
63 Scheme_hash_table::mark_smob (SCM s
)
65 Scheme_hash_table
*me
= (Scheme_hash_table
*) SCM_CELL_WORD_1 (s
);
66 scm_gc_mark (me
->hash_tab_
);
71 Scheme_hash_table::print_smob (SCM s
, SCM p
, scm_print_state
*)
74 scm_puts ("#<Scheme_hash_table ", p
);
75 Scheme_hash_table
*me
= (Scheme_hash_table
*) SCM_CELL_WORD_1 (s
);
76 scm_display (me
->hash_tab_
, p
);
82 Scheme_hash_table::try_retrieve (SCM k
, SCM
*v
){
84 SCM handle
= scm_hashq_get_handle (hash_tab_
, k
);
85 if (scm_is_pair (handle
))
87 *v
= scm_cdr (handle
);
95 Scheme_hash_table::contains (SCM k
) const
97 return scm_is_pair (scm_hashq_get_handle (hash_tab_
, k
));
101 Scheme_hash_table::set (SCM k
, SCM v
)
103 assert (scm_is_symbol (k
));
104 SCM handle
= scm_hashq_create_handle_x (hash_tab_
, k
, SCM_UNDEFINED
);
105 scm_set_cdr_x (handle
, v
);
109 Scheme_hash_table::get (SCM k
) const
111 /* SCM_UNSPECIFIED will stick out like a sore thumb, hopefully.
113 return scm_hashq_ref (hash_tab_
, k
, SCM_UNSPECIFIED
);
117 Scheme_hash_table::remove (SCM k
)
119 scm_hashq_remove_x (hash_tab_
, k
);
123 collect_handles (void *closure
, SCM key
, SCM value
, SCM result
)
126 return scm_acons(key
, value
, result
);
130 Scheme_hash_table::to_alist () const
132 return scm_internal_hash_fold ((SCM (*)(GUILE_ELLIPSIS
)) &collect_handles
, NULL
, SCM_EOL
, hash_tab_
);
135 IMPLEMENT_SMOBS (Scheme_hash_table
);
136 IMPLEMENT_DEFAULT_EQUAL_P (Scheme_hash_table
);