WeakIdentitySet.st
author Claus Gittinger <cg@exept.de>
Thu, 29 Sep 2011 13:19:18 +0200
changeset 13749 bb954305562f
parent 9317 60e972e65943
child 14316 cba356d31725
permissions -rw-r--r--
version_CVS
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
"
6421
58dca33cf0fc #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 4530
diff changeset
    12
"{ Package: 'stx:libbasic' }"
58dca33cf0fc #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 4530
diff changeset
    13
94
649321db4b9a Initial revision
claus
parents:
diff changeset
    14
IdentitySet subclass:#WeakIdentitySet
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 924
diff changeset
    15
	instanceVariableNames:''
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 924
diff changeset
    16
	classVariableNames:''
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 924
diff changeset
    17
	poolDictionaries:''
4350
5aa2a3b9fd5b category change
Claus Gittinger <cg@exept.de>
parents: 2329
diff changeset
    18
	category:'Collections-Weak'
94
649321db4b9a Initial revision
claus
parents:
diff changeset
    19
!
649321db4b9a Initial revision
claus
parents:
diff changeset
    20
649321db4b9a Initial revision
claus
parents:
diff changeset
    21
!WeakIdentitySet class methodsFor:'documentation'!
649321db4b9a Initial revision
claus
parents:
diff changeset
    22
649321db4b9a Initial revision
claus
parents:
diff changeset
    23
copyright
649321db4b9a Initial revision
claus
parents:
diff changeset
    24
"
649321db4b9a Initial revision
claus
parents:
diff changeset
    25
 COPYRIGHT (c) 1994 by Claus Gittinger
187
a5dc8e2fa93f size 0 bug
claus
parents: 159
diff changeset
    26
	      All Rights Reserved
94
649321db4b9a Initial revision
claus
parents:
diff changeset
    27
649321db4b9a Initial revision
claus
parents:
diff changeset
    28
 This software is furnished under a license and may be used
649321db4b9a Initial revision
claus
parents:
diff changeset
    29
 only in accordance with the terms of that license and with the
649321db4b9a Initial revision
claus
parents:
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
649321db4b9a Initial revision
claus
parents:
diff changeset
    31
 be provided or otherwise made available to, or used by, any
649321db4b9a Initial revision
claus
parents:
diff changeset
    32
 other person.  No title to or ownership of the software is
649321db4b9a Initial revision
claus
parents:
diff changeset
    33
 hereby transferred.
649321db4b9a Initial revision
claus
parents:
diff changeset
    34
"
649321db4b9a Initial revision
claus
parents:
diff changeset
    35
!
649321db4b9a Initial revision
claus
parents:
diff changeset
    36
649321db4b9a Initial revision
claus
parents:
diff changeset
    37
documentation
649321db4b9a Initial revision
claus
parents:
diff changeset
    38
