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 copy_scm_hashes (SCM dest
, SCM src
)
17 for (int i
= SCM_SYMBOL_LENGTH (src
); i
--;)
18 for (SCM s
= scm_vector_ref (src
, SCM_MAKINUM (i
)); ly_pair_p(s
); s
= ly_cdr (s
))
20 scm_hashq_set_x (dest
, ly_caar (s
), ly_cdar (s
));
25 Scheme_hash_table::Scheme_hash_table ()
29 hash_tab_
= scm_make_vector (gh_int2scm (119), SCM_EOL
);
34 Scheme_hash_table::Scheme_hash_table (Scheme_hash_table
const &src
)
38 elt_count_
= src
.elt_count_
;
41 hash_tab_
= scm_make_vector (gh_int2scm (src
.elt_count_
>? 11 ), SCM_EOL
);
42 copy_scm_hashes (hash_tab_
, src
.hash_tab_
);
46 Scheme_hash_table::operator = (Scheme_hash_table
const & src
)
51 elt_count_
= src
.elt_count_
;
52 hash_tab_
= scm_make_vector (gh_int2scm (src
.elt_count_
>? 11), SCM_EOL
);
53 copy_scm_hashes (hash_tab_
, src
.hash_tab_
);
57 Scheme_hash_table::mark_smob (SCM s
)
59 Scheme_hash_table
*me
= (Scheme_hash_table
*) SCM_CELL_WORD_1 (s
);
60 scm_gc_mark (me
->hash_tab_
);
65 Scheme_hash_table::print_smob (SCM s
, SCM p
, scm_print_state
*)
69 sprintf (str
, "#<Scheme_hash_table 0x%0lx ", SCM_UNPACK(s
));
70 Scheme_hash_table
*me
= (Scheme_hash_table
*) SCM_CELL_WORD_1 (s
);
71 scm_display (me
->hash_tab_
, p
);
77 Scheme_hash_table::try_retrieve (SCM k
, SCM
*v
)
79 SCM handle
= scm_hashq_get_handle (hash_tab_
, k
);
80 if (ly_pair_p (handle
))
91 Scheme_hash_table::elem_b (SCM k
) const
93 return ly_pair_p (scm_hashq_get_handle (hash_tab_
, k
));
97 Scheme_hash_table::set (SCM k
, SCM v
)
99 assert (gh_symbol_p (k
));
100 SCM handle
= scm_hashq_create_handle_x (hash_tab_
, k
, SCM_UNDEFINED
);
101 if (ly_cdr (handle
) == SCM_UNDEFINED
)
106 gh_set_cdr_x (handle
, v
);
109 resize if getting too large.
111 if (elt_count_
> 2 * SCM_SYMBOL_LENGTH (hash_tab_
))
113 SCM nh
= scm_make_vector (gh_int2scm (3* elt_count_
+1), SCM_EOL
);
114 copy_scm_hashes (nh
, hash_tab_
);
122 Scheme_hash_table::get (SCM k
)const
125 42 will stick out like a sore thumb, hopefully.
127 return scm_hashq_ref (hash_tab_
, k
, SCM_MAKINUM(42));
131 Scheme_hash_table::remove (SCM k
)
133 scm_hashq_remove_x (hash_tab_
, k
);
137 Scheme_hash_table::~Scheme_hash_table ()
142 Scheme_hash_table::to_alist () const
145 for (int i
= SCM_SYMBOL_LENGTH (hash_tab_
); i
--;)
146 for (SCM s
= scm_vector_ref (hash_tab_
, gh_int2scm (i
)); ly_pair_p(s
); s
= ly_cdr (s
))
148 l
= scm_acons (ly_caar (s
), ly_cdar (s
), l
);
157 IMPLEMENT_SMOBS (Scheme_hash_table
);
158 IMPLEMENT_DEFAULT_EQUAL_P (Scheme_hash_table
);