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