Hash-Tables

Create

make-hash-table [:test :size :rehash-size :rehash-threshold] ⇒ hash-table

Create and return a new hash-table. Default test is #'eql.

(defparameter ht (make-hash-table)) ;⇒ #<HASH-TABLE :TEST EQL :COUNT 0>
(setf (gethash :foo ht) 'bar)       ;⇒ BAR

alexandria:copy-hash-table hash-table [:key :test :size :rehash-size :rehash-threshold] ⇒ hash-table

Returns a shallow copy of hash-table with the same keys, values, and properties as the original.

(setf ht2 (alexandria:copy-hash-table ht)) ;⇒ #<HASH-TABLE :TEST EQL :COUNT 1>
(gethash :foo ht2)                         ;⇒ BAR, T

alexandria:alist-hash-table alist [:test :size :rehash-size :rehash-threshold] ⇒ hash-table

Return a hash-table containing the keys and values of the association-list alist. Takes the same arguments as make-hash-table.

(setf ht (alexandria:alist-hash-table '((:x . 7) (:y . 8)))) ;⇒ #<HASH-TABLE :TEST EQL :COUNT 2>
(gethash :x ht)                                              ;⇒ 7, T

alexandria:plist-hash-table plist [:test :size :rehash-size :rehash-threshold] ⇒ hash-table

Return a hash-table containing the keys and values of the property-list plist. Takes the same arguments as make-hash-table.

(setf ht (alexandria:plist-hash-table '(:x 10 :y 20))) ;⇒ #<HASH-TABLE :TEST EQL :COUNT 2>
(gethash :y ht)                                        ;⇒ 20, T

alexandria:hash-table-alist hash-table ⇒ list

Return an association-list containing the keys and values of hash-table.

(alexandria:hash-table-alist ht) ;⇒ ((:Y . 20) (:X . 10))

alexandria:hash-table-plist hash-table ⇒ list

Return a property-list containing the keys and values of hash-table.

(alexandria:hash-table-plist ht) ;⇒ (:Y 8 :X 7)

Predicates

hash-table-p object ⇒ boolean

Test if object is of type hash-table.

(setf ht (make-hash-table))         ;⇒ #<HASH-TABLE :TEST EQL :COUNT 0>
(hash-table-p ht)                   ;⇒ T
(hash-table-p '((:x . 7) (:y . 8))) ;⇒ NIL

Values

hash-table-count hash-table ⇒ n

Return the number of entries in hash-table.

(setf ht (make-hash-table))   ;⇒ #<HASH-TABLE :TEST EQL :COUNT 0>
(hash-table-count ht)         ;⇒ 0
(setf (gethash :foo ht) 'bar)
(hash-table-count ht)         ;⇒ 1

gethash key hash-table [default] ⇒ value, present-p

Return a key’s entry value in hash-table, or default if none. If entry is found, present-p is t. Store values with setf.

(gethash :foo ht)             ;⇒ NIL, NIL
(gethash :foo ht 'default)    ;⇒ DEFAULT, NIL
(setf (gethash :foo ht) 'bar)
(gethash :foo ht)             ;⇒ BAR, T

alexandria:ensure-gethash key hash-table [default] ⇒ value, present-p

Like gethash, but if key is not found in hash-table, save default as an entry before returning it.

(setf ht (make-hash-table))          ;⇒ #<HASH-TABLE :TEST EQL :COUNT 0>
(alexandria:ensure-gethash :z ht 99) ;⇒ 99, NIL
(gethash :z ht)                      ;⇒ 99, T

remhash key hash-table ⇒ present-p

Removes entry for key in hash-table. Returns t if there was such an entry, or nil otherwise.

(setf ht (alexandria:plist-hash-table '(:x 10 :y 20)))
(remhash :x ht)                                        ;⇒ T
(remhash :z ht)                                        ;⇒ NIL

clrhash hash-table ⇒ hash-table

Removes all entries from hash-table, and then returns that empty hash-table.

(setf ht (alexandria:plist-hash-table '(:x 10 :y 20)))
(hash-table-count ht)                                  ;⇒ 2
(clrhash ht)                                           ;⇒ #<HASH-TABLE :TEST EQL :COUNT 0>
(hash-table-count ht)                                  ;⇒ 0

alexandria:hash-table-keys hash-table ⇒ list

Returns a list containing the keys of hash-table.

(setf ht (alexandria:plist-hash-table '(:x 10 :y 20)))
(alexandria:hash-table-keys ht)                        ;⇒ (:Y :X)

alexandria:hash-table-values hash-table ⇒ list

Returns a list containing the values of hash-table.

(setf ht (alexandria:plist-hash-table '(:x 10 :y 20)))
(alexandria:hash-table-values ht)                      ;⇒ (20 10)

Iteration

maphash function hash-table ⇒ nil

Iterate over hash-table entries, calling function with two arguments, the key and value of that entry.

(setf ht (alexandria:plist-hash-table '(:x 10 :y 20))
      buf (list))
(maphash #'(lambda (key val)
             (push (list key val) buf)) ht)            ;⇒ NIL
buf                                                    ;⇒ ((:Y 20) (:X 10))

alexandria:maphash-keys function hash-table ⇒ nil

Like maphash, but calls function with each key in the hash-table.

(setf buf (list))
(alexandria:maphash-keys #'(lambda (key)
                             (push key buf)) ht) ;⇒ NIL
buf                                              ;⇒ (:Y :X)

alexandria:maphash-values function hash-table ⇒ nil

Like maphash, but calls function with each value in the hash-table.

(setf buf (list))
(alexandria:maphash-values #'(lambda (val)
                               (push val buf)) ht) ;⇒ NIL
buf                                                ;⇒ (20 10)

loop for [key|value] being the [hash-keys|hash-values] in hash-table

Using the extended form of loop, iterate over hash-table entries binding to key and value.

(loop
  for key being the hash-keys in ht
  for val being the hash-values in ht
  collect (list key val))             ;⇒ ((:X 10) (:Y 20))

iterate for (key value) in-hashtable hash-table

Using the iterate package, iterate over hash-table entries binding to key and value.

(iter (for (key val) in-hashtable ht)
  (collect (list key val)))           ;⇒ ((:X 10) (:Y 20))

Advanced

with-hash-table-iterator (iter hash-table) form* ⇒ result*

Generates an iterator function iter to successively step through entries in a hash-table. The iter function returns three values: a boolean if an entry was returned, the key from the hash-table entry, and the value from the hash-table entry.

(setf ht (alexandria:plist-hash-table '(:x 10 :y 20))
      buf (list))
(with-hash-table-iterator (next-entry ht)
  (loop
    (multiple-value-bind (entry-p key val) (next-entry)
      (if entry-p
        (push (list key val) buf)
        (return)))))                                     ;⇒ NIL
buf                                                      ;⇒ ((:Y 20) (:X 10))

hash-table-test hash-table ⇒ test

Return the equality test used for comparing keys in hash-table.

(setf ht (make-hash-table))      ;⇒ #<HASH-TABLE :TEST EQL :COUNT 0>
(setf test (hash-table-test ht)) ;⇒ EQL
(funcall test 6 6)               ;⇒ T

hash-table-size hash-table ⇒ size

Return the current size of hash-table, an implementation-dependent value used to determine the amount of entries a hash-table can hold before it has to grow.

(setf ht (make-hash-table)) ;⇒ #<HASH-TABLE :TEST EQL :COUNT 0>
(hash-table-size ht)        ;⇒ 16 (implementation-dependent)

hash-table-rehash-size hash-table ⇒ rehash-size

Return the current rehash size of hash-table, an implementation-dependent value used to determine the minimum amount to increase the size of the hash-table when it becomes full enough to require rehashing.

(setf ht (make-hash-table)) ;⇒ #<HASH-TABLE :TEST EQL :COUNT 0>
(hash-table-rehash-size ht) ;⇒ 1.5 (implementation-dependent)

hash-table-rehash-threshold hash-table ⇒ rehash-threshold

Return the current rehash threshold of hash-table, an implementation-dependent value used to determine how full the hash-table can get before it must grow.

(setf ht (make-hash-table))      ;⇒ #<HASH-TABLE :TEST EQL :COUNT 0>
(hash-table-rehash-threshold ht) ;⇒ 1.0 (implementation-dependent)

sxhash object ⇒ hash-code

Returns a hash-code for the object. The manner in which it is computed is implementation-dependent.

(eql (sxhash 'a) (sxhash 'a)) ;⇒ T
(eql (sxhash 'a) (sxhash 'b)) ;⇒ NIL