WeakIdentitySet.st
author ca
Thu, 02 Jan 1997 19:09:36 +0100
changeset 2026 654cba4db794
parent 1790 4187e9fc7357
child 2091 c11bb3e29a1b
permissions -rw-r--r--
dont forget to count down the tally when objects are collected.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
94
649321db4b9a Initial revision
claus
parents:
diff changeset
     1
"
649321db4b9a Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1994 by Claus Gittinger
187
a5dc8e2fa93f size 0 bug
claus
parents: 159
diff changeset
     3
	      All Rights Reserved
94
649321db4b9a Initial revision
claus
parents:
diff changeset
     4
649321db4b9a Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
649321db4b9a Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
649321db4b9a Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
649321db4b9a Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
649321db4b9a Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
649321db4b9a Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
649321db4b9a Initial revision
claus
parents:
diff changeset
    11
"
649321db4b9a Initial revision
claus
parents:
diff changeset
    12
649321db4b9a Initial revision
claus
parents:
diff changeset
    13
IdentitySet subclass:#WeakIdentitySet
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 924
diff changeset
    14
	instanceVariableNames:''
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 924
diff changeset
    15
	classVariableNames:''
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 924
diff changeset
    16
	poolDictionaries:''
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 924
diff changeset
    17
	category:'Collections-Unordered'
94
649321db4b9a Initial revision
claus
parents:
diff changeset
    18
!
649321db4b9a Initial revision
claus
parents:
diff changeset
    19
649321db4b9a Initial revision
claus
parents:
diff changeset
    20
!WeakIdentitySet class methodsFor:'documentation'!
649321db4b9a Initial revision
claus
parents:
diff changeset
    21
649321db4b9a Initial revision
claus
parents:
diff changeset
    22
copyright
649321db4b9a Initial revision
claus
parents:
diff changeset
    23
"
649321db4b9a Initial revision
claus
parents:
diff changeset
    24
 COPYRIGHT (c) 1994 by Claus Gittinger
187
a5dc8e2fa93f size 0 bug
claus
parents: 159
diff changeset
    25
	      All Rights Reserved
94
649321db4b9a Initial revision
claus
parents:
diff changeset
    26
649321db4b9a Initial revision
claus
parents:
diff changeset
    27
 This software is furnished under a license and may be used
649321db4b9a Initial revision
claus
parents:
diff changeset
    28
 only in accordance with the terms of that license and with the
649321db4b9a Initial revision
claus
parents:
diff changeset
    29
 inclusion of the above copyright notice.   This software may not
649321db4b9a Initial revision
claus
parents:
diff changeset
    30
 be provided or otherwise made available to, or used by, any
649321db4b9a Initial revision
claus
parents:
diff changeset
    31
 other person.  No title to or ownership of the software is
649321db4b9a Initial revision
claus
parents:
diff changeset
    32
 hereby transferred.
649321db4b9a Initial revision
claus
parents:
diff changeset
    33
"
649321db4b9a Initial revision
claus
parents:
diff changeset
    34
!
649321db4b9a Initial revision
claus
parents:
diff changeset
    35
649321db4b9a Initial revision
claus
parents:
diff changeset
    36
documentation
649321db4b9a Initial revision
claus
parents:
diff changeset
    37
