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