source: trunk/abcl/src/org/armedbear/lisp/substitute.lisp @ 11490

Last change on this file since 11490 was 11391, checked in by vvoutilainen, 16 years ago

ABCL license is GPL + Classpath exception. This was intended
by Peter Graves, the original author. For reference, see
http://sourceforge.net/mailarchive/forum.php?thread_name=20040721115302.839%40prufrock&forum_name=armedbear-j-announce

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1;;; substitute.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: substitute.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
33(in-package "COMMON-LISP")
34
35(export '(substitute substitute-if substitute-if-not))
36
37;;; From CMUCL.
38
39(defmacro real-count (count)
40  `(cond ((null ,count) most-positive-fixnum)
41         ((sys::fixnump ,count) (if (minusp ,count) 0 ,count))
42         ((integerp ,count) (if (minusp ,count) 0 most-positive-fixnum))
43         (t ,count)))
44
45(defun list-substitute* (pred new list start end count key test test-not old)
46  (let* ((result (list nil))
47   elt
48   (splice result)
49   (list list))           ; Get a local list for a stepper.
50    (do ((index 0 (1+ index)))
51      ((= index start))
52      (setq splice (cdr (rplacd splice (list (car list)))))
53      (setq list (cdr list)))
54    (do ((index start (1+ index)))
55      ((or (= index end) (null list) (= count 0)))
56      (setq elt (car list))
57      (setq splice
58      (cdr (rplacd splice
59       (list
60        (cond
61         ((case pred
62                              (normal
63                               (if test-not
64                                   (not
65                                    (funcall test-not old (sys::apply-key key elt)))
66                                   (funcall test old (sys::apply-key key elt))))
67                              (if (funcall test (sys::apply-key key elt)))
68                              (if-not (not (funcall test (sys::apply-key key elt)))))
69          (setq count (1- count))
70          new)
71                           (t elt))))))
72      (setq list (cdr list)))
73    (do ()
74      ((null list))
75      (setq splice (cdr (rplacd splice (list (car list)))))
76      (setq list (cdr list)))
77    (cdr result)))
78
79
80;;; Replace old with new in sequence moving from left to right by incrementer
81;;; on each pass through the loop. Called by all three substitute functions.
82(defun vector-substitute* (pred new sequence incrementer left right length
83                                start end count key test test-not old)
84  (let ((result (sys::make-sequence-like sequence length))
85  (index left))
86    (do ()
87      ((= index start))
88      (setf (aref result index) (aref sequence index))
89      (setq index (+ index incrementer)))
90    (do ((elt))
91      ((or (= index end) (= count 0)))
92      (setq elt (aref sequence index))
93      (setf (aref result index)
94      (cond ((case pred
95                     (normal
96                      (if test-not
97                          (not (funcall test-not old (sys::apply-key key elt)))
98                          (funcall test old (sys::apply-key key elt))))
99                     (if (funcall test (sys::apply-key key elt)))
100                     (if-not (not (funcall test (sys::apply-key key elt)))))
101       (setq count (1- count))
102       new)
103      (t elt)))
104      (setq index (+ index incrementer)))
105    (do ()
106      ((= index right))
107      (setf (aref result index) (aref sequence index))
108      (setq index (+ index incrementer)))
109    result))
110
111(defmacro subst-dispatch (pred)
112  `(if (listp sequence)
113       (if from-end
114           (nreverse (list-substitute* ,pred new (reverse sequence)
115                                       (- length end)
116                                       (- length start)
117                                       count key test test-not old))
118           (list-substitute* ,pred new sequence start end count key test test-not
119                             old))
120       (if from-end
121           (vector-substitute* ,pred new sequence -1 (1- length)
122                               -1 length (1- end)
123                               (1- start) count key test test-not old)
124           (vector-substitute* ,pred new sequence 1 0 length length
125                               start end count key test test-not old))))
126
127
128(defun substitute (new old sequence &key from-end (test #'eql) test-not
129                       (start 0) count end key)
130  (let* ((length (length sequence))
131   (end (or end length))
132   (count (real-count count)))
133    (subst-dispatch 'normal)))
134
135
136(defun substitute-if (new test sequence &key from-end (start 0) end count key)
137  (let* ((length (length sequence))
138   (end (or end length))
139   (count (real-count count))
140   test-not
141   old)
142    (subst-dispatch 'if)))
143
144
145(defun substitute-if-not (new test sequence &key from-end (start 0)
146                              end count key)
147  (let* ((length (length sequence))
148   (end (or end length))
149   (count (real-count count))
150   test-not
151   old)
152    (subst-dispatch 'if-not)))
Note: See TracBrowser for help on using the repository browser.