source: trunk/j/src/org/armedbear/lisp/replace.lisp @ 11297

Last change on this file since 11297 was 11297, checked in by ehuelsmann, 17 years ago

Set Id keyword for expansion.

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