2 scm-hash.cc -- implement Scheme_hash_table
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
15 #include "ly-smobs.icc"
18 Return: number of objects.
21 copy_scm_hashes (SCM dest, SCM src)
24 for (int i = scm_c_vector_length (src); i--;)
25 for (SCM s = scm_vector_ref (src, scm_from_int (i)); scm_is_pair (s); s = scm_cdr (s))
27 scm_hashq_set_x (dest, scm_caar (s), scm_cdar (s));
33 Scheme_hash_table::Scheme_hash_table ()
37 hash_tab_ = scm_make_vector (scm_from_int (119), SCM_EOL);
41 Scheme_hash_table::Scheme_hash_table (Scheme_hash_table const &src)
48 hash_tab_ = scm_make_vector (scm_from_int (max ((int) src.elt_count_, 11)), SCM_EOL);
49 elt_count_ = copy_scm_hashes (hash_tab_, src.hash_tab_);
53 Scheme_hash_table::operator = (Scheme_hash_table const &src)
58 hash_tab_ = scm_make_vector (scm_from_int (max ((int) src.elt_count_, 11)), SCM_EOL);
59 elt_count_ = copy_scm_hashes (hash_tab_, src.hash_tab_);
62 Scheme_hash_table::~Scheme_hash_table ()
67 Scheme_hash_table::mark_smob (SCM s)
69 Scheme_hash_table *me = (Scheme_hash_table *) SCM_CELL_WORD_1 (s);
70 scm_gc_mark (me->hash_tab_);
75 Scheme_hash_table::print_smob (SCM s, SCM p, scm_print_state*)
78 scm_puts ("#<Scheme_hash_table ", p);
79 Scheme_hash_table *me = (Scheme_hash_table *) SCM_CELL_WORD_1 (s);
80 scm_display (me->hash_tab_, p);
86 Scheme_hash_table::try_retrieve (SCM k, SCM *v){
88 SCM handle = scm_hashq_get_handle (hash_tab_, k);
89 if (scm_is_pair (handle))
91 *v = scm_cdr (handle);
99 Scheme_hash_table::contains (SCM k) const
101 return scm_is_pair (scm_hashq_get_handle (hash_tab_, k));
105 Scheme_hash_table::set (SCM k, SCM v)
107 assert (scm_is_symbol (k));
108 SCM handle = scm_hashq_create_handle_x (hash_tab_, k, SCM_UNDEFINED);
109 if (scm_cdr (handle) == SCM_UNDEFINED)
112 scm_set_cdr_x (handle, v);
115 resize if getting too large.
117 if (elt_count_ > 2 * scm_c_vector_length (hash_tab_))
119 SCM nh = scm_make_vector (scm_from_int (3 * elt_count_ + 1), SCM_EOL);
120 elt_count_ = copy_scm_hashes (nh, hash_tab_);
127 Scheme_hash_table::get (SCM k) const
130 42 will stick out like a sore thumb, hopefully.
132 return scm_hashq_ref (hash_tab_, k, scm_from_int (42));
136 Scheme_hash_table::remove (SCM k)
138 scm_hashq_remove_x (hash_tab_, k);
139 /* Do not decrease elt_count_ as this may cause underflow. The exact
140 value of elt_count_ is not important. */
144 Scheme_hash_table::to_alist () const
147 for (int i = scm_c_vector_length (hash_tab_); i--;)
148 for (SCM s = scm_vector_ref (hash_tab_, scm_from_int (i)); scm_is_pair (s);
150 lst = scm_acons (scm_caar (s), scm_cdar (s), lst);
154 IMPLEMENT_SMOBS (Scheme_hash_table);
155 IMPLEMENT_DEFAULT_EQUAL_P (Scheme_hash_table);