]> git.donarmstrong.com Git - lilypond.git/blob - lily/scm-hash.cc
97af8f6cd5f97ab742aafc029d7a87785d4fd18b
[lilypond.git] / lily / scm-hash.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "scm-hash.hh"
21
22 #include <cassert>
23
24 const char * const Scheme_hash_table::type_p_name_ = 0;
25
26 SCM
27 Scheme_hash_table::make_smob ()
28 {
29   return Smob1::make_smob (scm_c_make_hash_table (119));
30 }
31
32 int
33 Scheme_hash_table::print_smob (SCM p, scm_print_state *) const
34 {
35   scm_puts ("#<Scheme_hash_table  ", p);
36   scm_display (hash_tab (), p);
37   scm_puts ("> ", p);
38   return 1;
39 }
40
41 bool
42 Scheme_hash_table::try_retrieve (SCM k, SCM *v)
43 {
44
45   SCM handle = scm_hashq_get_handle (hash_tab (), k);
46   if (scm_is_pair (handle))
47     {
48       *v = scm_cdr (handle);
49       return true;
50     }
51   else
52     return false;
53 }
54
55 bool
56 Scheme_hash_table::contains (SCM k) const
57 {
58   return scm_is_pair (scm_hashq_get_handle (hash_tab (), k));
59 }
60
61 void
62 Scheme_hash_table::set (SCM k, SCM v)
63 {
64   assert (scm_is_symbol (k));
65   SCM handle = scm_hashq_create_handle_x (hash_tab (), k, SCM_UNDEFINED);
66   scm_set_cdr_x (handle, v);
67 }
68
69 SCM
70 Scheme_hash_table::get (SCM k) const
71 {
72   /* SCM_UNDEFINED is the default for unset elements, but
73      scm_hashq_ref cannot return it, so we do it a bit more awkwardly.
74   */
75   SCM handle = scm_hashq_get_handle (hash_tab (), k);
76   if (scm_is_pair (handle))
77     return scm_cdr (handle);
78   return SCM_UNDEFINED;
79 }
80
81 void
82 Scheme_hash_table::remove (SCM k)
83 {
84   scm_hashq_remove_x (hash_tab (), k);
85 }
86
87 static SCM
88 collect_handles (void * /* closure */,
89                  SCM key,
90                  SCM value,
91                  SCM result)
92 {
93   return scm_acons (key, value, result);
94 }
95
96 SCM
97 Scheme_hash_table::to_alist () const
98 {
99   return scm_internal_hash_fold ((scm_t_hash_fold_fn) &collect_handles,
100                                  NULL, SCM_EOL, hash_tab ());
101 }