2 scm-hash.cc -- implement Scheme_hash_table
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 #include "scm-hash.hh"
12 #include "ly-smobs.icc"
15 Return: number of objects.
18 copy_scm_hashes (SCM dest, SCM src)
21 for (int i = SCM_VECTOR_LENGTH (src); i--;)
22 for (SCM s = scm_vector_ref (src, SCM_MAKINUM (i)); ly_pair_p(s); s = ly_cdr (s))
24 scm_hashq_set_x (dest, ly_caar (s), ly_cdar (s));
31 Scheme_hash_table::Scheme_hash_table ()
35 hash_tab_ = scm_make_vector (gh_int2scm (119), SCM_EOL);
40 Scheme_hash_table::Scheme_hash_table (Scheme_hash_table const &src)
47 hash_tab_ = scm_make_vector (gh_int2scm (src.elt_count_ >? 11 ), SCM_EOL);
48 elt_count_ = copy_scm_hashes (hash_tab_, src.hash_tab_);
52 Scheme_hash_table::operator = (Scheme_hash_table const & src)
57 hash_tab_ = scm_make_vector (gh_int2scm (src.elt_count_ >? 11), SCM_EOL);
58 elt_count_ = copy_scm_hashes (hash_tab_, src.hash_tab_);
62 Scheme_hash_table::mark_smob (SCM s)
64 Scheme_hash_table *me = (Scheme_hash_table*) SCM_CELL_WORD_1 (s);
65 scm_gc_mark (me->hash_tab_);
70 Scheme_hash_table::print_smob (SCM s, SCM p, scm_print_state*)
74 sprintf (str, "#<Scheme_hash_table 0x%0lx ", SCM_UNPACK(s));
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 (ly_pair_p (handle))
96 Scheme_hash_table::elem_b (SCM k) const
98 return ly_pair_p (scm_hashq_get_handle (hash_tab_, k));
102 Scheme_hash_table::set (SCM k, SCM v)
104 assert (gh_symbol_p (k));
105 SCM handle = scm_hashq_create_handle_x (hash_tab_, k, SCM_UNDEFINED);
106 if (ly_cdr (handle) == SCM_UNDEFINED)
111 gh_set_cdr_x (handle, v);
114 resize if getting too large.
116 if (elt_count_ > 2 * SCM_VECTOR_LENGTH (hash_tab_))
118 SCM nh = scm_make_vector (gh_int2scm (3* elt_count_+1), SCM_EOL);
119 elt_count_ = copy_scm_hashes (nh, hash_tab_);
126 Scheme_hash_table::get (SCM k)const
129 42 will stick out like a sore thumb, hopefully.
131 return scm_hashq_ref (hash_tab_, k, SCM_MAKINUM(42));
135 Scheme_hash_table::remove (SCM k)
137 scm_hashq_remove_x (hash_tab_, k);
139 don't decrease elt_count_ , as this may cause underflow. The exact
140 value of elt_count_ is not important.
144 Scheme_hash_table::~Scheme_hash_table ()
149 Scheme_hash_table::to_alist () const
152 for (int i = SCM_VECTOR_LENGTH (hash_tab_); i--;)
153 for (SCM s = scm_vector_ref (hash_tab_, gh_int2scm (i)); ly_pair_p(s); s = ly_cdr (s))
155 l = scm_acons (ly_caar (s), ly_cdar (s), l);
164 IMPLEMENT_SMOBS (Scheme_hash_table);
165 IMPLEMENT_DEFAULT_EQUAL_P (Scheme_hash_table);