Merge branch 'lilypond/translation'
[lilypond/mpolesky.git] / lily / scm-hash.cc
blobbb73d52cd57bc7f90ce7105112be9120215f5d0d
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1999--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "scm-hash.hh"
22 #include <cstdio>
23 #include <algorithm>
24 using namespace std;
26 #include "ly-smobs.icc"
29 Return: number of objects.
31 SCM
32 copy_handle (void *closure, SCM handle)
34 SCM tab = (SCM) closure;
35 scm_hashq_set_x (tab, scm_car (handle), scm_cdr (handle));
36 return tab;
39 static void
40 copy_scm_hashes (SCM dest, SCM src)
42 scm_internal_hash_for_each_handle ( (SCM (*)(GUILE_ELLIPSIS)) &copy_handle, dest, src);
45 Scheme_hash_table::Scheme_hash_table ()
47 hash_tab_ = SCM_EOL;
48 smobify_self ();
49 hash_tab_ = scm_c_make_hash_table (119);
52 Scheme_hash_table::Scheme_hash_table (Scheme_hash_table const &src)
54 hash_tab_ = SCM_EOL;
55 smobify_self ();
56 copy (src);
59 void
60 Scheme_hash_table::copy (Scheme_hash_table const &src)
62 if (&src == this)
63 return;
65 hash_tab_ = scm_c_make_hash_table (SCM_HASHTABLE_N_ITEMS(src.hash_tab_));
66 copy_scm_hashes (hash_tab_, src.hash_tab_);
69 Scheme_hash_table::~Scheme_hash_table ()
73 SCM
74 Scheme_hash_table::mark_smob (SCM s)
76 Scheme_hash_table *me = (Scheme_hash_table *) SCM_CELL_WORD_1 (s);
77 scm_gc_mark (me->hash_tab_);
78 return SCM_EOL;
81 int
82 Scheme_hash_table::print_smob (SCM s, SCM p, scm_print_state*)
84 assert (unsmob (s));
85 scm_puts ("#<Scheme_hash_table ", p);
86 Scheme_hash_table *me = (Scheme_hash_table *) SCM_CELL_WORD_1 (s);
87 scm_display (me->hash_tab_, p);
88 scm_puts ("> ", p);
89 return 1;
92 bool
93 Scheme_hash_table::try_retrieve (SCM k, SCM *v){
95 SCM handle = scm_hashq_get_handle (hash_tab_, k);
96 if (scm_is_pair (handle))
98 *v = scm_cdr (handle);
99 return true;
101 else
102 return false;
105 bool
106 Scheme_hash_table::contains (SCM k) const
108 return scm_is_pair (scm_hashq_get_handle (hash_tab_, k));
111 void
112 Scheme_hash_table::set (SCM k, SCM v)
114 assert (scm_is_symbol (k));
115 SCM handle = scm_hashq_create_handle_x (hash_tab_, k, SCM_UNDEFINED);
116 scm_set_cdr_x (handle, v);
120 Scheme_hash_table::get (SCM k) const
122 /* SCM_UNSPECIFIED will stick out like a sore thumb, hopefully.
124 return scm_hashq_ref (hash_tab_, k, SCM_UNSPECIFIED);
127 void
128 Scheme_hash_table::remove (SCM k)
130 scm_hashq_remove_x (hash_tab_, k);
133 static SCM
134 collect_handles (void * /* closure */,
135 SCM key,
136 SCM value,
137 SCM result)
139 return scm_acons(key, value, result);
143 Scheme_hash_table::to_alist () const
145 return scm_internal_hash_fold ((SCM (*)(GUILE_ELLIPSIS)) &collect_handles, NULL, SCM_EOL, hash_tab_);
148 IMPLEMENT_SMOBS (Scheme_hash_table);
149 IMPLEMENT_DEFAULT_EQUAL_P (Scheme_hash_table);