WeakIdentitySet.st
author Claus Gittinger <cg@exept.de>
Tue, 19 Mar 2013 10:22:02 +0100
changeset 14894 81dee25ec587
parent 14316 cba356d31725
child 16238 12b738714ddb
child 18037 4cf874da38c9
permissions -rw-r--r--
class: WeakIdentitySet comment/format in: #add:
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:[
14316
cba356d31725 changed: #firstIfEmpty:
Stefan Vogel <sv@exept.de>
parents: 13749
diff changeset
   107
                    element == NilEntry ifTrue:[
cba356d31725 changed: #firstIfEmpty:
Stefan Vogel <sv@exept.de>
parents: 13749
diff changeset
   108
                        element := nil.
cba356d31725 changed: #firstIfEmpty:
Stefan Vogel <sv@exept.de>
parents: 13749
diff changeset
   109
                    ].
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
   110
                    ^ 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
   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
            ]
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
        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
   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
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
    ^ 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
   118
! !
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   119
359
claus
parents: 252
diff changeset
   120
!WeakIdentitySet methodsFor:'adding & removing'!
claus
parents: 252
diff changeset
   121
claus
parents: 252
diff changeset
   122
add:newElement 
14894
81dee25ec587 class: WeakIdentitySet
Claus Gittinger <cg@exept.de>
parents: 14316
diff changeset
   123
    "add the argument, newElement to the receiver. 
81dee25ec587 class: WeakIdentitySet
Claus Gittinger <cg@exept.de>
parents: 14316
diff changeset
   124
     Returns the argument, newElement (sigh).
81dee25ec587 class: WeakIdentitySet
Claus Gittinger <cg@exept.de>
parents: 14316
diff changeset
   125
81dee25ec587 class: WeakIdentitySet
Claus Gittinger <cg@exept.de>
parents: 14316
diff changeset
   126
     Redefined to avoid synchronization problems, in case of interrupts 
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
   127
     (otherwise, there could be some other operation on the receiver
2304
05f621e5d674 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2303
diff changeset
   128
       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
   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
    |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
   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
    (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
   133
        "/ 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
   134
        ^ 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
   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
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
        ret := super add:newElement.
6421
58dca33cf0fc #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 4530
diff changeset
   139
    ] 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
   140
        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
   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
    ^ 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
   143
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   144
    "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
   145
!
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
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
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
   148
    "redefined to avoid synchronization problems, in case of interrupts 
359
claus
parents: 252
diff changeset
   149
     (otherwise, there could be some other operation on the receiver
claus
parents: 252
diff changeset
   150
       done by another process, which garbles my contents)"
claus
parents: 252
diff changeset
   151
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
   152
    |ret|
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
    (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
   155
        "/ 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
   156
        ^ 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
   157
    ].
359
claus
parents: 252
diff changeset
   158
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
   159
    [
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
        ret := super remove:anObject ifAbsent:exceptionBlock
6421
58dca33cf0fc #valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents: 4530
diff changeset
   161
    ] 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
   162
        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
   163
    ].
f19df2d4c238 avoid creation of a blocks if possible - if not, cheap ones are now created
Claus Gittinger <cg@exept.de>
parents: 2260
diff changeset
   164
    ^ ret
359
claus
parents: 252
diff changeset
   165
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
   166
    "Modified: 29.1.1997 / 15:07:19 / cg"
359
claus
parents: 252
diff changeset
   167
! !
claus
parents: 252
diff changeset
   168
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   169
!WeakIdentitySet methodsFor:'element disposal'!
94
649321db4b9a Initial revision
claus
parents:
diff changeset
   170
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   171
update:something with:aParameter from:changedObject
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   172
    "an element died - must rehash"
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   173
1780
1b3a4ddc5b94 changed WeakArray to set emptied slots to zero
Claus Gittinger <cg@exept.de>
parents: 1290
diff changeset
   174
    |wasBlocked|
111
9cdd0ed47f38 default size is smaller
claus
parents: 94
diff changeset
   175
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   176
    something == #ElementExpired ifTrue:[
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
         must block interrupts here - finalization
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   179
         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
   180
         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
   181
         while rehashing
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
        wasBlocked := OperatingSystem blockInterrupts.
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   184
        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
   185
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   186
    ].
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   187
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   188
    "Created: 7.1.1997 / 17:00:33 / stefan"
94
649321db4b9a Initial revision
claus
parents:
diff changeset
   189
! !
649321db4b9a Initial revision
claus
parents:
diff changeset
   190
216
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   191
!WeakIdentitySet methodsFor:'enumerating'!
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   192
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   193
do:aBlock
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   194
    "perform the block for all members in the collection.
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   195
     This method has been rewritten to be more robust about
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   196
     changed contents while enumerating elements (which might
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   197
     happen during change&update processing, if dependents
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   198
     are added or removed within the update."
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   199
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   200
    |index "{ Class: SmallInteger }"
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   201
     element|
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   202
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   203
    index := 1.
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   204
    [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
   205
        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
   206
	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
   207
            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
   208
                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
   209
                    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
   210
		]
2102
7a0e81ed345b dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents: 2091
diff changeset
   211
            ]
