-/*
- scm-hash.cc -- implement Scheme_hash_table
-
- source file of the GNU LilyPond music typesetter
-
- (c) 1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
-
- */
+/*
+ This file is part of LilyPond, the GNU music typesetter.
+
+ Copyright (C) 1999--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
+
+ LilyPond is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ LilyPond is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
+*/
#include "scm-hash.hh"
-#include "hash-table-iter.hh"
-Scheme_hash_table::Scheme_hash_table ()
-{
- hash_func_ = ly_scm_hash;
- self_scm_ = SCM_EOL;
- smobify_self ();
-}
+#include <cassert>
-void
-Scheme_hash_table::operator =(Scheme_hash_table const & src)
+SCM
+Scheme_hash_table::make_smob ()
{
- Hash_table<SCM,SCM>::operator = (src);
-
- // we do not copy the self_scm_ field!
+ return Smob1::make_smob (scm_c_make_hash_table (119));
}
-void
-Scheme_hash_table::do_smobify_self ()
+int
+Scheme_hash_table::print_smob (SCM p, scm_print_state *) const
{
+ scm_puts ("#<Scheme_hash_table ", p);
+ scm_display (hash_tab (), p);
+ scm_puts ("> ", p);
+ return 1;
}
-#include "ly-smobs.icc"
-IMPLEMENT_SMOBS(Scheme_hash_table);
-
-SCM
-Scheme_hash_table::mark_smob (SCM s)
+bool
+Scheme_hash_table::try_retrieve (SCM k, SCM *v)
{
- /*
- can't typecheck naively, since GC bit lives in CAR of S
- */
- //assert (SMOB_IS_TYPE_B (Scheme_hash_table, s));
-
- Scheme_hash_table *me = SMOB_TO_TYPE(Scheme_hash_table,s);
- for (Hash_table_iter<SCM,SCM> i (*me); i.ok(); i++)
+
+ SCM handle = scm_hashq_get_handle (hash_tab (), k);
+ if (scm_is_pair (handle))
{
- scm_gc_mark (i.key());
- scm_gc_mark (i.val ());
+ *v = scm_cdr (handle);
+ return true;
}
- return SCM_EOL;
+ else
+ return false;
}
-
-Scheme_hash_table::Scheme_hash_table (Scheme_hash_table const &src)
- : Hash_table<SCM,SCM> (src)
+bool
+Scheme_hash_table::contains (SCM k) const
{
- hash_func_ = src.hash_func_;
- self_scm_ = SCM_EOL;
- smobify_self ();
+ return scm_is_pair (scm_hashq_get_handle (hash_tab (), k));
}
-int
-Scheme_hash_table::print_smob (SCM s, SCM p, scm_print_state*ps)
+void
+Scheme_hash_table::set (SCM k, SCM v)
{
- assert (SMOB_IS_TYPE_B (Scheme_hash_table, s));
- Scheme_hash_table *me = SMOB_TO_TYPE(Scheme_hash_table,s);
- for (Hash_table_iter<SCM,SCM> i (*me); i.ok(); i++)
- {
- scm_display (i.key(), p);
- scm_puts (" = ",p);
- scm_display (i.val (), p);
- scm_puts ("\n",p);
- }
- return 1;
+ assert (scm_is_symbol (k));
+ SCM handle = scm_hashq_create_handle_x (hash_tab (), k, SCM_UNDEFINED);
+ scm_set_cdr_x (handle, v);
}
+SCM
+Scheme_hash_table::get (SCM k) const
+{
+ /* SCM_UNDEFINED is the default for unset elements, but
+ scm_hashq_ref cannot return it, so we do it a bit more awkwardly.
+ */
+ SCM handle = scm_hashq_get_handle (hash_tab (), k);
+ if (scm_is_pair (handle))
+ return scm_cdr (handle);
+ return SCM_UNDEFINED;
+}
+void
+Scheme_hash_table::remove (SCM k)
+{
+ scm_hashq_remove_x (hash_tab (), k);
+}
-
-Scheme_hash_table::~Scheme_hash_table( )
+static SCM
+collect_handles (void * /* closure */,
+ SCM key,
+ SCM value,
+ SCM result)
{
- unsmobify_self ();
+ return scm_acons (key, value, result);
}
SCM
Scheme_hash_table::to_alist () const
{
- SCM l = SCM_EOL;
- for (Hash_table_iter<SCM,SCM> i (*this); i.ok(); i++)
- l = gh_cons (gh_cons (i.key (), i.val()), l);
- return l;
+ return scm_internal_hash_fold ((scm_t_hash_fold_fn) &collect_handles,
+ NULL, SCM_EOL, hash_tab ());
}
-