]> git.donarmstrong.com Git - lilypond.git/blob - lily/scm-hash.cc
patch::: 1.3.33.jcn3
[lilypond.git] / lily / scm-hash.cc
1 /*   
2   scm-hash.cc --  implement Scheme_hash_table
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include <stdio.h>
10
11 #include "scm-hash.hh"
12 #include "hash-table-iter.hh"
13
14
15 #ifdef usestl
16
17
18 Scheme_hash_table::Scheme_hash_table ()
19 {
20   self_scm_ = SCM_EOL;
21   smobify_self ();
22 }
23
24 void
25 Scheme_hash_table::operator =(Scheme_hash_table const & src)
26 {
27   Scm_stl_map::operator = (src);
28         
29   // we do not copy the self_scm_ field!
30 }
31
32 void
33 Scheme_hash_table::do_smobify_self ()
34 {
35 }
36
37
38 SCM
39 Scheme_hash_table::mark_smob (SCM s)
40 {
41   /*
42     can't typecheck naively, since GC bit lives in CAR of S
43    */
44   
45   Scheme_hash_table *me = SMOB_TO_TYPE(Scheme_hash_table,s);
46
47   for (Scm_stl_map::const_iterator i= me->begin (); i != me->end(); i++)
48     {
49       scm_gc_mark ((*i).first);
50       scm_gc_mark ((*i).second);
51     }
52   return SCM_EOL;
53 }
54
55
56 Scheme_hash_table::Scheme_hash_table (Scheme_hash_table const &src)
57   : Scm_stl_map (src)
58 {
59   self_scm_ = SCM_EOL;
60   smobify_self ();
61 }
62
63 int
64 Scheme_hash_table::print_smob (SCM s, SCM p, scm_print_state*)
65 {
66   assert (SMOB_IS_TYPE_B (Scheme_hash_table, s));
67   char str[1000];
68   sprintf (str, "#<Scheme_hash_table 0x%0x ", s);
69   scm_puts (str, p);      
70   Scheme_hash_table *me = SMOB_TO_TYPE(Scheme_hash_table,s);
71   for (Scm_stl_map ::const_iterator i = me->begin (); i != me->end(); i++)
72     {
73       scm_display ((*i).first, p);
74       scm_puts (" = ",p);      
75       scm_display ((*i).second, p);
76       scm_puts ("\n",p);            
77     }
78   scm_puts ("> ",p);        
79   return 1;
80 }
81
82 bool
83 Scheme_hash_table::try_retrieve (SCM k, SCM *v)
84 {
85   Scm_stl_map ::const_iterator i (find (k));
86   bool found = i != end ();
87   if (found)
88     *v = (*i).second;
89   return found;
90 }
91
92 bool
93 Scheme_hash_table::elem_b (SCM k) const
94 {
95   Scm_stl_map::const_iterator i (find (k));
96   return i != end ();
97 }
98
99 void
100 Scheme_hash_table::set (SCM k, SCM v)
101 {
102   (*this)[k] = v;
103   scm_unprotect_object (v);
104 }
105
106 // UGH. 
107 SCM
108 Scheme_hash_table::get (SCM k)const
109 {
110   return (*(Scheme_hash_table*)this)[k]; 
111 }
112
113
114 Scheme_hash_table::~Scheme_hash_table( )
115 {
116   unsmobify_self ();
117 }
118
119 SCM
120 Scheme_hash_table::to_alist () const
121 {
122   SCM l = SCM_EOL;
123   for (Scm_stl_map ::const_iterator i = begin (); i != end(); i++)
124     l = gh_cons (gh_cons ((*i).first, (*i).second), l);
125   return l;  
126 }
127
128
129 #include "ly-smobs.icc"
130 IMPLEMENT_UNSMOB(Scheme_hash_table,scheme_hash);
131 IMPLEMENT_SMOBS(Scheme_hash_table);
132
133 #else
134 Scheme_hash_table::Scheme_hash_table ()
135 {
136   hash_func_ = ly_scm_hash;
137   self_scm_ = SCM_EOL;
138   smobify_self ();
139 }
140
141 void
142 Scheme_hash_table::operator =(Scheme_hash_table const & src)
143 {
144   Hash_table<SCM,SCM>::operator = (src);
145         
146   // we do not copy the self_scm_ field!
147 }
148
149 void
150 Scheme_hash_table::do_smobify_self ()
151 {
152 }
153
154
155 SCM
156 Scheme_hash_table::mark_smob (SCM s)
157 {
158   /*
159     can't typecheck naively, since GC bit lives in CAR of S
160    */
161   //assert (SMOB_IS_TYPE_B (Scheme_hash_table, s));
162   
163   Scheme_hash_table *me = SMOB_TO_TYPE(Scheme_hash_table,s);
164   for (Hash_table_iter<SCM,SCM> i (*me); i.ok(); i++)
165     {
166       scm_gc_mark (i.key());
167       scm_gc_mark (i.val ());
168     }
169   return SCM_EOL;
170 }
171
172
173 Scheme_hash_table::Scheme_hash_table (Scheme_hash_table const &src)
174   : Hash_table<SCM,SCM> (src)
175 {
176   hash_func_ = src.hash_func_;
177   self_scm_ = SCM_EOL;
178   smobify_self ();
179 }
180
181 int
182 Scheme_hash_table::print_smob (SCM s, SCM p, scm_print_state*)
183 {
184   assert (SMOB_IS_TYPE_B (Scheme_hash_table, s));
185   char str[1000];
186   sprintf (str, "#<Scheme_hash_table 0x%0x ", s);
187   scm_puts (str, p);      
188   Scheme_hash_table *me = SMOB_TO_TYPE(Scheme_hash_table,s);
189   for (Hash_table_iter<SCM,SCM> i (*me); i.ok(); i++)
190     {
191       scm_display (i.key(), p);
192       scm_puts (" = ",p);      
193       scm_display (i.val (), p);
194       scm_puts ("\n",p);            
195     }
196   scm_puts ("> ",p);        
197   return 1;
198 }
199
200
201 void
202 Scheme_hash_table::set (SCM k, SCM v)
203 {
204   elem (k ) = v; 
205   scm_unprotect_object (v);
206 }
207
208 SCM
209 Scheme_hash_table::get (SCM k)const
210 {
211   return const_elem (k);
212 }
213
214
215 Scheme_hash_table::~Scheme_hash_table( )
216 {
217   unsmobify_self ();
218 }
219
220 SCM
221 Scheme_hash_table::to_alist () const
222 {
223   SCM l = SCM_EOL;
224   for (Hash_table_iter<SCM,SCM> i (*this); i.ok(); i++)
225     l = gh_cons (gh_cons (i.key (), i.val()), l);
226   return l;  
227 }
228
229
230 #include "ly-smobs.icc"
231 IMPLEMENT_UNSMOB(Scheme_hash_table,scheme_hash);
232 IMPLEMENT_SMOBS(Scheme_hash_table);
233 #endif