| 1 |
;;;-*- Mode:Lisp; Syntax:Common-Lisp; Package:CHAOS -*- |
| 2 |
;;; |
| 3 |
;;; Copyright (c) 2000-2015, Toshimi Sawada. All rights reserved. |
| 4 |
;;; |
| 5 |
;;; Redistribution and use in source and binary forms, with or without |
| 6 |
;;; modification, are permitted provided that the following conditions |
| 7 |
;;; are met: |
| 8 |
;;; |
| 9 |
;;; * Redistributions of source code must retain the above copyright |
| 10 |
;;; notice, this list of conditions and the following disclaimer. |
| 11 |
;;; |
| 12 |
;;; * Redistributions in binary form must reproduce the above |
| 13 |
;;; copyright notice, this list of conditions and the following |
| 14 |
;;; disclaimer in the documentation and/or other materials |
| 15 |
;;; provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED |
| 18 |
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 |
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 21 |
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 23 |
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 |
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 |
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
;;; |
| 29 |
(in-package :CHAOS) |
| 30 |
#|============================================================================== |
| 31 |
System: Chaos |
| 32 |
Module: comlib |
| 33 |
File: dag.lisp |
| 34 |
==============================================================================|# |
| 35 |
#-:chaos-debug |
| 36 |
(declaim (optimize (speed 3) (safety 0) #-GCL (debug 0))) |
| 37 |
#+:chaos-debug |
| 38 |
(declaim (optimize (speed 1) (safety 3) #-GCL (debug 3))) |
| 39 |
|
| 40 |
;;; dag node = ( datum List[node] . flag ) |
| 41 |
;;; flag is used for dag traverse, i.e. marked=already visited. |
| 42 |
|
| 43 |
(defstruct dag-node |
| 44 |
(datum nil :type t) |
| 45 |
(subnodes nil :type list) |
| 46 |
(flag nil :type (or null t))) |
| 47 |
|
| 48 |
;;; basic constructor |
| 49 |
(defmacro create-dag-node (datum subnodes) |
| 50 |
`(make-dag-node :datum ,datum :subnodes ,subnodes)) |
| 51 |
|
| 52 |
;;; adding sub node |
| 53 |
(defmacro add-subnodes (dag datums) |
| 54 |
(once-only (dag) |
| 55 |
`(setf (dag-node-subnodes ,dag) |
| 56 |
(nconc (dag-node-subnodes ,dag) |
| 57 |
(mapcar #'(lambda (d) (create-dag-node d nil)) |
| 58 |
,datums))))) |
| 59 |
|
| 60 |
(defmacro push-sub-node (dag datum) |
| 61 |
`(push (create-dag-node ,datum nil) |
| 62 |
(dag-node-subnodes ,dag))) |
| 63 |
|
| 64 |
;;; |
| 65 |
(defmacro dag-node-is-marked? (node) `(dag-node-flag ,node)) |
| 66 |
(defmacro mark-dag-node (node) `(setf (dag-node-flag ,node) t)) |
| 67 |
(defmacro unmark-dag-node (node) `(setf (dag-node-flag ,node) nil)) |
| 68 |
|
| 69 |
(defun unmark-all-dag-nodes (dag) |
| 70 |
(declare (type dag-node dag)) |
| 71 |
(unmark-dag-node dag) |
| 72 |
(dolist (sub (dag-node-subnodes dag)) |
| 73 |
(declare (type dag-node sub)) |
| 74 |
(unmark-all-dag-nodes sub))) |
| 75 |
|
| 76 |
;;; dag-dfs : dag function |
| 77 |
;;; traversing dag by depth first manner, apply function to each node. |
| 78 |
;;; |
| 79 |
(defun dag-dfs (dag &optional (function #'identity)) |
| 80 |
(declare (type dag-node dag) |
| 81 |
(type function function)) |
| 82 |
(labels ((do-dag-dfs (d) |
| 83 |
(unless (dag-node-is-marked? d) |
| 84 |
(dolist (sub (dag-node-subnodes d)) |
| 85 |
(unless (dag-node-is-marked? sub) |
| 86 |
(do-dag-dfs sub))) |
| 87 |
(funcall function d) |
| 88 |
(mark-dag-node d)))) |
| 89 |
(unmark-all-dag-nodes dag) |
| 90 |
(do-dag-dfs dag))) |
| 91 |
|
| 92 |
;;; dag-wfs : dag function |
| 93 |
;;; traversing dag by width first manner, apply function to each node. |
| 94 |
;;; |
| 95 |
(defun dag-wfs (dag &optional (function #'identity)) |
| 96 |
(declare (type dag-node dag) |
| 97 |
(type function function)) |
| 98 |
(labels ((do-dag-wfs (ld) |
| 99 |
(dolist (d ld) |
| 100 |
(unless (dag-node-is-marked? d) |
| 101 |
(funcall function d) |
| 102 |
(mark-dag-node d))) |
| 103 |
(dolist (d ld) |
| 104 |
(do-dag-wfs (dag-node-subnodes d))))) |
| 105 |
(unmark-all-dag-nodes dag) |
| 106 |
(do-dag-wfs (list dag)))) |
| 107 |
|
| 108 |
;;; bi-directional graph |
| 109 |
;;; |
| 110 |
(defstruct (bdag (:include dag-node)) |
| 111 |
(parent nil)) |
| 112 |
|
| 113 |
(defmacro create-bdag-node (datum subnodes) |
| 114 |
(once-only (subnodes datum) |
| 115 |
`(let ((bdag (make-bdag :datum ,datum :subnodes ,subnodes :parent nil))) |
| 116 |
(dolist (s ,subnodes) |
| 117 |
(setf (bdag-parent s) bdag)) |
| 118 |
bdag))) |
| 119 |
|
| 120 |
(defmacro add-bdag-subnodes (bdag datums) |
| 121 |
(once-only (bdag datums) |
| 122 |
`(setf (dag-node-subnodes ,bdag) |
| 123 |
(nconc (dag-node-subnodes ,bdag) |
| 124 |
(mapcar #'(lambda (d) |
| 125 |
(let ((sub (create-bdag-node d nil))) |
| 126 |
(setf (bdag-parent sub) ,bdag) |
| 127 |
sub)) |
| 128 |
,datums))))) |
| 129 |
|
| 130 |
(defmacro push-bdag-node (dag datum) |
| 131 |
(once-only (dag) |
| 132 |
`(push (let ((s (create-bdag-node ,datum nil))) |
| 133 |
(setf (bdag-parent s) ,dag) |
| 134 |
s) |
| 135 |
(dag-node-subnodes ,dag)))) |
| 136 |
|
| 137 |
|
| 138 |
(defun get-bdag-parents (bdag) |
| 139 |
(declare (type bdag bdag)) |
| 140 |
(let ((res nil) |
| 141 |
(parent (bdag-parent bdag))) |
| 142 |
(while parent |
| 143 |
(push parent res) |
| 144 |
(setq parent (bdag-parent parent))) |
| 145 |
res)) |
| 146 |
|
| 147 |
;;; EOF |