"
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    38
    WeakIdentitySets behave like IdentitySets, as long as the contained
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    39
    objects are still referenced by some other (non-weak) object.
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    40
    However, once the last non-weak reference ceases to exist,
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    41
    the object will be automatically removed from the Weakcollection
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    42
    (with some delay: it will be removed after the next garbage collect).
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    43
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    44
    This class was added to support dependencies which do not
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    45
    prevent objects from dying. (i.e. which do not fill up your memory
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    46
    if you forget to #release it).
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    47
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
    48
    [author:]
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
    49
        Claus Gittinger
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
    50
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    51
    [See also:]
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    52
        WeakArray WeakIdentityDictionary
94
649321db4b9a Initial revision
claus
parents:
diff changeset
    53
"
649321db4b9a Initial revision
claus
parents:
diff changeset
    54
! !
649321db4b9a Initial revision
claus
parents:
diff changeset
    55
111
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
    56
!WeakIdentitySet class methodsFor:'instance creation'!
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
    57
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
    58
new
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    59
    "return a new empty WeakIdentitySet"
111
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
    60
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
    61
    ^ self new:1
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    62
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    63
    "Modified: 23.4.1996 / 13:56:08 / cg"
111
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
    64
! !
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
    65
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    66
!WeakIdentitySet class methodsFor:'queries'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    67
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    68
goodSizeFrom:arg
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    69
    "return a good array size for the given argument.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    70
     Since WeakIdentitySets are mostly used for dependency management, we typically
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    71
     have only one element in the set. Therefore use exact size for small sets
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    72
     (instead of rounding up to the prime 11)."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    73
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    74
    arg <= 10 ifTrue:[
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    75
	arg < 1 ifTrue:[^ 1].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    76
	^ arg.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    77
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    78
    ^ super goodSizeFrom:arg
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    79
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    80
359
claus
parents: 252
diff changeset
    81
!WeakIdentitySet methodsFor:'adding & removing'!
claus
parents: 252
diff changeset
    82
claus
parents: 252
diff changeset
    83
add:newElement 
claus
parents: 252
diff changeset
    84
    "redefined to avoid synchronization promlems, in case
claus
parents: 252
diff changeset
    85
     of interrupts 
claus
parents: 252
diff changeset
    86
     (otherwise, there could be some other operation on the receiver
claus
parents: 252
diff changeset
    87
       done by another process, which garbles my contents)"
claus
parents: 252
diff changeset
    88
claus
parents: 252
diff changeset
    89
    |wasBlocked|
claus
parents: 252
diff changeset
    90
claus
parents: 252
diff changeset
    91
    wasBlocked := OperatingSystem blockInterrupts.
1790
4187e9fc7357 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1780
diff changeset
    92
    [
4187e9fc7357 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1780
diff changeset
    93
        super add:newElement.
4187e9fc7357 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1780
diff changeset
    94
    ] valueNowOrOnUnwindDo:[
4187e9fc7357 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1780
diff changeset
    95
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
4187e9fc7357 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1780
diff changeset
    96
    ]
4187e9fc7357 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1780
diff changeset
    97
4187e9fc7357 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1780
diff changeset
    98
    "Modified: 20.10.1996 / 14:04:29 / cg"
359
claus
parents: 252
diff changeset
    99
!
claus
parents: 252
diff changeset
   100
claus
parents: 252
diff changeset
   101
remove:anObject ifAbsent:exceptionBlock
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 924
diff changeset
   102
    "redefined to avoid synchronization problems, in case
359
claus
parents: 252
diff changeset
   103
     of interrupts 
claus
parents: 252
diff changeset
   104
     (otherwise, there could be some other operation on the receiver
claus
parents: 252
diff changeset
   105
       done by another process, which garbles my contents)"
claus
parents: 252
diff changeset
   106
claus
parents: 252
diff changeset
   107
    |wasBlocked|
claus
parents: 252
diff changeset
   108
claus
parents: 252
diff changeset
   109
    wasBlocked := OperatingSystem blockInterrupts.
claus
parents: 252
diff changeset
   110
    [
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 924
diff changeset
   111
        super remove:anObject ifAbsent:exceptionBlock
359
claus
parents: 252
diff changeset
   112
    ] valueNowOrOnUnwindDo:[
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 924
diff changeset
   113
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
359
claus
parents: 252
diff changeset
   114
    ]
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 924
diff changeset
   115
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 924
diff changeset
   116
    "Modified: 1.3.1996 / 21:16:10 / cg"
359
claus
parents: 252
diff changeset
   117
! !
claus
parents: 252
diff changeset
   118
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   119
!WeakIdentitySet methodsFor:'element disposal'!
94
649321db4b9a Initial revision
claus
parents:
diff changeset
   120
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   121
informDispose
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   122
    "an element died - must rehash"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   123
1780
1b3a4ddc5b94 changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   124
    |wasBlocked|
111
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
   125
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   126
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   127
     must block interrupts here - finalization
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   128
     may be done at low prio in the background
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   129
     finalizer, we do not want to be interrupted
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   130
     while rehashing
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   131
    "
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   132
    wasBlocked := OperatingSystem blockInterrupts.
2026
654cba4db794 dont forget to count down the tally when objects are collected.
ca
parents: 1790
diff changeset
   133
    keyArray forAllDeadIndicesDo:[:idx | tally := tally - 1] replacingCorpsesWith:DeletedEntry.
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   134
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
94
649321db4b9a Initial revision
claus
parents:
diff changeset
   135
! !
649321db4b9a Initial revision
claus
parents:
diff changeset
   136
216
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   137
!WeakIdentitySet methodsFor:'enumerating'!
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   138
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   139
do:aBlock
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   140
    "perform the block for all members in the collection.
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   141
     This method has been rewritten to be more robust about
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   142
     changed contents while enumerating elements (which might
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   143
     happen during change&update processing, if dependents
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   144
     are added or removed within the update."
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   145
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   146
    |index "{ Class: SmallInteger }"
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   147
     element|
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   148
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   149
    index := 1.
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   150
    [index <= keyArray size] whileTrue:[
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   151
	element := keyArray at:index.
1780
1b3a4ddc5b94 changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   152
	element == 0 ifTrue:[
1b3a4ddc5b94 changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   153
	    keyArray at:index put:DeletedEntry.
1b3a4ddc5b94 changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   154
	] ifFalse:[
1b3a4ddc5b94 changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   155
	    (element notNil and:[element ~~ DeletedEntry]) ifTrue:[
1b3a4ddc5b94 changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   156
	        aBlock value:element
1b3a4ddc5b94 changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   157
	    ]
216
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   158
	].
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   159
	index := index + 1
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   160
    ]
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   161
! !
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   162
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   163
!WeakIdentitySet methodsFor:'private'!
94
649321db4b9a Initial revision
claus
parents:
diff changeset
   164
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   165
keyContainerOfSize:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   166
    "return a container for keys and values of size n.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   167
     use WeakArrays here."
359
claus
parents: 252
diff changeset
   168
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   169
    |w|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   170
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   171
    w := WeakArray new:n.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   172
    w watcher:self.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   173
    ^ w
94
649321db4b9a Initial revision
claus
parents:
diff changeset
   174
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   175
636
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   176
!WeakIdentitySet class methodsFor:'documentation'!
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   177
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   178
version
2026
654cba4db794 dont forget to count down the tally when objects are collected.
ca
parents: 1790
diff changeset
   179
    ^ '$Header: /cvs/stx/stx/libbasic/WeakIdentitySet.st,v 1.20 1997-01-02 18:09:36 ca Exp $'
636
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   180
! !