Bump version.
[lilypond.git] / lily / scm-hash.cc
blob4013af327913b44da4b2dc38a0eb182d0f15a824
1 /*
2 scm-hash.cc -- implement Scheme_hash_table
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "scm-hash.hh"
11 #include <cstdio>
12 #include <algorithm>
13 using namespace std;
15 #include "ly-smobs.icc"
18 Return: number of objects.
20 SCM
21 copy_handle (void *closure, SCM handle)
23 SCM tab = (SCM) closure;
24 scm_hashq_set_x (tab, scm_car (handle), scm_cdr (handle));
25 return tab;
28 static void
29 copy_scm_hashes (SCM dest, SCM src)
31 scm_internal_hash_for_each_handle ( (SCM (*)(GUILE_ELLIPSIS)) &copy_handle, dest, src);
34 Scheme_hash_table::Scheme_hash_table ()
36 hash_tab_ = SCM_EOL;
37 smobify_self ();
38 hash_tab_ = scm_c_make_hash_table (119);
41 Scheme_hash_table::Scheme_hash_table (Scheme_hash_table const &src)
43 hash_tab_ = SCM_EOL;
44 smobify_self ();
45 copy (src);
48 void
49 Scheme_hash_table::copy (Scheme_hash_table const &src)
51 if (&src == this)
52 return;
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 ()
62 SCM
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_);
67 return SCM_EOL;
70 int
71 Scheme_hash_table::print_smob (SCM s, SCM p, scm_print_state*)
73 assert (unsmob (s));
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);
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 (scm_is_pair (handle))
87 *v = scm_cdr (handle);
88 return true;
90 else
91 return false;
94 bool
95 Scheme_hash_table::contains (SCM k) const
97 return scm_is_pair (scm_hashq_get_handle (hash_tab_, k));
100 void
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);
116 void
117 Scheme_hash_table::remove (SCM k)
119 scm_hashq_remove_x (hash_tab_, k);
122 static SCM
123 collect_handles (void * /* closure */,
124 SCM key,
125 SCM value,
126 SCM result)
128 return scm_acons(key, value, result);
132 Scheme_hash_table::to_alist () const
134 return scm_internal_hash_fold ((SCM (*)(GUILE_ELLIPSIS)) &collect_handles, NULL, SCM_EOL, hash_tab_);
137 IMPLEMENT_SMOBS (Scheme_hash_table);
138 IMPLEMENT_DEFAULT_EQUAL_P (Scheme_hash_table);