"
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    39
    WeakIdentitySets behave like IdentitySets, as long as the contained
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    40
    objects are still referenced by some other (non-weak) object.
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    41
    However, once the last non-weak reference ceases to exist,
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    42
    the object will be automatically removed from the Weakcollection
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    43
    (with some delay: it will be removed after the next garbage collect).
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    44
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    45
    This class was added to support dependencies which do not
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    46
    prevent objects from dying. (i.e. which do not fill up your memory
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    47
    if you forget to #release it).
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    48
2307
54503a6dbb71 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2304
diff changeset
    49
    [Warning:]
54503a6dbb71 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2304
diff changeset
    50
        If you use this, be very careful since the collections size changes
54503a6dbb71 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2304
diff changeset
    51
        'magically' - for example, testing for being nonEmpty and then
54503a6dbb71 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2304
diff changeset
    52
        removing the first element may fail, since the element may vanish inbetween.
54503a6dbb71 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2304
diff changeset
    53
        In general, never trust the value as returned by the size/isEmpty messages.
54503a6dbb71 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2304
diff changeset
    54
        WeakIdentitySet is not meant as a 'public' class.
2260
Claus Gittinger <cg@exept.de>
parents: 2102
diff changeset
    55
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
    56
    [author:]
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
    57
        Claus Gittinger
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
    58
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    59
    [See also:]
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    60
        WeakArray WeakIdentityDictionary
94
649321db4b9a Initial revision
claus
parents:
diff changeset
    61
"
649321db4b9a Initial revision
claus
parents:
diff changeset
    62
! !
649321db4b9a Initial revision
claus
parents:
diff changeset
    63
111
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
    64
!WeakIdentitySet class methodsFor:'instance creation'!
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
    65
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
    66
new
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    67
    "return a new empty WeakIdentitySet"
111
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
    68
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
    69
    ^ self new:1
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    70
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1232
diff changeset
    71
    "Modified: 23.4.1996 / 13:56:08 / cg"
111
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
    72
! !
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
    73
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    74
!WeakIdentitySet class methodsFor:'queries'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    75
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    76
goodSizeFrom:arg
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    77
    "return a good array size for the given argument.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    78
     Since WeakIdentitySets are mostly used for dependency management, we typically
2303
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
    79
     have only a small number of elements in the set. 
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
    80
     Therefore use exact size for small sets
9317
60e972e65943 comment
Claus Gittinger <cg@exept.de>
parents: 9241
diff changeset
    81
     (instead of rounding up to the next prime 11)."
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    82
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    83
    arg <= 10 ifTrue:[
9317
60e972e65943 comment
Claus Gittinger <cg@exept.de>
parents: 9241
diff changeset
    84
        arg < 1 ifTrue:[^ 1].
60e972e65943 comment
Claus Gittinger <cg@exept.de>
parents: 9241
diff changeset
    85
        ^ arg.
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    86
    ].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    87
    ^ super goodSizeFrom:arg
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    88
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    89
2303
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
    90
!WeakIdentitySet methodsFor:'accessing'!
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
    91
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
    92
firstIfEmpty:exceptionValue
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
    93
    "return the first element of the collection or the
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
    94
     value of the exceptionBlock, if empty.
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
    95
     Redefine, since the inherited method does not work if
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
    96
     elements change silently to nil"
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
    97
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
    98
    |index "{ Class: SmallInteger }"
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
    99
     element|
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   100
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   101
    index := 1.
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   102
    [index <= keyArray size] whileTrue:[
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   103
        element := keyArray basicAt:index.
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   104
        element notNil ifTrue:[
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   105
            element ~~ 0 ifTrue:[
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   106
                element ~~ DeletedEntry ifTrue:[
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   107
                    ^ element
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   108
                ]
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   109
            ]
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   110
        ].
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   111
        index := index + 1
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   112
    ].
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   113
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   114
    ^ exceptionValue value.
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   115
! !
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   116
359
claus
parents: 252
diff changeset
   117
!WeakIdentitySet methodsFor:'adding & removing'!
claus
parents: 252
diff changeset
   118
claus
parents: 252
diff changeset
   119
add:newElement 
2303
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   120
    "redefined to avoid synchronization problems, in case of interrupts 
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   121
     (otherwise, there could be some other operation on the receiver
2304
05f621e5d674 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2303
diff changeset
   122
       done by another process, which garbles my contents)"
2303
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   123
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   124
    |ret|
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   125
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   126
    (OperatingSystem blockInterrupts) ifTrue:[
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   127
        "/ already blocked
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   128
        ^ super add:newElement.
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   129
    ].
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   130
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   131
    [
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   132
        ret := super add:newElement.
6421
58dca33cf0fc #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 4530
diff changeset
   133
    ] ensure:[
2303
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   134
        OperatingSystem unblockInterrupts
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   135
    ].
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   136
    ^ ret
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   137
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   138
    "Modified: 29.1.1997 / 15:06:57 / cg"
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   139
!
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   140
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   141
remove:anObject ifAbsent:exceptionBlock
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   142
    "redefined to avoid synchronization problems, in case of interrupts 
359
claus
parents: 252
diff changeset
   143
     (otherwise, there could be some other operation on the receiver
claus
parents: 252
diff changeset
   144
       done by another process, which garbles my contents)"
claus
parents: 252
diff changeset
   145
2303
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   146
    |ret|
359
claus
parents: 252
diff changeset
   147
2303
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   148
    (OperatingSystem blockInterrupts) ifTrue:[
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   149
        "/ already blocked
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   150
        ^ super remove:anObject ifAbsent:exceptionBlock.
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   151
    ].
359
claus
parents: 252
diff changeset
   152
2303
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   153
    [
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   154
        ret := super remove:anObject ifAbsent:exceptionBlock
6421
58dca33cf0fc #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 4530
diff changeset
   155
    ] ensure:[
2303
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   156
        OperatingSystem unblockInterrupts
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   157
    ].
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   158
    ^ ret
359
claus
parents: 252
diff changeset
   159
2303
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   160
    "Modified: 29.1.1997 / 15:07:19 / cg"
359
claus
parents: 252
diff changeset
   161
! !
claus
parents: 252
diff changeset
   162
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   163
!WeakIdentitySet methodsFor:'element disposal'!
94
649321db4b9a Initial revision
claus
parents:
diff changeset
   164
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   165
update:something with:aParameter from:changedObject
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   166
    "an element died - must rehash"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   167
1780
1b3a4ddc5b94 changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   168
    |wasBlocked|
111
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
   169
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   170
    something == #ElementExpired ifTrue:[
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   171
        "
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   172
         must block interrupts here - finalization
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   173
         may be done at low prio in the background
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   174
         finalizer, we do not want to be interrupted
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   175
         while rehashing
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   176
        "
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   177
        wasBlocked := OperatingSystem blockInterrupts.
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   178
        keyArray forAllDeadIndicesDo:[:idx | tally := tally - 1] replacingCorpsesWith:DeletedEntry.
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   179
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   180
    ].
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   181
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   182
    "Created: 7.1.1997 / 17:00:33 / stefan"
