Hash :
99ff1c7c
Author :
Thomas de Grivel
Date :
2024-05-08T12:58:39
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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
;; cl-facts
;; Copyright 2011, 2023 Thomas de Grivel <thodg@kmx.io>
;;
;; Permission is hereby granted to use this software granted
;; the above copyright notice and this permission paragraph
;; are included in all copies and substantial portions of this
;; software.
;;
;; THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
;; PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
;; AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
;; THIS SOFTWARE.
(in-package :facts)
;;
;; Unlabeled Skip Lists
;; --------------------
#+nil
(pushnew :test *features*)
(declaim (optimize (debug 3) (speed 0)))
;; Types
(deftype positive-fixnum (&optional (low 0))
`(integer ,low ,most-positive-fixnum))
(deftype fixnum* (&optional (start '*) (end '*))
(let ((start (if (eq '* start) most-negative-fixnum start))
(end (if (eq '* end) most-positive-fixnum end)))
`(integer ,start ,end)))
(deftype fixnum-mult (mult &optional (start '*))
(declare (type (fixnum* 1) mult))
(let ((start (if (eq '* start)
(truncate most-negative-fixnum mult)
start)))
`(integer ,start
,(truncate most-positive-fixnum mult))))
(deftype fixnum-add (&optional (add '*) (start '*))
(let ((end (if (eq '* add)
(truncate most-positive-fixnum 2)
(- most-positive-fixnum (the positive-fixnum add)))))
`(fixnum* ,start ,end)))
(deftype fixnum-float (&optional (start '*) (end '*))
(let ((start (if (eq '* start) most-negative-fixnum start))
(end (if (eq '* end) most-positive-fixnum end)))
(declare (optimize (speed 1)))
`(long-float ,(coerce start 'long-float)
,(coerce end 'long-float))))
;; Unlabeled skip list node
(defstruct (usl-node (:constructor make-usl-node%))
(value nil)
(links nil :type simple-vector))
(defun make-usl-node (value height)
(declare (type (fixnum* 1) height))
(make-usl-node% :value value
:links (make-array height
:element-type '(or usl-node null)
:initial-element nil)))
(defun usl-node-next (node &optional (level 0))
(declare (type usl-node node)
(type (fixnum* 0) level))
(svref (usl-node-links node) level))
(define-setf-expander usl-node-next (node level &environment env)
(get-setf-expansion `(svref (usl-node-links (the usl-node ,node))
(the (fixnum* 0) ,level))
env))
(defun usl-node-height (node)
(declare (type usl-node node))
(the (fixnum* 1) (length (usl-node-links node))))
#+test
(defun test-usl-node ()
(let ((a (make-usl-node nil 2))
(b (make-usl-node #(1 2 3) 2))
(c (make-usl-node #(1 2 9) 1)))
(setf (usl-node-next a 0) b)
(setf (usl-node-next a 1) b)
(setf (usl-node-next b 0) c)
(setf (usl-node-next b 1) c)
a))
#+test
(test-usl-node)
;; Testing
#+test
(defun test-height-repartition (fun-fun max-height spacing)
(declare (type (fixnum* 1) max-height)
(type (function (fixnum fixnum) function) fun-fun))
(let ((height (make-array max-height
:element-type 'fixnum
:initial-element 0))
(rounds #1=1000000)
(fun (funcall fun-fun max-height spacing)))
(declare (type positive-fixnum rounds))
(format t "~&~S : ~D rounds, max-height ~D, spacing ~D~%"
fun-fun rounds max-height spacing)
(force-output)
(locally (declare (optimize speed))
(time
(dotimes (i rounds)
(incf (the (integer 0 #1#)
(aref height (1- (the positive-fixnum
(funcall fun)))))))))
(dotimes (i max-height)
(format t "~&height ~3D | p ~9,6F | 1/~D~%"
(1+ i)
(/ (aref height i) rounds)
(round rounds (1+ (aref height i)))))))
#+test
(defun cl-skip-list-random-level-fun (max-level spacing)
"Returns a random level for a new skip-list node, following Pugh's pattern of
L1: 50%, L2: 25%, L3: 12.5%, ..."
(declare (type fixnum spacing max-level)
(optimize speed))
(assert (= 2 spacing))
(lambda ()
(do ((level 1 (the fixnum (1+ level))))
((or (= level max-level)
(= (random 4) 3)) ;;
level)
(declare (type fixnum level)))))
#+test
(test-height-repartition #'cl-skip-list-random-level-fun 8 2)
;;
;; Random height
;; -------------
;;
;; ∀ U ∈ ℕ : 1 < U
;; ∀ n ∈ ℕ*
;; ∀ r ∈ ℕ : r ≤ n
;; ∀ random : ℕ* ⟶ ℕ
;; random(n) ∈ [0..n-1]
;; ∀ i ∈ [0..n-1], P(random(n) = i) = n⁻¹ (i)
;; Qᵣ := random(Uⁿ) < Uⁿ⁻ʳ
;;
;; (i) ⇒ P(random(n) < v) = ∑ᵢ₌₀ᵛ⁻¹ P(random(n) = i)
;; ⇒ P(random(n) < v) = v . n⁻¹ (ii)
;;
;; ⇒ P(random(Uⁿ) < Uⁿ⁻ʳ) = Uⁿ⁻ʳ . (Uⁿ)⁻¹
;; ⇔ P(Qᵣ) = U⁻ʳ (iii)
;;
;; P(Qₙ) = P(random(Uⁿ) < U⁰)
;; = P(random(Uⁿ) < 1)
;; = P(random(Uⁿ) = 0)
;; = U⁻ⁿ
;;
;; R := maxᵣ(Qᵣ)
;; = maxᵣ(random(Uⁿ) < Uⁿ⁻ʳ)
;; = maxᵣ(random(Uⁿ) + 1 ≤ Uⁿ⁻ʳ)
;; = maxᵣ(logᵤ(random(Uⁿ) + 1) ≤ n - r)
;; = maxᵣ(⌈logᵤ(random(Uⁿ) + 1)⌉ ≤ n - r)
;; = maxᵣ(r ≤ n - ⌈logᵤ(random(Uⁿ) + 1)⌉)
;; = n - ⌈logᵤ(random(Uⁿ) + 1)⌉ (iv)
;;
;; 0 ≤ random(Uⁿ) < Uⁿ
;; ⇔ 1 ≤ random(Uⁿ)+1 ≤ Uⁿ
;; ⇔ logᵤ(1) ≤ logᵤ(random(Uⁿ)+1) ≤ logᵤ(Uⁿ)
;; ⇔ 0 ≤ ⌈logᵤ(random(Uⁿ)+1)⌉ ≤ n
;; ⇔ -n ≤ -⌈logᵤ(random(Uⁿ)+1)⌉ ≤ 0
;; ⇔ 0 ≤ n - ⌈logᵤ(random(Uⁿ)+1)⌉ ≤ n
;; ⇔ 0 ≤ R ≤ n (v)
;;
#+test
(defun usl-random-height-fun* (max-height spacing)
(let* ((u (coerce spacing '(single-float 0.0s0)))
(n (the (fixnum-add 1 1) max-height))
(max (the (fixnum-add 1 1) (ceiling (expt u n)))))
(lambda ()
(declare (optimize (speed 3)))
(the positive-fixnum
(1+ (mod (- n (the fixnum
(ceiling
(log (1+ (random max))
u))))
n))))))
(defconstant +e+ 2.718281828459045235360287471352662497757l0)
#+test
(test-height-repartition #'usl-random-height-fun* 8 2)
#+test
(test-height-repartition #'usl-random-height-fun* 8 +e+)
(defun usl-height-table (max-height spacing)
(declare (type (fixnum* 2) max-height)
(type (fixnum-float 1 16) spacing))
(let ((table (make-array max-height
:element-type '(fixnum* 1)
:initial-element 1)))
(do ((h 0 (1+ h))
(w spacing (* spacing (the fixnum-float w)))
(end 1.0l0 (+ end w))
(pos 0 end))
((= max-height h))
(setf (aref table h) (truncate (the fixnum-float end))))
table))
(defun usl-random-height-fun (max-height spacing)
(declare (type positive-fixnum max-height)
(optimize speed))
(let* ((table (the (simple-array (fixnum* 1) (*))
(usl-height-table max-height spacing)))
(max (the positive-fixnum (1- (aref table (1- (length table)))))))
(lambda ()
(let ((k (random max)))
(do ((i 0 (1+ (the fixnum-add i))))
((< k (aref table i)) (- max-height i)))))))
#+test(test-height-repartition #'usl-random-height-fun 8 2)
#+test(test-height-repartition #'usl-random-height-fun 8 3)
#+test(test-height-repartition #'usl-random-height-fun 8 +e+)
(defun usl-random-height (max-height spacing)
(declare (type (fixnum* 1) max-height)
(type (fixnum-float 1) spacing))
(let ((max-height* (load-time-value #1=2))
(spacing* (load-time-value #2=+e+))
(fun* (load-time-value (usl-random-height-fun #1# #2#))))
(declare (type (function () (values (fixnum* 1) &optional)) fun*))
(unless (and (= max-height* max-height)
(= spacing* spacing))
(setf max-height* max-height
spacing* spacing
fun* (usl-random-height-fun max-height spacing)))
(funcall fun*)))
#+test
(time (test-height-repartition #'usl-random-height 8 2))
#+test
(time (test-height-repartition #'usl-random-height 8 3))
;; Skip list
(defstruct (usl (:constructor make-usl%))
(compare-fun nil :type (function (t t) fixnum))
(head nil :type usl-node)
(length 0 :type (fixnum* 0))
(spacing nil :type (fixnum-float 1))
(height-fun nil :type (function () (fixnum* 1))))
(defun make-usl (&key (compare #'compare:compare) (height 3) (spacing +e+))
(make-usl% :compare-fun compare
:spacing spacing
:head (make-usl-node nil height)
:height-fun (usl-random-height-fun height spacing)))
(defun usl-height (usl)
(usl-node-height (usl-head usl)))
(defun usl-compare (usl a b)
(declare (type usl usl))
(funcall (the (function (t t) (values fixnum &optional))
(usl-compare-fun usl))
a b))
;; Find
(defun usl-find (usl value &optional pred)
"Return the in-tree value if found.
PRED if given must be an array, fill it with predecessor usl nodes."
(declare (type usl usl)
(type (or null (simple-array t (*))) pred))
(with-slots (head) usl
(let ((node head))
(do ((level (1- (the (fixnum* 1) (usl-node-height head)))
(1- (the (fixnum* 0) level))))
((< level 0))
(do ((n node (usl-node-next n level)))
((or (null n)
(not (= -1 (usl-compare usl (usl-node-value n) value)))))
(setf node n))
(when (and pred (< level (length pred)))
(setf (aref pred level) node)))
(let* ((next (usl-node-next node 0))
(found (when next
(let ((next-value (usl-node-value next)))
(unless (= -1 (usl-compare usl value next-value))
value)))))
found))))
(defun usl-cursor (usl &optional value)
(let ((pred (make-array 1)))
(usl-find usl value pred)
(aref pred 0)))
#+test
(let ((usl (make-usl)))
(usl-cursor usl nil))
;; Insert
(defun usl-node-insert (pred new)
(declare (type (simple-array usl-node (*)) pred))
(dotimes (i (length pred))
(let ((node (aref pred i)))
(setf (usl-node-next new i) (usl-node-next node i)
(usl-node-next node i) new))))
(defun usl-insert (usl value)
(with-slots (head height-fun) usl
(declare (type (function nil (fixnum* 1)) height-fun))
(let* ((height (funcall height-fun))
(pred (make-array (the (fixnum* 1) height))))
(or (usl-find usl value pred)
(progn (usl-node-insert pred (make-usl-node value height))
(incf (usl-length usl))
value)))))
#+test
(defun test-usl-insert ()
(let ((usl (make-usl)))
(usl-insert usl #(1 1 1))
(usl-insert usl #(2 2 2))
(usl-insert usl #(0 0 0))
(usl-insert usl #(1 2 2))
(usl-insert usl #(0 0 1))
(usl-insert usl #(3 3 3))
usl))
#+test
(let ((*print-circle* t))
(format t "~%~A~&" (test-usl-insert)))
;; Get
(defun usl-get (usl value)
(usl-find usl value))
#+test
(let ((usl (test-usl-insert)))
(mapcar (lambda (x)
(format t "~&Get ~S -> ~S" x (usl-get usl x)))
(list 1 #(0 0 0) #(1 2 2) #(0 0 1) #(1 0 0) #(3 3 3) #(2 2 2))))
;; Each
(defun usl-each (usl fn &key start end)
(declare (type (function (t) t) fn))
(do ((node (usl-node-next (usl-cursor usl start)) (usl-node-next node)))
((or (null node)
(when end
(= -1 (usl-compare usl end (usl-node-value node))))))
(funcall fn (usl-node-value node))))
#+test
(defun test-usl-each ()
(usl-each (test-usl-insert)
(lambda (fact)
(format t "~&each ~S~%" fact))))
#+test
(test-usl-each)
;; Delete
(defun usl-delete (usl value)
(let* ((pred (make-array (the (fixnum* 1) (usl-height usl))))
(found (usl-find usl value pred)))
(when found
(let* ((node (usl-node-next (aref pred 0)))
(height (the (fixnum* 1) (usl-node-height node))))
(dotimes (i height)
(setf (usl-node-next (aref pred i) i)
(usl-node-next node i))))
(decf (usl-length usl))
found)))
#+test
(defun test-usl-delete ()
(let ((usl (test-usl-insert)))
(mapcar (lambda (x)
(format t "~&delete ~S -> ~S~%"
x (usl-delete usl x)))
(list #(1 2 2) #(0 0 0) #(1 2 2) #(3 3 3)))
(format t "~&~S~%" usl)))
#+test
(test-usl-delete)