(staff_eligible): new function.
[lilypond.git] / lily / scm-hash.cc
blob1a49a39b549f4e4b75785e58efa594e7d2db52f3
1 /*
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>
8 */
9 #include <stdio.h>
11 #include "scm-hash.hh"
12 #include "ly-smobs.icc"
15 Return: number of objects.
17 int
18 copy_scm_hashes (SCM dest, SCM src)
20 int k = 0;
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));
25 k++;
27 return k ;
31 Scheme_hash_table::Scheme_hash_table ()
33 hash_tab_ = SCM_EOL;
34 smobify_self ();
35 hash_tab_ = scm_make_vector (gh_int2scm (119), SCM_EOL);
36 elt_count_ = 0;
40 Scheme_hash_table::Scheme_hash_table (Scheme_hash_table const &src)
43 hash_tab_ = SCM_EOL;
44 elt_count_ = 0;
45 smobify_self ();
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_);
51 void
52 Scheme_hash_table::operator = (Scheme_hash_table const & src)
54 if (&src == this)
55 return;
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_);
61 SCM
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_);
66 return SCM_EOL;
69 int
70 Scheme_hash_table::print_smob (SCM s, SCM p, scm_print_state*)
72 assert (unsmob (s));
73 char str[1000];
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);
77 scm_puts ("> ",p);
78 return 1;
81 bool
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))
87 *v = ly_cdr (handle);
88 return true;
90 else
91 return false;
95 bool
96 Scheme_hash_table::elem_b (SCM k) const
98 return ly_pair_p (scm_hashq_get_handle (hash_tab_, k));
101 void
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)
108 elt_count_++;
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_);
120 hash_tab_ = nh;
124 // UGH.
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));
134 void
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
151 SCM l = SCM_EOL;
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);
157 return l;
164 IMPLEMENT_SMOBS (Scheme_hash_table);
165 IMPLEMENT_DEFAULT_EQUAL_P (Scheme_hash_table);