1 | ;;; subst.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003 Peter Graves |
---|
4 | ;;; $Id: subst.lisp 11391 2008-11-15 22:38:34Z vvoutilainen $ |
---|
5 | ;;; |
---|
6 | ;;; This program is free software; you can redistribute it and/or |
---|
7 | ;;; modify it under the terms of the GNU General Public License |
---|
8 | ;;; as published by the Free Software Foundation; either version 2 |
---|
9 | ;;; of the License, or (at your option) any later version. |
---|
10 | ;;; |
---|
11 | ;;; This program is distributed in the hope that it will be useful, |
---|
12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | ;;; GNU General Public License for more details. |
---|
15 | ;;; |
---|
16 | ;;; You should have received a copy of the GNU General Public License |
---|
17 | ;;; along with this program; if not, write to the Free Software |
---|
18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | ;;; |
---|
20 | ;;; As a special exception, the copyright holders of this library give you |
---|
21 | ;;; permission to link this library with independent modules to produce an |
---|
22 | ;;; executable, regardless of the license terms of these independent |
---|
23 | ;;; modules, and to copy and distribute the resulting executable under |
---|
24 | ;;; terms of your choice, provided that you also meet, for each linked |
---|
25 | ;;; independent module, the terms and conditions of the license of that |
---|
26 | ;;; module. An independent module is a module which is not derived from |
---|
27 | ;;; or based on this library. If you modify this library, you may extend |
---|
28 | ;;; this exception to your version of the library, but you are not |
---|
29 | ;;; obligated to do so. If you do not wish to do so, delete this |
---|
30 | ;;; exception statement from your version. |
---|
31 | |
---|
32 | (in-package "SYSTEM") |
---|
33 | |
---|
34 | ;;; From CMUCL. |
---|
35 | |
---|
36 | (defmacro satisfies-the-test (item elt) |
---|
37 | (let ((key-tmp (gensym))) |
---|
38 | `(let ((,key-tmp (apply-key key ,elt))) |
---|
39 | (cond (testp (funcall test ,item ,key-tmp)) |
---|
40 | (notp (not (funcall test-not ,item ,key-tmp))) |
---|
41 | (t (funcall test ,item ,key-tmp)))))) |
---|
42 | |
---|
43 | (defun %subst (new old tree key test testp test-not notp) |
---|
44 | (cond ((satisfies-the-test old tree) new) |
---|
45 | ((atom tree) tree) |
---|
46 | (t (let ((car (%subst new old (car tree) key test testp test-not notp)) |
---|
47 | (cdr (%subst new old (cdr tree) key test testp test-not notp))) |
---|
48 | (if (and (eq car (car tree)) |
---|
49 | (eq cdr (cdr tree))) |
---|
50 | tree |
---|
51 | (cons car cdr)))))) |
---|
52 | |
---|
53 | (defun subst (new old tree &key key (test #'eql testp) (test-not nil notp)) |
---|
54 | (%subst new old tree key test testp test-not notp)) |
---|
55 | |
---|
56 | (defun %subst-if (new test tree key) |
---|
57 | (cond ((funcall test (apply-key key tree)) new) |
---|
58 | ((atom tree) tree) |
---|
59 | (t (let ((car (%subst-if new test (car tree) key)) |
---|
60 | (cdr (%subst-if new test (cdr tree) key))) |
---|
61 | (if (and (eq car (car tree)) |
---|
62 | (eq cdr (cdr tree))) |
---|
63 | tree |
---|
64 | (cons car cdr)))))) |
---|
65 | |
---|
66 | (defun subst-if (new test tree &key key) |
---|
67 | (%subst-if new test tree key)) |
---|
68 | |
---|
69 | (defun %subst-if-not (new test tree key) |
---|
70 | (cond ((not (funcall test (apply-key key tree))) new) |
---|
71 | ((atom tree) tree) |
---|
72 | (t (let ((car (%subst-if-not new test (car tree) key)) |
---|
73 | (cdr (%subst-if-not new test (cdr tree) key))) |
---|
74 | (if (and (eq car (car tree)) |
---|
75 | (eq cdr (cdr tree))) |
---|
76 | tree |
---|
77 | (cons car cdr)))))) |
---|
78 | |
---|
79 | (defun subst-if-not (new test tree &key key) |
---|
80 | (%subst-if-not new test tree key)) |
---|
81 | |
---|
82 | (defun nsubst (new old tree &key key (test #'eql testp) (test-not nil notp)) |
---|
83 | (labels ((s (subtree) |
---|
84 | (cond ((satisfies-the-test old subtree) new) |
---|
85 | ((atom subtree) subtree) |
---|
86 | (t (do* ((last nil subtree) |
---|
87 | (subtree subtree (cdr subtree))) |
---|
88 | ((atom subtree) |
---|
89 | (if (satisfies-the-test old subtree) |
---|
90 | (setf (cdr last) new))) |
---|
91 | (if (satisfies-the-test old subtree) |
---|
92 | (return (setf (cdr last) new)) |
---|
93 | (setf (car subtree) (s (car subtree))))) |
---|
94 | subtree)))) |
---|
95 | (s tree))) |
---|
96 | |
---|
97 | (defun nsubst-if (new test tree &key key) |
---|
98 | (labels ((s (subtree) |
---|
99 | (cond ((funcall test (apply-key key subtree)) new) |
---|
100 | ((atom subtree) subtree) |
---|
101 | (t (do* ((last nil subtree) |
---|
102 | (subtree subtree (cdr subtree))) |
---|
103 | ((atom subtree) |
---|
104 | (if (funcall test (apply-key key subtree)) |
---|
105 | (setf (cdr last) new))) |
---|
106 | (if (funcall test (apply-key key subtree)) |
---|
107 | (return (setf (cdr last) new)) |
---|
108 | (setf (car subtree) (s (car subtree))))) |
---|
109 | subtree)))) |
---|
110 | (s tree))) |
---|
111 | |
---|
112 | (defun nsubst-if-not (new test tree &key key) |
---|
113 | (labels ((s (subtree) |
---|
114 | (cond ((not (funcall test (apply-key key subtree))) new) |
---|
115 | ((atom subtree) subtree) |
---|
116 | (t (do* ((last nil subtree) |
---|
117 | (subtree subtree (cdr subtree))) |
---|
118 | ((atom subtree) |
---|
119 | (if (not (funcall test (apply-key key subtree))) |
---|
120 | (setf (cdr last) new))) |
---|
121 | (if (not (funcall test (apply-key key subtree))) |
---|
122 | (return (setf (cdr last) new)) |
---|
123 | (setf (car subtree) (s (car subtree))))) |
---|
124 | subtree)))) |
---|
125 | (s tree))) |
---|