Coverage report: /home/ellis/.stash/quicklisp/dists/quicklisp/software/alexandria-20241012-git/alexandria-1/hash-tables.lisp
Kind | Covered | All | % |
expression | 0 | 108 | 0.0 |
branch | 0 | 2 | 0.0 |
Key
Not instrumented
Conditionalized out
Executed
Not executed
Both branches taken
One branch taken
Neither branch taken
1
(in-package :alexandria)
3
(defmacro ensure-gethash (key hash-table &optional default)
4
"Like GETHASH, but if KEY is not found in the HASH-TABLE saves the DEFAULT
5
under key before returning it. Secondary return value is true if key was
7
(once-only (key hash-table)
8
(with-unique-names (value presentp)
9
`(multiple-value-bind (,value ,presentp) (gethash ,key ,hash-table)
11
(values ,value ,presentp)
12
(values (setf (gethash ,key ,hash-table) ,default) nil))))))
14
(defun copy-hash-table (table &key key test size
15
rehash-size rehash-threshold)
16
"Returns a copy of hash table TABLE, with the same keys and values
17
as the TABLE. The copy has the same properties as the original, unless
18
overridden by the keyword arguments.
20
Before each of the original values is set into the new hash-table, KEY
21
is invoked on the value. As KEY defaults to CL:IDENTITY, a shallow
22
copy is returned by default."
23
(setf key (or key 'identity))
24
(setf test (or test (hash-table-test table)))
25
(setf size (or size (hash-table-size table)))
26
(setf rehash-size (or rehash-size (hash-table-rehash-size table)))
27
(setf rehash-threshold (or rehash-threshold (hash-table-rehash-threshold table)))
28
(let ((copy (make-hash-table :test test :size size
29
:rehash-size rehash-size
30
:rehash-threshold rehash-threshold)))
31
(maphash (lambda (k v)
32
(setf (gethash k copy) (funcall key v)))
36
(declaim (inline maphash-keys))
37
(defun maphash-keys (function table)
38
"Like MAPHASH, but calls FUNCTION with each key in the hash table TABLE."
39
(maphash (lambda (k v)
44
(declaim (inline maphash-values))
45
(defun maphash-values (function table)
46
"Like MAPHASH, but calls FUNCTION with each value in the hash table TABLE."
47
(maphash (lambda (k v)
52
(defun hash-table-keys (table)
53
"Returns a list containing the keys of hash table TABLE."
55
(maphash-keys (lambda (k)
60
(defun hash-table-values (table)
61
"Returns a list containing the values of hash table TABLE."
63
(maphash-values (lambda (v)
68
(defun hash-table-alist (table)
69
"Returns an association list containing the keys and values of hash table
72
(maphash (lambda (k v)
73
(push (cons k v) alist))
77
(defun hash-table-plist (table)
78
"Returns a property list containing the keys and values of hash table
81
(maphash (lambda (k v)
82
(setf plist (list* k v plist)))
86
(defun alist-hash-table (alist &rest hash-table-initargs)
87
"Returns a hash table containing the keys and values of the association list
88
ALIST. Hash table is initialized using the HASH-TABLE-INITARGS."
89
(let ((table (apply #'make-hash-table hash-table-initargs)))
91
(ensure-gethash (car cons) table (cdr cons)))
94
(defun plist-hash-table (plist &rest hash-table-initargs)
95
"Returns a hash table containing the keys and values of the property list
96
PLIST. Hash table is initialized using the HASH-TABLE-INITARGS."
97
(let ((table (apply #'make-hash-table hash-table-initargs)))
98
(do ((tail plist (cddr tail)))
100
(ensure-gethash (car tail) table (cadr tail)))