7a0e81ed345b dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents: 2091
diff changeset
   212
        ].
7a0e81ed345b dont clear entries while enumerating
Claus Gittinger <cg@exept.de>
parents: 2091
diff changeset
   213
        index := index + 1
216
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   214
    ]
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   215
! !
a8abff749575 *** empty log message ***
claus
parents: 187
diff changeset
   216
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   217
!WeakIdentitySet methodsFor:'private'!
94
649321db4b9a Initial revision
claus
parents:
diff changeset
   218
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   219
findKeyOrNil:key
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   220
    "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
   221
     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
   222
     the index of the first unused slot. 
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   223
     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
   224
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   225
     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
   226
              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
   227
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   228
    |index  "{ Class:SmallInteger }"
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   229
     length "{ Class:SmallInteger }"
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   230
     startIndex probe 
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   231
     delIndex "{ Class:SmallInteger }"|
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   232
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   233
    (OperatingSystem blockInterrupts) ifFalse:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   234
        "/
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   235
        "/ may never be entered with interrupts enabled
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
        OperatingSystem unblockInterrupts.
8492
d7fab3acf782 fix error message
Stefan Vogel <sv@exept.de>
parents: 7998
diff changeset
   238
        self error:'unblocked call of findKeyOrNil'.
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   239
    ].
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
    delIndex := 0.
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   242
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   243
    length := keyArray basicSize.
6953
6b3a197638f0 invoke hash/idHash only in one place (initialIndexFor..)
Claus Gittinger <cg@exept.de>
parents: 6421
diff changeset
   244
    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
   245
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   246
    [true] whileTrue:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   247
        probe := keyArray basicAt:index.
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   248
        key == probe ifTrue:[^ index].
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   249
        probe isNil ifTrue:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   250
            delIndex == 0 ifTrue:[^ index].
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   251
            keyArray basicAt:delIndex put:nil.
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   252
            ^ delIndex
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   253
        ].
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   254
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   255
        probe == 0 ifTrue:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   256
            probe := DeletedEntry.
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   257
            keyArray basicAt:index put:probe.
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   258
            tally := tally - 1.
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   259
        ].
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   260
        delIndex == 0 ifTrue:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   261
            probe == DeletedEntry ifTrue:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   262
                delIndex := index
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   263
            ]
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   264
        ].
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   265
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   266
        index == length ifTrue:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   267
            index := 1
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   268
        ] ifFalse:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   269
            index := index + 1
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   270
        ].
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   271
        index == startIndex ifTrue:[
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   272
            delIndex ~~ 0 ifTrue:[
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   273
                keyArray basicAt:delIndex put:nil.
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   274
                ^ delIndex
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   275
            ].
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   276
            ^ self grow findKeyOrNil:key
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   277
        ].
2315
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   278
    ]
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   279
2329
937fc97d5544 must always nil empty slots in #findKeyOrNil:
Claus Gittinger <cg@exept.de>
parents: 2315
diff changeset
   280
    "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
   281
!
a9ff2fda8bae mhmh - be very careful when searching for some item.
Claus Gittinger <cg@exept.de>
parents: 2307
diff changeset
   282
9241
f14bd89f89b6 +isWeakCollection
Claus Gittinger <cg@exept.de>
parents: 8492
diff changeset
   283
isWeakCollection
f14bd89f89b6 +isWeakCollection
Claus Gittinger <cg@exept.de>
parents: 8492
diff changeset
   284
    "return true, if the receiver has weak references to its elements."
f14bd89f89b6 +isWeakCollection
Claus Gittinger <cg@exept.de>
parents: 8492
diff changeset
   285
f14bd89f89b6 +isWeakCollection
Claus Gittinger <cg@exept.de>
parents: 8492
diff changeset
   286
    ^ true
f14bd89f89b6 +isWeakCollection
Claus Gittinger <cg@exept.de>
parents: 8492
diff changeset
   287
!
f14bd89f89b6 +isWeakCollection
Claus Gittinger <cg@exept.de>
parents: 8492
diff changeset
   288
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   289
keyContainerOfSize:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   290
    "return a container for keys and values of size n.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   291
     use WeakArrays here."
359
claus
parents: 252
diff changeset
   292
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   293
    |w|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   294
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   295
    w := WeakArray new:n.
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   296
    w addDependent:self.
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   297
    ^ w
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   298
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2026
diff changeset
   299
    "Modified: 7.1.1997 / 17:01:28 / stefan"
94
649321db4b9a Initial revision
claus
parents:
diff changeset
   300
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   301
636
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   302
!WeakIdentitySet class methodsFor:'documentation'!
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   303
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   304
version
14894
81dee25ec587 class: WeakIdentitySet
Claus Gittinger <cg@exept.de>
parents: 14316
diff changeset
   305
    ^ '$Header: /cvs/stx/stx/libbasic/WeakIdentitySet.st,v 1.39 2013-03-19 09:22:02 cg Exp $'
636
295267cdb5d5 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   306
! !
14894
81dee25ec587 class: WeakIdentitySet
Claus Gittinger <cg@exept.de>
parents: 14316
diff changeset
   307