Next: Hash Table Access, Up: Hash Tables [Contents][Index]
The principal function for creating a hash table is
make-hash-table
.
This function creates a new hash table according to the specified arguments. The arguments should consist of alternating keywords (particular symbols recognized specially) and values corresponding to them.
Several keywords make sense in make-hash-table
, but the only two
that you really need to know about are :test
and :weakness
.
:test test
This specifies the method of key lookup for this hash table. The
default is eql
; eq
and equal
are other
alternatives:
eql
Keys which are numbers are the same if they are equal
, that
is, if they are equal in value and either both are integers or both
are floating point; otherwise, two distinct objects are never
the same.
eq
Any two distinct Lisp objects are different as keys.
equal
Two Lisp objects are the same, as keys, if they are equal
according to equal
.
You can use define-hash-table-test
(see Defining Hash Comparisons) to
define additional possibilities for test.
:weakness weak
The weakness of a hash table specifies whether the presence of a key or value in the hash table preserves it from garbage collection.
The value, weak, must be one of nil
, key
,
value
, key-or-value
, key-and-value
, or t
which is an alias for key-and-value
. If weak is key
then the hash table does not prevent its keys from being collected as
garbage (if they are not referenced anywhere else); if a particular key
does get collected, the corresponding association is removed from the
hash table.
If weak is value
, then the hash table does not prevent
values from being collected as garbage (if they are not referenced
anywhere else); if a particular value does get collected, the
corresponding association is removed from the hash table.
If weak is key-and-value
or t
, both the key and
the value must be live in order to preserve the association. Thus,
the hash table does not protect either keys or values from garbage
collection; if either one is collected as garbage, that removes the
association.
If weak is key-or-value
, either the key or
the value can preserve the association. Thus, associations are
removed from the hash table when both their key and value would be
collected as garbage (if not for references from weak hash tables).
The default for weak is nil
, so that all keys and values
referenced in the hash table are preserved from garbage collection.
:size size
This specifies a hint for how many associations you plan to store in the hash table. If you know the approximate number, you can make things a little more efficient by specifying it this way but since the hash table memory is managed automatically, the gain in speed is rarely significant.
You can also create a hash table using the printed representation
for hash tables. The Lisp reader can read this printed
representation, provided each element in the specified hash table has
a valid read syntax (see Printed Representation and Read Syntax). For instance,
the following specifies a hash table containing the keys
key1
and key2
(both symbols) associated with val1
(a symbol) and 300
(a number) respectively.
#s(hash-table data (key1 val1 key2 300))
Note, however, that when using this in Emacs Lisp code, it’s
undefined whether this creates a new hash table or not. If you want
to create a new hash table, you should always use
make-hash-table
(see Self-Evaluating Forms).
The printed representation for a hash table consists of ‘#s’
followed by a list beginning with ‘hash-table’. The rest of the
list should consist of zero or more property-value pairs specifying
the hash table’s properties and initial contents. The properties and
values are read literally. Valid property names are test
,
weakness
and data
. The data
property
should be a list of key-value pairs for the initial contents; the
other properties have the same meanings as the matching
make-hash-table
keywords (:test
and :weakness
),
described above.
Note that you cannot specify a hash table whose initial contents include objects that have no read syntax, such as buffers and frames. Such objects may be added to the hash table after it is created.
Next: Hash Table Access, Up: Hash Tables [Contents][Index]