94
649321db4b9a Initial revision
claus
parents:
diff changeset
   183
! !
649321db4b9a Initial revision
claus
parents:
diff changeset
   184
216
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   185
!WeakIdentitySet methodsFor:'enumerating'!
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   186
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   187
do:aBlock
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   188
    "perform the block for all members in the collection.
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   189
     This method has been rewritten to be more robust about
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   190
     changed contents while enumerating elements (which might
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   191
     happen during change&update processing, if dependents
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   192
     are added or removed within the update."
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   193
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   194
    |index "{ Class: SmallInteger }"
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   195
     element|
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   196
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   197
    index := 1.
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   198
    [index <= keyArray size] whileTrue:[
2303
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   199
        element := keyArray basicAt:index.
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   200
	element notNil ifTrue:[
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   201
            element ~~ 0 ifTrue:[
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   202
                element ~~ DeletedEntry ifTrue:[
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   203
                    aBlock value:element
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   204
		]
2102
7a0e81ed345b dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents: 2091
diff changeset
   205
            ]
7a0e81ed345b dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents: 2091
diff changeset
   206
        ].
7a0e81ed345b dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents: 2091
diff changeset
   207
        index := index + 1
216
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   208
    ]
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   209
! !
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   210
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   211
!WeakIdentitySet methodsFor:'private'!
94
649321db4b9a Initial revision
claus
parents:
diff changeset
   212
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   213
findKeyOrNil:key
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   214
    "Look for the key in the receiver.  
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   215
     If it is found, return return the index, otherwise
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   216
     the index of the first unused slot. 
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   217
     Grow the receiver, if key was not found, and no unused slots were present.
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   218
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   219
     Warning: an empty slot MUST be filled by the sender - it is only to be sent
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   220
              by at:put: / add: - like methods."
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   221
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   222
    |index  "{ Class:SmallInteger }"
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   223
     length "{ Class:SmallInteger }"
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   224
     startIndex probe 
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   225
     delIndex "{ Class:SmallInteger }"|
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   226
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   227
    (OperatingSystem blockInterrupts) ifFalse:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   228
        "/
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   229
        "/ may never be entered with interrupts enabled
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   230
        "/
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   231
        OperatingSystem unblockInterrupts.
8492
d7fab3acf782 fix error message
Stefan Vogel <sv@exept.de>
parents: 7998
diff changeset
   232
        self error:'unblocked call of findKeyOrNil'.
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   233
    ].
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   234
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   235
    delIndex := 0.
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   236
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   237
    length := keyArray basicSize.
