]> git.donarmstrong.com Git - lilypond.git/blob - lily/scm-hash.cc
Issue 4550 (2/2) Avoid "using namespace std;" in included files
[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 <cstdio>
23 #include <algorithm>
24
25
26 /*
27   Return: number of objects.
28 */
29 SCM
30 copy_handle (void *closure, SCM handle)
31 {
32   SCM tab = (SCM) closure;
33   scm_hashq_set_x (tab, scm_car (handle), scm_cdr (handle));
34   return tab;
35 }
36
37 static void
38 copy_scm_hashes (SCM dest, SCM src)
39 {
40   scm_internal_hash_for_each_handle ((scm_t_hash_handle_fn) &copy_handle,
41                                      dest, src);
42 }
43
44 Scheme_hash_table::Scheme_hash_table ()
45 {
46   hash_tab_ = SCM_EOL;
47   smobify_self ();
48   hash_tab_ = scm_c_make_hash_table (119);
49 }
50
51 Scheme_hash_table::Scheme_hash_table (Scheme_hash_table const &src)
52   : Smob<Scheme_hash_table> ()
53 {
54   hash_tab_ = SCM_EOL;
55   smobify_self ();
56   copy (src);
57 }
58
59 void
60 Scheme_hash_table::copy (Scheme_hash_table const &src)
61 {
62   if (&src == this)
63     return;
64
65   hash_tab_ = scm_c_make_hash_table (SCM_HASHTABLE_N_ITEMS (src.hash_tab_));
66   copy_scm_hashes (hash_tab_, src.hash_tab_);
67 }
68
69 Scheme_hash_table::~Scheme_hash_table ()
70 {
71 }
72
73 SCM
74 Scheme_hash_table::mark_smob () const
75 {
76   scm_gc_mark (hash_tab_);
77   return SCM_EOL;
78 }
79
80 int
81 Scheme_hash_table::print_smob (SCM p, scm_print_state *) const
82 {
83   scm_puts ("#<Scheme_hash_table  ", p);
84   scm_display (hash_tab_, p);
85   scm_puts ("> ", p);
86   return 1;
87 }
88
89 bool
90 Scheme_hash_table::try_retrieve (SCM k, SCM *v)
91 {
92
93   SCM handle = scm_hashq_get_handle (hash_tab_, k);
94   if (scm_is_pair (handle))
95     {
96       *v = scm_cdr (handle);
97       return true;
98     }
99   else
100     return false;
101 }
102
103 bool
104 Scheme_hash_table::contains (SCM k) const
105 {
106   return scm_is_pair (scm_hashq_get_handle (hash_tab_, k));
107 }
108
109 void
110 Scheme_hash_table::set (SCM k, SCM v)
111 {
112   assert (scm_is_symbol (k));
113   SCM handle = scm_hashq_create_handle_x (hash_tab_, k, SCM_UNDEFINED);
114   scm_set_cdr_x (handle, v);
115 }
116
117 SCM
118 Scheme_hash_table::get (SCM k) const
119 {
120   /* SCM_UNSPECIFIED will stick out like a sore thumb, hopefully.
121   */
122   return scm_hashq_ref (hash_tab_, k, SCM_UNSPECIFIED);
123 }
124
125 void
126 Scheme_hash_table::remove (SCM k)
127 {
128   scm_hashq_remove_x (hash_tab_, k);
129 }
130
131 static SCM
132 collect_handles (void * /* closure */,
133                  SCM key,
134                  SCM value,
135                  SCM result)
136 {
137   return scm_acons (key, value, result);
138 }
139
140 SCM
141 Scheme_hash_table::to_alist () const
142 {
143   return scm_internal_hash_fold ((scm_t_hash_fold_fn) &collect_handles,
144                                  NULL, SCM_EOL, hash_tab_);
145 }