Frank Zalkow <
fza...@gmail.com> writes:
> both structures and hash tables map keys to values.
Wrong.
Structures don't map keys. Actually, the ANSI Common Lisp does all it
can to dissuade the reader believing that structure may ever map keys:
"defstruct defines readers for the slots and arranges for setf to
work properly on such reader functions."
Those are very strong words, in the mouth of the standard…
Try to define the functions:
(defgeneric get-value (container key))
(defgeneric set-value (container key value))
with hash-tables, it's trivial:
(defmethod get-value ((container hash-table) key)
(multiple-value-bind (value presentp) (gethash key container)
(if presentp
value
(error "key ~S not present" key))))
(defmethod set-value ((container hash-table) key value)
(multiple-value-bind (old-value presentp) (gethash key container)
(if presentp
(setf (gethash key container) value)
(error "key ~S not present" key))))
with structures, it's a little more complex:
(defstruct point x y z)
(defmethod get-value ((container point) key)
(ecase key
((x) (point-x container))
((y) (point-y container))
((z) (point-z container))))
(defmethod set-value ((container point) key value)
(ecase key
((x) (setf (point-x container) value))
((y) (setf (point-y container) value))
((z) (setf (point-z container) value))))
> I would like to know what are the advantages and disadvantages of
> those and what are more idiomatic to use in what cases.
>
> As far as I see main differences are
> - hash-tables can grow, structure slots cannot
> - structures have a distinct type, hash tables haven't
> - hash-tables do have arbitrary keys, structures do have symbols as
> slot names
> - structures have some extras like read-only slots etc., that hash
> tables do not have
So you know it.
> A thing I am not sure about: Hash-Tables are hold in memory similar to
> arrays.
It is not specified.
> Structures can be of type VECTOR or LIST - but I think it is
> rather uncommon to specify the type. It is more common to leave it
> unspecified and than they are neither of type VECTOR nor of type LIST,
> they are of their own type and they are stored in memory in a
> implementation-dependent way. What it is a common choice for this
> implementation-dependent way?
Look at the sources of the implementations.
You have the FREEDOM to do that!
A FREEDOM that has been won by hard work and a long sociological
combat. Use it! GO READ THE FINE SOURCES!!!
> It is like a hash table with fixed size?
No, it's like a red sound.
> I did this very simple and unscientific time test in LispWorks and CMUCL:
> (defun test-hash ()
> (let ((foo (make-hash-table)))
> (setf (gethash 'a foo) 1)
> (setf (gethash 'b foo) 2)
> (setf (gethash 'c foo) 3)
> foo))
>
> (defstruct my-struct a b c)
> (defun test-struct ()
> (let ((foo (make-my-struct)))
> (setf (my-struct-a foo) 1)
> (setf (my-struct-b foo) 2)
> (setf (my-struct-c foo) 3)
> foo))
>
> Uncompiled, test-struct is faster in LispWorks whereas test-hash is
> faster in CMUCL. Compiled, test-hash is faster in both implementations.
There you are…
--
__Pascal Bourguignon__
http://www.informatimago.com/
"Le mercure monte ? C'est le moment d'acheter !"