GNU Emacs
ELisp
Hash Tables

Hash Tables

A hash table is a very fast kind of lookup table, somewhat like an alist (Association Lists) in that it maps keys to corresponding values. It differs from an alist in these ways:

  • Lookup in a hash table is extremely fast for large tables—in fact, the time required is essentially independent of how many elements are stored in the table. For smaller tables (a few tens of elements) alists may still be faster because hash tables have a more-or-less constant overhead.
  • The correspondences in a hash table are in no particular order.
  • There is no way to share structure between two hash tables, the way two alists can share a common tail.

Emacs Lisp provides a general-purpose hash table data type, along with a series of functions for operating on them. Hash tables have a special printed representation, which consists of #s followed by a list specifying the hash table properties and contents. Creating Hash. (Hash notation, the initial # character used in the printed representations of objects with no read representation, has nothing to do with hash tables. Printed Representation.) Obarrays are also a kind of hash table, but they are a different type of object and are used only for recording interned symbols (Creating Symbols).

Creating Hash Tables

The principal function for creating a hash table is make-hash-table.

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 (Defining Hash) 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 (Printed Representation). 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 (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.

Hash Table Access

This section describes the functions for accessing and storing associations in a hash table. In general, any Lisp object can be used as a hash key, unless the comparison method imposes limits. Any Lisp object can also be used as the value.

gethash
This function looks up key in table, and returns its associated value—or default, if key has no association in table.
puthash
This function enters an association for key in table, with value value. If key already has an association in table, value replaces the old associated value. This function always returns value.
remhash
This function removes the association for key from table, if there is one. If key has no association, remhash does nothing. Common Lisp note: In Common Lisp, remhash returns non-nil if it actually removed an association and nil otherwise. In Emacs Lisp, remhash always returns nil.
clrhash
This function removes all the associations from hash table table, so that it becomes empty. This is also called clearing the hash table. clrhash returns the empty table.
maphash
This function calls function once for each of the associations in table. The function function should accept two arguments—a key listed in table, and its associated value. maphash returns nil. function is allowed to call puthash to set a new value for key and remhash to remove key, but should not add, remove or modify other associations in table.

Defining Hash Comparisons

You can define new methods of key lookup by means of define-hash-table-test. In order to use this feature, you need to understand how hash tables work, and what a hash code means. You can think of a hash table conceptually as a large array of many slots, each capable of holding one association. To look up a key, gethash first computes an integer, the hash code, from the key. It can reduce this integer modulo the length of the array, to produce an index in the array. Then it looks in that slot, and if necessary in other nearby slots, to see if it has found the key being sought. Thus, to define a new method of key lookup, you need to specify both a function to compute the hash code from a key, and a function to compare two keys directly. The two functions should be consistent with each other: that is, two keys' hash codes should be the same if the keys compare as equal. Also, since the two functions can be called at any time (such as by the garbage collector), the functions should be free of side effects and should return quickly, and their behavior should depend on only on properties of the keys that do not change.

define-hash-table-test
This function defines a new hash table test, named name. After defining name in this way, you can use it as the test argument in make-hash-table. When you do that, the hash table will use test-fn to compare key values, and hash-fn to compute a hash code from a key value. The function test-fn should accept two arguments, two keys, and return non-nil if they are considered the same. The function hash-fn should accept one argument, a key, and return an integer that is the hash code of that key. For good results, the function should use the whole range of fixnums for hash codes, including negative fixnums. The specified functions are stored in the property list of name under the property hash-table-test; the property value's form is (TEST-FN HASH-FN).
sxhash-equal
This function returns a hash code for Lisp object obj. This is an integer that reflects the contents of obj and the other Lisp objects it points to. If two objects obj1 and obj2 are equal, then (sxhash-equal OBJ1) and (sxhash-equal OBJ2) are the same integer. If the two objects are not equal, the values returned by sxhash-equal are usually different, but not always. sxhash-equal is designed to be reasonably fast (since it's used for indexing hash tables) so it won't recurse deeply into nested structures. In addition; once in a rare while, by luck, you will encounter two distinct-looking simple objects that give the same result from sxhash-equal. So you can't, in general, use sxhash-equal to check whether an object has changed. Common Lisp note: In Common Lisp a similar function is called sxhash. Emacs provides this name as a compatibility alias for sxhash-equal.
sxhash-eq
This function returns a hash code for Lisp object obj. Its result reflects identity of obj, but not its contents. If two objects obj1 and obj2 are eq, then (sxhash-eq OBJ1) and (sxhash-eq OBJ2) are the same integer.
sxhash-eql
This function returns a hash code for Lisp object obj suitable for eql comparison. In other words, it reflects identity of obj except for the case where the object is a bignum or a float number, in which case a hash code is generated for the value. If two objects obj1 and obj2 are eql, then (sxhash-eql OBJ1) and (sxhash-eql OBJ2) are the same integer.

This example creates a hash table whose keys are strings that are compared case-insensitively.

(defun string-hash-ignore-case (a)
  (sxhash-equal (upcase a)))

(define-hash-table-test 'ignore-case
  'string-equal-ignore-case 'string-hash-ignore-case)

(make-hash-table :test 'ignore-case)

Here is how you could define a hash table test equivalent to the predefined test value equal. The keys can be any Lisp object, and equal-looking objects are considered the same key.

(define-hash-table-test 'contents-hash 'equal 'sxhash-equal)

(make-hash-table :test 'contents-hash)

Lisp programs should not rely on hash codes being preserved between Emacs sessions, as the implementation of the hash functions uses some details of the object storage that can change between sessions and between different architectures.

Other Hash Table Functions

Here are some other functions for working with hash tables.

hash-table-p
This returns non-nil if table is a hash table object.
copy-hash-table
This function creates and returns a copy of table. Only the table itself is copied—the keys and values are shared.
hash-table-count
This function returns the actual number of entries in table.
hash-table-test
This returns the test value that was given when table was created, to specify how to hash and compare keys. See make-hash-table (Creating Hash).
hash-table-weakness
This function returns the weak value that was specified for hash table table.
hash-table-size
This returns the current allocation size of table. Since hash table allocation is managed automatically, this is rarely of interest.
Manual
Emacs Lisp 30.2
Texinfo Node
Hash Tables
Source Ref
emacs-30.2
Source
View upstream