source: branches/0.22.x/abcl/src/org/armedbear/lisp/count.lisp

Last change on this file was 12516, checked in by astalla, 15 years ago

Support for user-extensible sequences, adapted from SBCL.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1;;; count.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: count.lisp 12516 2010-03-03 21:05:41Z astalla $
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 "COMMON-LISP")
33
34(require "EXTENSIBLE-SEQUENCES-BASE")
35
36;;; From CMUCL.
37
38(defmacro vector-count-if (not-p from-end-p predicate sequence)
39  (let ((next-index (if from-end-p '(1- index) '(1+ index)))
40        (pred `(funcall ,predicate (sys::apply-key key (aref ,sequence index)))))
41    `(let ((%start ,(if from-end-p '(1- end) 'start))
42           (%end ,(if from-end-p '(1- start) 'end)))
43       (do ((index %start ,next-index)
44            (count 0))
45           ((= index %end) count)
46         (,(if not-p 'unless 'when) ,pred
47           (setq count (1+ count)))))))
48
49(defmacro list-count-if (not-p from-end-p predicate sequence)
50  (let ((pred `(funcall ,predicate (sys::apply-key key (pop sequence)))))
51    `(let ((%start ,(if from-end-p '(- length end) 'start))
52           (%end ,(if from-end-p '(- length start) 'end))
53           (sequence ,(if from-end-p '(reverse sequence) 'sequence)))
54       (do ((sequence (nthcdr %start ,sequence))
55            (index %start (1+ index))
56            (count 0))
57           ((or (= index %end) (null sequence)) count)
58         (,(if not-p 'unless 'when) ,pred
59           (setq count (1+ count)))))))
60
61(defun count (item sequence &rest args &key from-end (test #'eql test-p) (test-not nil test-not-p)
62       (start 0) end key)
63  (when (and test-p test-not-p)
64    (error "test and test-not both supplied"))
65  (let* ((length (length sequence))
66   (end (or end length)))
67    (let ((%test (if test-not-p
68         (lambda (x)
69           (not (funcall test-not item x)))
70         (lambda (x)
71           (funcall test item x)))))
72      (sequence::seq-dispatch sequence
73  (if from-end
74      (list-count-if nil t %test sequence)
75      (list-count-if nil nil %test sequence))
76  (if from-end
77      (vector-count-if nil t %test sequence)
78      (vector-count-if nil nil %test sequence))
79  (apply #'sequence:count item sequence args)))))
80
81(defun count-if (test sequence &rest args &key from-end (start 0) end key)
82  (let* ((length (length sequence))
83   (end (or end length)))
84    (sequence::seq-dispatch sequence
85        (if from-end
86            (list-count-if nil t test sequence)
87            (list-count-if nil nil test sequence))
88        (if from-end
89            (vector-count-if nil t test sequence)
90            (vector-count-if nil nil test sequence))
91  (apply #'sequence:count-if test sequence args))))
92
93(defun count-if-not (test sequence &rest args &key from-end (start 0) end key)
94  (let* ((length (length sequence))
95   (end (or end length)))
96    (sequence::seq-dispatch sequence
97        (if from-end
98            (list-count-if t t test sequence)
99            (list-count-if t nil test sequence))
100        (if from-end
101            (vector-count-if t t test sequence)
102            (vector-count-if t nil test sequence))
103  (apply #'sequence:count-if-not test sequence args))))
Note: See TracBrowser for help on using the repository browser.