data-structures - 更新深层数据结构(Common Lisp)
问题描述
我想要一些关于访问和更新多层深的数据结构的建议。我最初想使用一个广义引用 setf 函数,但无法让它工作。作为替代方案,下面列出的函数可以正常工作以查询和更新数据结构(称为*kb*
)。但它似乎比它需要的更复杂。有没有更简单的解决方案?
(defun fetch (node edge)
"Retrieves all the nodes in the kb associated with a node and edge."
(alexandria:hash-table-keys (cdr (assoc edge (gethash node *kb*)))))
(defun associate (node1 edge node2)
"Adds an association to the kb."
(declare (symbol node1 edge node2))
(when (not (gethash edge *edges*))
(error "Mentioned edge ~A was not predefined" edge))
(let ((alist (gethash node1 *kb*)))
(if alist
(let ((pair (assoc edge alist)))
(if pair
(setf (gethash node2 (cdr pair)) t)
(let ((ht (make-hash-table :test #'eq)))
(setf (gethash node2 ht) t)
(setf (gethash node1 *kb*) (acons edge ht alist)))))
(let ((ht (make-hash-table :test #'eq)))
(setf (gethash node2 ht) t)
(setf (gethash node1 *kb*) (acons edge ht nil))))))
作为背景,*kb*
数据结构是一个哈希表。哈希表键是符号,值是列表。每个 alist 由 key.value 对组成,其中键是符号,值是哈希表。后面的每个哈希表都简单地将一组符号表示为具有 t 个值的键。(ps:我为中间结构选择了alists,因为只有20个左右可能的边缘符号。)感谢您的任何见解......</p>
解决方案
推荐阅读
- javascript - 用javascript从json中获取数据不止一个数据
- android - 尝试调用“play-services-pay”Android SDK API 时“使用未知证书签名的包”
- python-3.x - 有没有办法在 KivyMD 中显示熊猫数据框?
- linux - 如何在 fio 中创建或打开给定数量的文件?
- sql - SQL:选择 max(A), B 但不想分组或聚合 B
- c# - 在 DbContext 中使用 Azure B2C 在 ASP.NET Core 上实现多租户
- r - 通过倾向得分的倒数加权后的协变量平衡
- c# - Xamarin 表单 - 根据单击的按钮更改页面内容
- reactjs - 如何在 React DraftJS 编辑器中存储 textAlignment 值?
- angular - 在 Angular 10 中使用羽毛笔编辑器自定义字体大小