Debian Codegraph @

comlib/tree-display.lisp

raw
1 ;;;-*- Mode:LISP; Package:CHAOS; Base:10; Syntax:Common-lisp -*-
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: tree-display.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 ;;;-----------------------------------------------------------------------------
41 ;;; customisable parameters
42 ;;;-----------------------------------------------------------------------------
43
44 ;;; how many space characters between trees
45 (defparameter tree-spacing 2)
46
47 ;;; print tree with leaves at bottom?
48 (defparameter leaves-at-bottom? nil)
49
50 ;;; USERS MUST BIND THESE FUNCTIONS
51 ;;; which define what a tree is (leaf & internal node) and
52 ;;; how to get its components.
53 ;;;
54 (declaim (special leaf? leaf-name leaf-info int-node-name int-node-children))
55
56 (defvar leaf? nil)
57 (defvar leaf-name nil)
58 (defvar leaf-info nil)
59 (defvar int-node-name nil)
60 (defvar int-node-children nil)
61
62 (defun make-augm-leaf (width root name info)
63 (declare (type t width root name info))
64 (list 'leaf width root name info))
65
66 (defun make-augm-pad (width)
67 (declare (type t width))
68 (list 'pad width))
69
70 (defun make-augm-int-node (width root name lpad rpad children)
71 (declare (type t width root name lpad rpad children))
72 (list nil width root name lpad rpad children))
73
74 (defun augm-tree-int-node? (x)
75 (declare (type list x)
76 (values (or null t)))
77 (null (car x)))
78
79 (defun augm-tree-pad? (x)
80 (declare (type list x))
81 (eq (car x) 'pad))
82
83 (defun augm-tree-width (x)
84 (declare (type list x)
85 (values fixnum))
86 (nth 1 x))
87
88 (defun augm-tree-root (x)
89 (declare (type list x)
90 (values t))
91 (nth 2 x))
92
93 (defun augm-tree-name (x)
94 (declare (type list x))
95 (nth 3 x))
96
97 (defun augm-leaf-info (x)
98 (declare (type list x))
99 (nth 4 x))
100
101 (defun augm-int-node-lpad (x)
102 (declare (type list x))
103 (nth 4 x))
104
105 (defun augm-int-node-rpad (x)
106 (declare (type list x))
107 (nth 5 x))
108
109 (defun augm-int-node-children (x)
110 (declare (type list x))
111 (nth 6 x))
112
113 (defun pad (width l)
114 (declare (type fixnum width)
115 (type list l)
116 (values list))
117 (if (> width 0)
118 (cons (make-augm-pad width) l)
119 l))
120
121 (defun field-width (x)
122 (declare (type t x)
123 (values fixnum))
124 " return number of chars in the written repr of x"
125 (labels ((l-length (l w)
126 (declare (type list l)
127 (type fixnum w)
128 (values fixnum))
129 (cond ((null l) w)
130 (t (l-length (cdr l) (+ w (field-width (car l)) 1)))
131 ))
132 #|| not used now
133 (s-length (i w)
134 (if (>= i 0)
135 (let ((c (elt x i)))
136 (s-length (- i 1)
137 (+ w (case c ((#\: #\") 2) (otherwise 1)))))
138 w))
139 ||#
140 )
141 ;;
142 (typecase x
143 (symbol (length (symbol-name (the symbol x))))
144 (character (case x ((#\space) 7) ((#\newline) 9) (otherwise 3)))
145 (number (length (format nil "~S" x)))
146 (list (+ (l-length (cdr x) (+ (field-width (car x)) 2)) 1))
147 (string (length (the simple-string x))
148 ;;(s-length (- (length x) 1) 2)
149 )
150 (t 0))))
151
152 (defun augment-tree (tree)
153 (declare (type list tree))
154 (if (funcall leaf? tree)
155 (let* ((name (funcall leaf-name tree))
156 (info (funcall leaf-info tree))
157 (name-width (field-width name))
158 (info-width (field-width info))
159 (tree-width (max name-width info-width)))
160 (declare (type fixnum name-width info-width tree-width))
161 (make-augm-leaf tree-width (truncate tree-width 2) name info))
162 (let* ((children (mapcar #'augment-tree (funcall int-node-children tree)))
163 (name (funcall int-node-name tree))
164 (name-width (field-width name))
165 (name-left (truncate name-width 2))
166 (name-right (- name-width name-left)))
167 (declare (type fixnum name-width name-left name-right)
168 (type list children))
169 (if (null children)
170 (make-augm-int-node name-width name-left name 0 0 '())
171 (let* ((first-child (car children))
172 (last-child (nth (- (length children) 1) children))
173 (width
174 (+ (* (- (the fixnum (length (the list children)))
175 1) tree-spacing)
176 (the fixnum
177 (apply #'+ (mapcar #'augm-tree-width children)))))
178 (left
179 (truncate (+ (- width (the fixnum
180 (augm-tree-width last-child)))
181 (+ (the fixnum (augm-tree-root first-child))
182 (the fixnum (augm-tree-root last-child))))
183 2.0))
184 (right
185 (- width left))
186 (max-left
187 (max name-left left))
188 (max-right
189 (max name-right right)))
190 (declare (type fixnum width left right max-left max-right))
191 ;;
192 (make-augm-int-node (+ max-left max-right) max-left name
193 (- max-left left) (- max-right right)
194 children))))))
195
196 ;;;
197 ;;;
198 (declaim (type hash-table _dup-hash_)
199 (type fixnum _ref-num-counter_)
200 (type list _dup-infos_))
201 (defvar _dup-hash_ (make-hash-table :test #'equal))
202 (defvar _ref-num-counter_ -1)
203 (defvar _dup-infos_ nil)
204
205 (defun augment-tree-as-graph (tree)
206 (clrhash _dup-hash_)
207 (setq _dup-infos_ nil)
208 (setq _ref-num-counter_ -1)
209 (traverse-tree-checking-dups tree)
210 (augment-tree-as-graph-aux tree))
211
212 (defstruct grph-info
213 (visited 0 :type fixnum)
214 name
215 ref-name)
216
217 (defstruct (grph-int-node-info (:include grph-info))
218 )
219
220 (defstruct (grph-leaf-info (:include grph-info))
221 info
222 )
223
224 (defun traverse-tree-checking-dups (tree)
225 (let ((info (gethash tree _dup-hash_)))
226 (cond (info ; duplicates
227 (incf (grph-info-visited info))
228 (when (= 1 (grph-info-visited info))
229 ;; this is the second time
230 (setf (grph-info-ref-name info)
231 (format nil "#~d" (incf _ref-num-counter_)))
232 ))
233 (t ; the first time
234 (cond ((funcall leaf? tree)
235 ;; leaf tree
236 (let ((name (funcall leaf-name tree))
237 (info (funcall leaf-info tree)))
238 (let ((leaf-info (make-grph-leaf-info :name name
239 :info info)))
240 (setf (gethash tree _dup-hash_) leaf-info))))
241 (t
242 ;; internal node
243 (let ((name (funcall int-node-name tree)))
244 (let ((int-info (make-grph-int-node-info :name name)))
245 (setf (gethash tree _dup-hash_) int-info))
246 (mapc #'traverse-tree-checking-dups
247 (funcall int-node-children tree)))))))))
248
249 (defun augment-tree-as-graph-aux (tree)
250 (let* ((info (gethash tree _dup-hash_))
251 (ref-num (decf (grph-info-visited info)))
252 (ref-name (grph-info-ref-name info))
253 (name (cond ((< ref-num 0)
254 (if ref-name
255 (format nil "~a=~a" ref-name (grph-info-name info))
256 (grph-info-name info)))
257 (t ref-name))))
258 (declare (type fixnum ref-num))
259 (cond ((grph-leaf-info-p info) ; leaf
260 (let* ((leaf-info (grph-leaf-info-info info))
261 (name-width (field-width name))
262 (info-width (field-width leaf-info))
263 (tree-width (max name-width info-width)))
264 (declare (type fixnum name-width info-width tree-width))
265 (make-augm-leaf tree-width (truncate tree-width 2) name leaf-info)))
266 (t ; int node
267 (let* ((children (if (and (>= ref-num 0) ref-name)
268 nil
269 (mapcar #'augment-tree-as-graph-aux
270 (funcall int-node-children tree))))
271 (name-width (field-width name))
272 (name-left (truncate name-width 2))
273 (name-right (- name-width name-left)))
274 (declare (type list children)
275 (type fixnum name-width name-left name-right))
276 (if (null children)
277 ;; (make-augm-int-node name-width name-left name 0 0 '())
278 (make-augm-leaf name-width (truncate name-width 2) name nil)
279 (let* ((first-child (car children))
280 (last-child (nth (- (the fixnum
281 (length children))
282 1)
283 children))
284 (width
285 (+ (* (- (the fixnum (length children))
286 1)
287 tree-spacing)
288 (the fixnum
289 (apply #'+ (mapcar #'augm-tree-width children)))))
290 (left
291 (truncate (+ (- width
292 (the fixnum (augm-tree-width last-child)))
293 (+ (the fixnum (augm-tree-root first-child))
294 (the fixnum (augm-tree-root last-child))))
295 2.0))
296 (right (- width left))
297 (max-left (max name-left left))
298 (max-right (max name-right right)))
299 (declare (type fixnum width left right max-left max-right))
300 (make-augm-int-node (+ max-left max-right) max-left name
301 (- max-left left) (- max-right right)
302 children))))))))
303
304 (defun any-int-nodes? (trees)
305 (if (null trees)
306 nil
307 (or (augm-tree-int-node? (car trees))
308 (any-int-nodes? (cdr trees)))))
309
310 (defun all-done? (trees)
311 (if (null trees)
312 t
313 (and (augm-tree-pad? (car trees))
314 (all-done? (cdr trees)))))
315
316 (defun print-seq (c n stream)
317 (declare (type fixnum n)
318 (type (or null t) c)
319 (type stream stream))
320 (when n
321 (dotimes (x n)
322 (princ c stream))))
323
324 (defun print-loop1 (l delay-leaves? stream)
325 (declare (type list l)
326 (type (or null t) delay-leaves?)
327 (type stream stream))
328 (when (consp l)
329 (let* ((tree (car l))
330 (tree-width (augm-tree-width tree)))
331 (declare (type fixnum tree-width))
332 (if (augm-tree-pad? tree)
333 (progn
334 (print-seq #\space tree-width stream)
335 (print-loop1 (cdr l) delay-leaves? stream))
336 (let* ((root (augm-tree-root tree))
337 (name (augm-tree-name tree))
338 (name-width (field-width name))
339 (name-left (truncate name-width 2))
340 (name-right (- name-width name-left)))
341 (declare (type fixnum root name-width name-left name-right))
342 (if (or (not delay-leaves?) (augm-tree-int-node? tree))
343 (progn
344 (print-seq #\space (- root name-left) stream)
345 (princ name stream)
346 (print-seq #\space (- tree-width root name-right) stream)
347 (print-loop1 (cdr l) delay-leaves? stream))
348 (progn
349 (print-seq #\space root stream)
350 (princ #\| stream)
351 (print-seq #\space (- tree-width root 1) stream)
352 (print-loop1 (cdr l) delay-leaves? stream))))))))
353
354 (defun print-loop2 (l new-trees delay-leaves? stream)
355 (if (consp l)
356 (let* ((tree (car l))
357 (tree-width (augm-tree-width tree)))
358 (declare (type fixnum tree-width))
359 (if (augm-tree-pad? tree)
360 (progn
361 (print-seq #\space tree-width stream)
362 (print-loop2 (cdr l) (append new-trees (list tree))
363 delay-leaves? stream))
364 (let* ((root (augm-tree-root tree))
365 (name (augm-tree-name tree))
366 (name-width (field-width name))
367 (name-left (truncate name-width 2))
368 (name-right (- name-width name-left)))
369 (declare (type fixnum root name-width name-left name-right))
370 (if (augm-tree-int-node? tree)
371 (let ((children (augm-int-node-children tree)))
372 (declare (type list children))
373 (if (null children)
374 (progn
375 (print-seq #\space (- root name-left) stream)
376 (princ name stream)
377 (print-seq #\space (- tree-width root name-right)
378 stream)
379 (print-loop2 (cdr l)
380 (append new-trees
381 (pad tree-width '()))
382 delay-leaves? stream))
383 (let* ((child1 (car children))
384 (root1 (augm-tree-root child1))
385 (width1 (augm-tree-width child1))
386 (lpad (augm-int-node-lpad tree))
387 (rpad (augm-int-node-rpad tree)))
388 (declare (type fixnum root1 width1))
389 (labels ((print-loop3 (l1 l2 right)
390 (if (consp l1)
391 (let* ((child (car l1))
392 (root (augm-tree-root child))
393 (width (augm-tree-width child)))
394 (print-seq #\space (+ root tree-spacing right)
395 stream)
396 (if (cdr l1)
397 (princ #\| stream)
398 (princ #\\ stream)
399 ;;(princ "\\ " stream)
400 )
401 (print-loop3 (cdr l1)
402 (cons child
403 (pad tree-spacing l2))
404 (- width (+ root 1))))
405 (progn
406 (print-seq #\space (+ right rpad) stream)
407 (print-loop2
408 (cdr l)
409 (append new-trees
410 (reverse (pad rpad l2)))
411 delay-leaves? stream)))))
412 (print-seq #\space (+ lpad root1) stream)
413 (if (cdr children)
414 (princ #\/ stream)
415 (princ #\|))
416 (print-loop3 (cdr children)
417 (cons child1 (pad lpad '()))
418 (- width1 (+ root1 1))
419 )))))
420 (if delay-leaves?
421 (progn
422 (print-seq #\space root stream)
423 (princ #\| stream)
424 (print-seq #\space (- tree-width root 1)
425 stream)
426 (print-loop2 (cdr l) (append new-trees
427 (list tree))
428 delay-leaves? stream))
429 (let* ((info (augm-leaf-info tree))
430 (info-width (field-width info))
431 (info-left (truncate info-width 2))
432 (info-right (- info-width info-left)))
433 (print-seq #\space (- root info-left) stream)
434 (print-seq #\space info-width stream);(princ info stream)
435 (print-seq #\space (- tree-width root info-right)
436 stream)
437 (print-loop2 (cdr l)
438 (append new-trees
439 (pad tree-width '()))
440 delay-leaves? stream)))))))
441 (when new-trees
442 (princ " " stream)
443 (print-next nil *print-indent* stream)
444 (print-trees new-trees stream))))
445
446 (defun print-trees (trees stream)
447 (if (not (all-done? trees))
448 (let ((delay-leaves? (and leaves-at-bottom? (any-int-nodes? trees))))
449 (print-loop1 trees delay-leaves? stream)
450 ;; (terpri stream)
451 (print-next nil *print-indent* stream)
452 (print-loop2 trees '() delay-leaves? stream))))
453
454 #||
455 (defun show-tree (inp &optional (show-sort nil) (stream *standard-output*))
456 (let ((tree (if (term? inp)
457 inp
458 (setf $$term (simple-parse inp *current-module* *universal-sort*)))))
459 (tree-display tree show-sort stream)))
460
461 ||#
462 ;;; EOF