src/dot.lisp @ 2e90748b1555

Stop passing NIL as :hash-function during hash table construction

At least in SBCL 2.0.6, passing :hash-function NIL to make-hash-table causes an
UNDEFINED-FUNCTION error. Since it's unlikely that any other CL implementation
will have useful behavior for an explicit NIL argument that's different from not
passing it at all, it's better to not pass :hash-function if it's set to NIL.
author Jacek TeMPOraL Złydach <temporal.pl@gmail.com>
date Sat, 18 Jul 2020 01:03:54 +0200
parents 412a127b7419
children 63712529f020 f11da0d6a058
(in-package :digraph.dot)


(defun find-dot-roots (digraph)
  (let ((nodes (vertices digraph))
        (roots nil)
        (test (digraph::digraph-test digraph)))
    (labels ((descendents (vertex)
               (map-depth-first #'identity digraph vertex))
             (prune (vertex)
               (setf nodes (set-difference nodes (descendents vertex)
                                           :test test)))
             (mark-root-and-prune (vertex)
               (push vertex roots)
               (prune vertex)))
      (mapc #'mark-root-and-prune (digraph::roots digraph))
      (loop :while nodes :do (mark-root-and-prune (pop nodes))))
    roots))


(defparameter *current-digraph* nil)


(defmethod cl-dot:graph-object-node ((graph (eql 'digraph)) (vertex t))
  (make-instance 'cl-dot:node
    :attributes `(:label ,(format nil "~A" vertex) :shape :circle)))

(defmethod cl-dot:graph-object-points-to ((graph (eql 'digraph)) (vertex t))
  (successors *current-digraph* vertex))


(defun draw (digraph &key (filename "digraph.png") (format :png))
  "Draw `digraph` with cl-dot."
  (let ((*current-digraph* digraph))
    (cl-dot:dot-graph
      (cl-dot:generate-graph-from-roots 'digraph (find-dot-roots digraph))
      filename
      :format format))
  digraph)