WeakIdentitySet.st
author Claus Gittinger <cg@exept.de>
Wed, 29 Jan 1997 22:10:56 +0100
changeset 2315 a9ff2fda8bae
parent 2307 54503a6dbb71
child 2329 937fc97d5544
permissions -rw-r--r--
mhmh - be very careful when searching for some item. 0-entries must be handled just like DeletedItems
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
2307
54503a6dbb71 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2304
diff changeset
    48
    [Warning:]
54503a6dbb71 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2304
diff changeset
    49
        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
    50
        'magically' - for example, testing for being nonEmpty and then
54503a6dbb71 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2304
diff changeset
    51
        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
    52
        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
    53
        WeakIdentitySet is not meant as a 'public' class.
2260
Claus Gittinger <cg@exept.de>
parents: 2102
diff changeset
    54
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
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    81
     (instead of rounding up to the prime 11)."
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:[
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    84
	arg < 1 ifTrue:[^ 1].
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
    85
	^ arg.
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.
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
    ] valueNowOrOnUnwindDo:[
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
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
    ] valueNowOrOnUnwindDo:[
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.  
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   215
     If it is found, return return the index of the first unused slot. 
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   216
     Grow the receiver, if key was not found, and no unused slots were present"
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   217
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   218
    |index  "{ Class:SmallInteger }"
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   219
     length "{ Class:SmallInteger }"
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   220
     startIndex probe 
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   221
     delIndex "{ Class:SmallInteger }" 
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   222
     wasBlocked|
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   223
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   224
    wasBlocked := OperatingSystem blockInterrupts.
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   225
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   226
    wasBlocked ifFalse:[
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   227
        "/ may never be entered with interrupts enabled
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   228
        self error:'oops - unblocked call of findKeyOrNil'
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   229
    ].
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   230
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   231
    [
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   232
        delIndex := 0.
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   233
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   234
        length := keyArray basicSize.
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   235
        index := key identityHash.
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   236
        index < 16r1FFFFFFF ifTrue:[
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   237
            index := index * 2
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   238
        ].
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   239
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   240
        index := index \\ length + 1.
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   241
        startIndex := index.
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   242
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   243
        [true] whileTrue:[
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   244
            probe := keyArray basicAt:index.
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   245
            key == probe ifTrue:[^ index].
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   246
            probe isNil ifTrue:[
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   247
                delIndex == 0 ifTrue:[^ index].
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   248
                keyArray basicAt:delIndex put:nil.
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   249
                ^ delIndex
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   250
            ].
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   251
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   252
            probe == 0 ifTrue:[
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   253
                probe := DeletedEntry.
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   254
                keyArray basicAt:index put:probe.
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   255
                tally := tally - 1.
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   256
            ].
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   257
            delIndex == 0 ifTrue:[
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   258
                probe == DeletedEntry ifTrue:[
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   259
                    delIndex := index
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   260
                ]
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   261
            ].
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   262
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   263
            index == length ifTrue:[
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   264
                index := 1
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   265
            ] ifFalse:[
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   266
                index := index + 1
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   267
            ].
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   268
            index == startIndex ifTrue:[
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   269
                delIndex ~~ 0 ifTrue:[
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   270
                    keyArray basicAt:delIndex put:nil.
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   271
                    ^ delIndex
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
                ^ self grow findKeyOrNil:key
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   274
            ].
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
    ] valueNowOrOnUnwindDo:[
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   277
        wasBlocked ifFalse:[
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   278
            OperatingSystem unblockInterrupts
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   279
        ]
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   280
    ]
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   281
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   282
    "Created: 29.1.1997 / 21:43:57 / cg"
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   283
    "Modified: 29.1.1997 / 22:09:55 / cg"
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   284
!
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   285
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   286
keyContainerOfSize:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   287
    "return a container for keys and values of size n.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   288
     use WeakArrays here."
359
claus
parents: 252
diff changeset
   289
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   290
    |w|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   291
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   292
    w := WeakArray new:n.
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   293
    w addDependent:self.
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   294
    ^ w
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   295
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   296
    "Modified: 7.1.1997 / 17:01:28 / stefan"
94
649321db4b9a Initial revision
claus
parents:
diff changeset
   297
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   298
636
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   299
!WeakIdentitySet class methodsFor:'documentation'!
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   300
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   301
version
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   302
    ^ '$Header: /cvs/stx/stx/libbasic/WeakIdentitySet.st,v 1.27 1997-01-29 21:10:33 cg Exp $'
636
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   303
! !