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:testand:weakness. -
:test TEST - This specifies the method of key lookup for this hash table. The default is
eql;eqandequalare 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 usedefine-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, ortwhich is an alias forkey-and-value. If weak iskeythen 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 isvalue, 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 iskey-and-valueort, 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 iskey-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 isnil, 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. If you specify too small a size, the hash table will grow automatically when necessary, but doing that takes some extra time. The default size is 65.
-
:rehash-size REHASH-SIZE - When you add an association to a hash table and the table is full, it grows automatically. This value specifies how to make the hash table larger, at that time. If rehash-size is an integer, it should be positive, and the hash table grows by adding approximately that much to the nominal size. If rehash-size is floating point, it had better be greater than 1, and the hash table grows by multiplying the old size by approximately that number. The default value is 1.5.
-
:rehash-threshold THRESHOLD - This specifies the criterion for when the hash table is full (so it should be made larger). The value, threshold, should be a positive floating-point number, no greater than 1. The hash table is full whenever the actual number of entries exceeds the nominal size multiplied by an approximation to this value. The default for threshold is 0.8125.
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 size 30 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 size, test, weakness, rehash-size, rehash-threshold, 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 (:size, :test, etc.), 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,
remhashdoes nothing. Common Lisp note: In Common Lisp,remhashreturns non-nilif it actually removed an association andnilotherwise. In Emacs Lisp,remhashalways returnsnil. -
clrhash - This function removes all the associations from hash table table, so that it becomes empty. This is also called clearing the hash table.
clrhashreturns 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.
maphashreturnsnil.
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-nilif 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 propertyhash-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 notequal, the values returned bysxhash-equalare usually different, but not always.sxhash-equalis 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 fromsxhash-equal. So you can't, in general, usesxhash-equalto check whether an object has changed. Common Lisp note: In Common Lisp a similar function is calledsxhash. Emacs provides this name as a compatibility alias forsxhash-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
eqlcomparison. I.e. 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 areeql, 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-
nilif 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-rehash-size - This returns the rehash size of table.
-
hash-table-rehash-threshold - This returns the rehash threshold of table.
-
hash-table-size - This returns the current nominal size of table.