WeakIdentityDictionary.st
author Stefan Vogel <sv@exept.de>
Wed, 08 Jan 1997 20:42:17 +0100
changeset 2091 c11bb3e29a1b
parent 2027 4a2cdf9946ae
child 2260 96d898c2874d
permissions -rw-r--r--
Use dependent mechamism of WeakArray instead of #watcher. Remove deprecated watcher stuff.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
62211a9bc04d Initial revision
claus
parents:
diff changeset
     1
"
62211a9bc04d Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1992 by Claus Gittinger
358
claus
parents: 159
diff changeset
     3
	      All Rights Reserved
6
62211a9bc04d Initial revision
claus
parents:
diff changeset
     4
62211a9bc04d Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
62211a9bc04d Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
62211a9bc04d Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
62211a9bc04d Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
62211a9bc04d Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
62211a9bc04d Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
62211a9bc04d Initial revision
claus
parents:
diff changeset
    11
"
62211a9bc04d Initial revision
claus
parents:
diff changeset
    12
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
    13
'From Smalltalk/X, Version:3.1.3 on 7-jan-1997 at 17:23:20'                     !
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
    14
6
62211a9bc04d Initial revision
claus
parents:
diff changeset
    15
IdentityDictionary subclass:#WeakIdentityDictionary
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    16
	instanceVariableNames:''
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    17
	classVariableNames:''
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    18
	poolDictionaries:''
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    19
	category:'Collections-Unordered'
6
62211a9bc04d Initial revision
claus
parents:
diff changeset
    20
!
62211a9bc04d Initial revision
claus
parents:
diff changeset
    21
10
claus
parents: 6
diff changeset
    22
!WeakIdentityDictionary class methodsFor:'documentation'!
claus
parents: 6
diff changeset
    23
88
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    24
copyright
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    25
"
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    26
 COPYRIGHT (c) 1992 by Claus Gittinger
358
claus
parents: 159
diff changeset
    27
	      All Rights Reserved
88
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    28
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    29
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    30
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    31
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    32
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    33
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    34
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    35
"
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    36
!
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    37
10
claus
parents: 6
diff changeset
    38
documentation
claus
parents: 6
diff changeset
    39
"
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    40
    WeakIdentityDictionaries behave like IdentityDictionaries, 
1343
b0ee705c63e0 Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents: 1290
diff changeset
    41
    as long as the keys are still referenced by some 
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    42
    other (non-weak) object.
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    43
    However, once the last non-weak reference ceases to exist,
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    44
    the object will be automatically removed from the Weakcollection
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    45
    (with some delay: it will be removed after the next garbage collect).
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    46
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    47
    This class was added to support keeping track of dependents without
88
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    48
    keeping the values alive - values simply become nil when no one else
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    49
    references it. The original dependency mechanism used a regular Dictionary,
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    50
    which usually leads to a lot of garbage being kept due to a forgotten
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    51
    release. Using a WeakDictionary may be incompatible to ST-80 but is much
81dacba7a63a *** empty log message ***
claus
parents: 63
diff changeset
    52
    more comfortable, since no manual release of dependents is needed.
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    53
1290
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
    54
    [author:]
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
    55
        Claus Gittinger
15ba3221b89b documentation
Claus Gittinger <cg@exept.de>
parents: 1263
diff changeset
    56
1263
92c1db6b0776 commentary
Claus Gittinger <cg@exept.de>
parents: 1256
diff changeset
    57
    [See also:]
1343
b0ee705c63e0 Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents: 1290
diff changeset
    58
        WeakArray WeakValueDictionary WeakIdentitySet
10
claus
parents: 6
diff changeset
    59
"
claus
parents: 6
diff changeset
    60
! !
claus
parents: 6
diff changeset
    61
359
claus
parents: 358
diff changeset
    62
!WeakIdentityDictionary methodsFor:'adding & removing'!
claus
parents: 358
diff changeset
    63
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    64
at:key put:anObject
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    65
    "add the argument anObject under key, aKey to the receiver.
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    66
     Return anObject (sigh).
1343
b0ee705c63e0 Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents: 1290
diff changeset
    67
     Redefined to block interrupts, to avoid trouble when dependencies
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    68
     are added within interrupting high prio processes."
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    69
359
claus
parents: 358
diff changeset
    70
    |wasBlocked|
claus
parents: 358
diff changeset
    71
claus
parents: 358
diff changeset
    72
    wasBlocked := OperatingSystem blockInterrupts.
1790
4187e9fc7357 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1780
diff changeset
    73
    [
4187e9fc7357 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1780
diff changeset
    74
        super at:key put:anObject.
4187e9fc7357 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1780
diff changeset
    75
    ] valueNowOrOnUnwindDo:[
4187e9fc7357 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1780
diff changeset
    76
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
4187e9fc7357 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1780
diff changeset
    77
    ].
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    78
    ^ anObject
1223
09d16bc18493 at:put: returns its arg
Claus Gittinger <cg@exept.de>
parents: 1054
diff changeset
    79
