source: trunk/abcl/src/org/armedbear/lisp/replace.lisp @ 12381

Last change on this file since 12381 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: 8.6 KB
Line 
1;;; replace.lisp
2;;;
3;;; Copyright (C) 2003-2005 Peter Graves
4;;; $Id: replace.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;;; Adapted from CMUCL.
33
34(in-package #:system)
35
36(eval-when (:compile-toplevel :load-toplevel :execute)
37  (defmacro seq-dispatch (sequence list-form array-form)
38    `(if (listp ,sequence)
39         ,list-form
40         ,array-form)))
41
42(eval-when (:compile-toplevel :execute)
43
44  ;;; If we are copying around in the same vector, be careful not to copy the
45  ;;; same elements over repeatedly.  We do this by copying backwards.
46  (defmacro mumble-replace-from-mumble ()
47    `(if (and (eq target-sequence source-sequence) (> target-start source-start))
48         (let ((nelts (min (- target-end target-start) (- source-end source-start))))
49           (do ((target-index (+ (the fixnum target-start) (the fixnum nelts) -1)
50                              (1- target-index))
51                (source-index (+ (the fixnum source-start) (the fixnum nelts) -1)
52                              (1- source-index)))
53               ((= target-index (the fixnum (1- target-start))) target-sequence)
54             (declare (fixnum target-index source-index))
55             (setf (aref target-sequence target-index)
56                   (aref source-sequence source-index))))
57         (do ((target-index target-start (1+ target-index))
58              (source-index source-start (1+ source-index)))
59             ((or (= target-index (the fixnum target-end))
60                  (= source-index (the fixnum source-end)))
61              target-sequence)
62           (declare (fixnum target-index source-index))
63           (setf (aref target-sequence target-index)
64                 (aref source-sequence source-index)))))
65
66  (defmacro list-replace-from-list ()
67    `(if (and (eq target-sequence source-sequence) (> target-start source-start))
68         (let ((new-elts (subseq source-sequence source-start
69                                 (+ (the fixnum source-start)
70                                    (the fixnum
71                                         (min (- (the fixnum target-end)
72                                                 (the fixnum target-start))
73                                              (- (the fixnum source-end)
74                                                 (the fixnum source-start))))))))
75           (do ((n new-elts (cdr n))
76                (o (nthcdr target-start target-sequence) (cdr o)))
77               ((null n) target-sequence)
78             (rplaca o (car n))))
79         (do ((target-index target-start (1+ target-index))
80              (source-index source-start (1+ source-index))
81              (target-sequence-ref (nthcdr target-start target-sequence)
82                                   (cdr target-sequence-ref))
83              (source-sequence-ref (nthcdr source-start source-sequence)
84                                   (cdr source-sequence-ref)))
85             ((or (= target-index (the fixnum target-end))
86                  (= source-index (the fixnum source-end))
87                  (null target-sequence-ref) (null source-sequence-ref))
88              target-sequence)
89           (declare (fixnum target-index source-index))
90           (rplaca target-sequence-ref (car source-sequence-ref)))))
91
92  (defmacro list-replace-from-mumble ()
93    `(do ((target-index target-start (1+ target-index))
94          (source-index source-start (1+ source-index))
95          (target-sequence-ref (nthcdr target-start target-sequence)
96                               (cdr target-sequence-ref)))
97         ((or (= target-index (the fixnum target-end))
98              (= source-index (the fixnum source-end))
99              (null target-sequence-ref))
100          target-sequence)
101       (declare (fixnum source-index target-index))
102       (rplaca target-sequence-ref (aref source-sequence source-index))))
103
104  (defmacro mumble-replace-from-list ()
105    `(do ((target-index target-start (1+ target-index))
106          (source-index source-start (1+ source-index))
107          (source-sequence (nthcdr source-start source-sequence)
108                           (cdr source-sequence)))
109         ((or (= target-index (the fixnum target-end))
110              (= source-index (the fixnum source-end))
111              (null source-sequence))
112          target-sequence)
113       (declare (fixnum target-index source-index))
114       (setf (aref target-sequence target-index) (car source-sequence))))
115
116  ) ; eval-when
117
118;;; The support routines for REPLACE are used by compiler transforms, so we
119;;; worry about dealing with end being supplied as or defaulting to nil
120;;; at this level.
121
122(defun list-replace-from-list* (target-sequence source-sequence target-start
123                                                target-end source-start source-end)
124  (when (null target-end) (setq target-end (length target-sequence)))
125  (when (null source-end) (setq source-end (length source-sequence)))
126  (list-replace-from-list))
127
128(defun list-replace-from-vector* (target-sequence source-sequence target-start
129                                                  target-end source-start source-end)
130  (when (null target-end) (setq target-end (length target-sequence)))
131  (when (null source-end) (setq source-end (length source-sequence)))
132  (list-replace-from-mumble))
133
134(defun vector-replace-from-list* (target-sequence source-sequence target-start
135                                                  target-end source-start source-end)
136  (when (null target-end) (setq target-end (length target-sequence)))
137  (when (null source-end) (setq source-end (length source-sequence)))
138  (mumble-replace-from-list))
139
140(defun vector-replace-from-vector* (target-sequence source-sequence
141                                                    target-start target-end source-start
142                                                    source-end)
143  (when (null target-end) (setq target-end (length target-sequence)))
144  (when (null source-end) (setq source-end (length source-sequence)))
145  (mumble-replace-from-mumble))
146
147(defun %replace (target-sequence source-sequence target-start target-end source-start source-end)
148  (declare (type (integer 0 #.most-positive-fixnum) target-start target-end source-start source-end))
149  (seq-dispatch target-sequence
150                (seq-dispatch source-sequence
151                              (list-replace-from-list)
152                              (list-replace-from-mumble))
153                (seq-dispatch source-sequence
154                              (mumble-replace-from-list)
155                              (mumble-replace-from-mumble))))
156
157;;; REPLACE cannot default end arguments to the length of sequence since it
158;;; is not an error to supply nil for their values.  We must test for ends
159;;; being nil in the body of the function.
160(defun replace (target-sequence source-sequence &key
161                                ((:start1 target-start) 0)
162                                ((:end1 target-end))
163                                ((:start2 source-start) 0)
164                                ((:end2 source-end)))
165  "The target sequence is destructively modified by copying successive
166elements into it from the source sequence."
167  (let ((target-end (or target-end (length target-sequence)))
168  (source-end (or source-end (length source-sequence))))
169    (%replace target-sequence source-sequence target-start target-end source-start source-end)))
Note: See TracBrowser for help on using the repository browser.