/
closer-weak.lisp
1242 lines (1100 loc) · 50.5 KB
/
closer-weak.lisp
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;; -*- coding:utf-8 -*-
;;;;**************************************************************************
;;;;FILE: closer-weak.lisp
;;;;LANGUAGE: Common-Lisp
;;;;SYSTEM: Common-Lisp
;;;;USER-INTERFACE: NONE
;;;;DESCRIPTION
;;;;
;;;; See package docstring.
;;;;
;;;; Read-time Features:
;;;;
;;;; :WEAK-TEST When testing, call the garbage collector
;;;; to break weak pointers sooner.
;;;;
;;;; :DEBUG-WEAK When compiling on clisp, use this implementation
;;;; instead of the native one.
;;;;
;;;;AUTHORS
;;;; <PJB> Pascal J. Bourguignon <pjb@informatimago.com>
;;;;MODIFICATIONS
;;;; 2013-03-17 <PJB> Added implementation for ccl.
;;;; 2006-05-19 <PJB> Created.
;;;;BUGS
;;;;LEGAL
;;;; AGPL3
;;;;
;;;; Copyright Pascal J. Bourguignon 2006 - 2016
;;;;
;;;; This program is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Affero General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU Affero General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Affero General Public License
;;;; along with this program. If not, see <http://www.gnu.org/licenses/>
;;;;**************************************************************************
(eval-when (:compile-toplevel :load-toplevel :execute)
(setf *readtable* (copy-readtable nil)))
;;; (push :debug-weak *features*) in clisp to have it on clisp for debugging...
(defpackage "COM.INFORMATIMAGO.CLEXT.CLOSER-WEAK"
(:documentation "
Closer to Weak objects.
Implements the specification: <http://clisp.cons.org/impnotes/weak.html>
for Common Lisp implementations that have weak-pointers.
WEAK-OR-RELATION is a primitive that cannot be implemented properly
without implementation support.
Currently works on:
WP WL WAR WOR WM WHT
allegro
ccl x x x x n -- WHT native.
clisp n n n n n n -- full support - native
cmucl n x x x x -- partial support (missing WEAK-OR-RELATION)
sbcl n x x x x -- partial support (missing WEAK-OR-RELATION)
Copyright Pascal J. Bourguignon 2006 - 2013
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
")
(:use "COMMON-LISP")
#+(and clisp debug-weak)
(:import-from
"EXT"
"WEAK-POINTER" "MAKE-WEAK-POINTER" "WEAK-POINTER-VALUE")
#+(and clisp (not debug-weak))
(:import-from
"EXT"
"WEAK-POINTER"
"MAKE-WEAK-POINTER" "WEAK-POINTER-P" "WEAK-POINTER-VALUE"
"MAKE-WEAK-LIST" "WEAK-LIST-P" "WEAK-LIST-LIST"
"WEAK-AND-RELATION"
"MAKE-WEAK-AND-RELATION" "WEAK-AND-RELATION-P" "WEAK-AND-RELATION-LIST"
"WEAK-OR-RELATION"
"MAKE-WEAK-OR-RELATION" "WEAK-OR-RELATION-P" "WEAK-OR-RELATION-LIST"
"MAKE-WEAK-MAPPING" "WEAK-MAPPING-P" "WEAK-MAPPING-PAIR"
"WEAK-MAPPING-VALUE"
"MAKE-WEAK-AND-MAPPING" "WEAK-AND-MAPPING-P" "WEAK-AND-MAPPING-PAIR"
"WEAK-AND-MAPPING-VALUE"
"WEAK-OR-MAPPING"
"MAKE-WEAK-OR-MAPPING" "WEAK-OR-MAPPING-P" "WEAK-OR-MAPPING-PAIR"
"WEAK-OR-MAPPING-VALUE"
"MAKE-WEAK-ALIST" "WEAK-ALIST-P" "WEAK-ALIST-TYPE"
"WEAK-ALIST-CONTENTS" "WEAK-ALIST-ASSOC" "WEAK-ALIST-RASSOC"
"WEAK-ALIST-VALUE"
"WEAK-MAPPING" "HASH-TABLE-WEAK-P")
#+cmu
(:import-from "EXTENSIONS"
"WEAK-POINTER" "MAKE-WEAK-POINTER" "WEAK-POINTER-VALUE")
#+sbcl
(:import-from "SB-EXT"
"WEAK-POINTER" "MAKE-WEAK-POINTER" "WEAK-POINTER-VALUE")
(:export
"WEAK-POINTER"
"MAKE-WEAK-POINTER" "WEAK-POINTER-P" "WEAK-POINTER-VALUE"
"MAKE-WEAK-LIST" "WEAK-LIST-P" "WEAK-LIST-LIST"
"WEAK-AND-RELATION"
"MAKE-WEAK-AND-RELATION" "WEAK-AND-RELATION-P" "WEAK-AND-RELATION-LIST"
"WEAK-OR-RELATION"
"MAKE-WEAK-OR-RELATION" "WEAK-OR-RELATION-P" "WEAK-OR-RELATION-LIST"
"MAKE-WEAK-MAPPING" "WEAK-MAPPING-P" "WEAK-MAPPING-PAIR"
"WEAK-MAPPING-VALUE"
"MAKE-WEAK-AND-MAPPING" "WEAK-AND-MAPPING-P" "WEAK-AND-MAPPING-PAIR"
"WEAK-AND-MAPPING-VALUE"
"WEAK-OR-MAPPING"
"MAKE-WEAK-OR-MAPPING" "WEAK-OR-MAPPING-P" "WEAK-OR-MAPPING-PAIR"
"WEAK-OR-MAPPING-VALUE"
"MAKE-WEAK-ALIST" "WEAK-ALIST-P" "WEAK-ALIST-TYPE"
"WEAK-ALIST-CONTENTS" "WEAK-ALIST-ASSOC" "WEAK-ALIST-RASSOC"
"WEAK-ALIST-VALUE"
"WEAK-MAPPING" "HASH-TABLE-WEAK-P")
#-(and (or ccl clisp) (not debug-weak))
(:shadow "HASH-TABLE" "MAKE-HASH-TABLE"
"HASH-TABLE-P" "HASH-TABLE-COUNT"
"HASH-TABLE-REHASH-SIZE"
"HASH-TABLE-REHASH-THRESHOLD" "HASH-TABLE-SIZE"
"HASH-TABLE-TEST" "GETHASH" "REMHASH"
"MAPHASH" "WITH-HASH-TABLE-ITERATOR" "CLRHASH")
#-(and (or ccl clisp) (not debug-weak))
(:export "HASH-TABLE" "MAKE-HASH-TABLE"
"HASH-TABLE-P" "HASH-TABLE-COUNT"
"HASH-TABLE-REHASH-SIZE"
"HASH-TABLE-REHASH-THRESHOLD" "HASH-TABLE-SIZE"
"HASH-TABLE-TEST" "GETHASH" "REMHASH"
"MAPHASH" "WITH-HASH-TABLE-ITERATOR" "CLRHASH"))
(defpackage "COM.INFORMATIMAGO.CLEXT.CLOSER-WEAK-USER"
(:nicknames "CLOSER-WEAK-USER" "C2WEAK-USER")
(:use "COMMON-LISP" "COM.INFORMATIMAGO.CLEXT.CLOSER-WEAK")
(:shadowing-import-from
"COM.INFORMATIMAGO.CLEXT.CLOSER-WEAK"
"HASH-TABLE" "MAKE-HASH-TABLE"
"HASH-TABLE-P" "HASH-TABLE-COUNT" "HASH-TABLE-REHASH-SIZE"
"HASH-TABLE-REHASH-THRESHOLD" "HASH-TABLE-SIZE" "HASH-TABLE-TEST"
"GETHASH" "REMHASH" "MAPHASH" "WITH-HASH-TABLE-ITERATOR" "CLRHASH"))
(in-package "COM.INFORMATIMAGO.CLEXT.CLOSER-WEAK")
(defun gc ()
"Calls the garbage collector."
#-(or ccl clisp cmu sbcl) (error "~S: Missing a garbage collector call for ~S" 'gc (lisp-implementationt-type))
#+ccl (ccl:gc)
#+clisp (ext:gc)
#+cmu (extensions:gc)
#+sbcl (sb-ext:gc :full t))
;;;---------------------------------------------------------------------
;;; Weak pointers
#+allegro
(defstruct (weak-pointer
(:constructor %make-weak-pointer))
(%pointer (excl:weak-vector 1)))
#+(or allegro ccl)
(defun make-weak-pointer (object)
#+allegro (let ((wp (%make-weak-pointer)))
(setf (aref (weak-pointer-%pointer wp) 0) object))
;; #+ccl (ccl:make-population :initial-contents (list object))
#+ccl (let ((h (cl:make-hash-table :test 'eq :weak :value)))
(setf (gethash '%weak-pointer% h) '%weak-pointer%
(gethash '%weak-object% h) object)
h))
#+(not clisp)
(defun weak-pointer-p (object)
"Returns true if the object is of type WEAK-POINTER."
#+allegro (typep object 'weak-pointer)
;;#+ccl (typep object 'ccl:population)
#+ccl (and (hash-table-p object)
(ccl:hash-table-weak-p object)
(eq (hash-table-test object) 'eq)
(eq (gethash '%weak-pointer% object) '%weak-pointer%))
#+cmu (typep object 'extensions:weak-pointer)
#+sbcl (typep object 'sb-ext:weak-pointer))
#+(and clisp debug-weak)
(defun weak-pointer-p (object)
"Returns true if the object is of type WEAK-POINTER."
(ext:weak-pointer-p object))
#+(or allegro ccl)
(defun weak-pointer-value (wp)
#+allegro (values (aref (weak-pointer-%pointer wp) 0)
(aref (weak-pointer-%pointer wp) 0))
#+ccl (gethash '%weak-object% wp))
#-(or allegro ccl clisp cmu sbcl)
(eval-when (:compile-toplevel :load-toplevel :execute)
(error "~A: Please implement WEAK-POINTER and WEAK-OR-RELATION for ~A"
'closer-weak (lisp-implementation-type)))
;;;---------------------------------------------------------------------
;;; Weak lists
#-(and (or ccl clisp) (not debug-weak))
(defstruct (weak-list (:constructor %make-weak-list))
"A WEAK-LIST is an ordered collection of references to objects that
does not keep the objects from being garbage-collected. It is
semantically equivalent to a list of WEAK-POINTERs, however with a
more efficient in-memory representation than a plain list of
WEAK-POINTERs would be."
head)
#-(and (or ccl clisp))
(defun make-weak-list (list)
"Creates a WEAK-LIST pointing to each of the elements in the given list."
(check-type list list)
(%make-weak-list :head (mapcar (function make-weak-pointer) list)))
#-(and (or ccl clisp) (not debug-weak))
(defun weak-list-list (weak-list)
"Returns a LIST of those objects from the weak-list that are still alive."
(let ((alive (delete-if (lambda (item)
(multiple-value-bind (object livep)
(weak-pointer-value item)
(declare (ignore object))
(not livep))) (weak-list-head weak-list))))
(setf (weak-list-head weak-list) alive)
(mapcar (function weak-pointer-value) alive)))
#-(and (or ccl clisp) (not debug-weak))
(defun (setf weak-list-list) (value weak-list)
"Replaces the list of objects stored by the weak-list."
(setf (weak-list-head weak-list)
(map 'list (function make-weak-pointer) value))
value)
#+ccl (deftype weak-list () 'ccl:population)
#+ccl (defun make-weak-list (list) (check-type list list) (ccl:make-population :initial-contents list))
#+ccl (defun weak-list-p (wl) (and (typep wl 'ccl:population) (eq :list (ccl:population-type wl))))
#+ccl (defun weak-list-list (wl) (ccl:population-contents wl))
#+ccl (defun (setf weak-list-list) (value wl) (setf (ccl:population-contents wl) value))
#-(and (or ccl clisp) (not debug-weak))
(setf (documentation 'weak-list-p 'function)
"Returns true if the object is of type WEAK-LIST.")
;;;---------------------------------------------------------------------
;;; Weak AND relations
#-(and clisp (not debug-weak))
(defstruct (weak-and-relation (:constructor %make-weak-and-relation)
(:conc-name %weak-and-relation-))
"A weak 'and' relation is an ordered collection of references to
objects, that does not keep the objects from being garbage-collected,
and which allows access to all the objects as long as all of them are
still alive. As soon as one of them is garbage-collected, the entire
collection of objects becomes empty."
objects)
#-(and clisp (not debug-weak))
(setf (documentation 'weak-and-relation-p 'function)
"Returns true if the object is of type WEAK-AND-RELATION.")
#-(and clisp (not debug-weak))
(defun make-weak-and-relation (list)
"Creates a WEAK-AND-RELATION between the objects in the given list."
(%make-weak-and-relation
:objects (map 'vector (function make-weak-pointer) list)))
#-(and clisp (not debug-weak))
(defun weak-and-relation-list (weak-and-relation)
"Returns the list of objects stored in the weak-and-relation.
The returned list must not be destructively modified."
(loop
:named :loop
:with res = '()
:for weak-pointer :across (%weak-and-relation-objects weak-and-relation)
:do (multiple-value-bind (object presp) (weak-pointer-value weak-pointer)
(if presp
(push object res)
(return-from :loop '())))
:finally (return-from :loop (nreverse res))))
;; (defun warp (war) (map 'list (lambda (x) (multiple-value-bind (o p) (weak-pointer-value x) (declare (ignore o)) p)) (%weak-and-relation-objects war)))
;;;---------------------------------------------------------------------
;;; Weak OR relations
;;; Unfortunately, it looks like this is a primitive operation that
;;; cannot be implemented with just weak pointers. CMUCL & SBCL are weak.
;; #+(or cmu sbcl)
;; (eval-when (:compile-toplevel :load-toplevel :execute)
;; (warn "~A: ~A is lacking a primitive WEAK-OR-RELATION."
;; 'closer-weak (lisp-implementation-type)))
#-(and clisp (not debug-weak))
(defstruct (weak-or-relation (:constructor %make-weak-or-relation)
(:conc-name %weak-or-relation-))
"A weak 'or' relation is an ordered collection of references to
objects, that keeps all objects from being garbage-collected as long
as one of them is still alive. In other words, each of them keeps all
others among them from being garbage-collected. When all of them are
unreferenced, the collection of objects becomes empty."
objects)
#-(and clisp (not debug-weak))
(setf (documentation 'weak-or-relation-p 'function)
"Returns true if the object is of type WEAK-OR-RELATION.")
#-(and clisp (not debug-weak))
(defun make-weak-or-relation (list)
"Creates a WEAK-OR-RELATION between the objects in the given list."
(%make-weak-or-relation
:objects (map 'vector (if (and list (null (cdr list)))
(function make-weak-pointer)
(function identity)) list)))
#-(and clisp (not debug-weak))
(defun weak-or-relation-list (weak-or-relation)
"Returns the list of objects stored in the WEAK-OR-RELATION.
The returned list must not be destructively modified."
(if (= 1 (length (%weak-or-relation-objects weak-or-relation)))
(multiple-value-bind (object alivep)
(weak-pointer-value (aref (%weak-or-relation-objects
weak-or-relation) 0))
(if alivep
(list object)
'()))
(map 'list (function identity)
(%weak-or-relation-objects weak-or-relation))))
;;;---------------------------------------------------------------------
(defgeneric alivep (object)
(:documentation "Returns whether the object is alive."))
(defgeneric pair-key (object)
(:documentation "Returns the key in the pair object."))
(defgeneric pair-value (object)
(:documentation "Returns the value in the pair object."))
(defgeneric (setf pair-value) (value object)
(:documentation "Changes the value in the pair object."))
(defmacro define-pair-methods (op alive-fun)
"Generates the methods for WEAK-AND-RELATIONs and WEAK-OR-RELATIONs."
(flet ((c (args) (intern (with-standard-io-syntax (format nil "~{~A~}" args)))))
`(progn
(defmethod initialize-instance ((self ,(c `(weak- ,op -relation)))
&key key value)
,(format nil "Initialize a WEAK-~A-RELATION object." op)
(setf (,(c `(%weak- ,op -relation-objects)) self)
(map 'vector (function make-weak-pointer) (list key value))))
(defmethod alivep ((self ,(c `(weak- ,op -relation))))
"Returns whether the object is alive."
,(if (eq op 'or)
;; our pseudo weak-or-relations are just
;; vectors of normal object references.
;; Unless there is only one element in it.
`(if (= 1 (length (%weak-or-relation-objects self)))
(,alive-fun (lambda (wo) (nth-value 1 (weak-pointer-value wo)))
(,(c `(%weak- ,op -relation-objects)) self))
t)
`(,alive-fun (lambda (wo) (nth-value 1 (weak-pointer-value wo)))
(,(c `(%weak- ,op -relation-objects)) self))))
(defmethod pair-key ((self ,(c `(weak- ,op -relation))))
"Returns the key in the pair object."
,(if (eq op 'or)
;; our pseudo weak-or-relations are just
;; vectors of normal object references.
;; Unless there is only one element in it. (There's 2 in pairs).
`(aref (,(c `(%weak- ,op -relation-objects)) self) 0)
`(weak-pointer-value
(aref (,(c `(%weak- ,op -relation-objects)) self) 0))))
(defmethod pair-value ((self ,(c `(weak- ,op -relation))))
"Returns the value in the pair object."
,(if (eq op 'or)
;; our pseudo weak-or-relations are just
;; vectors of normal object references.
;; Unless there is only one element in it. (There's 2 in pairs).
`(aref (,(c `(%weak- ,op -relation-objects)) self) 1)
`(weak-pointer-value
(aref (,(c `(%weak- ,op -relation-objects)) self) 1))))
(defmethod (setf pair-value) (value (self ,(c `(weak- ,op -relation))))
"Changes the value in the pair object."
(setf (aref (,(c `(%weak- ,op -relation-objects)) self) 1)
,(if (eq op 'or)
;; our pseudo weak-or-relations are just
;; vectors of normal object references.
;; Unless there is only one element in it.
;; (There's 2 in pairs).
`value
`(make-weak-pointer value)))))))
#-(and clisp (not debug-weak)) (define-pair-methods and every)
#-(and clisp (not debug-weak)) (define-pair-methods or some)
;;;---------------------------------------------------------------------
;;; Weak Associations
#-(and clisp (not debug-weak))
(defstruct (weak-mapping
(:constructor %make-weak-mapping)
(:conc-name %weak-mapping-))
"A weak association is a mapping from an object called key to an
object called value, that exists as long as the key is alive. In other
words, as long as the key is alive, it keeps the value from being
garbage-collected."
key value)
#-(and clisp (not debug-weak))
(setf (documentation 'weak-mapping-p 'function)
"Returns true if the object is of type WEAK-MAPPING.")
#-(and clisp (not debug-weak))
(defun make-weak-mapping (key value)
"Creates a WEAK-MAPPING."
(%make-weak-mapping :key (make-weak-pointer key) :value value))
#-(and clisp (not debug-weak))
(defmethod initialize-instance ((self weak-mapping) &key key value)
"Initialize a WEAK-MAPPING object."
(setf (%weak-mapping-key self) (make-weak-pointer key)
(%weak-mapping-value self) value))
#-(and clisp (not debug-weak))
(defun weak-mapping-pair (weak-mapping)
"Returns true if the object is of type WEAK-MAPPING."
(multiple-value-bind (key kpresp)
(weak-pointer-value (%weak-mapping-key weak-mapping))
(if kpresp
(values key (%weak-mapping-value weak-mapping) t)
(progn (setf (%weak-mapping-value weak-mapping) nil)
#+weak-test (gc)
(values nil nil nil)))))
#-(and clisp (not debug-weak))
(defun %weak-mapping-pair (weak-mapping)
"Returns true if the object is of type WEAK-MAPPING."
(multiple-value-bind (key kpresp)
(weak-pointer-value (%weak-mapping-key weak-mapping))
(if kpresp
(values key (%weak-mapping-value weak-mapping) t)
(values nil nil nil))))
#-(and clisp (not debug-weak))
(defun weak-mapping-value (weak-mapping)
"Returns three values: the original key, the original value, and T,
if the key has not yet been garbage-collected, else NIL, NIL, NIL."
(if (nth-value 1 (weak-pointer-value (%weak-mapping-key weak-mapping)))
(%weak-mapping-value weak-mapping)
(progn
(setf (%weak-mapping-value weak-mapping) nil)
#+weak-test (gc)
nil)))
#-(and clisp (not debug-weak))
(defun (setf weak-mapping-value) (value weak-mapping)
"Replaces the value stored in the weak-mapping.
It has no effect when the key has already been garbage-collected."
(if (nth-value 1 (weak-pointer-value (%weak-mapping-key weak-mapping)))
(setf (%weak-mapping-value weak-mapping) value)
(progn (setf (%weak-mapping-value weak-mapping) nil)
#+weak-test (ext:gc)
value)))
#-(and clisp (not debug-weak))
(defmethod alivep ((self weak-mapping))
"Returns whether the object is alive."
(nth-value 2 (%weak-mapping-pair self)))
#-(and clisp (not debug-weak))
(defmethod pair-key ((self weak-mapping))
"Returns the key in the pair object."
(nth-value 0 (%weak-mapping-pair self)))
#-(and clisp (not debug-weak))
(defmethod pair-value ((self weak-mapping))
"Returns the value in the pair object."
(nth-value 1 (%weak-mapping-pair self)))
#-(and clisp (not debug-weak))
(defmethod (setf pair-value) (value (self weak-mapping))
"Changes the value in the pair object."
(if (alivep self)
(setf (%weak-mapping-value self) value)
(progn
;; TODO: check this...
(setf (%weak-mapping-value self) nil)
#+weak-test (gc)
value)))
;;;---------------------------------------------------------------------
;;; Weak And/Or Mappings
(defmacro define-mapping (op)
"Generates the functions for WEAK-AND/OR-MAPPINGS."
(flet ((c (args) (intern (with-standard-io-syntax (format nil "~{~A~}" args)))))
`(progn
(defstruct (,(c `(weak- ,op -mapping) )
(:constructor ,(c `(%make-weak- ,op -mapping)))
(:conc-name ,(c `(%weak- ,op -mapping-))))
,(if (eq op 'or)
"A weak 'or' mapping is a mapping from a tuple of
objects called keys to an object called value, that keeps all keys and
the value from being garbage-collected as long as one of the keys is
still alive. In other words, each of the keys keeps all others among
them and the value from being garbage-collected. When all of them are
unreferenced, the entire mapping goes away."
"A weak 'and' mapping is a mapping from a tuple of
objects called keys to an object called value, that does not keep the
keys from being garbage-collected and that exists as long as all keys
are alive. As soon as one of the keys is garbage-collected, the entire
mapping goes away.")
keys value)
(setf (documentation ',(c `(weak- ,op -mapping-p)) 'function)
,(format nil
"Returns true if the object is of type WEAK-~A-MAPPING."
op))
(defun ,(c `(make-weak- ,op -mapping)) (keys value)
,(format nil "Creates a WEAK-~A-MAPPING between the keys objects in
the given list and the given value. The keys list must be non-empty." op)
(assert (etypecase keys
(list (not (null keys)))
(vector (plusp (length keys))))
(keys) "Keys must be a non null list, not ~S" keys)
(,(c `(%make-weak- ,op -mapping))
:keys (,(c `(make-weak- ,op -relation)) keys)
:value value))
(defun ,(c `(weak- ,op -mapping-pair)) (mapping)
,(if (eq op 'or)
"Returns three values: the list of keys, the value, and
T, if the keys have not yet been garbage-collected, else NIL, NIL,
NIL. The returned keys list must not be destructively modified."
"Returns three values: the list of keys, the value, and
T, if none of the keys have been garbage-collected, else NIL, NIL,
NIL. The returned keys list must not be destructively modified.")
(let ((keys (,(c `(%weak- ,op -mapping-keys)) mapping)))
(if (alivep keys)
(values (,(c `(weak- ,op -relation-list)) keys)
(,(c `(%weak- ,op -mapping-value)) mapping)
t)
(progn
(setf (,(c `(%weak- ,op -mapping-value)) mapping) nil)
#+weak-test (gc)
(values nil nil nil)))))
(defun ,(c `( weak- ,op -mapping-value)) (mapping)
,(if (eq op 'or)
"Returns the value, if the keys have not yet been
garbage-collected, else NIL."
"Returns the value, if none of the keys have been
garbage-collected, else NIL.")
(if (alivep (,(c `(%weak- ,op -mapping-keys)) mapping))
(,(c `(%weak- ,op -mapping-value)) mapping)
(progn
(setf (,(c `(%weak- ,op -mapping-value)) mapping) nil)
#+weak-test (gc)
nil)))
(defun (setf ,(c `( weak- ,op -mapping-value))) (value mapping)
,(if (eq op 'or)
"Replaces the value stored in the WEAK-OR-MAPPING.
It has no effect when the keys have already been garbage-collected."
"Replaces the value stored in the WEAK-AND-MAPPING.
It has no effect when some key has already been garbage-collected.")
(if (alivep (,(c `(%weak- ,op -mapping-keys)) mapping))
(setf (,(c `(%weak- ,op -mapping-value)) mapping) value)
(progn (setf (,(c `(%weak- ,op -mapping-value)) mapping) nil)
#+weak-test (gc)
value))))))
#-(and clisp (not debug-weak)) (define-mapping and)
#-(and clisp (not debug-weak)) (define-mapping or)
;;;---------------------------------------------------------------------
;;; Weak Association Lists
#-(and clisp (not debug-weak))
(defstruct (inverse-weak-mapping (:include weak-mapping)))
#-(and clisp (not debug-weak))
(defmethod initialize-instance ((self inverse-weak-mapping) &key key value)
(call-next-method self :key value :value key))
#-(and clisp (not debug-weak))
(defmethod alivep ((self inverse-weak-mapping))
(nth-value 2 (%weak-mapping-pair self)))
#-(and clisp (not debug-weak))
(defmethod pair-key ((self inverse-weak-mapping))
(%weak-mapping-value self))
#-(and clisp (not debug-weak))
(defmethod pair-value ((self inverse-weak-mapping))
(weak-pointer-value (%weak-mapping-key self)))
#-(and clisp (not debug-weak))
(defmethod (setf pair-value) (value (self inverse-weak-mapping))
(setf (%weak-mapping-key self) (make-weak-pointer value)))
#-(and clisp (not debug-weak))
(defclass weak-alist ()
((contents :accessor wal-contents
:initarg :contents)))
#-(and clisp (not debug-weak)) (defclass wal-key (weak-alist) ())
#-(and clisp (not debug-weak)) (defclass wal-value (weak-alist) ())
#-(and clisp (not debug-weak)) (defclass wal-key-and-value (weak-alist) ())
#-(and clisp (not debug-weak)) (defclass wal-key-or-value (weak-alist) ())
#-(and clisp (not debug-weak))
(defgeneric wal-pair-class (wal)
(:method ((wal weak-alist)) nil)
(:method ((wal wal-key)) 'weak-mapping)
(:method ((wal wal-value)) 'inverse-weak-mapping)
(:method ((wal wal-key-and-value)) 'weak-and-relation)
(:method ((wal wal-key-or-value)) 'weak-or-relation))
#-(and clisp (not debug-weak))
(defgeneric weak-alist-p (wal)
(:method ((wal t)) nil)
(:method ((wal wal-key)) t)
(:method ((wal wal-value)) t)
(:method ((wal wal-key-and-value)) t)
(:method ((wal wal-key-or-value)) t))
#-(and clisp (not debug-weak))
(defgeneric weak-alist-type (wal)
(:method ((wal weak-alist)) nil)
(:method ((wal wal-key)) :key)
(:method ((wal wal-value)) :value)
(:method ((wal wal-key-and-value)) :key-and-value)
(:method ((wal wal-key-or-value)) :key-or-value))
#-(and clisp (not debug-weak))
(defun make-weak-alist (&key (type :key) (initial-contents '()))
(let ((wal (make-instance (ecase type
(:key 'wal-key)
(:value 'wal-value)
(:key-and-value 'wal-key-and-value)
(:key-or-value 'wal-key-or-value)))))
(setf (weak-alist-contents wal) initial-contents)
wal))
#-(and clisp (not debug-weak))
(defgeneric weak-alist-contents (wal)
(:method ((wal weak-alist))
(let ((alive
#+weak-test
(loop
:for count = (length (wal-contents wal))
:then (length alive)
:for alive = (delete-if-not (function alivep) (wal-contents wal))
:then (delete-if-not (function alivep) alive)
:while (< (length alive) count)
:do (gc)
:finally (return alive))
#-weak-test
(delete-if-not (function alivep) (wal-contents wal))))
(setf (wal-contents wal) alive)
(mapcar (lambda (pair) (cons (pair-key pair) (pair-value pair))) alive))))
#-(and clisp (not debug-weak))
(defgeneric (setf weak-alist-contents) (value wal)
(:method ((value t) (wal weak-alist))
(let ((pair-class (wal-pair-class wal)))
(setf (wal-contents wal) (mapcar (lambda (pair) (make-instance pair-class
:key (car pair)
:value (cdr pair)))
value)))
value))
#-(and clisp (not debug-weak))
(defgeneric weak-alist-assoc (item self &key test test-not key))
#-(and clisp (not debug-weak))
(defmethod weak-alist-assoc (item (self weak-alist) &key (test (function eql))
(test-not nil) (key (function identity)))
(if test-not
(weak-alist-assoc item self :test (complement test-not) :key key)
(loop
:named :assoc
:for pair :in (wal-contents self)
:do (when (and (alivep pair)
(funcall test item (funcall key (pair-key pair))))
(return-from :assoc (cons (pair-key pair)
(pair-value pair))))
:finally (return-from :assoc nil))))
#-(and clisp (not debug-weak))
(defgeneric weak-alist-remove-assoc (item weak-alist &key test test-not key))
#-(and clisp (not debug-weak))
(defmethod weak-alist-remove-assoc (item (self weak-alist)
&key (test (function eql))
(test-not nil) (key (function identity)))
(if test-not
(weak-alist-remove-assoc item self :test (complement test-not) :key key)
(loop
:named :assoc
:for previous = nil :then current
:for current = (wal-contents self) :then (cdr current)
:do (when (and (alivep (car current))
(funcall test item
(funcall key (pair-key (car current)))))
(if previous
(setf (cdr previous) (cdr current))
(setf (wal-contents self) (cdr current)))
(return-from :assoc t))
:finally (return-from :assoc nil))))
#-(and clisp (not debug-weak))
(defgeneric weak-alist-rassoc (item weak-alist &key test test-not key))
#-(and clisp (not debug-weak))
(defmethod weak-alist-rassoc (item (self weak-alist) &key (test (function eql))
(test-not nil) (key (function identity)))
(if test-not
(weak-alist-rassoc item self :test (complement test-not) :key key)
(loop
:named :assoc
:for pair :in (wal-contents self)
:do (when (and (alivep pair)
(funcall test item (funcall key (pair-value pair))))
(return-from :assoc (cons (pair-key pair)
(pair-value pair))))
:finally (return-from :assoc nil))))
#-(and clisp (not debug-weak))
(defgeneric weak-alist-value (item weak-alist &key test test-not))
#-(and clisp (not debug-weak))
(defmethod weak-alist-value (item (self weak-alist)
&key (test (function eql)) (test-not nil))
(cdr (weak-alist-assoc item self :test test :test-not test-not)))
#-(and clisp (not debug-weak))
(defgeneric (setf weak-alist-value) (value item weak-alist &key test test-not))
#-(and clisp (not debug-weak))
(defmethod (setf weak-alist-value) (value item (self weak-alist)
&key (test (function eql)) (test-not nil))
(if test-not
(setf (weak-alist-value item self :test (complement test-not)) value)
(loop
:named :assoc
:for pair :in (wal-contents self)
:do (when (and (alivep pair) (funcall test item (pair-key pair)))
(return-from :assoc (setf (pair-value pair) value)))
:finally (progn
(push (make-instance (wal-pair-class self)
:key item :value value) (wal-contents self))
(return-from :assoc value)))))
;;;---------------------------------------------------------------------
;;; Weak Hash Tables
#-(and (or ccl clisp) (not debug-weak))
(defgeneric %gethash (key self &optional default)
(:method (key (self t) &optional default)
(common-lisp:gethash key self default)))
#-(and (or ccl clisp) (not debug-weak))
(defgeneric (setf %gethash) (value key self &optional default)
(:method (value key (self t) &optional default)
(setf (common-lisp:gethash key self default) value)))
#-(and (or ccl clisp) (not debug-weak))
(defgeneric %remhash (key self)
(:method (key (self t)) (common-lisp:remhash key self)))
#-(and (or ccl clisp) (not debug-weak))
(defgeneric %maphash (function self)
(:method (function (self t)) (common-lisp:maphash function self)))
#-(and (or ccl clisp) (not debug-weak))
(defgeneric %clrhash (self)
(:method ((self t)) (common-lisp:clrhash self)))
(defmacro define-forward (name)
(let ((method-name (intern (with-standard-io-syntax (format nil "%~A" name))))
(cl-name (intern (string name) "COMMON-LISP")))
`(progn
(defgeneric ,method-name (self) (:method ((self t)) (,cl-name self)))
(defun ,name (hash-table) (,method-name hash-table)))))
#-(and (or ccl clisp) (not debug-weak)) (define-forward hash-table-count)
#-(and (or ccl clisp) (not debug-weak)) (define-forward hash-table-rehash-size)
#-(and (or ccl clisp) (not debug-weak)) (define-forward hash-table-rehash-threshold)
#-(and (or ccl clisp) (not debug-weak)) (define-forward hash-table-size)
#-(and (or ccl clisp) (not debug-weak)) (define-forward hash-table-test)
#-(and (or ccl clisp) (not debug-weak))
(defclass weak-hash-table ()
((rehash-size :accessor %hash-table-rehash-size
:initarg :rehash-size
:initform 2.0)
(rehash-threshold :accessor %hash-table-rehash-threshold
:initarg :rehahs-threshold
:initform 0.90)
(test :accessor %hash-table-test
:initarg :test
:initform (function eql))
(pair-type :reader wht-pair-type
:initarg :weak
:initform :key)
(buckets :accessor wht-buckets)))
#-(and (or ccl clisp) (not debug-weak))
(defmethod %hash-table-count ((self weak-hash-table))
(let ((count 0))
(maphash (lambda (k v) (declare (ignore k v)) (incf count)) self)
count))
#-(and (or ccl clisp) (not debug-weak))
(defgeneric dump-wht (weak-hash-table &optional out))
#-(and (or ccl clisp) (not debug-weak))
(defmethod dump-wht ((self weak-hash-table) &optional (out *standard-output*))
(format out "~A~%" (class-name (class-of self)))
((lambda (items)
(format out (format nil "~~:{ ~~~DA = ~~A~~%~~}"
(reduce
(function max) items
:key (lambda (item) (length (string (first item))))))
items))
(list (list 'count (%hash-table-count self))
(list 'rehash-size (%hash-table-rehash-size self))
(list 'rehash-threshold (%hash-table-rehash-threshold self))
(list 'test (%hash-table-test self))
(list 'pair-type (wht-pair-type self))
(list '|(LENGTH WHT-BUCKETS)| (length (wht-buckets self)))))
(dotimes (i (length (wht-buckets self)))
(let* ((bucket (aref (wht-buckets self)i))
(pairs (cond ((null bucket) nil)
((weak-alist-p bucket) (weak-alist-contents bucket))
((alivep bucket) (acons (pair-key bucket)
(pair-value bucket)
'()))
(t nil))))
(when pairs
(format out " ~4D : (~S . ~S)~%"
i (car (first pairs)) (cdr (first pairs)))
(dolist (pair (rest pairs))
(format out " (~S . ~S)~%" (car pair) (cdr pair))))))
self)
#-(and (or ccl clisp) (not debug-weak))
(defun hash-table-weak-p (object)
"<http://clisp.cons.org/impnotes/hash-dict.html#make-hash>"
(and (typep object 'weak-hash-table) (wht-pair-type object)))
#-(and (or ccl clisp) (not debug-weak))
(defun equiv (a b) (or (and a b) (and (not a) (not b))))
#-(and (or ccl clisp) (not debug-weak))
(defun (setf hash-table-weak-p) (value object)
(unless (equiv (hash-table-weak-p object) value)
(error "Changing a weak hash table to a normal hash table or ~
vice-versa while preserving the table identity is not ~
supported in this ~A" (lisp-implementation-type)))
(unless (eq (hash-table-weak-p object) value)
(error "Changing the weak type of a weak hash table ~
is not implemented yet."))
value)
#-(and (or ccl clisp) (not debug-weak))
(defmethod initialize-instance ((self weak-hash-table)
&key (test (function eql) testp)
(size 37)
(rehash-size 2.0)
(rehash-threshold 0.90)
(weak :key)
(initial-contents nil)
&allow-other-keys)
(declare (ignorable test testp size rehash-size rehash-threshold))
(check-type weak (member :key :value :key-and-value :key-or-value))
(call-next-method)
(setf (wht-buckets self) (make-array size :initial-element nil))
(dolist (pair initial-contents)
(setf (%gethash (car pair) self) (cdr pair)))
self)
#-(and (or ccl clisp) (not debug-weak))
(defmethod %hash-table-size ((self weak-hash-table))
(array-dimension (wht-buckets self) 0))
#-(and (or ccl clisp) (not debug-weak))
(defgeneric wht-iterator (weak-hash-table))
#-(and (or ccl clisp) (not debug-weak))
(defmethod wht-iterator ((self weak-hash-table))
(let ((current-bucket 0)
(current-pairs nil))
(lambda ()
(loop
:named :next
:while (and (null current-pairs)
(< current-bucket (length (wht-buckets self))))
:do (let ((bucket (aref (wht-buckets self) current-bucket)))
(incf current-bucket)
(setf current-pairs
(cond ((null bucket) nil)
((weak-alist-p bucket) (weak-alist-contents bucket))
((alivep bucket) (acons (pair-key bucket)
(pair-value bucket)
'()))
(t nil))))
:finally (return-from :next
(if current-pairs
(let ((pair (pop current-pairs)))
(values t (car pair) (cdr pair)))
(values nil nil nil)))))))
#-(and (or ccl clisp) (not debug-weak))
(defgeneric %rehash-table (weak-hash-table new-size))
#-(and (or ccl clisp) (not debug-weak))
(defmethod %rehash-table ((self weak-hash-table) new-size)
(let ((new (make-hash-table
:weak (wht-pair-type self)
:test (hash-table-test self)
:size (round new-size)
:rehash-size (hash-table-rehash-size self)
:rehash-threshold (hash-table-rehash-threshold self))))
(maphash (lambda (k v) (setf (gethash k new) v)) self)
(setf (wht-buckets self) (wht-buckets new))
self))
#-(and (or ccl clisp) (not debug-weak))
(defgeneric check-reduce-size (weak-hash-table result-values))
#-(and (or ccl clisp) (not debug-weak))
(defmethod check-reduce-size ((self weak-hash-table) result-values)
(let ((threshold-count (truncate (hash-table-size self)
(hash-table-rehash-threshold self))))
(when (< (%hash-table-count self) threshold-count)
(%rehash-table
self
(if (integerp (hash-table-rehash-size self))
(- (hash-table-size self) (hash-table-rehash-size self))
(/ (hash-table-size self) (hash-table-rehash-size self))))))
(values-list result-values))
#-(and (or ccl clisp) (not debug-weak))
(defgeneric check-increase-size (table result-values))
#-(and (or ccl clisp) (not debug-weak))
(defmethod check-increase-size ((self weak-hash-table) result-values)
(let ((threshold-count (* (hash-table-size self)
(hash-table-rehash-threshold self))))
(when (< threshold-count (%hash-table-count self))
(%rehash-table
self
(if (integerp (hash-table-rehash-size self))
(+ (hash-table-size self) (hash-table-rehash-size self))
(* (hash-table-size self) (hash-table-rehash-size self))))))
(values-list result-values))
(defun %sxhash (object)
#+clisp (if (typep object 'fixnum)
object
(sxhash object))
#-clisp (sxhash object))
#-(and (or ccl clisp) (not debug-weak))
(defmethod %gethash (key (self weak-hash-table) &optional default)
(declare (ignorable default))
(let* ((h (mod (%sxhash key) (length (wht-buckets self))))
(bucket (aref (wht-buckets self) h)))
(cond
((null bucket) (values nil nil))
((weak-alist-p bucket)
(let ((pair (weak-alist-assoc key bucket
:test (%hash-table-test self))))
(if pair
(values (cdr pair) t)
(values nil nil))))
(t (if (and (alivep bucket)
(funcall (%hash-table-test self) key (pair-key bucket)))
(values (pair-value bucket) t)
(progn
(setf (aref (wht-buckets self) h) nil)
#+weak-test (gc)
(check-reduce-size self (list nil nil))))))))
;;; We probably need to use defsetf or define-setf-expander
;;; to process correctly the default value:
#-(and (or ccl clisp) (not debug-weak))
(defmethod (setf %gethash) (value key (self weak-hash-table) &optional default)
;; Should check when the new count goes above a threshold and increase
;; hash size.
(declare (ignore default))
(let* ((h (mod (%sxhash key) (length (wht-buckets self))))
(bucket (aref (wht-buckets self) h)))