;; SPDX-License-Identifier: EUPL-1.2 ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs (defpackage :ukkoclot/hash-tables (:documentation "Hash-table-oriented utilities.") (:use :c2cl) (:import-from :alexandria :with-gensyms) (:export :alist->hash-table :gethash-lazy :plist->hash-table)) (in-package :ukkoclot/hash-tables) (defun alist->hash-table (alist &rest args &key &allow-other-keys) "Turn an association list into a hash table. All key arguments are passed on to `make-hash-table'." (let ((ht (apply #'make-hash-table args))) (loop for (key . value) in alist do (setf (gethash key ht) value)) ht)) (defmacro gethash-lazy (key hash-table default-lazy) "`gethash' alternative with lazily evaluated default value." (with-gensyms (res unique) `(let* ((,unique ',unique) (,res (gethash ,key ,hash-table ,unique))) (if (eq ,res ,unique) ,default-lazy ,res)))) (defun plist->hash-table (plist &rest args &key &allow-other-keys) "Turn a property list into a hash table. All key arguments are passed on to `make-hash-table'." (let ((ht (apply #'make-hash-table args))) (loop for (key value) on plist by #'cddr do (setf (gethash key ht) value)) ht))