diff options
| author | 2025-10-09 21:58:43 +0300 | |
|---|---|---|
| committer | 2025-10-09 21:58:43 +0300 | |
| commit | 4da3ad1f569832845b58c3ce35149633a2bb665c (patch) | |
| tree | 5a09a0de66df7ec2e77f0fc9cc68ccbabc190934 /src/hash-tables.lisp | |
| download | ukkoclot-4da3ad1f569832845b58c3ce35149633a2bb665c.tar.gz ukkoclot-4da3ad1f569832845b58c3ce35149633a2bb665c.tar.xz ukkoclot-4da3ad1f569832845b58c3ce35149633a2bb665c.zip | |
Initial commit
Diffstat (limited to 'src/hash-tables.lisp')
| -rw-r--r-- | src/hash-tables.lisp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/hash-tables.lisp b/src/hash-tables.lisp new file mode 100644 index 0000000..9e41b26 --- /dev/null +++ b/src/hash-tables.lisp | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | ;; SPDX-License-Identifier: EUPL-1.2 | ||
| 2 | ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com> | ||
| 3 | (defpackage :ukkoclot/hash-tables | ||
| 4 | (:use :c2cl) | ||
| 5 | (:export :alist->hash-table :gethash-lazy :plist->hash-table)) | ||
| 6 | (in-package :ukkoclot/hash-tables) | ||
| 7 | |||
| 8 | (defun alist->hash-table (alist &rest args &key &allow-other-keys) | ||
| 9 | (let ((ht (apply #'make-hash-table args))) | ||
| 10 | (loop for (key . value) in alist do | ||
| 11 | (setf (gethash key ht) value)) | ||
| 12 | ht)) | ||
| 13 | |||
| 14 | (defmacro gethash-lazy (key hash-table default-lazy) | ||
| 15 | (let ((unique (gensym "UNIQUE-")) | ||
| 16 | (res (gensym "RES-"))) | ||
| 17 | `(let* ((,unique ',unique) | ||
| 18 | (,res (gethash ,key ,hash-table ,unique))) | ||
| 19 | (if (eq ,res ,unique) | ||
| 20 | ,default-lazy | ||
| 21 | ,res)))) | ||
| 22 | |||
| 23 | (defun plist->hash-table (plist &rest args &key &allow-other-keys) | ||
| 24 | (let ((ht (apply #'make-hash-table args))) | ||
| 25 | (loop for (key value) on plist by #'cddr do | ||
| 26 | (setf (gethash key ht) value)) | ||
| 27 | ht)) | ||