6953
6b3a197638f0 invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   238
    startIndex := index := self initialIndexForKey:key.
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   239
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   240
    [true] whileTrue:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   241
        probe := keyArray basicAt:index.
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   242
        key == probe ifTrue:[^ index].
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   243
        probe isNil ifTrue:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   244
            delIndex == 0 ifTrue:[^ index].
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   245
            keyArray basicAt:delIndex put:nil.
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   246
            ^ delIndex
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   247
        ].
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   248
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   249
        probe == 0 ifTrue:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   250
            probe := DeletedEntry.
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   251
            keyArray basicAt:index put:probe.
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   252
            tally := tally - 1.
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   253
        ].
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   254
        delIndex == 0 ifTrue:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   255
            probe == DeletedEntry ifTrue:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   256
                delIndex := index
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   257
            ]
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   258
        ].
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   259
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   260
        index == length ifTrue:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   261
            index := 1
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   262
        ] ifFalse:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   263
            index := index + 1
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   264
        ].
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   265
        index == startIndex ifTrue:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   266
            delIndex ~~ 0 ifTrue:[
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   267
                keyArray basicAt:delIndex put:nil.
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   268
                ^ delIndex
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   269
            ].
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   270
            ^ self grow findKeyOrNil:key
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   271
        ].
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   272
    ]
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   273
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   274
    "Modified: 30.1.1997 / 15:04:38 / cg"
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   275
!
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   276
9241
f14bd89f89b6 +isWeakCollection
Claus Gittinger <cg@exept.de>
parents: 8492
diff changeset
   277
isWeakCollection
f14bd89f89b6 +isWeakCollection
Claus Gittinger <cg@exept.de>
parents: 8492
diff changeset
   278
    "return true, if the receiver has weak references to its elements."
f14bd89f89b6 +isWeakCollection
Claus Gittinger <cg@exept.de>
parents: 8492
diff changeset
   279
f14bd89f89b6 +isWeakCollection
Claus Gittinger <cg@exept.de>
parents: 8492
diff changeset
   280
    ^ true
f14bd89f89b6 +isWeakCollection
Claus Gittinger <cg@exept.de>
parents: 8492
diff changeset
   281
!
f14bd89f89b6 +isWeakCollection
Claus Gittinger <cg@exept.de>
parents: 8492
diff changeset
   282
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   283
keyContainerOfSize:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   284
    "return a container for keys and values of size n.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   285
     use WeakArrays here."
359
claus
parents: 252
diff changeset
   286
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   287
    |w|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   288
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   289
    w := WeakArray new:n.
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   290
    w addDependent:self.
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   291
    ^ w
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   292
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   293
    "Modified: 7.1.1997 / 17:01:28 / stefan"
94
649321db4b9a Initial revision
claus
parents:
diff changeset
   294
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   295
636
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   296
!WeakIdentitySet class methodsFor:'documentation'!
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   297
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   298
version
13749
bb954305562f version_CVS
Claus Gittinger <cg@exept.de>
parents: 9317
diff changeset
   299
    ^ '$Header: /cvs/stx/stx/libbasic/WeakIdentitySet.st,v 1.37 2011-09-29 11:19:18 cg Exp $'
636
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   300
! !