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