无损修改哈希表



是否可以无损地向Common Lisp(SBCL(哈希表添加新的键值对?向哈希表添加新元素的标准方法是调用:

(setf (gethash key *hash-table*) value)

但是对CCD_ 1的调用修改CCD_。我有一个应用程序,我想利用哈希表查找的效率,但我也希望对其进行无损修改。我看到的工作是在对原始哈希表进行操作之前复制它,但这在我的情况下是不实际的,因为我正在处理的哈希表包含成千上万的元素,而复制大型哈希表,比如说,在一个循环中复制会抵消最初使用它们的计算效率优势。

根据您的需要,您可以只使用关联列表,使用assoc和其他函数在现有绑定的基础上建立新的绑定。assoc返回第一个匹配元素的事实意味着您可以隐藏绑定:

(let ((list '((:a . 1) (:b . 2))))
(acons :b 3 list))
=> ((:b . 3) (:a . 1) (:b . 2))

如果在结果列表中调用(assoc :b list),则条目将为(:b . 3),但原始列表未修改。

FSet

如果关联列表还不够,那么FSet库为Common Lisp提供了纯功能的数据结构,比如映射,它们是不可变的哈希表。它们被实现为平衡树,这比天真的方法要好。还有其他更高效的数据结构,但您可能需要自己实现它们(Hash array mapped trie(edit:请参阅https://github.com/danshapero/cl-hamt,谢谢@Flux(。话虽如此,FSet总体上已经足够好了。

FSet可通过Quicklisp 获得

USER> (ql:quickload :fset)

创建地图;请注意,如果您安装了适当的阅读器宏,打印的表示将被再次读取。但是,在没有修改语法表的情况下,您可以完美地使用该库。

USER> (fset:map (:a 0) (:b 1))
#{| (:A 0) (:B 1) |}

使用:c:的新绑定更新以前的映射

USER> (fset:with * :c 3)
#{| (:A 0) (:B 1) (:C 3) |}

使用:b的新绑定更新上一个映射,该绑定会遮挡上一个:

USER> (fset:with * :b 4)
#{| (:A 0) (:B 4) (:C 3) |}

所有中间地图均未修改:

USER> (list * ** *** )
(#{| (:A 0) (:B 4) (:C 3) |}
#{| (:A 0) (:B 1) (:C 3) |} 
#{| (:A 0) (:B 1) |})

我不认为您可以通过引用将一个哈希表传递给公共lisp中的另一个哈希值表。但我的想法是如何避免复制整个哈希表,然而,通过一次调用得到的结果是使用默认值参数位置CCD_ 9。

setf2中不存在key时,(gethash key ht default-value)返回默认值。

;; prepare three example hash-tables, where *h3* and *h2* gets the additional keys
;; and if a key is not present in *h3*, one should look up in *h2*, and if not there too, in *h1*.
(defparameter *h1* (make-hash-table))
(setf (gethash 'a *h1*) 1)
(setf (gethash 'b *h1*) 2)
(setf (gethash 'c *h1*) 3)
(defparameter *h2* (make-hash-table))
(setf (gethash 'd *h2*) 4)
(setf (gethash 'e *h2*) 5)
(defparameter *h3* (make-hash-table))
(setf (gethash 'f *h3*) 6)
;; the call
(gethash 'a *h3* (gethash 'a *h2* (gethash 'a *h1*)))
;; would give the desired result `1`.
;; let us assume, there is a chain of hash-tables *hk* *h(k-1)* ... *h2* *h1*
;; in which one should look up into that order.
;; Then it is to us to build the code
;; (gethash 'a *hk* (gethash 'a *h(k-1)* ...(gethash 'a *h2* (gethash 'a *h1*))...))
;; automatically for every lookup.

;; this macro does it:
(defmacro mget (key hash-tables-list)
(flet ((inject-last (e1 e2) `(,@e1 ,e2)))
(reduce #'inject-last 
(mapcar (lambda (ht) `(gethash ,key ,ht)) 
(nreverse hash-tables-list)))))
;; let's see its macroexpansion:
(macroexpand-1 '(mget 'a (*h3* *h2* *h1*)))
;; (GETHASH 'A *H3* (GETHASH 'A *H2* (GETHASH 'A *H1*))) ;
;; T
;; and run the code:
(mget 'a (*h2* *h1*))
;; 1 ;
;; NIL

可以附加下一个要查找的哈希表的信息在哈希表对象中。甚至可以自动生成列表(*h3* *h2* *h1*),从而只写入(gethash* key ht)然后调用mget。。。

当然,通过所有这些,散列访问会变慢。

这是在复制整个哈希表还是在每次调用时支付性能成本之间的权衡。。。

*h3*扩展的哈希表的自动查找

(setf (get '*h3* 'extendeds) '(*h2* *h1*))
(setf (get '*h2* 'extendeds) '(*h1*))
(defun collect-extendeds (hts)
(let ((res (loop for ht in hts
nconcing (get ht 'extendeds))))
(remove-duplicates res)))
;; this function can recursively retrieve all hashtables
(defun get-extendeds* (hts &optional (acc '()))
(let ((hts (if (listp hts) hts (list hts))))
(let ((nexts (collect-extendeds hts)))
(cond ((every #'null nexts) (nreverse (remove-duplicates (append hts acc))))
(t (get-extendeds* nexts (remove-duplicates (append hts acc))))))))
;; write a macro to retrieve key's value from all downstream hashtables
(defmacro geth (key ht)
`(mget ,key ,(get-extendeds* ht)))
(geth 'a *h3*)
;; 1 ;
;; NIL  ;; NIL because it was not in *h3* directly but in one of the hashtables
;; which it extends.
;; problem is if 'NIL is a value of an existing key,
;; one would still get 'NIL NIL.

最新更新