Hash join: Difference between revisions

Content added Content deleted
mNo edit summary
(Update emacs lisp)
Line 965: Line 965:
<syntaxhighlight lang="lisp">
<syntaxhighlight lang="lisp">
(defun make-multi-map (rows)
(defun make-multi-map (rows)
(let ((map1 '()))
(let ((multi-map nil))
(defun place_line (row)
(cl-loop for row in rows do
(let ((name (car row)))
(let* ((name (car row))
(if (assoc name map1)
(name-list (assoc name multi-map)))
(let ((rc-list (assoc name map1)))
(if name-list
(setcdr (nthcdr (1- (length rc-list)) rc-list) (list row)) )
(nconc name-list (list row))
(progn
(add-to-list 'map1 (list name row) 't)) ) )
(add-to-list 'multi-map (list name row) 't) ) ) ) )
(mapcar 'place_line rows)
map1))
multi-map) )


(defun join-tables (table1 table2)
(defun join-tables (table1 table2)
(let ((multi-map (make-multi-map table2))
(let ((multi-map (make-multi-map table2))
(result-table '()))
(result-table '()))
(cl-loop
(cl-loop for row in table1 do
(let ((multi-rc (assoc (cdr row) multi-map)))
for row in table1 do
(progn
(when multi-rc
(let ((multi-rc (assoc (cdr row) multi-map)))
(cl-loop for multi-line in (cdr multi-rc) do
(add-to-list 'result-table
(when multi-rc
(cl-loop for multi-line in (cdr multi-rc) do
(list (car row) (cdr row) (car multi-line) (cdr multi-line))
(add-to-list 'result-table (list row multi-line) 't))))))
't)))))
result-table))
result-table))



(let ((table1 '((27 . "Jonah")
(let ((table1 '((27 . "Jonah")
Line 998: Line 997:
("Alan" . "Zombies")
("Alan" . "Zombies")
("Glory" . "Buffy"))))
("Glory" . "Buffy"))))
(join-tables table1 table2))
(message "%s" (join-tables table1 table2)) )
</syntaxhighlight>
</syntaxhighlight>