]> git.donarmstrong.com Git - lilypond.git/blob - lily/scm-hash.cc
release: 1.3.75
[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
10 #include <stdio.h>
11 #include "ly-smobs.icc"
12 #include "scm-hash.hh"
13
14 Scheme_hash_table::Scheme_hash_table ()
15 {
16   smobify_self ();
17 }
18
19
20 Scheme_hash_table::Scheme_hash_table (Scheme_hash_table const &src)
21   : Scm_stl_map (src)
22 {
23   smobify_self ();
24 }
25
26 void
27 Scheme_hash_table::operator =(Scheme_hash_table const & src)
28 {
29   Scm_stl_map::operator = (src);
30         
31   // we do not copy the self_scm () field!
32 }
33
34
35
36
37 SCM
38 Scheme_hash_table::mark_smob (SCM s)
39 {
40   /*
41     can't typecheck naively, since GC bit lives in CAR of S
42    */
43   
44   Scheme_hash_table *me = (Scheme_hash_table*) SCM_CELL_WORD_1(s);
45
46   for (Scm_stl_map::const_iterator i= me->begin (); i != me->end(); i++)
47     {
48       scm_gc_mark ((*i).first);
49       scm_gc_mark ((*i).second);
50     }
51   return SCM_EOL;
52 }
53
54 int
55 Scheme_hash_table::print_smob (SCM s, SCM p, scm_print_state*)
56 {
57   assert (unsmob (s));
58   char str[1000];
59   sprintf (str, "#<Scheme_hash_table 0x%0x ", s);
60   scm_puts (str, p);      
61   Scheme_hash_table *me = unsmob(s);
62   for (Scm_stl_map::const_iterator i = me->begin (); i != me->end(); i++)
63     {
64       scm_display ((*i).first, p);
65       scm_puts (" = ",p);      
66       scm_display ((*i).second, p);
67       scm_puts ("\n",p);            
68     }
69   scm_puts ("> ",p);        
70   return 1;
71 }
72
73 bool
74 Scheme_hash_table::try_retrieve (SCM k, SCM *v)
75 {
76   Scm_stl_map ::const_iterator i (find (k));
77   bool found = i != end ();
78   if (found)
79     *v = (*i).second;
80   return found;
81 }
82
83 bool
84 Scheme_hash_table::elem_b (SCM k) const
85 {
86   Scm_stl_map::const_iterator i (find (k));
87   return i != end ();
88 }
89
90 void
91 Scheme_hash_table::set (SCM k, SCM v)
92 {
93   (*this)[k] = v;
94 }
95
96 // UGH. 
97 SCM
98 Scheme_hash_table::get (SCM k)const
99 {
100   return (*(Scheme_hash_table*)this)[k]; 
101 }
102
103
104 Scheme_hash_table::~Scheme_hash_table( )
105 {
106 }
107
108 SCM
109 Scheme_hash_table::to_alist () const
110 {
111   SCM l = SCM_EOL;
112   for (Scm_stl_map ::const_iterator i = begin (); i != end(); i++)
113     l = gh_cons (gh_cons ((*i).first, (*i).second), l);
114   return l;  
115 }
116
117
118 IMPLEMENT_UNSMOB(Scheme_hash_table,scheme_hash);
119 IMPLEMENT_SMOBS(Scheme_hash_table);
120 IMPLEMENT_DEFAULT_EQUAL_P(Scheme_hash_table);
121
122