source: branches/0.17.x/abcl/src/org/armedbear/lisp/delete.lisp

Last change on this file was 11391, checked in by vvoutilainen, 17 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: 7.4 KB
Line 
1;;; delete.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: delete.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 real-count (count)
37  `(cond ((null ,count) most-positive-fixnum)
38         ((fixnump ,count) (if (minusp ,count) 0 ,count))
39         ((integerp ,count) (if (minusp ,count) 0 most-positive-fixnum))
40         (t ,count)))
41
42(defmacro mumble-delete (pred)
43  `(do ((index start (1+ index))
44        (jndex start)
45        (number-zapped 0))
46     ((or (= index end) (= number-zapped count))
47      (do ((index index (1+ index))   ; copy the rest of the vector
48           (jndex jndex (1+ jndex)))
49        ((= index length)
50         (shrink-vector sequence jndex))
51        (aset sequence jndex (aref sequence index))))
52     (aset sequence jndex (aref sequence index))
53     (if ,pred
54         (setq number-zapped (1+ number-zapped))
55         (setq jndex (1+ jndex)))))
56
57(defmacro mumble-delete-from-end (pred)
58  `(do ((index (1- end) (1- index)) ; find the losers
59        (number-zapped 0)
60        (losers ())
61        this-element
62        (terminus (1- start)))
63     ((or (= index terminus) (= number-zapped count))
64      (do ((losers losers)       ; delete the losers
65           (index start (1+ index))
66           (jndex start))
67        ((or (null losers) (= index end))
68         (do ((index index (1+ index))   ; copy the rest of the vector
69              (jndex jndex (1+ jndex)))
70           ((= index length)
71            (shrink-vector sequence jndex))
72           (aset sequence jndex (aref sequence index))))
73        (aset sequence jndex (aref sequence index))
74        (if (= index (car losers))
75            (pop losers)
76            (setq jndex (1+ jndex)))))
77     (setq this-element (aref sequence index))
78     (when ,pred
79       (setq number-zapped (1+ number-zapped))
80       (push index losers))))
81
82(defmacro normal-mumble-delete ()
83  `(mumble-delete
84    (if test-not
85        (not (funcall test-not item (funcall-key key (aref sequence index))))
86        (funcall test item (funcall-key key (aref sequence index))))))
87
88(defmacro normal-mumble-delete-from-end ()
89  `(mumble-delete-from-end
90    (if test-not
91        (not (funcall test-not item (funcall-key key this-element)))
92        (funcall test item (funcall-key key this-element)))))
93
94(defmacro list-delete (pred)
95  `(let ((handle (cons nil sequence)))
96     (do ((current (nthcdr start sequence) (cdr current))
97          (previous (nthcdr start handle))
98          (index start (1+ index))
99          (number-zapped 0))
100       ((or (= index end) (= number-zapped count))
101        (cdr handle))
102       (cond (,pred
103              (rplacd previous (cdr current))
104              (setq number-zapped (1+ number-zapped)))
105             (t
106              (setq previous (cdr previous)))))))
107
108(defmacro list-delete-from-end (pred)
109  `(let* ((reverse (nreverse sequence))
110          (handle (cons nil reverse)))
111     (do ((current (nthcdr (- length end) reverse)
112                   (cdr current))
113          (previous (nthcdr (- length end) handle))
114          (index start (1+ index))
115          (number-zapped 0))
116       ((or (= index end) (= number-zapped count))
117        (nreverse (cdr handle)))
118       (cond (,pred
119              (rplacd previous (cdr current))
120              (setq number-zapped (1+ number-zapped)))
121             (t
122              (setq previous (cdr previous)))))))
123
124(defmacro normal-list-delete ()
125  '(list-delete
126    (if test-not
127        (not (funcall test-not item (funcall-key key (car current))))
128        (funcall test item (funcall-key key (car current))))))
129
130(defmacro normal-list-delete-from-end ()
131  '(list-delete-from-end
132    (if test-not
133        (not (funcall test-not item (funcall-key key (car current))))
134        (funcall test item (funcall-key key (car current))))))
135
136(defun delete (item sequence &key from-end (test #'eql) test-not (start 0)
137                    end count key)
138  (when key
139    (setq key (coerce-to-function key)))
140  (let* ((length (length sequence))
141   (end (or end length))
142   (count (real-count count)))
143    (if (listp sequence)
144        (if from-end
145            (normal-list-delete-from-end)
146            (normal-list-delete))
147        (if from-end
148            (normal-mumble-delete-from-end)
149            (normal-mumble-delete)))))
150
151(defmacro if-mumble-delete ()
152  `(mumble-delete
153    (funcall predicate (funcall-key key (aref sequence index)))))
154
155(defmacro if-mumble-delete-from-end ()
156  `(mumble-delete-from-end
157    (funcall predicate (funcall-key key this-element))))
158
159(defmacro if-list-delete ()
160  '(list-delete
161    (funcall predicate (funcall-key key (car current)))))
162
163(defmacro if-list-delete-from-end ()
164  '(list-delete-from-end
165    (funcall predicate (funcall-key key (car current)))))
166
167(defun delete-if (predicate sequence &key from-end (start 0) key end count)
168  (when key
169    (setq key (coerce-to-function key)))
170  (let* ((length (length sequence))
171   (end (or end length))
172   (count (real-count count)))
173    (if (listp sequence)
174        (if from-end
175            (if-list-delete-from-end)
176            (if-list-delete))
177        (if from-end
178            (if-mumble-delete-from-end)
179            (if-mumble-delete)))))
180
181(defmacro if-not-mumble-delete ()
182  `(mumble-delete
183    (not (funcall predicate (funcall-key key (aref sequence index))))))
184
185(defmacro if-not-mumble-delete-from-end ()
186  `(mumble-delete-from-end
187    (not (funcall predicate (funcall-key key this-element)))))
188
189(defmacro if-not-list-delete ()
190  '(list-delete
191    (not (funcall predicate (funcall-key key (car current))))))
192
193(defmacro if-not-list-delete-from-end ()
194  '(list-delete-from-end
195    (not (funcall predicate (funcall-key key (car current))))))
196
197(defun delete-if-not (predicate sequence &key from-end (start 0) end key count)
198  (when key
199    (setq key (coerce-to-function key)))
200  (let* ((length (length sequence))
201   (end (or end length))
202   (count (real-count count)))
203    (if (listp sequence)
204        (if from-end
205            (if-not-list-delete-from-end)
206            (if-not-list-delete))
207        (if from-end
208            (if-not-mumble-delete-from-end)
209            (if-not-mumble-delete)))))
Note: See TracBrowser for help on using the repository browser.