Доступ и изменение элементов хэш-таблицы
(defparameter *my-hash* (make-hash-table))
; *MY-HASH*
(setf (gethash 'one-entry *my-hash*) "one")
; "one"
(setf (gethash 'another-entry *my-hash*) 2/4)
; 1/2
(gethash 'one-entry *my-hash*)
; "one"
; T
(gethash 'another-entry *my-hash*)
; 1/2
; T
Проверка на существование ключей в таблице
(defparameter *my-hash* (make-hash-table))
; *MY-HASH*
(setf (gethash 'entry *my-hash*) nil)
(if (nth-value 1 (gethash 'entry *my-hash*))
"Key exists"
"Key does not exist")
; "Key exists"
(if (nth-value 1 (gethash 'no-entry *my-hash*))
"Key exists"
"Key does not exist")
; "Key does not exist"
Удаление элементов из хэш-таблицы
(defparameter *my-hash* (make-hash-table))
; *MY-HASH*
(setf (gethash 'first-key *my-hash*) 'one)
; ONE
(gethash 'first-key *my-hash*)
; ONE
; T
(remhash 'first-key *my-hash*)
; T
(gethash 'first-key *my-hash*)
; NIL
; NIL
(gethash 'no-entry *my-hash*)
; NIL
; NIL
(remhash 'no-entry *my-hash*)
; NIL
(gethash 'no-entry *my-hash*)
; NIL
; NIL
Итерация по элементам хэш-таблицы
(defparameter *my-hash* (make-hash-table))
; *MY-HASH*
(setf (gethash 'first-key *my-hash*) 'one)
; ONE
(setf (gethash 'second-key *my-hash*) 'two)
; TWO
(setf (gethash 'third-key *my-hash*) nil)
; NIL
(setf (gethash nil *my-hash*) 'nil-value)
; NIL-VALUE
(defun print-hash-entry (key value)
(format t "The value associated with the key ~S is ~S~%" key value))
; PRINT-HASH-ENTRY
(maphash #'print-hash-entry *my-hash*)
; The value associated with the key FIRST-KEY is ONE
; The value associated with the key SECOND-KEY is TWO
; The value associated with the key THIRD-KEY is NIL
; The value associated with the key NIL is NIL-VALUE
Ещё один вариант итерации по хэш-таблице
(with-hash-table-iterator (my-iterator *my-hash*)
(loop
(multiple-value-bind (entry-p key value)
(my-iterator)
(if entry-p
(print-hash-entry key value)
(return)))))
; The value associated with the key FIRST-KEY is ONE
; The value associated with the key SECOND-KEY is TWO
; The value associated with the key THIRD-KEY is NIL
; The value associated with the key NIL is NIL-VALUE
; NIL
Итерация с помощью loop
(loop for key being the hash-keys of *my-hash*
do (print key))
; FIRST-KEY
; SECOND-KEY
; THIRD-KEY
; NIL
; NIL
(loop for key being the hash-keys of *my-hash*
using (hash-value value)
do (format t "The value associated with the key ~S is ~S~%" key value))
; The value associated with the key FIRST-KEY is ONE
; The value associated with the key SECOND-KEY is TWO
; The value associated with the key THIRD-KEY is NIL
; The value associated with the key NIL is NIL-VALUE
; NIL
(loop for value being the hash-values of *my-hash*
do (print value))
; ONE
; TWO
; NIL
; NIL-VALUE
; NIL
(loop for value being the hash-values of *my-hash*
using (hash-key key)
do (format t "~&~A -> ~A" key value))
; FIRST-KEY -> ONE
; SECOND-KEY -> TWO
; THIRD-KEY -> NIL
; NIL -> NIL-VALUE
; NIL
Посчитать колличество элементов
(defparameter *my-hash* (make-hash-table))
; *MY-HASH*
(hash-table-count *my-hash*)
; 0
(setf (gethash 'first *my-hash*) 1)
; 1
(setf (gethash 'second *my-hash*) 2)
; 2
(setf (gethash 'third *my-hash*) 3)
; 3
(hash-table-count *my-hash*)
; 3
(setf (gethash 'second *my-hash*) 'two)
; TWO
(hash-table-count *my-hash*)
; 3
(clrhash *my-hash*) ; отчистить от данных
; #
(hash-table-count *my-hash*)
; 0
Создать хэш-таблицу изначально большого размера.
(defparameter *my-hash* (make-hash-table :size 100000))
; *MY-HASH*
(hash-table-size *my-hash*)
; 100000
(time (dotimes (n 100000) (setf (gethash n *my-hash*) n)))
; Compiling LAMBDA NIL:
; Compiling Top-Level Form:
;
; Evaluation took:
; 0.04 seconds of real time
; 0.04 seconds of user run time
; 0.0 seconds of system run time
; 0 page faults and
; 0 bytes consed.
; NIL
Если мы не знаем финального размера таблицы, но можем предугадать как она будет увеличиваться - можно снабдить make-hash-table этими данными через ключ :rehash-size. Целое число указывает абсолютное увеличение размера, а дробное - относительное
(defparameter *my-hash* (make-hash-table :rehash-size 100000))
; *MY-HASH*
(hash-table-size *my-hash*)
; 65
(hash-table-rehash-size *my-hash*)
; 100000
(time (dotimes (n 100000) (setf (gethash n *my-hash*) n)))
; Compiling LAMBDA NIL:
; Compiling Top-Level Form:
;
; Evaluation took:
; 0.07 seconds of real time
; 0.05 seconds of user run time
; 0.01 seconds of system run time
; 0 page faults and
; 2001360 bytes consed.
; NIL
Источники: