| 1 |
;;;-*- Mode:Lisp; Syntax:Common-Lisp; Package:CHAOS -*- |
| 2 |
;;; |
| 3 |
;;; Copyright (c) 2000-2018, Toshimi Sawada. All rights reserved. |
| 4 |
;;; |
| 5 |
;;; Redistribution and use in source and binary forms, with or without |
| 6 |
;;; modification, are permitted provided that the following conditions |
| 7 |
;;; are met: |
| 8 |
;;; |
| 9 |
;;; * Redistributions of source code must retain the above copyright |
| 10 |
;;; notice, this list of conditions and the following disclaimer. |
| 11 |
;;; |
| 12 |
;;; * Redistributions in binary form must reproduce the above |
| 13 |
;;; copyright notice, this list of conditions and the following |
| 14 |
;;; disclaimer in the documentation and/or other materials |
| 15 |
;;; provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED |
| 18 |
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 |
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 21 |
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 23 |
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 |
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 |
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
;;; |
| 29 |
(in-package :CHAOS) |
| 30 |
#|============================================================================== |
| 31 |
System: Chaos |
| 32 |
Module: comlib |
| 33 |
File: list.lisp |
| 34 |
==============================================================================|# |
| 35 |
#-:chaos-debug |
| 36 |
(declaim (optimize (speed 3) (safety 0) #-GCL (debug 0))) |
| 37 |
#+:chaos-debug |
| 38 |
(declaim (optimize (speed 1) (safety 3) #-GCL (debug 3))) |
| 39 |
|
| 40 |
;;; A collection of utilities on List structure |
| 41 |
|
| 42 |
;;; ************** |
| 43 |
;;; List Structure______________________________________________________________ |
| 44 |
;;; ************** |
| 45 |
|
| 46 |
;;; flatten-list |
| 47 |
;;; flattens list L, i.e., returns a single list containing the |
| 48 |
;;; same atoms as L but with any internal lists 'dissolved'. |
| 49 |
;;; For example, |
| 50 |
;;; (flatten-list '(a (b c) d)) ==> (a b c d) |
| 51 |
;;; Recursively flattens components of L, according to the following rules: |
| 52 |
;;; - an atom is already flattened. |
| 53 |
;;; - a list whose CAR is also a list is flattened by appending the |
| 54 |
;;; flattened CAR to the flattened CDR (this is what dissolves internal |
| 55 |
;;; lists). |
| 56 |
;;; - a list whose CAR is an atom is flattened by just flattening the CDR |
| 57 |
;;; and CONSing the original CAR onto the result. |
| 58 |
;;; These rules were chosen with some attention to minimizing CONSing." |
| 59 |
|
| 60 |
(defun flatten-list (L) |
| 61 |
;; (declare (optimize (speed 3) (safety 0))) |
| 62 |
(cond ((null L) '()) |
| 63 |
((atom L) L) |
| 64 |
((consp L) |
| 65 |
(if (consp (car L)) |
| 66 |
(append (flatten-list (car L)) (flatten-list (cdr L))) |
| 67 |
(cons (car L) (flatten-list (cdr L))))) |
| 68 |
)) |
| 69 |
|
| 70 |
;;; firstn |
| 71 |
;;; Returns a new list the same as List with only the first N elements. |
| 72 |
|
| 73 |
(defun firstn (list &optional (n 1)) |
| 74 |
(declare ;; (optimize (speed 3) (safety 0)) |
| 75 |
(type list list) |
| 76 |
(type fixnum n)) |
| 77 |
(cond ((> n (length list)) list) |
| 78 |
((< n 0) nil) |
| 79 |
(t (ldiff list (nthcdr n list))))) |
| 80 |
|
| 81 |
;;; in-order-union |
| 82 |
;;; Append and remove duplicates. Like union, but the objects are |
| 83 |
;;; guarranteed to stay in order. |
| 84 |
|
| 85 |
(defun in-order-union (list1 list2 &optional (test #'eql)) |
| 86 |
(remove-duplicates (append list1 list2) :from-end t :test test)) |
| 87 |
|
| 88 |
;;; true-list-p |
| 89 |
;;; Returns t if the term is a non-dotted list. Note that nil is a true list. |
| 90 |
|
| 91 |
(defun true-list-p (term) |
| 92 |
;; (declare (optimize (speed 3) (safety 0))) |
| 93 |
(and (listp term) |
| 94 |
(not (cdr (last term))))) |
| 95 |
|
| 96 |
;;; rotate-list |
| 97 |
;;; Returns a new list rotated at numth element. |
| 98 |
;;; |
| 99 |
(defun rotate-list (list num minusp) |
| 100 |
(declare (type fixnum num) |
| 101 |
(type list list)) |
| 102 |
(let ((len (length list)) |
| 103 |
(new-stack (copy-list list))) |
| 104 |
(declare (type fixnum len) |
| 105 |
(type list new-stack)) |
| 106 |
(when (>= (abs num) len) |
| 107 |
(return-from rotate-list nil)) |
| 108 |
(cond ((or (< num 0) (and (= num 0) minusp)) |
| 109 |
(setq num (- len (1+ (abs num)))) |
| 110 |
(print num) |
| 111 |
(setq new-stack |
| 112 |
(setq new-stack (nconc (nthcdr num new-stack) |
| 113 |
(firstn new-stack num)))) |
| 114 |
) |
| 115 |
(t (rotatef (nth 0 new-stack) |
| 116 |
(nth num new-stack)))) |
| 117 |
new-stack)) |
| 118 |
|
| 119 |
|
| 120 |
;;; delete-nth |
| 121 |
;;; Returns a new list deleted the nth element |
| 122 |
;;; |
| 123 |
(defun delete-nth (nth lst) |
| 124 |
(declare (fixnum nth) |
| 125 |
(type list lst)) |
| 126 |
(let ((len (length lst)) |
| 127 |
(new-lst nil)) |
| 128 |
(when (>= nth len) |
| 129 |
(return-from delete-nth nil)) |
| 130 |
(setq new-lst (nconc (firstn lst nth) |
| 131 |
(nthcdr (1+ nth) lst))) |
| 132 |
new-lst)) |
| 133 |
|
| 134 |
|
| 135 |
;;; EOF |