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

Last change on this file was 15734, checked in by Mark Evenson, 8 months ago

Implement vector-to-vector REPLACE as a primitive

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