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