/usr/share/common-lisp/source/cl-ppcre/charset.lisp is in cl-ppcre 2.0.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CL-PPCRE; Base: 10 -*-
;;; $Header: /usr/local/cvsrep/cl-ppcre/charset.lisp,v 1.10 2009/09/17 19:17:30 edi Exp $
;;; A specialized set implementation for characters by Nikodemus Siivola.
;;; Copyright (c) 2008, Nikodemus Siivola. All rights reserved.
;;; Copyright (c) 2008-2009, Dr. Edmund Weitz. All rights reserved.
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;; * Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;; * Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials
;;; provided with the distribution.
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(in-package :cl-ppcre)
(defconstant +probe-depth+ 3
"Maximum number of collisions \(for any element) we accept before we
allocate more storage. This is now fixed, but could be made to vary
depending on the size of the storage vector \(e.g. in the range of
1-4). Larger probe-depths mean more collisions are tolerated before
the table grows, but increase the constant factor.")
(defun make-char-vector (size)
"Returns a vector of size SIZE to hold characters. All elements are
initialized to #\Null except for the first one which is initialized to
#\?."
(declare #.*standard-optimize-settings*)
(declare (type (integer 2 #.(1- array-total-size-limit)) size))
;; since #\Null always hashes to 0, store something else there
;; initially, and #\Null everywhere else
(let ((result (make-array size
:element-type #-:lispworks 'character #+:lispworks 'lw:simple-char
:initial-element (code-char 0))))
(setf (char result 0) #\?)
result))
(defstruct (charset (:constructor make-charset ()))
;; this is set to 0 when we stop hashing and just use a CHAR-CODE
;; indexed vector
(depth +probe-depth+ :type fixnum)
;; the number of characters in this set
(count 0 :type fixnum)
;; the storage vector
(vector (make-char-vector 12) :type (simple-array character (*))))
;; seems to be necessary for some Lisps like ClozureCL
(defmethod make-load-form ((set charset) &optional environment)
(make-load-form-saving-slots set :environment environment))
(declaim (inline mix))
(defun mix (code hash)
"Given a character code CODE and a hash code HASH, computes and
returns the \"next\" hash code. See comments below."
(declare #.*standard-optimize-settings*)
;; mixing the CHAR-CODE back in at each step makes sure that if two
;; characters collide (their hashes end up pointing in the same
;; storage vector index) on one round, they should (hopefully!) not
;; collide on the next
(sxhash (logand most-positive-fixnum (+ code hash))))
(declaim (inline compute-index))
(defun compute-index (hash vector)
"Computes and returns the index into the vector VECTOR corresponding
to the hash code HASH."
(declare #.*standard-optimize-settings*)
(1+ (mod hash (1- (length vector)))))
(defun in-charset-p (char set)
"Checks whether the character CHAR is in the charset SET."
(declare #.*standard-optimize-settings*)
(declare (character char) (charset set))
(let ((vector (charset-vector set))
(depth (charset-depth set))
(code (char-code char)))
(declare (fixnum depth))
;; as long as the set remains reasonably small, we use non-linear
;; hashing - the first hash of any character is its CHAR-CODE, and
;; subsequent hashes are computed by MIX above
(cond ((or
;; depth 0 is special - each char maps only to its code,
;; nothing else
(zerop depth)
;; index 0 is special - only #\Null maps to it, no matter
;; what the depth is
(zerop code))
(eq char (char vector code)))
(t
;; otherwise hash starts out as the character code, but
;; maps to indexes 1-N
(let ((hash code))
(tagbody
:retry
(let* ((index (compute-index hash vector))
(x (char vector index)))
(cond ((eq x (code-char 0))
;; empty, no need to probe further
(return-from in-charset-p nil))
((eq x char)
;; got it
(return-from in-charset-p t))
((zerop (decf depth))
;; max probe depth reached, nothing found
(return-from in-charset-p nil))
(t
;; nothing yet, try next place
(setf hash (mix code hash))
(go :retry))))))))))
(defun add-to-charset (char set)
"Adds the character CHAR to the charset SET, extending SET if
necessary. Returns CHAR."
(declare #.*standard-optimize-settings*)
(or (%add-to-charset char set t)
(%add-to-charset/expand char set)
(error "Oops, this should not happen..."))
char)
(defun %add-to-charset (char set count)
"Tries to add the character CHAR to the charset SET without
extending it. Returns NIL if this fails. Counts CHAR as new
if COUNT is true and it is added to SET."
(declare #.*standard-optimize-settings*)
(declare (character char) (charset set))
(let ((vector (charset-vector set))
(depth (charset-depth set))
(code (char-code char)))
(declare (fixnum depth))
;; see comments in IN-CHARSET-P for algorithm
(cond ((or (zerop depth) (zerop code))
(unless (eq char (char vector code))
(setf (char vector code) char)
(when count
(incf (charset-count set))))
char)
(t
(let ((hash code))
(tagbody
:retry
(let* ((index (compute-index hash vector))
(x (char vector index)))
(cond ((eq x (code-char 0))
(setf (char vector index) char)
(when count
(incf (charset-count set)))
(return-from %add-to-charset char))
((eq x char)
(return-from %add-to-charset char))
((zerop (decf depth))
;; need to expand the table
(return-from %add-to-charset nil))
(t
(setf hash (mix code hash))
(go :retry))))))))))
(defun %add-to-charset/expand (char set)
"Extends the charset SET and then adds the character CHAR to it."
(declare #.*standard-optimize-settings*)
(declare (character char) (charset set))
(let* ((old-vector (charset-vector set))
(new-size (* 2 (length old-vector))))
(tagbody
:retry
;; when the table grows large (currently over 1/3 of
;; CHAR-CODE-LIMIT), we dispense with hashing and just allocate a
;; storage vector with space for all characters, so that each
;; character always uses only the CHAR-CODE
(multiple-value-bind (new-depth new-vector)
(if (>= new-size #.(truncate char-code-limit 3))
(values 0 (make-char-vector char-code-limit))
(values +probe-depth+ (make-char-vector new-size)))
(setf (charset-depth set) new-depth
(charset-vector set) new-vector)
(flet ((try-add (x)
;; don't count - old characters are already accounted
;; for, and might count the new one multiple times as
;; well
(unless (%add-to-charset x set nil)
(assert (not (zerop new-depth)))
(setf new-size (* 2 new-size))
(go :retry))))
(try-add char)
(dotimes (i (length old-vector))
(let ((x (char old-vector i)))
(if (eq x (code-char 0))
(when (zerop i)
(try-add x))
(unless (zerop i)
(try-add x))))))))
;; added and expanded, /now/ count the new character.
(incf (charset-count set))
t))
(defun map-charset (function charset)
"Calls FUNCTION with all characters in SET. Returns NIL."
(declare #.*standard-optimize-settings*)
(declare (function function))
(let* ((n (charset-count charset))
(vector (charset-vector charset))
(size (length vector)))
;; see comments in IN-CHARSET-P for algorithm
(when (eq (code-char 0) (char vector 0))
(funcall function (code-char 0))
(decf n))
(loop for i from 1 below size
for char = (char vector i)
unless (eq (code-char 0) char) do
(funcall function char)
;; this early termination test should be worth it when
;; mapping across depth 0 charsets.
(when (zerop (decf n))
(return-from map-charset nil))))
nil)
(defun create-charset-from-test-function (test-function start end)
"Creates and returns a charset representing all characters with
character codes between START and END which satisfy TEST-FUNCTION."
(declare #.*standard-optimize-settings*)
(loop with charset = (make-charset)
for code from start below end
for char = (code-char code)
when (and char (funcall test-function char))
do (add-to-charset char charset)
finally (return charset)))
|