1343
b0ee705c63e0 Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents: 1290
diff changeset
    80
    "Modified: 6.5.1996 / 12:22:26 / stefan"
1790
4187e9fc7357 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1780
diff changeset
    81
    "Modified: 20.10.1996 / 14:04:46 / cg"
359
claus
parents: 358
diff changeset
    82
!
claus
parents: 358
diff changeset
    83
claus
parents: 358
diff changeset
    84
removeKey:aKey ifAbsent:aBlock
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    85
    "remove the association under aKey from the collection,
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    86
     return the value previously stored there..
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    87
     If it was not in the collection return the result 
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    88
     from evaluating aBlock.
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    89
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    90
    Redefined to avoid synchronization problems, in case
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    91
    of interrupts (otherwise, there could be some other operation 
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
    92
    on the receiver done by another process, which garbles my contents)."
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
    93
1343
b0ee705c63e0 Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents: 1290
diff changeset
    94
    |wasBlocked ret|
359
claus
parents: 358
diff changeset
    95
claus
parents: 358
diff changeset
    96
    wasBlocked := OperatingSystem blockInterrupts.
claus
parents: 358
diff changeset
    97
    [
1343
b0ee705c63e0 Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents: 1290
diff changeset
    98
        ret := super removeKey:aKey ifAbsent:aBlock
359
claus
parents: 358
diff changeset
    99
    ] valueNowOrOnUnwindDo:[
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
   100
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts]
1343
b0ee705c63e0 Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents: 1290
diff changeset
   101
    ].
b0ee705c63e0 Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents: 1290
diff changeset
   102
    ^ ret
1054
62ce964f1d2e commentary
Claus Gittinger <cg@exept.de>
parents: 635
diff changeset
   103
1256
249fd57abee5 commentary
Claus Gittinger <cg@exept.de>
parents: 1223
diff changeset
   104
    "Modified: 22.4.1996 / 17:39:57 / cg"
1343
b0ee705c63e0 Correct return value when removing elements.
Stefan Vogel <sv@exept.de>
parents: 1290
diff changeset
   105
    "Modified: 6.5.1996 / 12:44:51 / stefan"
359
claus
parents: 358
diff changeset
   106
! !
claus
parents: 358
diff changeset
   107
10
claus
parents: 6
diff changeset
   108
!WeakIdentityDictionary methodsFor:'element disposal'!
claus
parents: 6
diff changeset
   109
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   110
update:something with:aParameter from:changedObject
10
claus
parents: 6
diff changeset
   111
    "an element (either key or value) died - rehash"
6
62211a9bc04d Initial revision
claus
parents:
diff changeset
   112
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   113
    |wasBlocked|
358
claus
parents: 159
diff changeset
   114
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   115
    something == #ElementExpired ifTrue:[
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   116
        "
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   117
         have to block here - dispose may be done at a low priority
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   118
         from the background finalizer. If new views are opened in
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   119
         the foreground, the dependency dictionary may get corrupted
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   120
         otherwise
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   121
        "
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   122
        wasBlocked := OperatingSystem blockInterrupts.
10
claus
parents: 6
diff changeset
   123
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   124
        keyArray 
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   125
            forAllDeadIndicesDo:[:idx | 
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   126
                                    (valueArray at:idx) notNil ifTrue:[
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   127
                                        keyArray basicAt:idx put:DeletedEntry.
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   128
                                        valueArray basicAt:idx put:nil.
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   129
                                        tally := tally - 1.
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   130
                                    ]
2027
4a2cdf9946ae replace items with DeletedEntry, when objects are collected.
ca
parents: 1790
diff changeset
   131
                                ]
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   132
            replacingCorpsesWith:DeletedEntry.
358
claus
parents: 159
diff changeset
   133
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   134
        wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   135
    ]
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   136
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   137
    "Created: 7.1.1997 / 16:59:30 / stefan"
6
62211a9bc04d Initial revision
claus
parents:
diff changeset
   138
! !
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   139
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   140
!WeakIdentityDictionary methodsFor:'private'!
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   141
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   142
keyContainerOfSize:n
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   143
    "return a container for keys of size n.
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   144
     use WeakArrays here."
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   145
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   146
    |w|
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   147
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   148
    w := WeakArray new:n.
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   149
    w addDependent:self.
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   150
    ^ w
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   151
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   152
    "Modified: 7.1.1997 / 17:01:15 / stefan"
609
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   153
! !
12be97f6d5a7 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 530
diff changeset
   154
635
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   155
!WeakIdentityDictionary class methodsFor:'documentation'!
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   156
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   157
version
2091
c11bb3e29a1b Use dependent mechamism of WeakArray instead of #watcher.
Stefan Vogel <sv@exept.de>
parents: 2027
diff changeset
   158
    ^ '$Header: /cvs/stx/stx/libbasic/WeakIdentityDictionary.st,v 1.23 1997-01-08 19:42:15 stefan Exp $'
635
86cbe76f5a20 version at the end
Claus Gittinger <cg@exept.de>
parents: 609
diff changeset
   159
! !