lilypond-1.3.145
[lilypond.git] / lily / scm-hash.cc
blob08cac970949a14a460360f7035c68583e95e2c71
1 /*
2 scm-hash.cc -- implement Scheme_hash_table
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9 #include <stdio.h>
11 #include "scm-hash.hh"
12 #include "ly-smobs.icc"
15 Scheme_hash_table::Scheme_hash_table ()
17 smobify_self ();
21 Scheme_hash_table::Scheme_hash_table (Scheme_hash_table const &src)
22 : Scm_stl_map (src)
24 smobify_self ();
27 void
28 Scheme_hash_table::operator = (Scheme_hash_table const & src)
30 Scm_stl_map::operator = (src);
32 // we do not copy the self_scm () field!
35 SCM
36 Scheme_hash_table::mark_smob (SCM s)
39 can't typecheck naively, since GC bit lives in CAR of S
42 Scheme_hash_table *me = (Scheme_hash_table*) SCM_CELL_WORD_1 (s);
44 for (Scm_stl_map::const_iterator i= me->begin (); i != me->end (); i++)
46 scm_gc_mark ((*i).first);
47 scm_gc_mark ((*i).second);
49 return SCM_EOL;
52 int
53 Scheme_hash_table::print_smob (SCM s, SCM p, scm_print_state*)
55 assert (unsmob (s));
56 char str[1000];
57 sprintf (str, "#<Scheme_hash_table 0x%0x ", s);
58 scm_puts (str, p);
59 Scheme_hash_table *me = unsmob (s);
60 for (Scm_stl_map::const_iterator i = me->begin (); i != me->end (); i++)
62 scm_display ((*i).first, p);
63 scm_puts (" = ",p);
64 scm_display ((*i).second, p);
65 scm_puts ("\n",p);
67 scm_puts ("> ",p);
68 return 1;
71 bool
72 Scheme_hash_table::try_retrieve (SCM k, SCM *v)
74 Scm_stl_map ::const_iterator i (find (k));
75 bool found = i != end ();
76 if (found)
77 *v = (*i).second;
78 return found;
81 bool
82 Scheme_hash_table::elem_b (SCM k) const
84 Scm_stl_map::const_iterator i (find (k));
85 return i != end ();
88 void
89 Scheme_hash_table::set (SCM k, SCM v)
91 (*this)[k] = v;
94 // UGH.
95 SCM
96 Scheme_hash_table::get (SCM k)const
98 return (* (Scheme_hash_table*)this)[k];
101 void
102 Scheme_hash_table::remove (SCM k)
104 Scm_stl_map::erase (k);
107 Scheme_hash_table::~Scheme_hash_table ()
112 Scheme_hash_table::to_alist () const
114 SCM l = SCM_EOL;
115 for (Scm_stl_map ::const_iterator i = begin (); i != end (); i++)
116 l = gh_cons (gh_cons ((*i).first, (*i).second), l);
117 return l;
122 IMPLEMENT_UNSMOB (Scheme_hash_table,scheme_hash);
123 IMPLEMENT_SMOBS (Scheme_hash_table);
124 IMPLEMENT_DEFAULT_EQUAL_P (Scheme_hash_table);