]> git.donarmstrong.com Git - lilypond.git/blob - lily/scm-hash.cc
Update.
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "scm-hash.hh"
10
11 #include <cstdio>
12
13 #include "ly-smobs.icc"
14
15 /*
16   Return: number of objects.
17 */
18 int
19 copy_scm_hashes (SCM dest, SCM src)
20 {
21   int k = 0;
22   for (int i = scm_c_vector_length (src); i--;)
23     for (SCM s = scm_vector_ref (src, scm_from_int (i)); scm_is_pair (s); s = scm_cdr (s))
24       {
25         scm_hashq_set_x (dest, scm_caar (s), scm_cdar (s));
26         k++;
27       }
28   return k;
29 }
30
31 Scheme_hash_table::Scheme_hash_table ()
32 {
33   hash_tab_ = SCM_EOL;
34   smobify_self ();
35   hash_tab_ = scm_make_vector (scm_int2num (119), SCM_EOL);
36   elt_count_ = 0;
37 }
38
39 Scheme_hash_table::Scheme_hash_table (Scheme_hash_table const &src)
40
41 {
42   hash_tab_ = SCM_EOL;
43   elt_count_ = 0;
44   smobify_self ();
45
46   hash_tab_ = scm_make_vector (scm_int2num (src.elt_count_ >? 11), SCM_EOL);
47   elt_count_ = copy_scm_hashes (hash_tab_, src.hash_tab_);
48 }
49
50 void
51 Scheme_hash_table::operator = (Scheme_hash_table const &src)
52 {
53   if (&src == this)
54     return;
55
56   hash_tab_ = scm_make_vector (scm_int2num (src.elt_count_ >? 11), SCM_EOL);
57   elt_count_ = copy_scm_hashes (hash_tab_, src.hash_tab_);
58 }
59
60 Scheme_hash_table::~Scheme_hash_table ()
61 {
62 }
63
64 SCM
65 Scheme_hash_table::mark_smob (SCM s)
66 {
67   Scheme_hash_table *me = (Scheme_hash_table *) SCM_CELL_WORD_1 (s);
68   scm_gc_mark (me->hash_tab_);
69   return SCM_EOL;
70 }
71
72 int
73 Scheme_hash_table::print_smob (SCM s, SCM p, scm_print_state*)
74 {
75   assert (unsmob (s));
76   char str[1000];
77   sprintf (str, "#<Scheme_hash_table 0x%0lx ", SCM_UNPACK (s));
78   Scheme_hash_table *me = (Scheme_hash_table *) SCM_CELL_WORD_1 (s);
79   scm_display (me->hash_tab_, p);
80   scm_puts ("> ", p);
81   return 1;
82 }
83
84 bool
85 Scheme_hash_table::try_retrieve (SCM k, SCM *v)
86 {
87   SCM handle = scm_hashq_get_handle (hash_tab_, k);
88   if (scm_is_pair (handle))
89     {
90       *v = scm_cdr (handle);
91       return true;
92     }
93   else
94     return false;
95 }
96
97 bool
98 Scheme_hash_table::contains (SCM k) const
99 {
100   return scm_is_pair (scm_hashq_get_handle (hash_tab_, k));
101 }
102
103 void
104 Scheme_hash_table::set (SCM k, SCM v)
105 {
106   assert (scm_is_symbol (k));
107   SCM handle = scm_hashq_create_handle_x (hash_tab_, k, SCM_UNDEFINED);
108   if (scm_cdr (handle) == SCM_UNDEFINED)
109     elt_count_++;
110
111   scm_set_cdr_x (handle, v);
112
113   /*
114     resize if getting too large.
115   */
116   if (elt_count_ > 2 * scm_c_vector_length (hash_tab_))
117     {
118       SCM nh = scm_make_vector (scm_int2num (3 * elt_count_ + 1), SCM_EOL);
119       elt_count_ = copy_scm_hashes (nh, hash_tab_);
120       hash_tab_ = nh;
121     }
122 }
123
124 // UGH.
125 SCM
126 Scheme_hash_table::get (SCM k) const
127 {
128   /*
129     42 will stick out like a sore thumb, hopefully.
130   */
131   return scm_hashq_ref (hash_tab_, k, scm_from_int (42));
132 }
133
134 void
135 Scheme_hash_table::remove (SCM k)
136 {
137   scm_hashq_remove_x (hash_tab_, k);
138   /* Do not decrease elt_count_ as this may cause underflow.  The exact
139      value of elt_count_ is not important. */
140 }
141
142 SCM
143 Scheme_hash_table::to_alist () const
144 {
145   SCM lst = SCM_EOL;
146   for (int i = scm_c_vector_length (hash_tab_); i--;)
147     for (SCM s = scm_vector_ref (hash_tab_, scm_int2num (i)); scm_is_pair (s);
148          s = scm_cdr (s))
149       lst = scm_acons (scm_caar (s), scm_cdar (s), lst);
150   return lst;
151 }
152
153 IMPLEMENT_SMOBS (Scheme_hash_table);
154 IMPLEMENT_DEFAULT_EQUAL_P (Scheme_hash_table);